python实现文件助手中查看微信撤回消息


Posted in Python onApril 29, 2019

利用python实现防撤回,对方撤回的消息可在自己的微信文件传输助手中查看。

如果想变成可执行文件放在电脑中运行,可用pyinstaller将此程序打包成exe文件。

pyinstaller 文件名.py -F

执行程序后,消息防撤回就启动了。

程序完整代码

# -*-encoding:utf-8-*-
import os
import re
import shutil
import time
import itchat
from itchat.content import *

# 说明:可以撤回的有文本文字、语音、视频、图片、位置、名片、分享、附件

# {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
msg_dict = {}

# 文件存储临时目录
rev_tmp_dir = r"G:\python代码\微信\撤回到文件助手"
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(
   "<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
 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"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None:
  # 获取消息的id
  old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
  old_msg = msg_dict.get(old_msg_id, {})
  if len(old_msg_id) < 11:
   itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
   os.remove(rev_tmp_dir + face_bug)
  else:
   msg_body = "告诉你一个秘密~" + "\n" \
      + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \
      + old_msg.get('msg_time_rec') + "\n" \
      + "撤回了什么 ⇣" + "\n" \
      + r"" + old_msg.get('msg_content')
   # 如果是分享存在链接
   if old_msg['msg_type'] == "Sharing": msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url')

   # 将撤回消息发送到文件助手
   itchat.send(msg_body, toUserName='filehelper')
   # 有文件的话也要将文件发送回去
   if old_msg["msg_type"] == "Picture" \
     or old_msg["msg_type"] == "Recording" \
     or old_msg["msg_type"] == "Video" \
     or old_msg["msg_type"] == "Attachment":
    file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
    itchat.send(msg=file, toUserName='filehelper')
    os.remove(rev_tmp_dir + old_msg['msg_content'])
   # 删除字典旧消息
   msg_dict.pop(old_msg_id)

if __name__ == '__main__':
 itchat.auto_login()
 itchat.get_QR
 itchat.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python操作MongoDB基础知识
Nov 01 Python
Python中实现对list做减法操作介绍
Jan 09 Python
Python实现比较两个列表(list)范围
Jun 12 Python
Python中用psycopg2模块操作PostgreSQL方法
Nov 28 Python
浅谈python连续赋值可能引发的错误
Nov 10 Python
python 将字符串完成特定的向右移动方法
Jun 11 Python
详解pyinstaller selenium python3 chrome打包问题
Oct 18 Python
Django 实现外键去除自动添加的后缀‘_id’
Nov 15 Python
基于pytorch 预训练的词向量用法详解
Jan 06 Python
Python 使用双重循环打印图形菱形操作
Aug 09 Python
Python Parser的用法
May 12 Python
Python代码实现双链表
May 25 Python
Python实现微信消息防撤回功能的实例代码
Apr 29 #Python
python控制nao机器人身体动作实例详解
Apr 29 #Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 #Python
解决Python找不到ssl模块问题 No module named _ssl的方法
Apr 29 #Python
GitHub 热门:Python 算法大全,Star 超过 2 万
Apr 29 #Python
python实现nao机器人手臂动作控制
Apr 29 #Python
python机器人运动范围问题的解答
Apr 29 #Python
You might like
PHP在字符串中查找指定字符串并删除的代码
2008/10/02 PHP
PHP实现上传多文件示例代码
2017/02/20 PHP
搜索附近的人PHP实现代码
2018/02/11 PHP
超级强大的表单验证
2006/06/26 Javascript
爆炸式的JS圆形浮动菜单特效代码
2010/03/03 Javascript
YUI的Tab切换实现代码
2010/04/11 Javascript
js 判断一个元素是否在页面中存在
2012/12/27 Javascript
Jquery获取第一个子元素简单实例
2016/06/02 Javascript
ros::spin() 和 ros::spinOnce()函数的区别及详解
2016/10/01 Javascript
javascript跨域请求包装函数与用法示例
2016/11/03 Javascript
微信小程序 数据交互与渲染实例详解
2017/01/21 Javascript
微信小程序 数据遍历的实现
2017/04/05 Javascript
正则表达式基本语法及表单验证操作详解【基于JS】
2017/04/07 Javascript
vue左侧菜单,树形图递归实现代码
2018/08/24 Javascript
JS XMLHttpRequest原理与使用方法深入详解
2020/04/30 Javascript
nuxt 路由、过渡特效、中间件的实现代码
2020/11/06 Javascript
介绍Python中几个常用的类方法
2015/04/08 Python
Python使用matplotlib实现的图像读取、切割裁剪功能示例
2018/04/28 Python
python 统计列表中不同元素的数量方法
2018/06/29 Python
python 简单照相机调用系统摄像头实现方法 pygame
2018/08/03 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
pyqt5 从本地选择图片 并显示在label上的实例
2019/06/13 Python
Python创建或生成列表的操作方法
2019/06/19 Python
Python urllib库如何添加headers过程解析
2020/10/05 Python
Python urllib request模块发送请求实现过程解析
2020/12/10 Python
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
英国天然抗衰老护肤品品牌:Nakin Skin Care
2019/04/16 全球购物
屈臣氏马来西亚官网:Watsons马来西亚
2019/06/15 全球购物
俄罗斯金苹果网上化妆品和香水商店:Goldapple
2019/12/01 全球购物
工商管理专业实习生自我鉴定
2013/09/29 职场文书
保护环境倡议书500字
2014/05/19 职场文书
幼儿园小班教师随笔
2015/08/14 职场文书
2016年校园社会综合治理宣传月活动总结
2016/03/16 职场文书
详解Python常用的魔法方法
2021/06/03 Python
Python实现生活常识解答机器人
2021/06/28 Python
CentOS7环境下MySQL8常用命令小结
2022/06/10 Servers