From b6c647cacbd004013020292ed90573cafb318749 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 29 Apr 2026 10:46:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89MaiBot=E5=AE=98=E6=96=B9=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=BB=93=E6=9E=84=E4=BF=AE=E5=A4=8D=E7=BE=A4=E8=81=8A?= =?UTF-8?q?=E8=A2=AB=E8=AF=86=E5=88=AB=E4=B8=BA=E7=A7=81=E8=81=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 变更项:\n1. outbound message_info 从旧 sender_info/receiver_info 改为官方 BaseMessageInfo 顶层字段:user_info + group_info。\n2. 群聊消息携带 group_info,私聊消息不携带 group_info,避免服务端按私聊兜底路由。\n3. 调整 _resolve_reply_route:优先解析官方新结构,保留对历史 sender_info/receiver_info 回包的兼容。\n4. 增补中文注释,说明协议字段语义与兼容策略,便于后续排障与升级。 --- plugins/maibot_adapter/main.py | 58 ++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/plugins/maibot_adapter/main.py b/plugins/maibot_adapter/main.py index 0a9975a..951c760 100644 --- a/plugins/maibot_adapter/main.py +++ b/plugins/maibot_adapter/main.py @@ -518,32 +518,22 @@ class MaiBotAdapterPlugin(MessagePluginInterface): f"sender={sender}, message_id={message_id}" ) - sender_info: Dict[str, Any] = { - "user_info": { - "platform": self._platform_name, - "user_id": sender, - "user_nickname": sender_name, - } + # 按 MaiBot 官方 BaseMessageInfo 结构组装: + # 1. user_info:消息“发送者”的用户信息(群聊/私聊都必须存在); + # 2. group_info:仅群聊存在,私聊应为 None/缺省; + # 3. 不再发送 sender_info/receiver_info 这套旧字段,避免服务端按私聊路径兜底解析。 + user_info: Dict[str, Any] = { + "platform": self._platform_name, + "user_id": sender, + "user_nickname": sender_name, } - receiver_info: Dict[str, Any] = {} - + group_info: Optional[Dict[str, Any]] = None if roomid: - receiver_info["group_info"] = { + group_info = { "platform": self._platform_name, "group_id": roomid, "group_name": group_name, } - # 群聊场景只保留 group_info,不再混入 receiver.user_info: - # 1. sender 身份已经通过 sender_info.user_info 完整表达; - # 2. 若群聊再附带 receiver.user_info,部分下游会优先按“用户会话”归类; - # 3. 这正是你目前看到“群消息被显示成私聊”的高概率根因。 - # 4. 因此这里明确约束:群聊 receiver 只承载群维度路由信息。 - else: - receiver_info["user_info"] = { - "platform": self._platform_name, - "user_id": sender, - "user_nickname": sender_name, - } api_message = { "message_info": { @@ -563,8 +553,8 @@ class MaiBotAdapterPlugin(MessagePluginInterface): "abot_route_type": route_type, "abot_route_source": route_source, }, - "sender_info": sender_info, - "receiver_info": receiver_info, + "user_info": user_info, + "group_info": group_info, }, "message_segment": { "type": "text", @@ -636,9 +626,31 @@ class MaiBotAdapterPlugin(MessagePluginInterface): def _resolve_reply_route(self, message_info: Dict[str, Any]) -> Optional[Dict[str, str]]: """从 MaiBot 返回的 message_info 中解析微信路由。""" + # 优先按官方当前结构解析: + # 1. group_info 存在 => 群回复; + # 2. user_info 存在 => 私聊回复。 + root_group_info = message_info.get("group_info") or {} + root_user_info = message_info.get("user_info") or {} + group_id = str(root_group_info.get("group_id", "") or "").strip() + root_user_id = str(root_user_info.get("user_id", "") or "").strip() + + if group_id: + return { + "route_type": "group", + "target": group_id, + "at_target": root_user_id, + } + + if root_user_id: + return { + "route_type": "private", + "target": root_user_id, + "at_target": "", + } + + # 兼容历史 sender_info/receiver_info 结构,避免升级期间回包偶发失配。 receiver_info = message_info.get("receiver_info") or {} sender_info = message_info.get("sender_info") or {} - receiver_group_info = receiver_info.get("group_info") or {} receiver_user_info = receiver_info.get("user_info") or {} sender_user_info = sender_info.get("user_info") or {}