python 微信二维码接口
python实现微信防撤回神器本文实例为大家分享了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\>(.*?)\<\/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) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/nmyphp/article/details/84582914