include weixin-ilink-gateway source

This commit is contained in:
mayunfei committed 2026-12-04 10:12:20 +08:00
1 parent 5f8428a318
commit 43a6f7b72b
51 files changed
+5351 -1

No files matched your search

+169
View File
@@ -0,0 +1,169 @@
# weixin-ilink-gateway
**[English](README.md)**
基于微信官方插件 [`@tencent-weixin/openclaw-weixin`](https://www.npmjs.com/package/@tencent-weixin/openclaw-weixin) 封装的轻量 HTTP 网关,将 iLink Bot 协议的扫码登录、消息收发、媒体上传下载、Webhook 推送与接收等能力封装为 REST API,方便任何语言和平台接入微信 ClawBot。
基于 [Bun](https://bun.sh) + [Hono](https://hono.dev) + [Zod](https://zod.dev) 构建。
## 功能特性
- **扫码登录** — 简单的二维码登录流程,支持实时轮询扫码状态
- **消息接收** — 后台长轮询,内存环形缓冲区 + 可选 Webhook 推送
- **消息发送** — 支持文本、图片、视频、文件;URL 媒体自动下载并上传至 CDN
- **媒体处理** — 上传、预签名、下载,自动处理 AES 加解密
- **状态持久化** — 登录凭证、同步游标、上下文令牌重启后自动恢复
- **Webhook 推送** — 收到消息后自动推送至配置的 URL,失败自动重试;同时支持入站 Webhook
## 快速开始
```bash
git clone https://github.com/kadxy/weixin-ilink-gateway.git
cd weixin-ilink-gateway
cp .env.example .env # 编辑 .env,至少设置 API_KEY
docker compose up -d
```
服务默认运行在 `http://localhost:29527`。
### 从源码运行
> 需要 [Bun](https://bun.sh) v1.0+
```bash
bun install
bun run dev # 文件变更时自动重载
```
默认端口 **3456**(可通过 `PORT` 环境变量覆盖)。
## 配置项
| 变量 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `API_KEY` | **是** | — | API 请求的 Bearer Token(至少 12 位) |
| `PORT` | 否 | `3456` | 服务监听端口 |
| `DATA_DIR` | 否 | `./data` | 持久化状态文件目录 |
| `WEBHOOK_URLS` | 否 | — | 消息推送目标 URL,多个用逗号分隔 |
| `WEBHOOK_TOKEN` | 否 | — | 启用 `POST /webhook/:token` 入站端点 |
| `MAX_DOWNLOAD_SIZE_MB` | 否 | `128` | URL 媒体下载的最大文件大小(MB) |
| `DOWNLOAD_TIMEOUT_MS` | 否 | `30000` | 下载外部媒体 URL 的超时时间(毫秒) |
| `MAX_BUFFER_SIZE` | 否 | `1000` | 内存环形缓冲区最大消息数 |
## API 参考
除特别说明外,所有接口均需要 `Authorization: Bearer <API_KEY>` 认证。
### 登录认证
```text
POST /auth/login 发起扫码登录,返回 { qr_url }
POST /auth/logout 登出并停止消息轮询
```
### 状态
```text
GET /status 登录状态、轮询状态、Webhook 配置
```
### 消息(需登录)
```text
POST /messages/send 发送一条或多条消息
GET /messages/updates?after=<seq> 获取缓冲区中的新消息
```
<details>
<summary>发送请求体示例</summary>
```json
{
"to": "<user_id 或 \"self\">",
"items": [
{ "type": "text", "text": "你好!" },
{ "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": "(可选,自动从缓存中解析)"
}
```
每种消息类型支持 `url`(网关自动下载并上传至 CDN)或 `cdn`(已上传的 CDN 引用)两种方式。`file` 类型还需要提供 `file_name`。
> **注意:** 发送消息需要目标用户的 `context_token`。该令牌会在轮询器收到该用户的消息时自动缓存。在此之前,向该用户发送消息会失败。要初始化令牌,需要先让目标用户向 Bot 发送一条消息。
</details>
### 媒体(需登录)
```text
POST /media/upload 通过 multipart 表单上传文件(字段:file, to)
POST /media/presign 获取预签名 URL,用于直接上传至 CDN
POST /media/download 从 CDN 下载并解密文件
```
<details>
<summary>上传响应示例</summary>
```json
{
"ok": true,
"media_type": "image",
"file_name": "photo.png",
"filekey": "...",
"encrypt_query_param": "...",
"aes_key": "...",
"file_size": 12345,
"file_size_ciphertext": 12352
}
```
返回的 `encrypt_query_param` 和 `aes_key` 可作为 `cdn` 引用传入 `/messages/send`。
</details>
### 输入状态(需登录)
```text
POST /typing/start 发送输入状态指示器 { "to": "<user_id>" }
POST /typing/stop 取消输入状态指示器 { "to": "<user_id>" }
```
### 配置(需登录)
```text
POST /config 获取指定用户的 Bot 配置(如 typing ticket)
```
### Webhook(无需认证)
```text
POST /webhook/:token 通过入站 Webhook 向自己发送文本消息
```
支持 `application/json`(`{ "text": "..." }`)或 `text/plain` 请求体。
需要设置 `WEBHOOK_TOKEN` 环境变量,URL 中的 `:token` 必须与之匹配。
### 调试控制台(无需认证)
```text
GET /console 浏览器调试控制台
```
内置的单页调试界面,在浏览器中输入 API Key 即可扫码登录、发送测试消息等。支持中英文自动切换。
## 项目结构
```text
src/
├── sdk/ 微信 iLink 协议客户端(登录、API、CDN、加解密)
└── gateway/ HTTP 网关层(路由、轮询、状态管理)
```
## 协议
[MIT](LICENSE)