Skip to content

Commit ab2bbff

Browse files
authored
Merge pull request #1746 from Seayon/fix-wechat-at-message-parsing
✨ feat(wechatpadpro): 增强群聊消息中的@消息处理逻辑
2 parents ec32825 + 19022d6 commit ab2bbff

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,46 @@ async def _process_message_content(
620620
if abm.type == MessageType.GROUP_MESSAGE:
621621
parts = content.split(":\n", 1)
622622
if len(parts) == 2:
623-
abm.message_str = parts[1]
624-
abm.message.append(Plain(abm.message_str))
623+
message_content = parts[1]
624+
abm.message_str = message_content
625+
626+
# 检查是否@了机器人,参考 gewechat 的实现方式
627+
# 微信大部分客户端在@用户昵称后面,紧接着是一个\u2005字符(四分之一空格)
628+
at_me = False
629+
630+
# 检查 msg_source 中是否包含机器人的 wxid
631+
# wechatpadpro 的格式: <atuserlist>wxid</atuserlist>
632+
# gewechat 的格式: <atuserlist><![CDATA[wxid]]></atuserlist>
633+
msg_source = raw_message.get("msg_source", "")
634+
if f"<atuserlist>{abm.self_id}</atuserlist>" in msg_source or f"<atuserlist>{abm.self_id}," in msg_source or f",{abm.self_id}</atuserlist>" in msg_source:
635+
at_me = True
636+
637+
# 也检查 push_content 中是否有@提示
638+
push_content = raw_message.get("push_content", "")
639+
if "在群聊中@了你" in push_content:
640+
at_me = True
641+
642+
if at_me:
643+
# 被@了,在消息开头插入At组件(参考gewechat的做法)
644+
bot_nickname = await self._get_group_member_nickname(abm.group_id, abm.self_id)
645+
abm.message.insert(0, At(qq=abm.self_id, name=bot_nickname or abm.self_id))
646+
647+
# 只有当消息内容不仅仅是@时才添加Plain组件
648+
if "\u2005" in message_content:
649+
# 检查@之后是否还有其他内容
650+
parts = message_content.split("\u2005")
651+
if len(parts) > 1 and any(part.strip() for part in parts[1:]):
652+
abm.message.append(Plain(message_content))
653+
else:
654+
# 检查是否只包含@机器人
655+
is_pure_at = False
656+
if bot_nickname and message_content.strip() == f"@{bot_nickname}":
657+
is_pure_at = True
658+
if not is_pure_at:
659+
abm.message.append(Plain(message_content))
660+
else:
661+
# 没有@机器人,作为普通文本处理
662+
abm.message.append(Plain(message_content))
625663
else:
626664
abm.message.append(Plain(abm.message_str))
627665
else: # 私聊消息

0 commit comments

Comments
 (0)