赞
踩
我们在使用微信时,如果发错消息,可以撤回,但你有没有想过在某些特殊情况下,防止对方撤回消息呢?今天我们就使用Python实现一下查看好友撤回的微信消息的功能。运行程序,手机端微信扫描弹出的二维码即可登录网页版微信,程序会将网页版微信收到的所有消息都缓存下来,当检测到有消息(语音、文字、图片等)撤回时,将撤回消息的缓存版本通过文件传输助手发送到自己的手机上,如下图所示。
实现微信消息防撤回功能主要通过Python内置的os模块、re模块、time模块、platform模块,以及第三方模块itchat实现。具体步骤如下:
(1)由于使用了第三方模块itchat,所以需要先安装该模块。使用pip命令安装模块的命令如下:
pip install itchat
(2)导入程序中需要使用的模块,具体代码如下:
import re
import os
import time
import itchat
import platform
from itchat.content import *
(3)实现逻辑。首先使用platform模块获取操作系统底层的数据,根据不同的操作系统传入不同的登录参数,登陆成功后,缓存接收到的所有信息并监听是否有消息撤回。代码如下:
import re
import os
import time
import itchat
import platform
from itchat.content import *
msg_info = {}
face_package = None
@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, isMpChat=True)
def handle_rsg(msg):
**global** face\_package # 接收消息的时间 msg\_time\_receive = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 发信人 **try**: msg\_from = itchat.search\_friends(userName\=msg\['FromUserName'\])\['NickName'\] **except**: msg\_from = 'WeChat Official Accounts' # 发信时间 msg\_time\_send = msg\['CreateTime'\] # 信息ID msg\_id = msg\['MsgId'\] msg\_content = **None** msg\_link = **None** # 文本或者好友推荐 **if** msg\['Type'\] == 'Text' **or** msg\['Type'\] == 'Friends': msg\_content = msg\['Text'\] print('\[Text/Friends\]: %s' % msg\_content) # 附件/视频/图片/语音 **elif** msg\['Type'\] == 'Attachment' **or** msg\['Type'\] == "Video" **or** msg\['Type'\] == 'Picture' **or** msg\['Type'\] == 'Recording': msg\_content = msg\['FileName'\] msg\['Text'\](str(msg\_content)) print('\[Attachment/Video/Picture/Recording\]: %s' % msg\_content) # 举一反三代码 # 位置信息 **elif** msg\['Type'\] == 'Map': x, y, location = re.search("<location x=**\\"**(.\*?)**\\"** y=**\\"**(.\*?)**\\"**.\*label=**\\"**(.\*?)**\\"**.\*", msg\['OriContent'\]).group(1, 2, 3) **if** location **is None**: msg\_content = r"纬度:" \+ x.\_\_str\_\_() + ", 经度:" \+ y.\_\_str\_\_() **else**: msg\_content = r"" \+ location print('\[Map\]: %s' % msg\_content) # 分享的音乐/文章 **elif** msg\['Type'\] == 'Sharing': msg\_content = msg\['Text'\] msg\_link = msg\['Url'\] print('\[Sharing\]: %s' % msg\_content) msg\_info.update( { msg\_id: { "msg\_from": msg\_from, "msg\_time\_send": msg\_time\_send, "msg\_time\_receive": msg\_time\_receive, "msg\_type": msg\["Type"\], "msg\_content": msg\_content, "msg\_link": msg\_link } } ) face\_package = msg\_content
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def monitor(msg):
**if** '撤回了一条消息' **in** msg\['Content'\]: recall\_msg\_id = re.search("\\<msgid\\>(.\*?)\\<\\/msgid\\>", msg\['Content'\]).group(1) recall\_msg = msg\_info.get(recall\_msg\_id) print('\[Recall\]: %s' % recall\_msg) # 表情包 **if** len(recall\_msg\_id) < 11: itchat.send\_file(face\_package, toUserName\='filehelper') **else**: msg\_prime = '---' \+ recall\_msg.get('msg\_from') + '撤回了一条消息\---**\\n**' \\ '消息类型:' \+ recall\_msg.get('msg\_type') + '**\\n**' \\ '时间:' \+ recall\_msg.get('msg\_time\_receive') + '**\\n**' \\ '内容:' \+ recall\_msg.get('msg\_content') **if** recall\_msg\['msg\_type'\] == 'Sharing': msg\_prime += '**\\n**链接:' \+ recall\_msg.get('msg\_link') itchat.send\_msg(msg\_prime, toUserName\='filehelper') **if** recall\_msg\['msg\_type'\] == 'Attachment' **or** recall\_msg\['msg\_type'\] == "Video" **or** recall\_msg\['msg\_type'\] == 'Picture' **or** recall\_msg\['msg\_type'\] == 'Recording': file = '@fil@%s' % (recall\_msg\['msg\_content'\]) itchat.send(msg\=file, toUserName\='filehelper') os.remove(recall\_msg\['msg\_content'\]) msg\_info.pop(recall\_msg\_id)
if __name__ == ‘__main__’:
**if** platform.platform()\[:7\] == 'Windows':
itchat.auto\_login(enableCmdQR\=**False**, hotReload\=**True**)
**else**:
itchat.auto\_login(enableCmdQR\=**True**, hotReload\=**True**)
itchat.run()
以上就是“3步实现用Python防止微信消息撤回功能”的全部内容,希望对你有所帮助。
关于Python技术储备
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、Python必备开发工具
三、Python视频合集
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、Python练习题
检查学习结果。
六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
最后祝大家天天进步!!
上面这份完整版的Python全套学习资料已经上传至CSDN官方,朋友如果需要可以直接微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。