Skip to content

Commit c184722

Browse files
authored
Merge pull request #2207 from Abyss-Seeker/master
支持更多语言(英语)的微信客户端
2 parents f495213 + 9a371a4 commit c184722

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

channel/wechat/wechat_message.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def __init__(self, itchat_msg, is_group=False):
1414
self.create_time = itchat_msg["CreateTime"]
1515
self.is_group = is_group
1616

17+
notes_join_group = ["加入群聊","加入了群聊","invited","joined"] # 可通过添加对应语言的加入群聊通知中的关键词适配更多
18+
notes_exit_group = ["移出了群聊","removed"] # 可通过添加对应语言的踢出群聊通知中的关键词适配更多
19+
notes_patpat = ["拍了拍我","tickled my","tickled me"] # 可通过添加对应语言的拍一拍通知中的关键词适配更多
20+
1721
if itchat_msg["Type"] == TEXT:
1822
self.ctype = ContextType.TEXT
1923
self.content = itchat_msg["Text"]
@@ -26,30 +30,38 @@ def __init__(self, itchat_msg, is_group=False):
2630
self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
2731
self._prepare_fn = lambda: itchat_msg.download(self.content)
2832
elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000:
29-
if is_group and ("加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"]):
33+
if is_group and (any(note_join_group in itchat_msg["Content"] for note_join_group in notes_join_group)): # 若有任何在notes_join_group列表中的字符串出现在NOTE中
3034
# 这里只能得到nickname, actual_user_id还是机器人的id
31-
if "加入了群聊" in itchat_msg["Content"]:
35+
if "加入群聊" not in itchat_msg["Content"]:
3236
self.ctype = ContextType.JOIN_GROUP
3337
self.content = itchat_msg["Content"]
34-
self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1]
38+
if "invited" in itchat_msg["Content"]: # 匹配英文信息
39+
self.actual_user_nickname = re.findall(r'invited\s+(.+?)\s+to\s+the\s+group\s+chat', itchat_msg["Content"])[0]
40+
elif "joined" in itchat_msg["Content"]: # 匹配通过二维码加入的英文信息
41+
self.actual_user_nickname = re.findall(r'"(.*?)" joined the group chat via the QR Code shared by', itchat_msg["Content"])[0]
42+
elif "加入了群聊" in itchat_msg["Content"]:
43+
self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1]
3544
elif "加入群聊" in itchat_msg["Content"]:
3645
self.ctype = ContextType.JOIN_GROUP
3746
self.content = itchat_msg["Content"]
3847
self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
3948

40-
elif is_group and ("移出了群聊" in itchat_msg["Content"]):
49+
elif is_group and (any(note_exit_group in itchat_msg["Content"] for note_exit_group in notes_exit_group)): # 若有任何在notes_exit_group列表中的字符串出现在NOTE中
4150
self.ctype = ContextType.EXIT_GROUP
4251
self.content = itchat_msg["Content"]
4352
self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
4453

4554
elif "你已添加了" in itchat_msg["Content"]: #通过好友请求
4655
self.ctype = ContextType.ACCEPT_FRIEND
4756
self.content = itchat_msg["Content"]
48-
elif "拍了拍我" in itchat_msg["Content"]:
57+
elif any(note_patpat in itchat_msg["Content"] for note_patpat in notes_patpat): # 若有任何在notes_patpat列表中的字符串出现在NOTE中:
4958
self.ctype = ContextType.PATPAT
5059
self.content = itchat_msg["Content"]
5160
if is_group:
52-
self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
61+
if "拍了拍我" in itchat_msg["Content"]: # 识别中文
62+
self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
63+
elif ("tickled my" in itchat_msg["Content"] or "tickled me" in itchat_msg["Content"]):
64+
self.actual_user_nickname = re.findall(r'^(.*?)(?:tickled my|tickled me)', itchat_msg["Content"])[0]
5365
else:
5466
raise NotImplementedError("Unsupported note message: " + itchat_msg["Content"])
5567
elif itchat_msg["Type"] == ATTACHMENT:

0 commit comments

Comments
 (0)