1. 程式人生 > >微信防撤回

微信防撤回

con rev hand sts location end pat msg alt

  1 # -*-encoding:utf-8-*-
  2 import os
  3 import re
  4 import shutil
  5 import time
  6 import itchat
  7 from itchat.content import *
  8 
  9 # 說明:可以撤回的有文本文字、語音、視頻、圖片、位置、名片、分享、附件
 10 
 11 # {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
 12 msg_dict = {}
13 14 # 文件存儲臨時目錄 15 rev_tmp_dir = "D:\PycharmProjects\CRM_System\臨時文件" 16 if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir) 17 18 # 表情有一個問題 | 接受信息和接受note的msg_id不一致 巧合解決方案 19 face_bug = None 20 21 22 # 將接收到的消息存放在字典中,當接收到新消息時對字典中超時的消息進行清理 | 不接受不具有撤回功能的信息 23 # [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
24 @itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO]) 25 def handler_receive_msg(msg): 26 global face_bug 27 # 獲取的是本地時間戳並格式化本地時間戳 e: 2017-04-21 21:30:08 28 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 29 # 消息ID 30 msg_id = msg[
MsgId] 31 # 消息時間 32 msg_time = msg[CreateTime] 33 # 消息發送人昵稱 | 這裏也可以使用RemarkName備註 但是自己或者沒有備註的人為None 34 msg_from = (itchat.search_friends(userName=msg[FromUserName]))["NickName"] 35 # 消息內容 36 msg_content = None 37 # 分享的鏈接 38 msg_share_url = None 39 if msg[Type] == Text 40 or msg[Type] == Friends: 41 msg_content = msg[Text] 42 elif msg[Type] == Recording 43 or msg[Type] == Attachment 44 or msg[Type] == Video 45 or msg[Type] == Picture: 46 msg_content = r"" + msg[FileName] 47 # 保存文件 48 msg[Text](rev_tmp_dir + msg[FileName]) 49 elif msg[Type] == Card: 50 msg_content = msg[RecommendInfo][NickName] + r" 的名片" 51 elif msg[Type] == Map: 52 x, y, location = re.search( 53 "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg[OriContent]).group(1, 2, 3) 54 if location is None: 55 msg_content = r"緯度->" + x.__str__() + " 經度->" + y.__str__() 56 else: 57 msg_content = r"" + location 58 elif msg[Type] == Sharing: 59 msg_content = msg[Text] 60 msg_share_url = msg[Url] 61 face_bug = msg_content 62 # 更新字典 63 msg_dict.update( 64 { 65 msg_id: { 66 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, 67 "msg_type": msg["Type"], 68 "msg_content": msg_content, "msg_share_url": msg_share_url 69 } 70 } 71 ) 72 73 # 收到note通知類消息,判斷是不是撤回並進行相應操作 74 @itchat.msg_register([NOTE]) 75 def send_msg_helper(msg): 76 global face_bug 77 if re.search(r"\<\!\[CDATA\[.*撤回了一條消息\]\]\>", msg[Content]) is not None: 78 # 獲取消息的id 79 old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg[Content]).group(1) 80 old_msg = msg_dict.get(old_msg_id, {}) 81 if len(old_msg_id) < 11: 82 itchat.send_file(rev_tmp_dir + face_bug, toUserName=filehelper) 83 os.remove(rev_tmp_dir + face_bug) 84 else: 85 msg_body = "告訴你一個秘密~" + "\n" 86 + old_msg.get(msg_from) + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" 87 + old_msg.get(msg_time_rec) + "\n" 88 + "撤回了什麽 ?" + "\n" 89 + r"" + old_msg.get(msg_content) 90 # 如果是分享存在鏈接 91 if old_msg[msg_type] == "Sharing": msg_body += "\n就是這個鏈接? " + old_msg.get(msg_share_url) 92 93 # 將撤回消息發送到文件助手 94 itchat.send(msg_body, toUserName=filehelper) 95 # 有文件的話也要將文件發送回去 96 if old_msg["msg_type"] == "Picture" 97 or old_msg["msg_type"] == "Recording" 98 or old_msg["msg_type"] == "Video" 99 or old_msg["msg_type"] == "Attachment": 100 file = @fil@%s % (rev_tmp_dir + old_msg[msg_content]) 101 itchat.send(msg=file, toUserName=filehelper) 102 os.remove(rev_tmp_dir + old_msg[msg_content]) 103 # 刪除字典舊消息 104 msg_dict.pop(old_msg_id) 105 106 107 if __name__ == __main__: 108 itchat.auto_login(hotReload=True) 109 itchat.run()

微信防撤回