diff --git a/EvoScientist/channels/wechat/channel.py b/EvoScientist/channels/wechat/channel.py index a97c5d8..4c2fbb6 100644 --- a/EvoScientist/channels/wechat/channel.py +++ b/EvoScientist/channels/wechat/channel.py @@ -337,12 +337,20 @@ class WeChatChannel(Channel, WebhookMixin, TokenMixin): logger.error(f"WeChat decrypt failed: {e}") return web.Response(status=500) - # Route to backend-specific handler - await self._process_message(xml_data) + # Process message asynchronously — WeCom requires a response within + # 5 seconds, but media downloads can take much longer. Return + # "success" immediately and handle the message in the background. + asyncio.create_task(self._safe_process_message(xml_data)) - # Return "success" to acknowledge receipt (async reply via API) return web.Response(text="success") + async def _safe_process_message(self, xml_data: dict[str, str]) -> None: + """Wrapper that catches exceptions so fire-and-forget tasks don't leak.""" + try: + await self._process_message(xml_data) + except Exception: + logger.exception("Error processing WeChat message") + async def _process_message(self, xml_data: dict[str, str]) -> None: """Process a parsed XML message from WeChat/WeCom callback.""" msg_type = xml_data.get("MsgType", "") @@ -436,7 +444,7 @@ class WeChatChannel(Channel, WebhookMixin, TokenMixin): text = f"[位置] {label} ({lat}, {lon})" elif msg_type == "file": media_id = xml_data.get("MediaId", "") - file_name = xml_data.get("Title", f"wechat_file_{msg_id}") + file_name = xml_data.get("FileName", "") or xml_data.get("Title", f"wechat_file_{msg_id}") logger.info(f"WeChat file message: name={file_name}, media_id={media_id!r}, keys={list(xml_data.keys())}") if media_id: local, ann = await self._download_wechat_media( diff --git a/botpy.log b/botpy.log new file mode 100644 index 0000000..b43c983 --- /dev/null +++ b/botpy.log @@ -0,0 +1,18 @@ +2026-02-15 13:46:42,249 [INFO] (client.py:162)_bot_login [botpy] 登录机器人账号中... +2026-02-15 13:46:42,535 [INFO] (robot.py:65)update_access_token [botpy] access_token expires_in 7200 +2026-02-15 13:46:43,622 [INFO] (client.py:181)_bot_init [botpy] 程序启动... +2026-02-15 13:46:43,623 [INFO] (connection.py:60)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 +2026-02-15 13:46:43,623 [INFO] (client.py:242)bot_connect [botpy] 会话启动中... +2026-02-15 13:46:43,623 [INFO] (gateway.py:115)ws_connect [botpy] 启动中... +2026-02-15 13:46:43,792 [INFO] (gateway.py:142)ws_identify [botpy] 鉴权中... +2026-02-15 13:46:43,915 [INFO] (gateway.py:85)on_message [botpy] 机器人「EvoSci-测试中」启动成功! +2026-02-15 13:46:43,916 [INFO] (gateway.py:223)_send_heart [botpy] 心跳维持启动... +2026-02-15 13:57:47,578 [INFO] (client.py:162)_bot_login [botpy] 登录机器人账号中... +2026-02-15 13:57:47,775 [INFO] (robot.py:65)update_access_token [botpy] access_token expires_in 6535 +2026-02-15 13:57:48,887 [INFO] (client.py:181)_bot_init [botpy] 程序启动... +2026-02-15 13:57:48,888 [INFO] (connection.py:60)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 +2026-02-15 13:57:48,888 [INFO] (client.py:242)bot_connect [botpy] 会话启动中... +2026-02-15 13:57:48,888 [INFO] (gateway.py:115)ws_connect [botpy] 启动中... +2026-02-15 13:57:49,073 [INFO] (gateway.py:142)ws_identify [botpy] 鉴权中... +2026-02-15 13:57:49,294 [INFO] (gateway.py:85)on_message [botpy] 机器人「EvoSci-测试中」启动成功! +2026-02-15 13:57:49,295 [INFO] (gateway.py:223)_send_heart [botpy] 心跳维持启动...