python使用wxpy轻松实现微信防撤回的方法
最近比较闲就随便瞎看,看到了微信防撤回就顺便跟着学着实现一下
使用的是wxpy,安装方法pipinstallwxpy(我使用的是python2.7),这样实现起来比较快,反正也只是练手
首现看了两个别人实现的代码,然后看了看wxpy的文档:http://wxpy.readthedocs.io/zh/latest/utils.html(萌新,顺便锻炼下看文档)
我实现的代码:
importwxpy
importre
#实例化微信对象
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()
defhandle_receive_msg(msg):
"""
监听消息
:parammsg:接收到的消息
:return:
"""
#原信息数据
raw=msg.raw
#如果消息的状态码是4即撤回消息
ifraw.get('Status')==4:
#如果是群消息
ifisinstance(msg.chat,wxpy.api.chats.group.Group):
#获取群成员的puid
puid=msg.member.puid
#获取发送者的昵称
name=msg.member.nick_name
#如果是好友消息
elifisinstance(msg.chat,wxpy.api.chats.friend.Friend):
#获取好友的puid
puid=msg.chat.puid
#获取好友的昵称
name=msg.chat.nick_name
else:
puid=None
name=None
ifpuid:
#被撤回消息的msgid的匹配规则
msg_id_regex=re.compile('(\d+) ')
#获取被撤回消息的msgid
old_msg_id=msg_id_regex.findall(raw.get('Content'))[0]
#获取该发送者的最后5次的消息记录
chat_msgs=msgs.get(puid)
#遍历消息记录
forchat_msginchat_msgs[::-1]:
#跳过不是被撤回的信息
ifstr(chat_msg.id)!=old_msg_id:
continue
chat=chat_msg.chat
#如果被撤回的信息是文本信息
ifchat_msg.type=="Text":
#如果消息长度过长则不予处理
iflen(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)
#如果被撤回的是位置信息
elifchat_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:
#如果是群消息
ifisinstance(msg.chat,wxpy.api.chats.group.Group):
#获取群成员的puid
puid=msg.member.puid
#如果是好友消息
elifisinstance(msg.chat,wxpy.api.chats.friend.Friend):
#获取好友的puid
puid=msg.chat.puid
else:
puid=None
ifpuid:
#记录消息
msgs.setdefault(puid,[]).append(msg)
#截取消息保留最大5条记录
msgs[puid]=msg[puid][-5:]
#使机器人后台运行,并进入交互模式
wxpy.embed()
也未做过多的测试和优化,测试了文本、图片、语音、位置信息,视频、名片、分享、文件消息未做测试
ps:这里语音消息撤回后发出来的是语音文件,位置信息发出来的是地址的文本
======================割======================
如果想要添加聊天机器人可以加入:
#指定一个好友
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])
defreply_msg(msg):
"""
自动回复消息
:parammsg:接收到的信息数据
:return:回复文本
"""
#do_reply会自动回复消息并返回消息文本
tu_ling.do_reply(msg)
以上这篇python使用wxpy轻松实现微信防撤回的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。