Python 查看微信撤回消息

使用python查看微信撤回消息

前段时间看到有一些用python查看微信撤回消息的文章,发现挺有意思的,便自己尝试了一下,实现步骤记录如下,想玩的也可以试一下。

 

 

1.安装itchat

itchat是一个开源的python微信库,支持发送消息、图片、视频、地图、名片、文件等,还可以实现自动回复等多种功能。之前我也写过一篇关于构建微信聊天机器人的文章,当时文章中的源码用的是wxpy库,其实python第三方库的安装步骤基本上都差不多,具体可见我之前的那篇文章https://blog.csdn.net/weixin_42281010/article/details/80468955

python3安装itchat:

pip3 install itchat

 

 

2.具体实现

运行环境: ubuntu

编译器: vim

运行程序:

扫描二维码,登陆web版微信

出现该提示,运行成功

开始测试

我让朋友发了两条消息,分别为文本和图片消息

程序将对方撤回的文本信息和图片均发送到文件传输助手

(除文本消息、图片外,对方撤回的视频、地图、名片、文件等信息也可在文件传输助手中查看)

 

 

3.源码

该源码来源于网络

  1 import os
  2 import re
  3 import shutil
  4 import time
  5 import itchat
  6 from itchat.content import *
  7 
  8 # 说明:可以撤回的有文本文字、语音、视频、图片、位置、名片、分享、附件
  9 
 10 # {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
 11 msg_dict = {}
 12 
 13 # 文件存储临时目录
 14 rev_tmp_dir = "xx/"
 15 if not os.path.exists(rev_tmp_dir):
 16     os.mkdir(rev_tmp_dir)
 17 
 18 # 表情有一个问题 | 接受信息和接受note的msg_id不一致 巧合解决方案
 19 face_bug = None
 20 
 21 # 将接收到的消息存放在字典中,当接收到新消息时对字典中超时的消息进行清理 | 不接受不具有撤回功能的信息
 22 # [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
 23 @itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])
 24 def handler_receive_msg(msg):
 25    global face_bug
 26    # 获取的是本地时间戳并格式化本地时间戳 e: 2017-04-21 21:30:08
 27    msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
 28    # 消息ID
 29    msg_id = msg['MsgId']
 30    print(msg_id)
 31    # 消息时间
 32    msg_time = msg['CreateTime']
 33    # 消息发送人昵称 | 这里也可以使用RemarkName备注 但是自己或者没有备注的人为None
 34    msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"]
 35    print(msg_from)
 36    # 消息内容
 37    msg_content = None
 38    # 分享的链接
 39    msg_share_url = None
 40    if msg['Type'] == 'Text'or msg['Type'] == 'Friends':
 41        msg_content = msg['Text']
 42        # itchat.send(msg_content, toUserName='filehelper')
 43    elif msg['Type'] == 'Recording'or msg['Type'] == 'Attachment'or msg['Type'] == 'Video'or msg['Type'] == 'Picture':
 44        msg_content = r"" + msg['FileName']
 45        # 保存文件
 46        msg['Text'](rev_tmp_dir + msg['FileName'])
 47    elif msg['Type'] == 'Card':
 48        msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
 49        # itchat.send(msg_content, toUserName='filehelper')
 50    elif msg['Type'] == 'Map':
 51        x, y, location = re.search('<location x="(.*?)" y="(.*?)".*label="(.*?)".*', msg['OriContent']).group(1, 2, 3)
 52        if location is None:
 53            msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()
 54        else:
 55            msg_content = r"" + location
 56    elif msg['Type'] == 'Sharing':
 57        msg_content = msg['Text']
 58        msg_share_url = msg['Url']
 59    face_bug = msg_content
 60    # 更新字典
 61    msg_dict.update(
 62        {
 63            msg_id: {
 64                "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
 65                "msg_type": msg["Type"],
 66                "msg_content": msg_content, "msg_share_url": msg_share_url
 67            }
 68        }
 69    )
 70    print(msg_dict)
 71 
 72 # 收到note通知类消息,判断是不是撤回并进行相应操作
 73 @itchat.msg_register([NOTE])
 74 def send_msg_helper(msg):
 75    global face_bug
 76    if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None:
 77        # 获取消息的id
 78        print("发现撤回消息")
 79        old_msg_id = re.search("<msgid>(.*?)</msgid>", msg['Content']).group(1)
 80        print(old_msg_id)
 81        old_msg = msg_dict.get(old_msg_id, {})
 82        print(old_msg_id)
 83        if len(old_msg_id) < 11:
 84            itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
 85            os.remove(rev_tmp_dir + face_bug)
 86        else:
 87            msg_body = "告诉你一个秘密~" + " " + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + " " + old_msg.get('msg_    time_rec') + " " + "撤回了什么 ⇣" + " " + r"" + old_msg.get('msg_content')
 88            # 如果是分享存在链接
 89            print(msg_body)
 90            if old_msg['msg_type'] == "Sharing": msg_body += " 就是这个链接➣ " + old_msg.get('msg_share_url')
 91 
 92            # 将撤回消息发送到文件助手
 93            itchat.send(msg_body, toUserName='filehelper')
 94            # 有文件的话也要将文件发送回去
 95            if old_msg["msg_type"] == "Picture" or old_msg["msg_type"] == "Recording" or old_msg["msg_type"] == "Video" or old_msg["msg_type"] == "    Attachment":
 96                file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
 97                itchat.send(msg=file, toUserName='filehelper')
 98                os.remove(rev_tmp_dir + old_msg['msg_content'])
 99            # 删除字典旧消息
100            msg_dict.pop(old_msg_id)
101 
102 if __name__ == '__main__':
103    itchat.auto_login(hotReload=True)
104    itchat.run()

                    

 

4.关于itchat的一些资料

需注意的是查看的撤回信息只有在程序运行时才有用,以前撤回的信息是看不到的。登陆的web版微信需是注册了一段时间的账号,用近期注册的微信号登陆则程序会报错。

其实itchat库除了查看微信撤回消息之外,与wxpy库类似,也可以构建一个微信聊天机器人,除此之外还有其他有趣的功能,我找了一些关于itchat的资料和文章,供大家参考。

http://www.php.cn/xiaochengxu-364486.html

https://itchat.readthedocs.io/zh/latest/

https://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/

https://blog.csdn.net/enweitech/article/details/79585043

https://segmentfault.com/a/1190000009420701#articleHeader25%20原文:python实现微信接口

 

 

  • 6
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值