python基于itchat模块实现微信防撤回


Posted in Python onApril 29, 2019

有时候,女神发来一条消息,说约你看电影,她考虑了一下,又撤回了,不约你了…而你又想知道她究竟发了什么,该怎么办?微信防撤回了解一下。

环境要求

Python3
电脑

安装itchat

pip install itchat

使用代码

新建chehui.py,拷贝以下代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'jiangwenwen'

import itchat
from itchat.content import *
import time
import re
import os

print("该程序由里客云资源站开发,网址:likeyunba.com")
print("作者:TANKING")
print("打开程序会弹出一个二维码,微信扫码")
print("如果二维码弹不出,那就在你这个程序的同一个目录下找到QR.png双击打开扫码")
print("扫码后,出现Start auto replying就可以实时监控消息了...")

msg_information = {}
# 针对表情包的内容
face_bug = None

@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, isMpChat=True)
def handle_receive_msg(msg):
 global face_bug
 # 接收消息的时间
 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
 # 在好友列表列表中查询发送信息的好友昵称
 msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName']
 # 信息发送的时间
 msg_time = msg['CreateTime']
 # 每条信息的ID
 msg_id = msg['MsgId']
 # 储存信息的内容
 msg_content = None
 # 储存分享的连接,比如分享的文章和音乐
 msg_share_url = None

 # 如果发送的消息是文本或者好友推荐
 if msg['Type'] == 'Text' or msg['Type'] == 'Friends':
 msg_content = msg['Text']
 print(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))

 # 如果消息是推荐的名片
 elif msg['Type'] == 'Card':
 # 内容是推荐人的昵称和性别
 msg_content = msg['RecommendInfo']['NickName'] + '的名片'
 if msg['RecommendInfo']['Sex'] == 1:
  msg_content += '性别为男'
 else:
  msg_content += '性别为女'

 print(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

 # 如果消息是分享的音乐或者文章,详细的内容为文章的标题或者分享的名字
 elif msg['Type'] == 'Sharing':
 msg_content = msg['Text']
 msg_share_url = msg['Url']
 print(msg_share_url)
 face_bug = msg_content

 # 将信息存储在字典中,每一个msg_id对应一条消息
 msg_information.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
  }
 }
)

#这个是用于监听是否有friend消息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def information(msg):
 # 这里如果这里的msg['Content']中包含消息撤回和id,就执行下面的语句
 if '撤回了一条消息' in msg['Content']:
 old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
 # 得到消息
 old_msg = msg_information.get(old_msg_id)
 print(old_msg)

 # 如果发送的是表情
 if len(old_msg_id)<11:
  itchat.send_file(face_bug, toUserName='filehelper')
 # 发送撤回的提示给文件助手
 else:
  msg_body = "【"\
   + old_msg.get('msg_from') + "撤回了】\n"\
   + old_msg.get("msg_type") + "消息:" + "\n"\
   + old_msg.get("msg_time_rec") + "\n"\
   + r"" + old_msg.get("msg_content")

 # 如果分享的文件被撤回了,那么就将分享的url加在msg_body中发送给文件助手
 if old_msg['msg_type'] == "Sharing":
  msg_body += "\n就是这个链接>" + old_msg.get('msg_share_url')

 # 将撤回消息发送到文件助手
 itchat.send_msg(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" % (old_msg['msg_content'])
  itchat.send(msg=file, toUserName='filehelper')
  os.remove(old_msg['msg_content'])

 # 删除字典旧信息
 msg_information.pop(old_msg_id)

itchat.auto_login(hotReload=True)
itchat.run()

CMD运行即可。

考虑到有一些人没有Python环境,我已经打包成可执行文件了,直接双击exe就可以在电脑运行。

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

Python 相关文章推荐
Django集成百度富文本编辑器uEditor攻略
Jul 04 Python
Python中的深拷贝和浅拷贝详解
Jun 03 Python
Python卸载模块的方法汇总
Jun 07 Python
在Python中获取两数相除的商和余数方法
Nov 10 Python
python3对接mysql数据库实例详解
Apr 30 Python
python图形用户接口实例详解
Dec 16 Python
Python requests模块基础使用方法实例及高级应用(自动登陆,抓取网页源码)实例详解
Feb 14 Python
Pycharm配置lua编译环境过程图解
Nov 28 Python
call在Python中改进数列的实例讲解
Dec 09 Python
Python中for后接else的语法使用
May 18 Python
python利用while求100内的整数和方式
Nov 07 Python
Python IO文件管理的具体使用
Mar 20 Python
手把手教你使用Python创建微信机器人
Apr 29 #Python
python实现微信防撤回神器
Apr 29 #Python
python实现文件助手中查看微信撤回消息
Apr 29 #Python
Python实现微信消息防撤回功能的实例代码
Apr 29 #Python
python控制nao机器人身体动作实例详解
Apr 29 #Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 #Python
解决Python找不到ssl模块问题 No module named _ssl的方法
Apr 29 #Python
You might like
收音机的保养
2021/03/01 无线电
Nginx下配置codeigniter框架方法
2015/04/07 PHP
js不是基础的基础
2006/12/24 Javascript
asp.net下利用js实现返回上一页的实现方法小集
2009/11/24 Javascript
Jquery选择器 $实现原理
2009/12/02 Javascript
用nodejs访问ActiveX对象,以操作Access数据库为例。
2011/12/15 NodeJs
Javascript selection的兼容性写法介绍
2013/12/20 Javascript
Firefox中使用outerHTML的2种解决方法
2014/06/07 Javascript
jquery实现人性化的有选择性禁用鼠标右键
2014/06/30 Javascript
百度UEditor编辑器如何关闭抓取远程图片功能
2015/03/03 Javascript
JavaScript数组前面插入元素的方法
2015/04/06 Javascript
jquery+css3实现网页背景花瓣随机飘落特效
2015/08/17 Javascript
JavaScript操作HTML元素和样式的方法详解
2015/10/21 Javascript
JS如何生成一个不重复的ID的函数
2016/12/25 Javascript
VueJs路由跳转——vue-router的使用详解
2017/01/10 Javascript
那些精彩的JavaScript代码片段
2017/01/12 Javascript
Node.js中的不安全跳转如何防御详解
2018/10/21 Javascript
vue 实现v-for循环回来的数据动态绑定id
2019/11/07 Javascript
javascript实现评分功能
2020/06/24 Javascript
[00:56]2014DOTA2国际邀请赛 DK、iG 赛前探访
2014/07/10 DOTA
Python pass 语句使用示例
2014/03/11 Python
Python 中urls.py:URL dispatcher(路由配置文件)详解
2017/03/24 Python
Python wxPython库消息对话框MessageDialog用法示例
2018/09/03 Python
django创建超级用户过程解析
2019/09/18 Python
keras 指定程序在某块卡上训练实例
2020/06/22 Python
python把一个字符串切开的实例方法
2020/09/27 Python
CSS3中的content属性使用示例
2015/07/20 HTML / CSS
Holland & Barrett爱尔兰:英国领先的健康零售商
2019/03/31 全球购物
ajax是什么及其工作原理
2012/02/08 面试题
高二地理教学反思
2014/01/24 职场文书
烹调加工管理制度
2014/02/04 职场文书
小学生暑假家长评语
2014/04/17 职场文书
志愿者爱心公益活动策划方案
2014/09/15 职场文书
2015届本科毕业生自我鉴定
2014/09/27 职场文书
社保代办委托书怎么写
2014/10/06 职场文书
基于Python实现nc批量转tif格式
2022/08/14 Python