本文实例为大家分享了python实现微信防撤回神器的具体代码,供大家参考,具体内容如下
手写辛苦,希望给赞
#!/usr/local/bin/python3 # coding=utf-8 import os import re import time import _thread import itchat from itchat.content import * # 可以撤回的消息格式:文本、语音、视频、图片、位置、名片、分享、附件 # 存储收到的消息 # 格式:{msg_id:{msg_from,msg_to,msg_time,msg_time_rec,msg_tye,msg_content,msg_share_url}} msg_dict = {} # 存储消息中文件的临时目录,程序启动时,先清空 rev_tmp_dir = "/Users/chenlong/d1/wechat/rev/" if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir) else: for f in os.listdir(rev_tmp_dir): path = os.path.join(rev_tmp_dir, f) if os.path.isfile(path): os.remove(path) # 表情有一个问题:消息和撤回提示的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, FRIENDS, NOTE], isFriendChat=True, isGroupChat=True, isMpChat=True) def handler_reveive_msg(msg): global face_bug msg_time_rev = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) msg_id = msg['MsgId'] msg_time = msg['CreateTime'] msg_share_url = None group_name = None # 获取发送人 if 'ActualNickName' in msg: sender_info = set_sender_group_chat(msg) msg_from = sender_info['msg_from'] group_name = sender_info['group_name'] else: msg_from = (itchat.search_friends(userName=msg['FromUserName']))['RemarkName'] # 优先使用备注 if msg_from is None: msg_from = msg['FromUserName'] # 获取消息内容 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 + " 经度->" + y 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_rev": msg_time_rev, "msg_type": msg['Type'], "msg_content": msg_content, "msg_share_url": msg_share_url, "group_name": group_name } }) # 遍历本地消息字典,清除2分钟之前的消息,并删除缓存的消息对应的文件 def clear_timeout_msg(): need_del_msg_ids = [] for m in msg_dict: msg_time = msg_dict[m]['msg_time'] if int(time.time()) - msg_time > 120: need_del_msg_ids.append(m) if len(need_del_msg_ids) > 0: for i in need_del_msg_ids: old_msg = msg_dict.get(i) if old_msg['msg_type'] == PICTURE or old_msg['msg_type'] == RECORDING or old_msg['msg_type'] == VIDEO or old_msg['msg_type'] == ATTACHMENT: os.remove(rev_tmp_dir + old_msg['msg_content']) msg_dict.pop(i) # 设置发送人,当消息是群消息的时候 def set_sender_group_chat(msg): msg_from = msg['ActualNickName'] # 查找用户备注名称 friends = itchat.get_friends(update=True) from_user = msg['ActualUserName'] for f in friends: if from_user == f['UserName']: msg_from = f['RemarkName'] or f['NickName'] break groups = itchat.get_chatrooms(update=True) for g in groups: if msg['FromUserName'] == g['UserName']: group_name = g['NickName'] break return {'msg_from': msg_from, 'group_name': group_name} # 监听通知,判断是撤回通知,则将消息发给文件助手 @itchat.msg_register([NOTE], isFriendChat=True, isGroupChat=True, isMpChat=True) def send_msg_helper(msg): global face_bug if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None: old_msg_id = re.search("\<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 = old_msg.get('msg_from') + "撤回了" + old_msg.get('msg_type') + "消息\n" + old_msg.get('msg_time_rev') + "\n" + old_msg.get('msg_content') if old_msg.get('group_name') is not None: msg_body = old_msg.get('group_name') + ">" + msg_body 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(hotReload=True, enableCmdQR=2) itchat.run() # 子线程清除超时消息 _thread.start_new_thread(clear_timeout_msg)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]