如何使用 Python 实现微信消息防撤回
在微信上突然看到“XXX撤回一条消息”的时候,心里痒不痒?现在就教你如何实现消息防撤回的功能。不限手机型号、手机无需root,只要微信号可以登上网页版就可以了(部分不常用和新申请的账号会登不上网页版)。
环境
Centos7
python2.7
itchat
代码
# coding:utf-8
import itchat
from itchat.content import TEXT
from itchat.content import *
import sys
import time
import rereload(sys)
sys.setdefaultencoding('utf8')
import osmsg_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_bugmsg_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'] # 信息发送的时间msg_id = msg['MsgId'] # 每条信息的idmsg_content = None # 储存信息的内容msg_share_url = None # 储存分享的链接,比如分享的文章和音乐print msg['Type']print msg['MsgId']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)) # 下载文件# print msg_contentelif msg['Type'] == 'Card': # 如果消息是推荐的名片msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 内容就是推荐人的昵称和性别if msg['RecommendInfo']['Sex'] == 1:msg_content += '性别为男'else:msg_content += '性别为女'print msg_contentelif 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"" + locationelif msg['Type'] == 'Sharing': # 如果消息为分享的音乐或者文章,详细的内容为文章的标题或者是分享的名字msg_content = msg['Text']msg_share_url = msg['Url'] # 记录分享的urlprint msg_share_urlface_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) # 在返回的content查找撤回的消息的idold_msg = msg_information.get(old_msg_id) # 得到消息print old_msgif 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.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True)
def handle_receive_msg(msg):global face_bugmsg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的时间# groupid = msg['FromUserName']# chatroom = itchat.search_chatrooms(userName=groupid)msg_Actual_from = msg['ActualNickName']# msg_Actual_from = msg['User']# msg_from = msg_Actual_from['Self']['NickName']msg_from = msg_Actual_frommsg_time = msg['CreateTime'] # 信息发送的时间msg_id = msg['MsgId'] # 每条信息的idmsg_content = None # 储存信息的内容msg_share_url = None # 储存分享的链接,比如分享的文章和音乐print msg['Type']print msg['MsgId']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)) # 下载文件# print msg_contentelif msg['Type'] == 'Card': # 如果消息是推荐的名片msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 内容就是推荐人的昵称和性别if msg['RecommendInfo']['Sex'] == 1:msg_content += '性别为男'else:msg_content += '性别为女'print msg_contentelif 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"" + locationelif msg['Type'] == 'Sharing': # 如果消息为分享的音乐或者文章,详细的内容为文章的标题或者是分享的名字msg_content = msg['Text']msg_share_url = msg['Url'] # 记录分享的urlprint msg_share_urlface_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}})##这个是用于监听是否有Group消息撤回
@itchat.msg_register(NOTE, 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) # 在返回的content查找撤回的消息的idold_msg = msg_information.get(old_msg_id) # 得到消息print old_msgif 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)# Main (enableCmdQr = True 时,将会生成二维码图片,如 =2 时二维码乱码的话 改为1 即可
itchat.auto_login(enableCmdQR=2, hotReload=True)
itchat.run()
代码来源:知乎用户 shanewa
如果想看懂代码的话,在有python一点基础的前提下搞懂 Python 中的 装饰器 概念。下面是用到itchat中主要的一段代码
def msg_register(self, msgType, isFriendChat=False, isGroupChat=False, isMpChat=False):''' a decorator constructorreturn a specific decorator based on information given '''if not (isinstance(msgType, list) or isinstance(msgType, tuple)):msgType = [msgType]def _msg_register(fn):for _msgType in msgType:if isFriendChat:self.functionDict['FriendChat'][_msgType] = fnif isGroupChat:self.functionDict['GroupChat'][_msgType] = fnif isMpChat:self.functionDict['MpChat'][_msgType] = fnif not any((isFriendChat, isGroupChat, isMpChat)):self.functionDict['FriendChat'][_msgType] = fnreturn fnreturn _msg_register
运行
1.连接云服务器,安装pip
2.使用pip 安装 itchat库 (一个利用微信网页版api接口写的,有兴趣的话可以鼓捣鼓捣)
3.将脚本文件上传到云服务器
4.运行代码
1)扫码登录
2)登录成功
结束
可能好多人会说,我没有服务器,该怎么办!其实我放在服务器上是为了二十四小时都在运行,这样可以一直都在防撤回状态了。如果你也想这么做的话就参考下面这篇文章
来,教你们如何“薅羊毛”
可以“薅”好几台服务器。不过也可以先在自己电脑上试一试。
如何使用 Python 实现微信消息防撤回相关推荐
- Python实现微信消息防撤回
微信(WeChat)是腾讯公司于2011年1月21日推出的一款社交软件,8年时间微信做到日活10亿,日消息量450亿.在此期间微信也推出了不少的功能如:"摇一摇"."漂流 ...
- 微信防撤回python_python使用wxpy实现微信消息防撤回脚本
本文实例为大家分享了python实现微信消息防撤回的具体代码,供大家参考,具体内容如下 使用了sqlite3保存数据,当有人撤回消息时取出数据发送到文件传输助手. 文件的话会先保存到本地,语音会以文件 ...
- #Python #微信 #消息防撤回 Python实现微信防撤回
微信(WeChat)是腾讯公司于2011年1月21日推出的一款社交软件,8年时间微信做到日活10亿,日消息量450亿.在此期间微信也推出了不少的功能如:"摇一摇"."漂流 ...
- 微信防撤回python代码_python实现微信消息防撤回
微信(WeChat)是腾讯公司于2011年1月21日推出的一款社交软件,8年时间微信做到日活10亿,日消息量450亿.在此期间微信也推出了不少的功能如:"摇一摇"."漂 ...
- 微信消息防撤回,这款开源神器文字、语音、图片都支持撤回查看!
前言 微信在2014年的时候,发布的v5.3.1 版本中推出了消息撤回功能,用户可以选择撤回 2 分钟内发送的最后一条信息. 现在很多即时通讯的软件都有撤回这个功能. 腾讯为了照顾手残党,在微信和QQ ...
- 微信多开工具,微信防撤回、QQ防撤回、Tim防撤回工具,微信消息防撤回、QQ消息防撤回、Tim消息防撤回,无视撤回功能,不错过每一条消息
这是一款非常小巧精致的防撤回和微信多开工具,只有130kb的大小,但功能却非常强大. 一.软件简介 这是一个开源项目,程序源码全部开源,因此这款工具具有极高的安全性,毕竟代码就公开在大家的眼皮底下,谁 ...
- 用python实现语音的传输功能_用Python+WechatPCAPI实现微信消息防撤回
干货分享 干货 | Python学习资源整理分享mp.weixin.qq.com 引言 最近逛GitHub时发现了一个半开源的微信PC版的API接口WechatPCAPI,研究了一下,发现还是很好用 ...
- 用Python+WechatPCAPI实现微信消息防撤回
干货 二十多本Python好书,从入门到进阶,点这自取 引言 最近逛GitHub时发现了一个半开源的微信PC版的API接口WechatPCAPI,研究了一下,发现还是很好用的,所以就顺便用这个库写了个 ...
- 微信消息防撤回,这次做点不一样的
经过一段时间的研究,微信的防护措施果然名不虚传,稍有不慎就会面临封号的危险,所以对微信的逆向务必慎之又慎. 防撤回的教程网上已经很多了,但是今天要做些不一样的事情. 首先,如果让你来写撤回这个方法,你 ...
最新文章
- 2W 字详解 Redis 6.0 集群环境搭建实践
- tomcat1.8,tomcat-users.xml文件中如下修改才可以使用app manager登录,其中的roles有哪些枚举需要确认
- linux 查看软连接_linux删除原理
- python爬虫企业级技术点_Python爬虫必备技术点(二)
- ajax绑定事件页面重复提交,ajax防止用户重复提交点击事件
- 如何获取ubuntu源码包里面的源码进行编译
- 计算机视觉领域还能耍什么花样?
- 等保2.0丨2021 必须了解的40个问题
- linux、mysql、nginx、tomcat 环境下压力测试的主要调试参数
- STM32cubemx教程及STM32入门(四)串口通信
- regedit 导入注册表
- 通过Python计算经纬度点任意角度任意距离的经纬度点
- 分享一个很香的k8s.gcr.io Docker镜像拉取方法
- 使用POI和EasyExcel实现Excel导入和导出功能
- 黑崎一护为什么没有和朽木露琪亚在一起
- 图形学基础笔记III:图形管线中的多边形裁剪算法、Sutherland-Hodgman、Guard Band Clipping
- Thinkphp框架初始及安装部署
- pyecharts与mysql_pyecharts画图总结
- PTA-厘米换算英尺英寸
- 盘点 “互联网女皇” 看好的20家创新公司
热门文章
- 如何弄ad装配图_[分享][BCW]上海西康路189弄——“世界最美购物中心”幕墙的诞生...
- 使用缓冲流有什么好处_使用档案密集柜有什么好处?不看一看会后悔
- swift linux服务器,Swift on Linux —— Hello, world!
- Java获取元注解_Java 元注解
- 微型计算机一般只具有定点运算功能对吗,大学计算机基础模拟卷2及答案剖析.doc...
- mysql用大白话解释_大白话 golang 教程-22-关系型数据库访问
- labuladong的算法小抄pdf_东哥手写正则通配符算法,结构清晰,包教包会!
- matlab模拟砂轮形貌,基于多颗磨粒随机分布的虚拟砂轮建模及磨削力预测
- android图片上加有汉字,Android 为图片添加文字水印
- docker springboot读取配置文件_Docker从入门到掉坑(三):容器太多,操作好麻烦