From b90e45590add79c7dd3e1ba8d61eeef23b6b9980 Mon Sep 17 00:00:00 2001 From: Dong_master <2213070223@qq.com> Date: Sat, 15 Mar 2025 01:15:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=BD=93=E5=BE=AE=E4=BF=A1=E7=BE=A4?= =?UTF-8?q?=E6=9C=89at=E6=B6=88=E6=81=AF=E6=97=B6=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AAat=E5=B7=B2=E8=BE=BE=E5=88=B0?= =?UTF-8?q?=E8=83=BD=E6=BF=80=E6=B4=BB=E7=AE=A1=E7=90=86=E5=91=98=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/sources/gewechat.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/platform/sources/gewechat.py b/pkg/platform/sources/gewechat.py index e73b85a8..9a9d2e33 100644 --- a/pkg/platform/sources/gewechat.py +++ b/pkg/platform/sources/gewechat.py @@ -62,20 +62,38 @@ class GewechatMessageConverter(adapter.MessageConverter): bot_account_id: str ) -> platform_message.MessageChain: + + # print(message) + 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}' + # 获取机器人在群内信息 + # bot_room_datas = bot.get_chatroom_member_detail( + # self.config["app_id"], + # message['Data']['FromUserName']['string'], + # [message['Data']["ToUserName"]['string']] + # )['data'] + # print(bot_room_datas) + # # 拿到机器人在群内名称 + # at_string = '' + # for bot_data in bot_room_datas: + # bot_room_name = bot_data['nickName'] + # at_string += f"@{bot_room_name}" + # 正则表达式模式,匹配'@'后跟任意数量的非空白字符 + pattern = r'@\S+' + # at_string = f"@{bot_account_id}" content_list = [] - if at_string in message["Data"]["Content"]["string"]: + if '@' 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))) + # print(re.sub(pattern, '', message["Data"]["Content"]["string"])) + content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"]))) else: content_list = [platform_message.Plain(message["Data"]["Content"]["string"])] From 4b2e248646f173c0cbb4d238d833a39c13867361 Mon Sep 17 00:00:00 2001 From: Dong_master <2213070223@qq.com> Date: Sat, 15 Mar 2025 22:13:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=BD=93=E6=98=AF=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E7=BE=A4=E8=81=8Aat=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=97=B6?= =?UTF-8?q?=E5=80=99=E6=9B=BF=E6=8D=A2@=E4=BF=A1=E6=81=AF=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/sources/gewechat.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/pkg/platform/sources/gewechat.py b/pkg/platform/sources/gewechat.py index 9a9d2e33..3186497f 100644 --- a/pkg/platform/sources/gewechat.py +++ b/pkg/platform/sources/gewechat.py @@ -68,31 +68,24 @@ class GewechatMessageConverter(adapter.MessageConverter): if message["Data"]["MsgType"] == 1: # 检查消息开头,如果有 wxid_sbitaz0mt65n22:\n 则删掉 regex = re.compile(r"^wxid_.*:") - # print(message) + 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:]) - # 获取机器人在群内信息 - # bot_room_datas = bot.get_chatroom_member_detail( - # self.config["app_id"], - # message['Data']['FromUserName']['string'], - # [message['Data']["ToUserName"]['string']] - # )['data'] - # print(bot_room_datas) - # # 拿到机器人在群内名称 - # at_string = '' - # for bot_data in bot_room_datas: - # bot_room_name = bot_data['nickName'] - # at_string += f"@{bot_room_name}" + # 正则表达式模式,匹配'@'后跟任意数量的非空白字符 pattern = r'@\S+' - # at_string = f"@{bot_account_id}" + at_string = f"@{bot_account_id}" content_list = [] - if '@' in message["Data"]["Content"]["string"]: + if at_string in message["Data"]["Content"]["string"]: content_list.append(platform_message.At(target=bot_account_id)) - # print(re.sub(pattern, '', message["Data"]["Content"]["string"])) + 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)) + print(re.sub(pattern, '', message["Data"]["Content"]["string"])) content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"]))) else: content_list = [platform_message.Plain(message["Data"]["Content"]["string"])] From 84a80a5ec834f8a007a860f17aa295d966e97ef3 Mon Sep 17 00:00:00 2001 From: Dong_master <2213070223@qq.com> Date: Sat, 15 Mar 2025 22:21:21 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=BD=93=E6=98=AF=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E7=BE=A4=E8=81=8Aat=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=97=B6?= =?UTF-8?q?=E5=80=99=E6=9B=BF=E6=8D=A2@=E4=BF=A1=E6=81=AF=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/sources/gewechat.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/platform/sources/gewechat.py b/pkg/platform/sources/gewechat.py index 3186497f..05785093 100644 --- a/pkg/platform/sources/gewechat.py +++ b/pkg/platform/sources/gewechat.py @@ -63,12 +63,11 @@ class GewechatMessageConverter(adapter.MessageConverter): ) -> platform_message.MessageChain: - # print(message) if message["Data"]["MsgType"] == 1: # 检查消息开头,如果有 wxid_sbitaz0mt65n22:\n 则删掉 regex = re.compile(r"^wxid_.*:") - print(message) + # print(message) line_split = message["Data"]["Content"]["string"].split("\n") @@ -82,10 +81,9 @@ class GewechatMessageConverter(adapter.MessageConverter): 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))) - # 更优雅的替换@机器人,仅仅限于单独AT的情况 + # 更优雅的替换改名后@机器人,仅仅限于单独AT的情况 elif '在群聊中@了你' in message["Data"]["PushContent"]: content_list.append(platform_message.At(target=bot_account_id)) - print(re.sub(pattern, '', message["Data"]["Content"]["string"])) content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"]))) else: content_list = [platform_message.Plain(message["Data"]["Content"]["string"])]