Files
WeChatBot/weixin-ilink-gateway/README.md
T

5.3 KiB

weixin-ilink-gateway

简体中文

A lightweight HTTP gateway built on top of @tencent-weixin/openclaw-weixin that wraps the WeChat iLink Bot protocol into a clean REST API — QR code login, messaging, media upload/download, webhook push & receive, all as simple HTTP endpoints.

Built with Bun + Hono + Zod.

Features

  • QR Code Login — Simple login flow with real-time scan status polling
  • Message Receiving — Background long-poll with in-memory ring buffer and optional webhook push
  • Message Sending — Text, image, video, and file support; URL-based media is automatically downloaded and uploaded to CDN
  • Media Handling — Upload, presign, and download with transparent AES encryption/decryption
  • State Persistence — Credentials, sync cursors, and context tokens survive restarts
  • Webhook Delivery — Fire-and-forget delivery with automatic retry; also supports inbound webhooks

Quick Start

git clone https://github.com/kadxy/weixin-ilink-gateway.git
cd weixin-ilink-gateway

cp .env.example .env   # Edit to set at least API_KEY

docker compose up -d

The service will be available at http://localhost:29527.

Running from Source

Requires Bun v1.0+

bun install
bun run dev       # Starts with auto-reload on file changes

Default port is 3456 (override with PORT env var).

Configuration

Variable Required Default Description
API_KEY Yes — Bearer token for authenticating API requests (min 12 chars)
PORT No 3456 Server listen port
DATA_DIR No ./data Directory for persisted state files
WEBHOOK_URLS No — Comma-separated URLs for push-delivering incoming messages
WEBHOOK_TOKEN No — Enables the POST /webhook/:token inbound endpoint
MAX_DOWNLOAD_SIZE_MB No 128 Max file size (MB) for URL-based media downloads
DOWNLOAD_TIMEOUT_MS No 30000 Timeout (ms) for downloading media from external URLs
MAX_BUFFER_SIZE No 1000 Max messages kept in the in-memory ring buffer

API Reference

All endpoints require Authorization: Bearer <API_KEY> unless noted otherwise.

Auth

POST /auth/login          Start QR code login, returns { qr_url }
POST /auth/logout         Logout and stop message polling

Status

GET  /status              Login state, polling status, webhook config

Messages (requires login)

POST /messages/send                  Send one or more message items
GET  /messages/updates?after=<seq>   Get buffered incoming messages
Send body example
{
  "to": "<user_id or \"self\">",
  "items": [
    { "type": "text", "text": "Hello!" },
    { "type": "image", "url": "https://example.com/photo.jpg" },
    { "type": "video", "cdn": { "encrypt_query_param": "...", "aes_key": "..." } },
    { "type": "file", "file_name": "doc.pdf", "url": "https://example.com/doc.pdf" }
  ],
  "context_token": "(optional, auto-resolved from cache)"
}

Each item supports either url (the gateway downloads and re-uploads to CDN) or cdn (a pre-uploaded reference). The file type additionally requires file_name.

Note: Sending messages requires a context_token for the target user. The gateway auto-caches this token when an incoming message is received from that user via the poller. Until then, sends to that user will fail. To bootstrap, send a message to the bot from the target user first.

Media (requires login)

POST /media/upload        Upload a file via multipart form (fields: file, to)
POST /media/presign       Get a presigned URL for direct CDN upload
POST /media/download      Download and decrypt a file from CDN
Upload response
{
  "ok": true,
  "media_type": "image",
  "file_name": "photo.png",
  "filekey": "...",
  "encrypt_query_param": "...",
  "aes_key": "...",
  "file_size": 12345,
  "file_size_ciphertext": 12352
}

The returned encrypt_query_param and aes_key can be passed as a cdn reference in /messages/send.

Typing (requires login)

POST /typing/start        Send typing indicator      { "to": "<user_id>" }
POST /typing/stop         Cancel typing indicator    { "to": "<user_id>" }

Config (requires login)

POST /config              Get bot config (e.g. typing ticket) for a user

Webhook (no auth required)

POST /webhook/:token      Send a text message to self via inbound webhook

Accepts application/json ({ "text": "..." }) or text/plain body. Requires the WEBHOOK_TOKEN env var to be set; the :token path parameter must match.

Console (no auth required)

GET  /console                 Browser-based debug console

A built-in single-page UI for testing the API. Enter your API key in the browser, then login via QR code, send test messages, and more. Supports English and Chinese (auto-detected from browser language).

Project Structure

src/
├── sdk/          WeChat iLink protocol client (auth, API, CDN, crypto)
└── gateway/      HTTP gateway layer (routes, poller, state management)

License

MIT