AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug]因为以微信为对象的计划任务缺乏“context_token”,主动推送信息无法发送至微信

Open xhmmmm opened this issue 7 months ago • 1 comments

What happened / 发生了什么

日志记录:

[2026-04-16 08:00:19.685] [Core] [INFO] [runners.tool_loop_agent_runner:883]: Agent 使用工具: ['send_message_to_user']

[2026-04-16 08:00:19.685] [Core] [INFO] [runners.tool_loop_agent_runner:930]: 使用工具:send_message_to_user,参数:【模型生成内容】

[2026-04-16 08:00:19.685] [Core] [WARN] [v4.23.1] [weixin_oc.weixin_oc_adapter:848]: weixin_oc(【配置名】): context token missing for 【聊天UMO】, skip send. You should send one message to refresh context_token.

[2026-04-16 08:00:20.328] [Core] [INFO] [runners.tool_loop_agent_runner:1128]: Tool `send_message_to_user` Result: Message sent to session 【聊天UMO】

可以发现,是因为缺乏context_token导致的。 正常情况下,在24小时内只要用户通过微信发送过对话,机器人就可以主动从微信发信息给用户。 但是这个24小时机制是通过context_token实现的,但是AstrBot的计划任务发起时,创建会话线程是不会包含context_token的,只有在原本的会话线程没关闭的情况下,context_token本来就包含在线程内时,才可能发送成功。 具体到用户层面,表现为如果前不久发过信息,那么近期计划任务可以执行;但如果过了几个小时,会话线程被AstrBot关闭,则计划任务发起会话时不会包含context_token,导致发送失败。

一个可能的解决方案:https://linux.do/t/topic/1802951

Reproduce / 如何复现?

创建一个计划任务,让机器人八个小时后发送信息到微信,并且期间用户不主动从微信发信息给机器人。 然后会发现机器人无法发送信息到微信。

AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器

AstrBot v4.23.1 Windows Luncher部署 连接个人微信(扫码登陆微信ClawBotd)

OS

Windows

Logs / 报错日志

[2026-04-16 08:00:19.685] [Core] [INFO] [runners.tool_loop_agent_runner:883]: Agent 使用工具: ['send_message_to_user']

[2026-04-16 08:00:19.685] [Core] [INFO] [runners.tool_loop_agent_runner:930]: 使用工具:send_message_to_user,参数:【模型生成内容】

[2026-04-16 08:00:19.685] [Core] [WARN] [v4.23.1] [weixin_oc.weixin_oc_adapter:848]: weixin_oc(【配置名】): context token missing for 【聊天UMO】, skip send. You should send one message to refresh context_token.

[2026-04-16 08:00:20.328] [Core] [INFO] [runners.tool_loop_agent_runner:1128]: Tool `send_message_to_user` Result: Message sent to session 【聊天UMO】

Are you willing to submit a PR? / 你愿意提交 PR 吗?

  • [ ] Yes!

Code of Conduct

xhmmmm avatar Apr 16 '26 02:04 xhmmmm

Hi @xhmmmm! I'm Dosu and I’m helping the AstrBot team.

这个问题的根本原因已经确认:context_token 仅保存在内存字典 self._context_tokens 中,只有用户发送消息时才会被写入 [1],且不会被持久化到磁盘或序列化到 MessageSession 中。

发送时的检查逻辑 在 _send_items_to_session 方法中,如果 _context_tokens 字典里找不到对应用户的 token,就直接 skip [2]。

计划任务创建会话时,MessageSession 只序列化了 platform_name、message_type、session_id 三个字段 [3],不包含任何 adapter 级别的状态(如 context_token)。因此当会话线程超时关闭、用户未在近期发消息时,_context_tokens 中没有对应条目,发送必然失败。

可能的改进方向:

  1. 持久化 context_token:将 _context_tokens 持久化到数据库/文件,在 adapter 启动或计划任务触发前加载,确保即使会话关闭后仍可使用最后缓存的 token。
  2. 在 MessageSession 中携带 adapter 元数据:扩展 MessageSession,让计划任务序列化时能保存 context_token。
  3. 发送失败时的提示优化:目前日志虽然 warn 了但工具返回的是 "Message sent to session ..." 成功信息 [2],这会误导 Agent 以为发送成功。

另外值得注意的是,还有一个相关的已知问题影响微信公众号适配器的主动推送场景 [4],以及 Agent 间歇性不调用 send_message_to_user 工具的问题 [5]。

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about AstrBot Share Dosu with your team

dosubot[bot] avatar Apr 16 '26 02:04 dosubot[bot]