最近比较闲就随便瞎看,看到了微信防撤回就顺便跟着学着实现一下
使用的是wxpy,安装方法pip install wxpy(我使用的是python2.7),这样实现起来比较快,反正也只是练手
首现看了两个别人实现的代码,然后看了看wxpy的文档:http://wxpy.readthedocs.io/zh/latest/utils.html(萌新,顺便锻炼下看文档)
我实现的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
import wxpy
import 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()
|
也未做过多的测试和优化,测试了文本、图片、语音、位置信息,视频、名片、分享、文件消息未做测试
ps:这里语音消息撤回后发出来的是语音文件,位置信息发出来的是地址的文本
======================割======================
如果想要添加聊天机器人 可以加入:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 指定一个好友
friend = bot.friends().search( '您对朋友的备注' )
# 指定一个群
group = bot.groups().search( '群名' )
# 实例化图灵机器人 免费申请图灵机器人: http://www.tuling123.com/
tu_ling = wxpy.Tuling(api_key = "自行注册" )
# chats指定对哪些用户起作用, 如果chats=None(默认值)对所有用户起作用
@bot .register(chats = [friend, group])
def reply_msg(msg):
"""
自动回复消息
:param msg: 接收到的信息数据
:return: 回复文本
"""
# do_reply会自动回复消息并返回消息文本
tu_ling.do_reply(msg)
|
以上这篇python使用wxpy轻松实现微信防撤回的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_41864652/article/details/80997092