介绍
这篇文章将为大家详细讲解有关python如何使用wxpy轻松实现微信防撤回,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
<强>实现的代码:强>
import wxpy import 再保险 , #,实例化微信对象 时间=bot wxpy.Bot (cache_path=True) , #,启用puid 并指定puid所需映射数据保存的路径 bot.enable_puid (path=& # 39; wxpy_puid.pkl& # 39;) , #,用于存放每个用户最近发送的消息 时间=msgs dict类型() , #,信息的类型 msg_types =, { ,& # 39;文本# 39;:,& # 39;文本& # 39;, ,& # 39;地图# 39;:,& # 39;位置& # 39;, ,& # 39;图片# 39;:,& # 39;图片& # 39;, ,& # 39;视频# 39;:,& # 39;视频& # 39;, ,& # 39;附件# 39;:,& # 39;文件& # 39;, ,& # 39;分享# 39;:,& # 39;分享& # 39;, ,& # 39;卡# 39;:,& # 39;名片& # 39;, ,& # 39;记录# 39;:,& # 39;语音& # 39;, } , , @bot.register () def handle_receive_msg(味精): ,“““ ,监听消息 ,:param 味精:接收到的消息 ,返回: ,“““ ,#原信息数据=,raw  msg.raw , ,#如果消息的状态码是4,即撤回消息 ,if raw.get(& # 39;状态# 39;),==,4: #才能,如果是群消息 if 才能isinstance (msg.chat, wxpy.api.chats.group.Group): ,,#,获取群成员的puid ,,puid =msg.member.puid ,,#,获取发送者的昵称 ,,name =msg.member.nick_name #才能,如果是好友消息 elif 才能isinstance (msg.chat, wxpy.api.chats.friend.Friend): ,,#,获取好友的puid ,,puid =msg.chat.puid ,,#,获取好友的昵称 ,,name =msg.chat.nick_name 其他的才能: ,,puid =,没有 ,,name =,没有 if 才能;puid: ,,#,被撤回消息的是否的匹配规则 ,,msg_id_regex =, re.compile (& # 39; & lt; msgid> (\ d +) & lt;/msgid> & # 39;) ,,#,获取被撤回消息的是否 ,,old_msg_id =, msg_id_regex.findall (raw.get(& # 39;内容# 39;))[0] ,,#,获取该发送者的最后5次的消息记录 ,,chat_msgs =, msgs.get (puid) ,,#,遍历消息记录 ,,for chat_msg 拷贝chat_msgs [:: 1): ,,,#,跳过不是被撤回的信息 ,,,if str (chat_msg.id), !=, old_msg_id: ,,,,继续 ,,,chat =chat_msg.chat ,,,#,如果被撤回的信息是文本信息 ,,,if chat_msg.type ==,“Text": ,,,,#,如果消息长度过长,则不予处理 ,,,,if len (chat_msg.text),祝辞=,150: ,,,,,warning =,“【您撤回的消息过长,有炸群嫌疑,不予处理! ! !】“ ,,,,,bot.file_helper.send (& # 39; % s撤回了一条文本消息——【% s】& # 39; .decode (& # 39; utf - 8 # 39;), %,(名字,,警告) ,,,,,休息 ,,,,#,将此消息转发出来 ,,,,chat_msg.forward(聊天,,前缀=& # 39;% s撤回了一条文本消息,消息内容为:& # 39;.decode (& # 39; utf - 8 # 39;), %,名称) ,,,#,如果被撤回的是位置信息 ,,,elif chat_msg.type ==,“Map": ,,,,#,位置信息的匹配规则 ,,,,map_regex =, re.compile(" # 39;标签=?+ ?)“& # 39;) ,,,,#,获取位置信息中的位置 ,,,,map =, map_regex.findall (chat_msg.raw.get (“OriContent")) [0] ,,,,#,将位置信息发出来 ,,,,msg.reply (& # 39; % s撤回了一条位置消息,位置信息为:【% s】& # 39; .decode (& # 39; utf - 8 # 39;), %,(名称,地图)) ,,,: ,,,,#,获取信息的类型 ,,,,msg_type =, msg_types.get (chat_msg.type) .decode (& # 39; utf - 8 # 39;) ,,,,#,将信息转发出来 ,,,,chat_msg.forward(聊天,,前缀=& # 39;% s撤回了一条% s消息,,消息内容为:& # 39;.decode (& # 39; utf - 8 # 39;), %,(名字,,msg_type)) ,才能打破 ,其他的: #才能,如果是群消息 if 才能isinstance (msg.chat, wxpy.api.chats.group.Group): ,,#,获取群成员的puid ,,puid =msg.member.puid #才能,如果是好友消息 elif 才能isinstance (msg.chat, wxpy.api.chats.friend.Friend): ,,#,获取好友的puid ,,puid =msg.chat.puid 其他的才能: ,,puid =,没有 if 才能;puid: ,,#,记录消息 ,,msgs.setdefault (puid, []) .append(味精) ,,#,截取消息,保留最大5条记录 ,,公司[puid],=,味精[puid] [5:] , #,使机器人后台运行,并进入交互模式 null null null null null null null null null null null null null null null null nullpython如何使用wxpy轻松实现微信防撤回