對方發送過來的消息,我們通過dict進行保存,記錄消息id和對應的消息內容,當對方撤回消息的時候,我們根據檢測到的消息id,找到對應的dict中的消息內容,實現撤回的消息精准復原。
撤回的消息發送到文件傳輸助手
1 # coding:utf-8 2 import itchat 3 from itchat.content import TEXT 4 from itchat.content import * 5 import sys 6 import time 7 import re 8 9 reload(sys) 10 sys.setdefaultencoding('utf8') 11 import os 12 13 msg_information = {} 14 face_bug = None # 針對表情包的內容 15 16 17 @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, 18 isMpChat=True) 19 def handle_receive_msg(msg): 20 global face_bug 21 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的時間 22 msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] # 在好友列表中查詢發送信息的好友昵稱 23 msg_time = msg['CreateTime'] # 信息發送的時間 24 msg_id = msg['MsgId'] # 每條信息的id 25 msg_content = None # 儲存信息的內容 26 msg_share_url = None # 儲存分享的鏈接,比如分享的文章和音樂 27 print msg['Type'] 28 print msg['MsgId'] 29 if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發送的消息是文本或者好友推薦 30 msg_content = msg['Text'] 31 print msg_content 32 33 # 如果發送的消息是附件、視屏、圖片、語音 34 elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \ 35 or msg['Type'] == 'Picture' \ 36 or msg['Type'] == 'Recording': 37 msg_content = msg['FileName'] # 內容就是他們的文件名 38 msg['Text'](str(msg_content)) # 下載文件 39 # print msg_content 40 elif msg['Type'] == 'Card': # 如果消息是推薦的名片 41 msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內容就是推薦人的昵稱和性別 42 if msg['RecommendInfo']['Sex'] == 1: 43 msg_content += '性別為男' 44 else: 45 msg_content += '性別為女' 46 47 print msg_content 48 elif msg['Type'] == 'Map': # 如果消息為分享的位置信息 49 x, y, location = re.search( 50 "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3) 51 if location is None: 52 msg_content = r"緯度->" + x.__str__() + " 經度->" + y.__str__() # 內容為詳細的地址 53 else: 54 msg_content = r"" + location 55 elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細的內容為文章的標題或者是分享的名字 56 msg_content = msg['Text'] 57 msg_share_url = msg['Url'] # 記錄分享的url 58 print msg_share_url 59 face_bug = msg_content 60 61 ##將信息存儲在字典中,每一個msg_id對應一條信息 62 msg_information.update( 63 { 64 msg_id: { 65 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, 66 "msg_type": msg["Type"], 67 "msg_content": msg_content, "msg_share_url": msg_share_url 68 } 69 } 70 ) 71 72 73 ##這個是用於監聽是否有friend消息撤回 74 @itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True) 75 def information(msg): 76 # 這里如果這里的msg['Content']中包含消息撤回和id,就執行下面的語句 77 if '撤回了一條消息' in msg['Content']: 78 old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 在返回的content查找撤回的消息的id 79 old_msg = msg_information.get(old_msg_id) # 得到消息 80 print old_msg 81 if len(old_msg_id) < 11: # 如果發送的是表情包 82 itchat.send_file(face_bug, toUserName='filehelper') 83 else: # 發送撤回的提示給文件助手 84 msg_body = "【" \ 85 + old_msg.get('msg_from') + " 撤回了 】\n" \ 86 + old_msg.get("msg_type") + " 消息:" + "\n" \ 87 + old_msg.get('msg_time_rec') + "\n" \ 88 + r"" + old_msg.get('msg_content') 89 # 如果是分享的文件被撤回了,那么就將分享的url加在msg_body中發送給文件助手 90 if old_msg['msg_type'] == "Sharing": 91 msg_body += "\n就是這個鏈接➣ " + old_msg.get('msg_share_url') 92 93 # 將撤回消息發送到文件助手 94 itchat.send_msg(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' % (old_msg['msg_content']) 101 itchat.send(msg=file, toUserName='filehelper') 102 os.remove(old_msg['msg_content']) 103 # 刪除字典舊消息 104 msg_information.pop(old_msg_id) 105 106 107 @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True) 108 def handle_receive_msg(msg): 109 global face_bug 110 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的時間 111 # groupid = msg['FromUserName'] 112 # chatroom = itchat.search_chatrooms(userName=groupid) 113 msg_Actual_from = msg['ActualNickName'] 114 # msg_Actual_from = msg['User'] 115 # msg_from = msg_Actual_from['Self']['NickName'] 116 msg_from = msg_Actual_from 117 msg_time = msg['CreateTime'] # 信息發送的時間 118 msg_id = msg['MsgId'] # 每條信息的id 119 msg_content = None # 儲存信息的內容 120 msg_share_url = None # 儲存分享的鏈接,比如分享的文章和音樂 121 print msg['Type'] 122 print msg['MsgId'] 123 if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發送的消息是文本或者好友推薦 124 msg_content = msg['Text'] 125 print msg_content 126 127 # 如果發送的消息是附件、視屏、圖片、語音 128 elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \ 129 or msg['Type'] == 'Picture' \ 130 or msg['Type'] == 'Recording': 131 msg_content = msg['FileName'] # 內容就是他們的文件名 132 msg['Text'](str(msg_content)) # 下載文件 133 # print msg_content 134 elif msg['Type'] == 'Card': # 如果消息是推薦的名片 135 msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內容就是推薦人的昵稱和性別 136 if msg['RecommendInfo']['Sex'] == 1: 137 msg_content += '性別為男' 138 else: 139 msg_content += '性別為女' 140 141 print msg_content 142 elif msg['Type'] == 'Map': # 如果消息為分享的位置信息 143 x, y, location = re.search( 144 "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3) 145 if location is None: 146 msg_content = r"緯度->" + x.__str__() + " 經度->" + y.__str__() # 內容為詳細的地址 147 else: 148 msg_content = r"" + location 149 elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細的內容為文章的標題或者是分享的名字 150 msg_content = msg['Text'] 151 msg_share_url = msg['Url'] # 記錄分享的url 152 print msg_share_url 153 face_bug = msg_content 154 155 ##將信息存儲在字典中,每一個msg_id對應一條信息 156 msg_information.update( 157 { 158 msg_id: { 159 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, 160 "msg_type": msg["Type"], 161 "msg_content": msg_content, "msg_share_url": msg_share_url 162 } 163 } 164 ) 165 166 167 ##這個是用於監聽是否有Group消息撤回 168 @itchat.msg_register(NOTE, isGroupChat=True, isMpChat=True) 169 def information(msg): 170 171 # 這里如果這里的msg['Content']中包含消息撤回和id,就執行下面的語句 172 if '撤回了一條消息' in msg['Content']: 173 old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 在返回的content查找撤回的消息的id 174 old_msg = msg_information.get(old_msg_id) # 得到消息 175 print old_msg 176 if len(old_msg_id) < 11: # 如果發送的是表情包 177 itchat.send_file(face_bug, toUserName='filehelper') 178 else: # 發送撤回的提示給文件助手 179 msg_body = "【" \ 180 + old_msg.get('msg_from') + " 群消息撤回提醒】\n" \ 181 + " 撤回了 " + old_msg.get("msg_type") + " 消息:" + "\n" \ 182 + old_msg.get('msg_time_rec') + "\n" \ 183 + r"" + old_msg.get('msg_content') 184 # 如果是分享的文件被撤回了,那么就將分享的url加在msg_body中發送給文件助手 185 if old_msg['msg_type'] == "Sharing": 186 msg_body += "\n就是這個鏈接➣ " + old_msg.get('msg_share_url') 187 188 # 將撤回消息發送到文件助手 189 itchat.send_msg(msg_body, toUserName='filehelper') 190 # 有文件的話也要將文件發送回去 191 if old_msg["msg_type"] == "Picture" \ 192 or old_msg["msg_type"] == "Recording" \ 193 or old_msg["msg_type"] == "Video" \ 194 or old_msg["msg_type"] == "Attachment": 195 file = '@fil@%s' % (old_msg['msg_content']) 196 itchat.send(msg=file, toUserName='filehelper') 197 os.remove(old_msg['msg_content']) 198 # 刪除字典舊消息 199 msg_information.pop(old_msg_id) 200 201 202 # Main (enableCmdQr = True 時,將會生成二維碼圖片,如 =2 時二維碼亂碼的話 改為1 即可 203 itchat.auto_login(enableCmdQR=2, hotReload=True) 204 itchat.run()
撤回的消息發送到原處
1 # coding:utf-8 2 import itchat 3 from itchat.content import TEXT 4 from itchat.content import * 5 import sys 6 import time 7 import re 8 9 reload(sys) 10 sys.setdefaultencoding('utf8') 11 import os 12 13 msg_information = {} 14 face_bug = None # 針對表情包的內容 15 16 17 @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, 18 isMpChat=True) 19 def handle_receive_msg(msg): 20 global face_bug 21 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的時間 22 msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] # 在好友列表中查詢發送信息的好友昵稱 23 msg_time = msg['CreateTime'] # 信息發送的時間 24 msg_id = msg['MsgId'] # 每條信息的id 25 msg_content = None # 儲存信息的內容 26 msg_share_url = None # 儲存分享的鏈接,比如分享的文章和音樂 27 print msg['Type'] 28 print msg['MsgId'] 29 if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發送的消息是文本或者好友推薦 30 msg_content = msg['Text'] 31 print msg_content 32 33 # 如果發送的消息是附件、視屏、圖片、語音 34 elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \ 35 or msg['Type'] == 'Picture' \ 36 or msg['Type'] == 'Recording': 37 msg_content = msg['FileName'] # 內容就是他們的文件名 38 msg['Text'](str(msg_content)) # 下載文件 39 # print msg_content 40 elif msg['Type'] == 'Card': # 如果消息是推薦的名片 41 msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內容就是推薦人的昵稱和性別 42 if msg['RecommendInfo']['Sex'] == 1: 43 msg_content += '性別為男' 44 else: 45 msg_content += '性別為女' 46 47 print msg_content 48 elif msg['Type'] == 'Map': # 如果消息為分享的位置信息 49 x, y, location = re.search( 50 "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3) 51 if location is None: 52 msg_content = r"緯度->" + x.__str__() + " 經度->" + y.__str__() # 內容為詳細的地址 53 else: 54 msg_content = r"" + location 55 elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細的內容為文章的標題或者是分享的名字 56 msg_content = msg['Text'] 57 msg_share_url = msg['Url'] # 記錄分享的url 58 print msg_share_url 59 face_bug = msg_content 60 61 ##將信息存儲在字典中,每一個msg_id對應一條信息 62 msg_information.update( 63 { 64 msg_id: { 65 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, 66 "msg_type": msg["Type"], 67 "msg_content": msg_content, "msg_share_url": msg_share_url 68 } 69 } 70 ) 71 72 73 ##這個是用於監聽是否有friend消息撤回 74 @itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True) 75 def information(msg): 76 # 這里如果這里的msg['Content']中包含消息撤回和id,就執行下面的語句 77 if '撤回了一條消息' in msg['Content']: 78 old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 在返回的content查找撤回的消息的id 79 old_msg = msg_information.get(old_msg_id) # 得到消息 80 print old_msg 81 if len(old_msg_id) < 11: # 如果發送的是表情包 82 itchat.send_file(face_bug, toUserName=msg['FromUserName']) 83 else: # 發送撤回的提示給文件助手 84 msg_body = "【" \ 85 + old_msg.get('msg_from') + " 撤回了 】\n" \ 86 + old_msg.get("msg_type") + " 消息:" + "\n" \ 87 + old_msg.get('msg_time_rec') + "\n" \ 88 + r"" + old_msg.get('msg_content') 89 # 如果是分享的文件被撤回了,那么就將分享的url加在msg_body中發送給文件助手 90 if old_msg['msg_type'] == "Sharing": 91 msg_body += "\n就是這個鏈接➣ " + old_msg.get('msg_share_url') 92 93 # 將撤回消息發送到文件助手 94 itchat.send_msg(msg_body, toUserName=msg['FromUserName']) 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' % (old_msg['msg_content']) 101 itchat.send(msg=file, toUserName=msg['FromUserName']) 102 os.remove(old_msg['msg_content']) 103 # 刪除字典舊消息 104 msg_information.pop(old_msg_id) 105 106 107 @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True) 108 def handle_receive_msg(msg): 109 global face_bug 110 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的時間 111 # groupid = msg['FromUserName'] 112 # chatroom = itchat.search_chatrooms(userName=groupid) 113 msg_Actual_from = msg['ActualNickName'] 114 # msg_Actual_from = msg['User'] 115 # msg_from = msg_Actual_from['Self']['NickName'] 116 msg_from = msg_Actual_from 117 msg_time = msg['CreateTime'] # 信息發送的時間 118 msg_id = msg['MsgId'] # 每條信息的id 119 msg_content = None # 儲存信息的內容 120 msg_share_url = None # 儲存分享的鏈接,比如分享的文章和音樂 121 print msg['Type'] 122 print msg['MsgId'] 123 if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發送的消息是文本或者好友推薦 124 msg_content = msg['Text'] 125 print msg_content 126 127 # 如果發送的消息是附件、視屏、圖片、語音 128 elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \ 129 or msg['Type'] == 'Picture' \ 130 or msg['Type'] == 'Recording': 131 msg_content = msg['FileName'] # 內容就是他們的文件名 132 msg['Text'](str(msg_content)) # 下載文件 133 # print msg_content 134 elif msg['Type'] == 'Card': # 如果消息是推薦的名片 135 msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內容就是推薦人的昵稱和性別 136 if msg['RecommendInfo']['Sex'] == 1: 137 msg_content += '性別為男' 138 else: 139 msg_content += '性別為女' 140 141 print msg_content 142 elif msg['Type'] == 'Map': # 如果消息為分享的位置信息 143 x, y, location = re.search( 144 "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3) 145 if location is None: 146 msg_content = r"緯度->" + x.__str__() + " 經度->" + y.__str__() # 內容為詳細的地址 147 else: 148 msg_content = r"" + location 149 elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細的內容為文章的標題或者是分享的名字 150 msg_content = msg['Text'] 151 msg_share_url = msg['Url'] # 記錄分享的url 152 print msg_share_url 153 face_bug = msg_content 154 155 ##將信息存儲在字典中,每一個msg_id對應一條信息 156 msg_information.update( 157 { 158 msg_id: { 159 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, 160 "msg_type": msg["Type"], 161 "msg_content": msg_content, "msg_share_url": msg_share_url 162 } 163 } 164 ) 165 166 167 ##這個是用於監聽是否有Group消息撤回 168 @itchat.msg_register(NOTE, isGroupChat=True, isMpChat=True) 169 def information(msg): 170 171 # 這里如果這里的msg['Content']中包含消息撤回和id,就執行下面的語句 172 if '撤回了一條消息' in msg['Content']: 173 old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 在返回的content查找撤回的消息的id 174 old_msg = msg_information.get(old_msg_id) # 得到消息 175 print old_msg 176 if len(old_msg_id) < 11: # 如果發送的是表情包 177 itchat.send_file(face_bug, toUserName=msg['FromUserName']) 178 else: # 發送撤回的提示給文件助手 179 msg_body = "【" \ 180 + old_msg.get('msg_from') + " 群消息撤回提醒】\n" \ 181 + " 撤回了 " + old_msg.get("msg_type") + " 消息:" + "\n" \ 182 + old_msg.get('msg_time_rec') + "\n" \ 183 + r"" + old_msg.get('msg_content') 184 # 如果是分享的文件被撤回了,那么就將分享的url加在msg_body中發送給文件助手 185 if old_msg['msg_type'] == "Sharing": 186 msg_body += "\n就是這個鏈接➣ " + old_msg.get('msg_share_url') 187 188 # 將撤回消息發送到文件助手 189 itchat.send_msg(msg_body, toUserName=msg['FromUserName']) 190 # 有文件的話也要將文件發送回去 191 if old_msg["msg_type"] == "Picture" \ 192 or old_msg["msg_type"] == "Recording" \ 193 or old_msg["msg_type"] == "Video" \ 194 or old_msg["msg_type"] == "Attachment": 195 file = '@fil@%s' % (old_msg['msg_content']) 196 itchat.send(msg=file, toUserName=msg['FromUserName']) 197 os.remove(old_msg['msg_content']) 198 # 刪除字典舊消息 199 msg_information.pop(old_msg_id) 200 201 202 # Main (enableCmdQr = True 時,將會生成二維碼圖片,如 =2 時二維碼亂碼的話 改為1 即可 203 itchat.auto_login(enableCmdQR=2, hotReload=True) 204 itchat.run()
