# App


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## 消息去重

WeCom
在未收到及时响应时会重发消息。[`_is_dup`](https://TheSecondStep.github.io/solveit_wxbot/app.html#_is_dup)
用内存字典记录最近 `DEDUP_WINDOW` 秒内已处理的
`MsgId`，同时清理过期条目，防止重复触发 AI 调用。

## XML 解析

WeCom 消息体为 XML
格式，[`_parse_xml`](https://TheSecondStep.github.io/solveit_wxbot/app.html#_parse_xml)
将其转为普通字典，简化后续字段提取。

## Webhook 路由

辅助函数： -
**[`_verify_and_decrypt`](https://TheSecondStep.github.io/solveit_wxbot/app.html#_verify_and_decrypt)**：校验签名并解密
WeCom 消息体，返回 XML 文本。 -
**[`_extract_msg`](https://TheSecondStep.github.io/solveit_wxbot/app.html#_extract_msg)**：从解密
XML 中提取 `(user_id, content, msg_id)`，支持 `text`、`image`、`link`
消息类型。

路由： - **GET `/wecom`**：WeCom 服务器 URL 验证，校验签名后返回解密的
`echostr`。 - **POST
`/wecom`**：接收加密消息，验签、解密、去重后，将消息异步交给
[`process_message`](https://TheSecondStep.github.io/solveit_wxbot/dialog.html#process_message)
处理；图片消息传递图片
URL，链接消息传递链接地址，不支持的类型回复提示文字，立即返回空响应以满足
WeCom 3 秒超时要求。

## 服务启动与停止

[`start_bot`](https://TheSecondStep.github.io/solveit_wxbot/app.html#start_bot)
在后台守护线程中运行 uvicorn，返回 server 实例供后续调用
[`stop_bot`](https://TheSecondStep.github.io/solveit_wxbot/app.html#stop_bot)
优雅关闭，便于在 notebook 中交互式控制服务生命周期。

------------------------------------------------------------------------

<a
href="https://github.com/TheSecondStep/solveit_wxbot/blob/main/solveit_wxbot/app.py#L97"
target="_blank" style="float:right; font-size:smaller">source</a>

### start_bot

``` python

def start_bot(
    
)->Server:

```

*Start the uvicorn server in a daemon thread and return the server
instance.*

------------------------------------------------------------------------

<a
href="https://github.com/TheSecondStep/solveit_wxbot/blob/main/solveit_wxbot/app.py#L111"
target="_blank" style="float:right; font-size:smaller">source</a>

### stop_bot

``` python

def stop_bot(
    server:Server
)->None:

```

*Signal the uvicorn server to shut down gracefully.*
