diff --git a/pkg/platform/sources/gewechat.py b/pkg/platform/sources/gewechat.py index e73b85a8..05785093 100644 --- a/pkg/platform/sources/gewechat.py +++ b/pkg/platform/sources/gewechat.py @@ -62,20 +62,29 @@ class GewechatMessageConverter(adapter.MessageConverter): bot_account_id: str ) -> platform_message.MessageChain: + + if message["Data"]["MsgType"] == 1: # 检查消息开头,如果有 wxid_sbitaz0mt65n22:\n 则删掉 regex = re.compile(r"^wxid_.*:") + # print(message) line_split = message["Data"]["Content"]["string"].split("\n") if len(line_split) > 0 and regex.match(line_split[0]): message["Data"]["Content"]["string"] = "\n".join(line_split[1:]) - at_string = f'@{bot_account_id}' + # 正则表达式模式,匹配'@'后跟任意数量的非空白字符 + pattern = r'@\S+' + at_string = f"@{bot_account_id}" content_list = [] if at_string in message["Data"]["Content"]["string"]: content_list.append(platform_message.At(target=bot_account_id)) - content_list.append(platform_message.Plain(message["Data"]["Content"]["string"].replace(at_string, "", 1))) + content_list.append(platform_message.Plain(message["Data"]["Content"]["string"].replace(at_string, '', 1))) + # 更优雅的替换改名后@机器人,仅仅限于单独AT的情况 + elif '在群聊中@了你' in message["Data"]["PushContent"]: + content_list.append(platform_message.At(target=bot_account_id)) + content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"]))) else: content_list = [platform_message.Plain(message["Data"]["Content"]["string"])]