修复群回复@目标误判并增强数字前缀清洗
变更项:\n1. 在 _resolve_reply_route 中读取 additional_config.platform_io_target_user_id,群聊优先用该字段作为 at_target。\n2. 增加对 platform_io_account_id 的防误用判断,避免把机器人账号当作 @ 目标。\n3. 增强 _sanitize_reply_text:支持清理无标点的长数字前缀(如 484377749在呢)。\n4. 补充中文注释,明确平台字段优先级与防误判逻辑。
This commit is contained in:
@@ -652,6 +652,10 @@ class MaiBotAdapterPlugin(MessagePluginInterface):
|
||||
|
||||
def _resolve_reply_route(self, message_info: Dict[str, Any]) -> Optional[Dict[str, str]]:
|
||||
"""从 MaiBot 返回的 message_info 中解析微信路由。"""
|
||||
additional_config = message_info.get("additional_config") or {}
|
||||
cfg_target_user_id = str(additional_config.get("platform_io_target_user_id", "") or "").strip()
|
||||
cfg_account_id = str(additional_config.get("platform_io_account_id", "") or "").strip()
|
||||
|
||||
# 优先按官方当前结构解析:
|
||||
# 1. group_info 存在 => 群回复;
|
||||
# 2. user_info 存在 => 私聊回复。
|
||||
@@ -661,10 +665,16 @@ class MaiBotAdapterPlugin(MessagePluginInterface):
|
||||
root_user_id = str(root_user_info.get("user_id", "") or "").strip()
|
||||
|
||||
if group_id:
|
||||
# 群聊 @ 目标优先用平台显式给出的 target_user_id:
|
||||
# 1. platform_io_target_user_id 才是这次应被 @ 的真实用户;
|
||||
# 2. 避免误用 platform_io_account_id(机器人自身账号)导致 @ 错对象。
|
||||
at_target = cfg_target_user_id or root_user_id
|
||||
if at_target and cfg_account_id and at_target == cfg_account_id:
|
||||
at_target = ""
|
||||
return {
|
||||
"route_type": "group",
|
||||
"target": group_id,
|
||||
"at_target": root_user_id,
|
||||
"at_target": at_target,
|
||||
}
|
||||
|
||||
if root_user_id:
|
||||
@@ -686,10 +696,13 @@ class MaiBotAdapterPlugin(MessagePluginInterface):
|
||||
sender_user_id = str(sender_user_info.get("user_id", "") or "").strip()
|
||||
|
||||
if group_id:
|
||||
at_target = cfg_target_user_id or receiver_user_id or sender_user_id
|
||||
if at_target and cfg_account_id and at_target == cfg_account_id:
|
||||
at_target = ""
|
||||
return {
|
||||
"route_type": "group",
|
||||
"target": group_id,
|
||||
"at_target": receiver_user_id or sender_user_id,
|
||||
"at_target": at_target,
|
||||
}
|
||||
|
||||
if receiver_user_id:
|
||||
@@ -851,6 +864,10 @@ class MaiBotAdapterPlugin(MessagePluginInterface):
|
||||
# 2. 这些数字通常长度较长(>=6),且紧跟中文/英文标点或空白;
|
||||
# 3. 只在“前缀位置”清理,避免误伤正文里的正常数字内容。
|
||||
text = re.sub(r"^\s*\d{6,}\s*[,,::;;\-—_~\s]*", "", text)
|
||||
# 兼容“数字前缀直接连正文”的情况,例如“484377749在呢”:
|
||||
# 1. 仅在开头匹配长数字;
|
||||
# 2. 后面必须跟非数字字符,避免误删纯数字回复(如验证码类内容可按需保留)。
|
||||
text = re.sub(r"^\s*\d{6,}(?=\D)", "", text)
|
||||
|
||||
# 若清理后前部还有一层“孤立标点”,再做一次轻量修整,避免出现“,在呢怎么了”这种残留。
|
||||
text = re.sub(r"^\s*[,,::;;\-—_~]+\s*", "", text)
|
||||
|
||||
Reference in New Issue
Block a user