From a9dbff756b14063ad095ae012db4cd59613f98ca Mon Sep 17 00:00:00 2001 From: Seayon <153660639@qq.com> Date: Fri, 6 Jun 2025 16:53:31 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(wechatpadpro):=20=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E7=BE=A4=E8=81=8A=E6=B6=88=E6=81=AF=E4=B8=AD=E7=9A=84?= =?UTF-8?q?@=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加对群聊消息中@机器人场景的精确识别和处理,提升了消息解析的准确性。 支持多种@格式的检测,包括 msg_source 和 push_content 的判断。 --- .../wechatpadpro/wechatpadpro_adapter.py | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index 7e7755501..8db301dc8 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -8,7 +8,7 @@ import aiohttp import websockets from astrbot import logger -from astrbot.api.message_components import Plain, Image +from astrbot.api.message_components import Plain, Image, At from astrbot.api.platform import Platform, PlatformMetadata from astrbot.core.message.message_event_result import MessageChain from astrbot.core.platform.astrbot_message import ( @@ -569,8 +569,46 @@ class WeChatPadProAdapter(Platform): if abm.type == MessageType.GROUP_MESSAGE: parts = content.split(":\n", 1) if len(parts) == 2: - abm.message_str = parts[1] - abm.message.append(Plain(abm.message_str)) + message_content = parts[1] + abm.message_str = message_content + + # 检查是否@了机器人,参考 gewechat 的实现方式 + # 微信大部分客户端在@用户昵称后面,紧接着是一个\u2005字符(四分之一空格) + at_me = False + + # 检查 msg_source 中是否包含机器人的 wxid + # wechatpadpro 的格式: wxid + # gewechat 的格式: + msg_source = raw_message.get("msg_source", "") + if f"{abm.self_id}" in msg_source or f"{abm.self_id}," in msg_source or f",{abm.self_id}" in msg_source: + at_me = True + + # 也检查 push_content 中是否有@提示 + push_content = raw_message.get("push_content", "") + if "在群聊中@了你" in push_content: + at_me = True + + if at_me: + # 被@了,在消息开头插入At组件(参考gewechat的做法) + bot_nickname = await self._get_group_member_nickname(abm.group_id, abm.self_id) + abm.message.insert(0, At(qq=abm.self_id, name=bot_nickname or abm.self_id)) + + # 只有当消息内容不仅仅是@时才添加Plain组件 + if "\u2005" in message_content: + # 检查@之后是否还有其他内容 + parts = message_content.split("\u2005") + if len(parts) > 1 and any(part.strip() for part in parts[1:]): + abm.message.append(Plain(message_content)) + else: + # 检查是否只包含@机器人 + is_pure_at = False + if bot_nickname and message_content.strip() == f"@{bot_nickname}": + is_pure_at = True + if not is_pure_at: + abm.message.append(Plain(message_content)) + else: + # 没有@机器人,作为普通文本处理 + abm.message.append(Plain(message_content)) else: abm.message.append(Plain(abm.message_str)) else: # 私聊消息