Skip to content

Commit e9c6abb

Browse files
committed
update python_wechat
1 parent 77803dc commit e9c6abb

File tree

1 file changed

+25
-135
lines changed

1 file changed

+25
-135
lines changed

python_wechat.py

Lines changed: 25 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import time
1313
import itchat
1414
import logging
15-
import datetime
1615
from itchat.content import *
1716

1817
# 初始化
@@ -52,11 +51,10 @@ def get_msg_list(msg):
5251

5352
msg_type = msg["MsgType"] # 消息类型
5453
msg_content = msg["Content"] # 消息内容
55-
msg_time = datetime.datetime.fromtimestamp(msg["CreateTime"]) # 消息发送时间
54+
msg_time = msg["CreateTime"] # 消息发送时间
5655

5756
msg_file = msg["FileName"] # 消息中所带文件的名称
5857
msg_file_length = msg["FileSize"] # 消息中所带文件的大小
59-
msg_file_length = int(msg_file_length) if msg_file_length.strip() else 0
6058
msg_voice_length = msg["VoiceLength"] # 消息中所带语音的长度(毫秒)
6159
msg_play_length = msg["PlayLength"] # 消息中所带视频的长度(秒)
6260
msg_url = msg["Url"] # 消息中所带链接的地址
@@ -75,7 +73,7 @@ def get_msg_list(msg):
7573
we_type = msg["Type"] # 消息类型
7674
we_text = msg["Text"] # 消息内容
7775

78-
logging.warning("show: nick_name=%s, wind_name=%s, we_type=%s, we_text=%s, msg_time=%s", msg_nick_name, wind_name, we_type, we_text, msg_time)
76+
logging.warning("show: msg_nick_name=%s, wind_name=%s, we_type=%s, we_text=%s", msg_nick_name, wind_name, we_type, we_text)
7977
return msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_file_length, msg_voice_length, msg_play_length, msg_url, \
8078
wind_name, msg_user_name, msg_nick_name, is_at, we_type, we_text
8179

@@ -84,25 +82,25 @@ def get_msg_list(msg):
8482
@my.msg_register([TEXT, PICTURE, RECORDING, VIDEO, MAP, CARD, NOTE, SHARING, ATTACHMENT], isFriendChat=True, isGroupChat=True)
8583
def text_reply(msg):
8684
"""
87-
消息自动接收, 接受全部的消息
85+
消息自动接收, 接受全部的消息(自己发送的消息除外)
8886
"""
87+
# 跳过来自自己的消息
88+
if msg["FromUserName"] == my.loginInfo["User"]["UserName"]:
89+
return
90+
8991
# 消息提取
92+
msg_list = get_msg_list(msg)
9093
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_file_length, msg_voice_length, msg_play_length, msg_url, \
91-
wind_name, msg_user_name, msg_nick_name, is_at, we_type, we_text = get_msg_list(msg)
94+
wind_name, msg_user_name, msg_nick_name, is_at, we_type, we_text = msg_list
9295

93-
# 消息过滤, 只监测文字、图片、语音、注解、分享等
94-
if we_type not in ["Text", "Picture", "Recording", "Note", "Sharing"]:
96+
# 消息过滤, 只监测文字、图片、语音、名片、注解、分享等
97+
if we_type not in ["Text", "Picture", "Recording", "Card", "Note", "Sharing"]:
9598
logging.warning("message type isn't included, ignored")
9699
return
97100

98-
# 处理来自自己的消息
99-
if from_user_name == my.loginInfo["User"]["UserName"]:
100-
logging.warning("message is from myself, ignored")
101-
return
102-
103101
# 消息存储,删除过期消息
104-
my.msg_store[msg_id] = msg
105-
for _id in [_id for _id in my.msg_store if time.time() - my.msg_store[_id]["CreateTime"] > 120]:
102+
my.msg_store[msg_id] = msg_list
103+
for _id in [_id for _id in my.msg_store if time.time() - my.msg_store[_id][5] > 120]:
106104
my.msg_store.pop(_id)
107105

