首页 > 编程 > Python > 正文

python使用wxpy轻松实现微信防撤回的方法

2020-02-16 01:18:05
字体:
来源:转载
供稿:网友

最近比较闲就随便瞎看,看到了微信防撤回就顺便跟着学着实现一下

使用的是wxpy,安装方法pip install wxpy(我使用的是python2.7),这样实现起来比较快,反正也只是练手

首现看了两个别人实现的代码,然后看了看wxpy的文档:http://wxpy.readthedocs.io/zh/latest/utils.html(萌新,顺便锻炼下看文档)

我实现的代码:

import wxpyimport re # 实例化微信对象bot = wxpy.Bot(cache_path=True) # 启用puid 并指定puid所需映射数据保存的路径bot.enable_puid(path='wxpy_puid.pkl') # 用于存放每个用户最近发送的消息msgs = dict() # 信息的类型msg_types = { 'Text': '文本', 'Map': '位置', 'Picture': '图片', 'Video': '视频', 'Attachment': '文件', 'Sharing': '分享', 'Card': '名片', 'Recording': '语音',}  @bot.register()def handle_receive_msg(msg): """ 监听消息 :param msg: 接收到的消息 :return: """ # 原信息数据 raw = msg.raw  # 如果消息的状态码是4 即撤回消息 if raw.get('Status') == 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  else:   puid = None   name = None  if puid:   # 被撤回消息的msgid的匹配规则   msg_id_regex = re.compile('<msgid>(/d+)</msgid>')   # 获取被撤回消息的msgid   old_msg_id = msg_id_regex.findall(raw.get('Content'))[0]   # 获取该发送者的最后5次的消息记录   chat_msgs = msgs.get(puid)   # 遍历消息记录   for chat_msg in chat_msgs[::-1]:    # 跳过不是被撤回的信息    if str(chat_msg.id) != old_msg_id:     continue    chat = chat_msg.chat    # 如果被撤回的信息是文本信息    if chat_msg.type == "Text":     # 如果消息长度过长 则不予处理     if len(chat_msg.text) >= 150:      warning = "【您撤回的消息过长,有炸群嫌疑,不予处理!!!】"      bot.file_helper.send('%s撤回了一条文本消息--【%s】'.decode('utf-8') % (name, warning))      break     # 将此消息转发出来     chat_msg.forward(chat, prefix='%s撤回了一条文本消息,消息内容为:'.decode('utf-8') % name)    # 如果被撤回的是位置信息    elif chat_msg.type == "Map":     # 位置信息的匹配规则     map_regex = re.compile(r'label="(.+?)"')     # 获取位置信息中的位置     map = map_regex.findall(chat_msg.raw.get("OriContent"))[0]     # 将位置信息发出来     msg.reply('%s撤回了一条位置消息,位置信息为:【%s】'.decode('utf-8') % (name, map))    else:     # 获取信息的类型     msg_type = msg_types.get(chat_msg.type).decode('utf-8')     # 将信息转发出来     chat_msg.forward(chat, prefix='%s撤回了一条%s消息, 消息内容为:'.decode('utf-8') % (name, msg_type))    break else:  # 如果是群消息  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  else:   puid = None  if puid:   # 记录消息   msgs.setdefault(puid, []).append(msg)   # 截取消息 保留最大5条记录   msgs[puid] = msg[puid][-5:] # 使机器人后台运行,并进入交互模式wxpy.embed()            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表