Skip to content

Commit ca70e62

Browse files
committed
update python_wechat.py
1 parent 1d48d41 commit ca70e62

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

python_wechat.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616
from itchat.content import *
1717

1818
# 初始化
19-
newInstance = itchat.new_instance()
20-
newInstance.auto_login(hotReload=False, enableCmdQR=2)
21-
newInstance.global_keys = ["人工智能", "机器学习", "算法", "数据挖掘"]
22-
newInstance.to_user_name = "filehelper"
19+
my = itchat.new_instance()
20+
my.auto_login(hotReload=False, enableCmdQR=2)
21+
my.global_keys = ["人工智能", "机器学习", "算法", "数据挖掘"]
22+
my.to_user_name = "filehelper"
2323

24-
# 获取自己的属性字典:UserName, NickName, RemarkName, Sex(1 or 2)
25-
newInstance.owner = newInstance.loginInfo["User"]
24+
# my还包括的以下属性,注意用点.查看:
25+
# (1) alive 是否还活着,isLogging 是否已登陆
26+
# (2) loginInfo 登陆信息,其中的User属性为自己的信息User字典类,包括UserName, NickName, RemarkName, Sex(1 or 2)等
27+
# (3) memberList 通讯录列表,每一项为一个User字典类
28+
# (4) chatroomList 群聊列表,每一项为一个Chatroom字典类,包括UserName, NickName, RemarkName, MemberCount等
29+
# (5) mpList 订阅号列表,每一项为一个MassivePlatform字典类,包括UserName, NickName等
2630

27-
# 获取通讯录: {UserName: UserInstance}
28-
newInstance.friends = {user["UserName"]: user for user in newInstance.get_friends(update=True)}
31+
# 获取并更新通讯录: {UserName: UserInstance}
32+
my.friends = {user["UserName"]: user for user in my.get_friends(update=True)}
2933

3034
# 消息存储队列
31-
newInstance.msg_store = {}
35+
my.msg_store = {}
3236

3337

3438
# 消息提取函数
@@ -49,14 +53,16 @@ def get_msg_list(msg):
4953
msg_file = msg["FileName"] # 消息中所带文件的名称
5054
msg_url = msg["Url"] # 消息中带有的链接地址
5155

56+
wind_name = msg["User"]["RemarkName"] if msg["User"].get("RemarkName") else (
57+
msg["User"]["NickName"] if msg["User"].get("NickName") else to_user_name
58+
)
59+
5260
if from_user_name.startswith("@@"):
5361
# 群消息
54-
wind_name = msg["User"]["RemarkName"] if msg["User"]["RemarkName"] else msg["User"]["NickName"]
55-
nick_name = msg["ActualNickName"] if (msg["ActualUserName"] not in newInstance.friends) or \
56-
(not newInstance.friends[msg["ActualUserName"]]["RemarkName"]) else newInstance.friends[msg["ActualUserName"]]["RemarkName"]
62+
nick_name = msg["ActualNickName"] if (msg["ActualUserName"] not in my.friends) or \
63+
(not my.friends[msg["ActualUserName"]]["RemarkName"]) else my.friends[msg["ActualUserName"]]["RemarkName"]
5764
else:
5865
# 个人消息
59-
wind_name = msg["User"]["RemarkName"] if msg["User"]["RemarkName"] else msg["User"]["NickName"]
6066
nick_name = wind_name
6167

6268
we_type = msg["Type"] # 消息类型
@@ -66,69 +72,64 @@ def get_msg_list(msg):
6672

6773