108106
# 保存消息中的内容(图片、语音等),不保存动态图片
@@ -113,15 +111,15 @@ def text_reply(msg):
113111
except Exception as excep:
114112
logging.error("downloading %s to .Cache/ error: %s", msg_file, excep)
115113

116-
# ==== 处理红包消息 ====
114+
# ==== 处理群消息 ====
117115
if from_user_name.startswith("@@"):
118116
# ==== 处理红包消息 ====
119117
if we_type == "Note" and we_text.find("收到红包,请在手机上查看") >= 0:
120-
my.send("【%s】中【%s】发红包啦,快抢!" % (wind_name, msg_nick_name), toUserName=my.to_user_name)
118+
my.send("【%s】中有人发红包啦,快抢!" % wind_name, toUserName=my.to_user_name)
121119
# ==== 处理关键词消息 ====
122120
for key in my.global_keys:
123121
if we_type == "Text" and we_text.find(key) >= 0:
124-
my.send("【%s】中【%s】提及关键字:%s" % (wind_name, msg_nick_name, key), toUserName=my.to_user_name)
122+
my.send("【%s】中【%s】提及了关键字:%s" % (wind_name, msg_nick_name, key), toUserName=my.to_user_name)
125123
my.send(we_text, toUserName=my.to_user_name)
126124
break
127125
# ==== 群内是否被@ ====
@@ -131,20 +129,22 @@ def text_reply(msg):
131129

132130
# ==== 撤回消息处理(必须为最后一步) ====
133131
if we_type == "Note" and we_text.find("撤回了一条消息") >= 0:
134-
old_msg = my.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
135-
if not old_msg:
132+
msg_list = my.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
133+
if not msg_list:
136134
logging.warning("not message id in my.msg_store")
137135
return
138136

139137
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_file_length, msg_voice_length, msg_play_length, msg_url, \
140-
wind_name, msg_user_name, msg_nick_name, is_at, we_type, we_text = get_msg_list(old_msg)
138+
wind_name, msg_user_name, msg_nick_name, is_at, we_type, we_text = msg_list
141139
my.send("【%s】中【%s】撤回了自己发送的消息:\nType: %s\nTime: %s\n%s" % (wind_name, msg_nick_name, we_type, msg_time, msg_file), toUserName=my.to_user_name)
142140

143-
if we_type == "Text":
144-
my.send(we_text, toUserName=my.to_user_name)
141+
if we_type in ["Text", "Card"]:
142+
my.send(str(we_text), toUserName=my.to_user_name)
145143
elif we_type == "Sharing":
146144
my.send(we_text + "\n" + msg_url, toUserName=my.to_user_name)
147-
elif (we_type in ["Picture", "Recording"]) and (not msg_file.endswith(".gif")):
145+
elif we_type == "Recording":
146+
my.send_file(".Cache/" + msg_file, toUserName=my.to_user_name)
147+
elif we_type == "Picture" and (not msg_file.endswith(".gif")):
148148
my.send_image(".Cache/" + msg_file, toUserName=my.to_user_name)
149149

