openapi: 3.1.0 info: title: WeChat iLink Gateway description: Universal HTTP gateway for Tencent WeChat iLink Bot API. version: 0.1.0 servers: - url: http://localhost:3456 description: Local development security: - bearerAuth: [] webhooks: newMessage: post: summary: Outgoing webhook — new message notification description: | When `WEBHOOK_URLS` is configured, the gateway POSTs each incoming user message to every URL. The payload is the raw WeChat message object (unmodified). Retries up to 2 times with 1s delay on failure. tags: [Webhook] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/WeixinMessage" responses: "200": description: Webhook received successfully paths: # --------------------------------------------------------------------------- # Incoming Webhook # --------------------------------------------------------------------------- /webhook/{token}: post: summary: Incoming webhook (send text to self) description: | Receives text from external services and sends it to the bot's own WeChat as a text message. Authenticates via token in URL path (set via `WEBHOOK_TOKEN` env var). Does NOT require Bearer auth. Supports `text/plain` body or JSON `{"text": "..."}`. tags: [Webhook] security: [] parameters: - name: token in: path required: true schema: type: string description: Must match `WEBHOOK_TOKEN` environment variable requestBody: required: true content: text/plain: schema: type: string example: Hello from external service application/json: schema: type: object required: [text] properties: text: type: string example: Hello from external service responses: "200": description: Message sent content: application/json: schema: type: object required: [ok, client_id] properties: ok: type: boolean example: true client_id: type: string "400": $ref: "#/components/responses/BadRequest" "401": $ref: "#/components/responses/Unauthorized" "500": description: Send failed content: application/json: schema: type: object required: [ok, message] properties: ok: type: boolean example: false message: type: string # --------------------------------------------------------------------------- # Auth # --------------------------------------------------------------------------- /auth/login: post: summary: Start QR code login description: | Initiates a WeChat QR code login flow. Returns a QR code URL immediately. The server polls for scan confirmation in the background. Rejects with 409 if already logged in. tags: [Auth] responses: "200": description: QR code generated content: application/json: schema: type: object required: [ok, qr_url] properties: ok: type: boolean example: true qr_url: type: string description: Base64-encoded QR code image content "409": $ref: "#/components/responses/Conflict" /auth/logout: post: summary: Logout and stop polling description: Clears credentials, context tokens, stops message polling, and resets login state. tags: [Auth] responses: "200": description: Logged out content: application/json: schema: type: object required: [ok, message] properties: ok: type: boolean example: true message: type: string example: Logged out "400": $ref: "#/components/responses/BadRequest" # --------------------------------------------------------------------------- # Status # --------------------------------------------------------------------------- /status: get: summary: Get gateway status description: Returns current login state, polling status, and webhook configuration. Does not require login. tags: [Status] responses: "200": description: Current status content: application/json: schema: type: object required: [ok, logged_in, user_id, login_status, login_error, qr_url, polling, webhook_urls] properties: ok: type: boolean example: true logged_in: type: boolean user_id: type: string nullable: true login_status: type: string enum: [idle, waiting, scanned, confirmed, failed] login_error: type: string nullable: true qr_url: type: string nullable: true polling: type: boolean webhook_urls: type: array items: type: string # --------------------------------------------------------------------------- # Messages # --------------------------------------------------------------------------- /messages/send: post: summary: Send messages description: | Sends one or more message items to a user. Supports text, image, video, and file. Media can be provided as a URL (auto-uploaded) or a pre-uploaded CDN reference. The `to` field accepts a user ID or `"self"` to message yourself. A `context_token` is auto-injected from cache if not provided. tags: [Messages] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/SendMessageRequest" responses: "200": description: Send results (per item) content: application/json: schema: type: object required: [ok, results] properties: ok: type: boolean description: true if all items sent successfully results: type: array items: $ref: "#/components/schemas/SendResult" warnings: type: array items: type: string "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" /messages/updates: get: summary: Get buffered messages description: | Returns messages from the in-memory buffer. Optionally filter by sequence number. Messages are received via background long-polling and buffered (max 1000). tags: [Messages] parameters: - name: after in: query required: false schema: type: integer description: Only return messages with seq > this value responses: "200": description: Buffered messages content: application/json: schema: type: object required: [ok, count, messages] properties: ok: type: boolean example: true count: type: integer messages: type: array items: $ref: "#/components/schemas/WeixinMessage" "401": $ref: "#/components/responses/Unauthorized" # --------------------------------------------------------------------------- # Media # --------------------------------------------------------------------------- /media/upload: post: summary: Upload media file description: | Uploads a file to WeChat CDN. Automatically detects media type from MIME type or file extension. Returns CDN reference fields that can be used in `/messages/send` with the `cdn` format. tags: [Media] requestBody: required: true content: multipart/form-data: schema: type: object required: [file, to] properties: file: type: string format: binary to: type: string description: Target user ID or `"self"` responses: "200": description: Upload successful content: application/json: schema: type: object required: [ok, media_type, file_name, filekey, encrypt_query_param, aes_key, file_size, file_size_ciphertext] properties: ok: type: boolean example: true media_type: type: string enum: [image, video, file] file_name: type: string filekey: type: string encrypt_query_param: type: string description: CDN encrypted query parameter for download aes_key: type: string description: Base64-encoded AES key for decryption file_size: type: integer description: Original file size in bytes file_size_ciphertext: type: integer description: Encrypted file size in bytes "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" /media/presign: post: summary: Get presigned upload URL description: | Returns a presigned CDN upload URL for client-side upload. Use this when you want to encrypt and upload the file yourself. tags: [Media] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/MediaPresignRequest" responses: "200": description: Presigned URL content: application/json: schema: type: object required: [ok] properties: ok: type: boolean example: true upload_param: type: string thumb_upload_param: type: string upload_full_url: type: string "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" /media/download: post: summary: Download and decrypt media description: Downloads an encrypted file from WeChat CDN, decrypts it, and returns the raw binary. tags: [Media] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/MediaDownloadRequest" responses: "200": description: Decrypted file binary content: application/octet-stream: schema: type: string format: binary headers: Content-Disposition: schema: type: string example: attachment; filename="photo.jpg" "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" # --------------------------------------------------------------------------- # Typing # --------------------------------------------------------------------------- /typing/start: post: summary: Send typing indicator description: Shows a "typing..." indicator to the target user. Automatically fetches and caches the typing ticket. tags: [Typing] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/ToRequest" responses: "200": $ref: "#/components/responses/Ok" "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" /typing/stop: post: summary: Cancel typing indicator description: Cancels the "typing..." indicator for the target user. tags: [Typing] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/ToRequest" responses: "200": $ref: "#/components/responses/Ok" "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" # --------------------------------------------------------------------------- # Config # --------------------------------------------------------------------------- /config: post: summary: Get bot configuration description: | Retrieves bot configuration for a target user, including typing ticket. Auto-injects context_token from cache if not provided. tags: [Config] requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/ConfigRequest" responses: "200": description: Bot configuration content: application/json: schema: type: object required: [ok] properties: ok: type: boolean example: true typing_ticket: type: string "400": $ref: "#/components/responses/ValidationError" "401": $ref: "#/components/responses/Unauthorized" # ============================================================================= # Components # ============================================================================= components: securitySchemes: bearerAuth: type: http scheme: bearer description: API key set via `API_KEY` environment variable # --------------------------------------------------------------------------- # Schemas # --------------------------------------------------------------------------- schemas: ToRequest: type: object required: [to] properties: to: type: string description: Target user ID or `"self"` ConfigRequest: type: object required: [to] properties: to: type: string description: Target user ID or `"self"` context_token: type: string description: Optional override; auto-injected from cache if omitted CdnRef: type: object required: [encrypt_query_param, aes_key] properties: encrypt_query_param: type: string aes_key: type: string file_size_ciphertext: type: integer SendItemText: type: object required: [type, text] properties: type: type: string enum: [text] text: type: string SendItemImageUrl: type: object required: [type, url] properties: type: type: string enum: [image] url: type: string description: Image URL (will be auto-downloaded and uploaded to CDN) SendItemImageCdn: type: object required: [type, cdn] properties: type: type: string enum: [image] cdn: $ref: "#/components/schemas/CdnRef" SendItemVideoUrl: type: object required: [type, url] properties: type: type: string enum: [video] url: type: string SendItemVideoCdn: type: object required: [type, cdn] properties: type: type: string enum: [video] cdn: $ref: "#/components/schemas/CdnRef" SendItemFileUrl: type: object required: [type, url, file_name] properties: type: type: string enum: [file] url: type: string file_name: type: string SendItemFileCdn: type: object required: [type, cdn, file_name] properties: type: type: string enum: [file] cdn: $ref: "#/components/schemas/CdnRef" file_name: type: string SendItem: oneOf: - $ref: "#/components/schemas/SendItemText" - $ref: "#/components/schemas/SendItemImageUrl" - $ref: "#/components/schemas/SendItemImageCdn" - $ref: "#/components/schemas/SendItemVideoUrl" - $ref: "#/components/schemas/SendItemVideoCdn" - $ref: "#/components/schemas/SendItemFileUrl" - $ref: "#/components/schemas/SendItemFileCdn" discriminator: propertyName: type SendMessageRequest: type: object required: [to, items] properties: to: type: string description: Target user ID or `"self"` items: type: array minItems: 1 items: $ref: "#/components/schemas/SendItem" context_token: type: string description: Optional override; auto-injected from cache if omitted SendResult: type: object required: [type, ok] properties: type: type: string enum: [text, image, video, file] ok: type: boolean client_id: type: string error: type: string MediaPresignRequest: type: object required: [to, filekey, media_type, rawsize, rawfilemd5, filesize, aeskey] properties: to: type: string filekey: type: string media_type: type: integer description: "1=image, 2=video, 3=file, 4=voice" rawsize: type: integer rawfilemd5: type: string filesize: type: integer description: AES-ECB padded size aeskey: type: string description: Hex-encoded AES key thumb_rawsize: type: integer thumb_rawfilemd5: type: string thumb_filesize: type: integer no_need_thumb: type: boolean MediaDownloadRequest: type: object required: [full_url, aes_key, file_name] properties: full_url: type: string format: uri aes_key: type: string description: Base64-encoded AES key file_name: type: string CDNMedia: type: object properties: encrypt_query_param: type: string aes_key: type: string encrypt_type: type: integer full_url: type: string MessageItem: type: object properties: type: type: integer description: "0=NONE, 1=TEXT, 2=IMAGE, 3=VOICE, 4=FILE, 5=VIDEO" create_time_ms: type: integer update_time_ms: type: integer is_completed: type: boolean msg_id: type: string text_item: type: object properties: text: type: string image_item: type: object properties: media: $ref: "#/components/schemas/CDNMedia" mid_size: type: integer voice_item: type: object properties: media: $ref: "#/components/schemas/CDNMedia" playtime: type: integer text: type: string file_item: type: object properties: media: $ref: "#/components/schemas/CDNMedia" file_name: type: string md5: type: string len: type: string video_item: type: object properties: media: $ref: "#/components/schemas/CDNMedia" video_size: type: integer play_length: type: integer WeixinMessage: type: object description: Original WeChat message format (unmodified) properties: seq: type: integer message_id: type: string description: String to avoid JS precision loss on large integers from_user_id: type: string to_user_id: type: string client_id: type: string create_time_ms: type: integer update_time_ms: type: integer session_id: type: string group_id: type: string message_type: type: integer description: "0=NONE, 1=USER, 2=BOT" message_state: type: integer description: "0=NEW, 1=GENERATING, 2=FINISH" item_list: type: array items: $ref: "#/components/schemas/MessageItem" context_token: type: string # --------------------------------------------------------------------------- # Reusable responses # --------------------------------------------------------------------------- responses: Ok: description: Success content: application/json: schema: type: object required: [ok] properties: ok: type: boolean example: true BadRequest: description: Bad request content: application/json: schema: type: object required: [ok, message] properties: ok: type: boolean example: false message: type: string Unauthorized: description: Missing/invalid Bearer token or not logged in content: application/json: schema: type: object required: [ok, message] properties: ok: type: boolean example: false message: type: string Conflict: description: Already logged in content: application/json: schema: type: object required: [ok, message] properties: ok: type: boolean example: false message: type: string ValidationError: description: Request validation failed content: application/json: schema: type: object required: [ok, message] properties: ok: type: boolean example: false message: type: string