{"record":{"id":"35b0f0dd3d79750e","repo":"HKUDS/Vibe-Trading","slug":"wechat-send-text-error-ret-ret-errcode-errcod","errorCode":null,"errorMessage":"WeChat send text error (ret={ret}, errcode={errcode}): {data.get('errmsg', '')}","messagePattern":"WeChat send text error \\(ret=(.+?), errcode=(.+?)\\): (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"agent/src/channels/weixin.py","lineNumber":1321,"sourceCode":"            \"client_id\": client_id,\n            \"message_type\": MESSAGE_TYPE_BOT,\n            \"message_state\": MESSAGE_STATE_FINISH,\n        }\n        if item_list:\n            weixin_msg[\"item_list\"] = item_list\n        if context_token:\n            weixin_msg[\"context_token\"] = context_token\n\n        body: dict[str, Any] = {\n            \"msg\": weixin_msg,\n            \"base_info\": BASE_INFO,\n        }\n\n        data = await self._api_post(\"ilink/bot/sendmessage\", body)\n        ret = data.get(\"ret\", 0)\n        errcode = data.get(\"errcode\", 0)\n        if (ret is not None and ret != 0) or (errcode is not None and errcode != 0):\n            raise RuntimeError(\n                f\"WeChat send text error (ret={ret}, errcode={errcode}): {data.get('errmsg', '')}\"\n            )\n\n    async def _send_media_file(\n        self,\n        to_user_id: str,\n        media_path: str,\n        context_token: str,\n    ) -> None:\n        \"\"\"Upload a local file to WeChat CDN and send it as a media message.\n\n        Follows the exact protocol from ``@tencent-weixin/openclaw-weixin`` v1.0.3:\n        1. Generate a random 16-byte AES key (client-side).\n        2. Call ``getuploadurl`` with file metadata + hex-encoded AES key.\n        3. AES-128-ECB encrypt the file and POST to CDN (``{cdnBaseUrl}/upload``).\n        4. Read ``x-encrypted-param`` header from CDN response as the download param.\n        5. Send a ``sendmessage`` with the appropriate media item referencing the upload.\n        \"\"\"","sourceCodeStart":1303,"sourceCodeEnd":1339,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/channels/weixin.py#L1303-L1339","documentation":"_send_text posts to ilink/bot/sendmessage and treats any non-zero ret or errcode in the response as a failure, raising RuntimeError with both codes and errmsg. This is the generic outbound text-send failure covering all WeChat-side rejections (rate limit, invalid token, blocked content, server error).","triggerScenarios":"Any _send_text call (from send or _flush_tool_hints) where the API responds with ret != 0 or errcode != 0 — e.g. context_token invalid, exceeding the ~7 msgs / 5 min iLink rate limit, content filtered, or recipient unreachable.","commonSituations":"Agent bursts (multiple tool-hint flushes plus replies) hitting the iLink per-user rate limit; stale context_token after session pause; sending URLs/words WeChat content policy rejects; server-side incidents returning errcode with errmsg.","solutions":["Match errcode against WeChat iLink docs in the message: rate-limit codes -> queue and slow down sends; token codes -> refresh context token.","Coalesce/throttle outbound messages (the code already buffers tool hints — extend the pattern to all sends).","Retry once after refreshing the context_token for transient token staleness.","If content-filtered, rephrase or drop the offending content."],"exampleFix":"// before\nawait self._send_text(user_id, chat_id, text, ctx_token)\n\n// after\ntry:\n    await self._send_text(user_id, chat_id, text, ctx_token)\nexcept RuntimeError as e:\n    if 'rate' in str(e).lower():\n        await asyncio.sleep(60); await self._send_text(user_id, chat_id, text, ctx_token)\n    else:\n        raise","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await channel.send(msg)\nexcept RuntimeError as e:\n    if \"send text error\" in str(e):\n        await asyncio.sleep(60)\n        await channel.send(msg)  # single retry after backoff\n    else:\n        raise","preventionTips":["Throttle/coalesce outbound messages to stay under ~7 msgs / 5 min per user.","Refresh context_token before retrying token-related errcodes.","Parse errcode from the message to distinguish rate limits from content blocks."],"tags":["wechat","send-message","api-error","rate-limit"],"backgroundTag":"send-message-api-error","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}