150150
return
@@ -219,7 +219,7 @@ def text_reply(msg):
219219
"""
220220

221221
"""
222-
群消息(来自别人)
222+
群消息:
223223
{
224224
'MsgId': '7844877618948840992',
225225
'FromUserName': '@@8dc5df044444d1fb8e3972e755b47adf9d07f5a032cae90a4d822b74ee1e4880',
@@ -332,114 +332,4 @@ def text_reply(msg):
332332
'Type': 'Text',
333333
'Text': '就是那个,那个协议我们手上有吗'
334334
}
335-
336-
群消息(来自自己):
337-
WARNING: root: message: {
338-
'MsgId': '6658361167561279652',
339-
'FromUserName': '@e79dde912b8f817514c01f399ca9ba12',
340-
'ToUserName': '@@cbd264b76a28d4f5bad27197de60735bc082c95ab49891cf64d745ff4be17e30',
341-
'MsgType': 1,
342-
'Content': '看来最近是没有线下活动了',
343-
'Status': 3,
344-
'ImgStatus': 1,
345-
'CreateTime': 1498455090,
346-
'VoiceLength': 0,
347-
'PlayLength': 0,
348-
'FileName': '',
349-
'FileSize': '',
350-
'MediaId': '',
351-
'Url': '',
352-
'AppMsgType': 0,
353-
'StatusNotifyCode': 0,
354-
'StatusNotifyUserName': '',
355-
'HasProductId': 0,
356-
'Ticket': '',
357-
'ImgHeight': 0,
358-
'ImgWidth': 0,
359-
'SubMsgType': 0,
360-
'NewMsgId': 6658361167561279652,
361-
'OriContent': '',
362-
'ActualNickName': '齐现虎',
363-
'IsAt': False,
364-
'ActualUserName': '@e79dde912b8f817514c01f399ca9ba12',
365-
'User': <Chatroom: {
366-
'MemberList': <ContactList: [
367-
<ChatroomMember: {
368-
'MemberList': <ContactList: []>,
369-
'Uin': 0,
370-
'UserName': '@143b0d468a5be892768e372aae5d3c97f20dd73c6f28a99092fb96d4fc7862e3',
371-
'NickName': '李小盛',
372-
'AttrStatus': 235557,
373-
'PYInitial': '',
374-
'PYQuanPin': '',
375-
'RemarkPYInitial': '',
376-
'RemarkPYQuanPin': '',
377-
'MemberStatus': 0,
378-
'DisplayName': '',
379-
'KeyWord': ''
380-
}>,
381-
<ChatroomMember: {
382-
'MemberList': <ContactList: []>,
383-
'Uin': 0,
384-
'UserName': '@edb9eb66b235ef72237ad025290813159b6d3e9cdaa15284ad7f6748b38c2c1f',
385-
'NickName': '小z@德研社',
386-
'AttrStatus': 103461,
387-
'PYInitial': '',
388-
'PYQuanPin': '',
389-
'RemarkPYInitial': '',
390-
'RemarkPYQuanPin': '',
391-
'MemberStatus': 0,
392-
'DisplayName': '小z研值512',
393-
'KeyWord': ''
394-
}>,
395-
]>,
396-
'Uin': 0,
397-
'UserName': '@@cbd264b76a28d4f5bad27197de60735bc082c95ab49891cf64d745ff4be17e30',
398-
'NickName': '德研社z战队',
399-
'HeadImgUrl': '/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=688463282&username=@@cbd264b76a28d4f5bad27197de60735bc082c95ab49891cf64d745ff4be17e30&skey=',
400-
'ContactFlag': 2,
401-
'MemberCount': 70,
402-
'RemarkName': '',
403-
'HideInputBarFlag': 0,
404-
'Sex': 0,
405-
'Signature': '',
406-
'VerifyFlag': 0,
407-
'OwnerUin': 0,
408-
'PYInitial': 'DYSZZD',
409-
'PYQuanPin': 'deyanshezzhandui',
410-
'RemarkPYInitial': '',
411-
'RemarkPYQuanPin': '',
412-
'StarFriend': 0,
413-
'AppAccountFlag': 0,
414-
'Statues': 0,
415-
'AttrStatus': 0,
416-
'Province': '',
417-
'City': '',
418-
'Alias': '',
419-
'SnsFlag': 0,
420-
'UniFriend': 0,
421-
'DisplayName': '',
422-
'ChatRoomId': 0,
423-
'KeyWord': '',
424-
'EncryChatRoomId': '@be08ab93d4d5440069d6617df937b689',
425-
'IsOwner': 0,
426-
'IsAdmin': None,
427-
'Self': <ChatroomMember: {
428-
'MemberList': <ContactList: []>,
429-
'Uin': 0,
430-
'UserName': '@e79dde912b8f817514c01f399ca9ba12',
431-
'NickName': '齐现虎',
432-
'AttrStatus': 2147600869,
433-
'PYInitial': '',
434-
'PYQuanPin': '',
435-
'RemarkPYInitial': '',
436-
'RemarkPYQuanPin': '',
437-
'MemberStatus': 0,
438-
'DisplayName': '',
439-
'KeyWord': 'qix'
440-
}>
441-
}>,
442-
'Type': 'Text',
443-
'Text': '看来最近是没有线下活动了'
444-
}
445335
"""

0 commit comments

Comments
 (0)