对方发送过来的消息,我们通过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()
来源:oschina
链接:https://my.oschina.net/u/4385912/blog/3909875