6874
# 消息注册,主要处理群消息
69-
@newInstance.msg_register([TEXT, MAP, CARD, NOTE, SHARING, PICTURE, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, isGroupChat=True)
75+
@my.msg_register([TEXT, MAP, CARD, NOTE, SHARING, PICTURE, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, isGroupChat=True)
7076
def text_reply(msg):
7177
"""
7278
消息自动接收, 接受全部的消息
7379
"""
7480
# 消息提取
7581
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(msg)
7682

77-
# 消息过滤,过滤自己发送的消息
78-
if from_user_name == newInstance.owner["UserName"]:
79-
logging.warning("message from myself, skip")
80-
return
81-
8283
# 消息过滤, 只监测文字、注解、分享、图片、语音、视频、附件等
8384
if we_type not in ["Text", "Note", "Sharing", "Picture", "Recording", "Video", "Attachment"]:
8485
logging.warning("message ignored")
8586
return
8687

87-
if we_type in ["Picture", "Recording", "Video", "Attachment"]:
88-
re_length = re.search("[\"\s]length=\"(?P<length>[\d]+?)\"", msg_content, flags=re.IGNORECASE)
89-
if (not msg_content) or (re_length and (int(re_length.group("length")) < 5000000)):
90-
we_text(".Cache/" + msg_file)
91-
logging.warning("downloading %s to .Cache/", msg_file)
92-
93-
# 消息存储
94-
newInstance.msg_store[msg_id] = msg
88+
# 处理来自自己的消息
89+
if from_user_name == my.loginInfo["User"]["UserName"]:
90+
return
9591

96-
# 删除过期消息
97-
ids_list = [_id for _id in newInstance.msg_store if time.time() - newInstance.msg_store[_id]["CreateTime"] > 120]
98-
for _id in ids_list:
92+
# 消息存储,删除过期消息
93+
my.msg_store[msg_id] = msg
94+
for _id in [_id for _id in my.msg_store if time.time() - my.msg_store[_id]["CreateTime"] > 120]:
9995
logging.warning("delete message, message_id = %s", _id)
100-
newInstance.msg_store.pop(_id)
96+
my.msg_store.pop(_id)
10197

10298
# 处理群消息
10399
if from_user_name.startswith("@@"):
104100
# 红包消息处理
105101
if we_type == "Note" and we_text.find("收到红包,请在手机上查看") >= 0:
106-
newInstance.send("【%s】中有红包,快抢!\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_time), toUserName=newInstance.to_user_name)
102+
my.send("【%s】中有红包,快抢!" % wind_name, toUserName=my.to_user_name)
107103

108104
# 提到自己消息处理
109105
if msg["IsAt"]:
110-
newInstance.send("【%s】中有@你的消息:\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, msg_content, msg_time), toUserName=newInstance.to_user_name)
106+
my.send("【%s】中有@你的消息:\nFrom: %s\nTime: %s\n%s" % (wind_name, nick_name, msg_time, msg_content), toUserName=my.to_user_name)
111107

112-
for key in newInstance.global_keys:
108+
for key in my.global_keys:
113109
if msg_content.find(key) >= 0:
114-
newInstance.send("【%s】中有关键字【%s】:\nFrom: %s\nContent: %s\nTime: %s" % (wind_name, key, nick_name, msg_content, msg_time), toUserName=newInstance.to_user_name)
110+
my.send("【%s】中有关键字【%s】:\nFrom: %s\nTime: %s\n%s" % (wind_name, key, nick_name, msg_time, msg_content), toUserName=my.to_user_name)
115111
break
116112

117113
# 撤回消息处理
118114
if we_type == "Note" and we_text.find("撤回了一条消息") >= 0:
119-
old_msg = newInstance.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
115+
old_msg = my.msg_store.get(msg_content[msg_content.find("<msgid>")+7: msg_content.find("</msgid>")])
120116
if not old_msg:
121117
return
122118

123119
msg_id, from_user_name, to_user_name, msg_type, msg_content, msg_time, msg_file, msg_url, wind_name, nick_name, we_type, we_text = get_msg_list(old_msg)
124120
if we_type in ["Picture", "Recording", "Video", "Attachment"]:
121+
re_length = re.search("[\"\s]length=\"(?P<length>[\d]+?)\"", msg_content, flags=re.IGNORECASE)
122+
if (not msg_content) or (re_length and (int(re_length.group("length")) < 5000000)):
123+
we_text(".Cache/" + msg_file)
124+
logging.warning("downloading %s to .Cache/", msg_file)
125+
# 更改内容
125126
msg_content = msg_file
126127
elif we_type == "Sharing":
127128
msg_content = we_text + ": " + msg_url
128129

129-
newInstance.send("【%s】中有消息被撤回:\nFrom: %s\nType: %s\nContent: %s\nTime: %s" % (wind_name, nick_name, we_type, msg_content, msg_time), toUserName=newInstance.to_user_name)
130+
my.send("【%s】中有消息被撤回:\nFrom: %s\nType: %s\nTime: %s\n%s" % (wind_name, nick_name, we_type, msg_time, msg_content), toUserName=my.to_user_name)
130131
return
131132

132133

133134
# 运行程序
134-
newInstance.run()
135+
my.run()

0 commit comments

Comments
 (0)