App
FastHTML webhook routes, deduplication, logging setup, and uvicorn server startup.
消息去重
WeCom 在未收到及时响应时会重发消息。_is_dup 用内存字典记录最近 DEDUP_WINDOW 秒内已处理的 MsgId,同时清理过期条目,防止重复触发 AI 调用。
XML 解析
WeCom 消息体为 XML 格式,_parse_xml 将其转为普通字典,简化后续字段提取。
Webhook 路由
辅助函数: - _verify_and_decrypt:校验签名并解密 WeCom 消息体,返回 XML 文本。 - _extract_msg:从解密 XML 中提取 (user_id, content, msg_id),支持 text、image、link 消息类型。
路由: - GET /wecom:WeCom 服务器 URL 验证,校验签名后返回解密的 echostr。 - POST /wecom:接收加密消息,验签、解密、去重后,将消息异步交给 process_message 处理;图片消息传递图片 URL,链接消息传递链接地址,不支持的类型回复提示文字,立即返回空响应以满足 WeCom 3 秒超时要求。
服务启动与停止
start_bot 在后台守护线程中运行 uvicorn,返回 server 实例供后续调用 stop_bot 优雅关闭,便于在 notebook 中交互式控制服务生命周期。
start_bot
def start_bot(
)->Server:
Start the uvicorn server in a daemon thread and return the server instance.
stop_bot
def stop_bot(
server:Server
)->None:
Signal the uvicorn server to shut down gracefully.