社交工具现已经成为了我们日常生活中不可缺少的联络沟通工具了,然后有时你会碰到别人给你发消息,然后他俄然来一波骚操作(对方已撤回一条消息)。。我就问你尴尬不尴尬老铁!!但是我们是一名pythoner,我们要想办法自己处理!
实现代码
# -*-encoding:utf-8-*-import osimport reimport shutilimport timeimport itchatfrom itchat.content import *# 说明:能够撤回的有文本文字、语音、视频、图片、方位、手刺、共享、附件# msg_dict = {}# 文件存储暂时目录rev_tmp_dir = "/home/alic/RevDir/"if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir)# 表情有一个问题 | 承受信息和承受note的msg_id不一致 偶然解决方案face_bug = None# 将接收到的音讯存放在字典中,当接收到新音讯时对字典中超时的音讯进行清理 | 不承受不具有撤回功能的信息# [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])def handler_receive_msg(msg): global face_bug # 获取的是本地时刻戳并格式化本地时刻戳 e: 2017-04-21 21:30:08 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 音讯ID msg_id = msg['MsgId'] # 音讯时刻 msg_time = msg['CreateTime'] # 音讯发送人昵称 | 这里也能够运用RemarkName备注 但是自己或者没有备注的人为None msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"] # 音讯内容 msg_content = None # 共享的链接 msg_share_url = None if msg['Type'] == 'Text' or msg['Type'] == 'Friends': msg_content = msg['Text'] elif msg['Type'] == 'Recording' or msg['Type'] == 'Attachment' or msg['Type'] == 'Video' or msg['Type'] == 'Picture': msg_content = r"" + msg['FileName'] # 保存文件 msg['Text'](rev_tmp_dir + msg['FileName']) elif msg['Type'] == 'Card': msg_content = msg['RecommendInfo']['NickName'] + r" 的手刺" elif msg['Type'] == 'Map': x, y, location = re.search( "" + x.__str__() + " 经度->" + y.__str__() else: msg_content = r"" + location elif msg['Type'] == 'Sharing': msg_content = msg['Text'] msg_share_url = msg['Url'] face_bug = msg_content # 更新字典 msg_dict.update( { msg_id: { "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, "msg_type": msg["Type"], "msg_content": msg_content, "msg_share_url": msg_share_url } } )# 收到note通知类音讯,判断是不是撤回并进行相应操作@itchat.msg_register([NOTE])def send_msg_helper(msg): global face_bug if re.search(r"", msg['Content']) is not None: # 获取音讯的id old_msg_id = re.search("(.*?)", msg['Content']).group(1) old_msg = msg_dict.get(old_msg_id, {}) if len(old_msg_id)
下面让我们来看看效果!牛吗?!
领取专属 10元无门槛券
私享最新 技术干货