Files
inquiry_robot/inquiry-agent/agent/handlers/echo_text.py
T

62 lines
1.9 KiB
Python

"""
私聊文字 echo 处理器(联调用)。
本文件职责:收到 text 入站后写入 outbox 回复;不加 Graph/TMS。
测试环境正文加 [test] 前缀(对齐运维告警约定)。
禁止:无 sender_id 仍回复;禁止在此改六态。
"""
from __future__ import annotations
import logging
from typing import Any
from agent.channel.wecom.models import InboundMessage
logger = logging.getLogger(__name__)
def handle_echo_text(
message: InboundMessage,
store: Any,
*,
ytd_env: str,
reply_template: str,
) -> None:
"""
处理一条私聊文字:按模板生成回复并入 outbox。
参数:
reply_template:可含 {content} 占位,默认来自 WECOM_CALLBACK_REPLY_TEXT
副作用:写 outbox;不直接调企微(由 outbox 线程发送)。
"""
if not message.has_sender():
logger.warning("echo 跳过:无 sender_id msg_id=%s", message.message_id)
return
if message.msg_type != "text":
logger.info("echo 跳过非文字类型 type=%s msg_id=%s", message.msg_type, message.message_id)
return
if not (message.content or "").strip():
logger.info("echo 跳过空正文 msg_id=%s", message.message_id)
return
body = reply_template.replace("{content}", message.content.strip())
if ytd_env == "test" and not body.startswith("[test]"):
body = f"[test] {body}"
dedupe = f"echo:{message.message_id}"
ok, info = store.enqueue_outbound(
touser=message.sender_id,
content=body,
dedupe_key=dedupe,
)
# 联调需要能在 err 日志里对上企微气泡;正文截断避免超长刷屏
preview = body if len(body) <= 200 else (body[:200] + "…")
logger.info(
"echo 已排队 outbox ok=%s info=%s sender=%s reply=%s",
ok,
info,
message.sender_id,
preview,
)