From 1e92828f1a7d44d6ca7d6736306b2017abbfa513 Mon Sep 17 00:00:00 2001 From: Abyss-Seeker <112790054+Abyss-Seeker@users.noreply.github.com> Date: Sun, 4 Aug 2024 10:14:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9B=B4=E5=A4=9A=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=EF=BC=88=E8=8B=B1=E8=AF=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入了notes_join_group,notes_exit_group,notes_patpat列表,可以在加入群聊,退出群聊和拍一拍消息中匹配更多的字符。在此完成了英语(invited, removed, tickled)的匹配,使如果微信语言是英文的话也可以正常识别啦!同时,以后也可以通过加list和判断语句的方式支持更多语言! --- channel/wechat/wechat_message.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/channel/wechat/wechat_message.py b/channel/wechat/wechat_message.py index b8b1d91..75915b4 100644 --- a/channel/wechat/wechat_message.py +++ b/channel/wechat/wechat_message.py @@ -14,6 +14,10 @@ class WechatMessage(ChatMessage): self.create_time = itchat_msg["CreateTime"] self.is_group = is_group + notes_join_group = ["加入群聊","加入了群聊","invited"] # 可通过添加对应语言的加入群聊通知中的关键词适配更多 + notes_exit_group = ["移出了群聊","removed"] # 可通过添加对应语言的踢出群聊通知中的关键词适配更多 + notes_patpat = ["拍了拍我","tickled my","tickled me"] # 可通过添加对应语言的拍一拍通知中的关键词适配更多 + if itchat_msg["Type"] == TEXT: self.ctype = ContextType.TEXT self.content = itchat_msg["Text"] @@ -26,18 +30,21 @@ class WechatMessage(ChatMessage): self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径 self._prepare_fn = lambda: itchat_msg.download(self.content) elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000: - if is_group and ("加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"]): + if is_group and (any(note_join_group in itchat_msg["Content"] for note_join_group in notes_join_group)): # 若有任何在notes_join_group列表中的字符串出现在NOTE中 # 这里只能得到nickname, actual_user_id还是机器人的id - if "加入了群聊" in itchat_msg["Content"]: + if "加入群聊" not in itchat_msg["Content"]: self.ctype = ContextType.JOIN_GROUP self.content = itchat_msg["Content"] - self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1] + if "invited" in itchat_msg["Content"]: # 匹配英文信息 + self.actual_user_nickname = re.findall(r'invited\s+(.+?)\s+to\s+the\s+group\s+chat', itchat_msg["Content"])[0] + elif "加入了群聊" in itchat_msg["Content"]: + self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1] elif "加入群聊" in itchat_msg["Content"]: self.ctype = ContextType.JOIN_GROUP self.content = itchat_msg["Content"] self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0] - elif is_group and ("移出了群聊" in itchat_msg["Content"]): + elif is_group and (any(note_exit_group in itchat_msg["Content"] for note_exit_group in notes_exit_group)): # 若有任何在notes_exit_group列表中的字符串出现在NOTE中 self.ctype = ContextType.EXIT_GROUP self.content = itchat_msg["Content"] self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0] @@ -45,11 +52,14 @@ class WechatMessage(ChatMessage): elif "你已添加了" in itchat_msg["Content"]: #通过好友请求 self.ctype = ContextType.ACCEPT_FRIEND self.content = itchat_msg["Content"] - elif "拍了拍我" in itchat_msg["Content"]: + elif any(note_patpat in itchat_msg["Content"] for note_patpat in notes_patpat): # 若有任何在notes_patpat列表中的字符串出现在NOTE中: self.ctype = ContextType.PATPAT self.content = itchat_msg["Content"] if is_group: - self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0] + if "拍了拍我" in itchat_msg["Content"]: # 识别中文 + self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0] + elif ("tickled my" in itchat_msg["Content"] or "tickled me" in itchat_msg["Content"]): + self.actual_user_nickname = re.findall(r'^(.*?)(?:tickled my|tickled me)', itchat_msg["Content"])[0] else: raise NotImplementedError("Unsupported note message: " + itchat_msg["Content"]) elif itchat_msg["Type"] == ATTACHMENT: