170 lines
5.3 KiB
Markdown
170 lines
5.3 KiB
Markdown
# weixin-ilink-gateway
|
|
|
|
**[简体中文](README_zh.md)**
|
|
|
|
A lightweight HTTP gateway built on top of [`@tencent-weixin/openclaw-weixin`](https://www.npmjs.com/package/@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](https://bun.sh) + [Hono](https://hono.dev) + [Zod](https://zod.dev).
|
|
|
|
## 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
|
|
|
|
```bash
|
|
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](https://bun.sh) v1.0+
|
|
|
|
```bash
|
|
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
|
|
|
|
```text
|
|
POST /auth/login Start QR code login, returns { qr_url }
|
|
POST /auth/logout Logout and stop message polling
|
|
```
|
|
|
|
### Status
|
|
|
|
```text
|
|
GET /status Login state, polling status, webhook config
|
|
```
|
|
|
|
### Messages (requires login)
|
|
|
|
```text
|
|
POST /messages/send Send one or more message items
|
|
GET /messages/updates?after=<seq> Get buffered incoming messages
|
|
```
|
|
|
|
<details>
|
|
<summary>Send body example</summary>
|
|
|
|
```json
|
|
{
|
|
"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.
|
|
|
|
</details>
|
|
|
|
### Media (requires login)
|
|
|
|
```text
|
|
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
|
|
```
|
|
|
|
<details>
|
|
<summary>Upload response</summary>
|
|
|
|
```json
|
|
{
|
|
"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`.
|
|
|
|
</details>
|
|
|
|
### Typing (requires login)
|
|
|
|
```text
|
|
POST /typing/start Send typing indicator { "to": "<user_id>" }
|
|
POST /typing/stop Cancel typing indicator { "to": "<user_id>" }
|
|
```
|
|
|
|
### Config (requires login)
|
|
|
|
```text
|
|
POST /config Get bot config (e.g. typing ticket) for a user
|
|
```
|
|
|
|
### Webhook (no auth required)
|
|
|
|
```text
|
|
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)
|
|
|
|
```text
|
|
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
|
|
|
|
```text
|
|
src/
|
|
├── sdk/ WeChat iLink protocol client (auth, API, CDN, crypto)
|
|
└── gateway/ HTTP gateway layer (routes, poller, state management)
|
|
```
|
|
|
|
## License
|
|
|
|
[MIT](LICENSE)
|