weixin-grokbot-adapter
# weixin-grokbot-adapter
Standalone **WeChat ClawBot** adapter for [Grok Bot](https://grok.com) and [Cursor](https://cursor.com). It exposes a **local MCP stdio** server and a CLI so you can log in, receive messages, send text or media, and show a typing indicator — **without installing OpenClaw**.
This is an independent client for the official Weixin **iLink** HTTPS JSON API (the protocol behind WeChat ClawBot). The protocol matches [Tencent/openclaw-weixin](https://github.com/Tencent/openclaw-weixin) `3.0.0-beta-1` (MIT). The official npm package `@tencent-weixin/openclaw-weixin-cli` only installs an OpenClaw plugin and **must not** be used as a dependency or installer here.
**Requirements:** Node.js 20 or newer. A WeChat client with the official **ClawBot** plugin enabled, used to scan the bind QR code.
Zero runtime dependencies. The optional package `qrcode-terminal` can be installed if you want the login command to print a scannable QR code in the terminal.
## Install
From a local checkout (this project is not published to npm yet):
```bash
git clone <your-fork-or-local-path> weixin-grokbot-adapter
cd weixin-grokbot-adapter
node src/cli.js --help
```
Optional, so the `weixin-grokbot` command is on your `PATH`:
```bash
npm install --global .
# or, without a global install:
npm link
```
Optional terminal QR codes:
```bash
npm install qrcode-terminal
```
## Login
Login is **CLI-only**. The MCP server will never request a QR code or start a bind flow, so a bot host cannot pop a login prompt just by connecting the tool.
```bash
# Check whether credentials already exist (safe, no QR code)
node src/cli.js status
# In a real terminal, on a machine you control:
node src/cli.js login
```
Scan the printed URL (or QR code) with the WeChat account that has the ClawBot plugin enabled. If the phone shows a PIN, type it into the terminal. On success, credentials are written to `credentials.json` in the state directory with mode `600`.
To unbind this machine only (deletes local credentials and session state; it does not call a remote unbind API):
```bash
node src/cli.js logout
```
## Add as a local stdio MCP (Grok Bot / Cursor)
1. Complete `login` in a real terminal first.
2. Add a **local stdio MCP** server. Do not point Grok Bot at a cloud shell that cannot reach your state directory.
Example configuration:
```json
{
"command": "node",
"args": ["/absolute/path/to/weixin-grokbot-adapter/src/cli.js", "mcp"]
}
```
If you installed the CLI globally:
```json
{
"command": "weixin-grokbot",
"args": ["mcp"]
}
```
Use the `node` executable from your own machine (`which node`), and the absolute path to *this* checkout’s `src/cli.js`. You can also set `WEIXIN_GROKBOT_STATE_DIR` in the MCP server environment so the tool sees the same credentials you created with `login`.
### MCP tools
| Tool | Purpose |
| --- | --- |
| `weixin_status` | Whether a bot token is saved locally. Never starts login. |
| `weixin_poll` | One `getUpdates` long-poll (default ~35s). Empty results are normal. |
| `weixin_send` | Send a text message. Defaults to the last inbound peer. |
| `weixin_send_media` | Upload a local file or `http(s)` URL and send it as image, video, or file. |
| `weixin_typing` | Show or cancel the official typing indicator. |
Replies attach the cached `context_token` from the last inbound message for that user when one is available.
## CLI
```bash
node src/cli.js status
node src/cli.js poll [--timeout 35000] [--reset-cursor]
node src/cli.js send --text 'Hello' [--to 'id@im.wechat']
node src/cli.js send-media --file ./photo.jpg [--to 'id@im.wechat'] [--caption 'here'] [--type image]
node src/cli.js send-media --url 'https://example.com/file.pdf' --type file
node src/cli.js typing [--to 'id@im.wechat'] [--cancel]
node src/cli.js notify start|stop
node src/cli.js mcp
```
If no `--to` is given, the adapter uses the most recently seen inbound WeChat user.
## Configuration
All of these are optional except when you run the webhook forwarder. A logged-in `credentials.json` still supplies the API base URL returned by the server (it may redirect after bind).
| Variable | Default | Purpose |
| --- | --- | --- |
| `WEIXIN_GROKBOT_STATE_DIR` | `~/.weixin-grokbot-adapter` | Credentials, cursors, context tokens, inbound media |
| `WEIXIN_GROKBOT_BASE_URL` | `https://ilinkai.weixin.qq.com` | iLink API (used for a fresh login) |
| `WEIXIN_GROKBOT_CDN_BASE_URL` | `https://novac2c.cdn.weixin.qq.com/c2c` | Fallback CDN origin |
| `WEIXIN_GROKBOT_BOT_AGENT` | `GrokBotAdapter/1.1.0` | `base_info.bot_agent` |
| `WEIXIN_GROKBOT_LONG_POLL_TIMEOUT_MS` | `35000` | Client timeout for `getUpdates` |
| `WEIXIN_GROKBOT_API_TIMEOUT_MS` | `15000` | Timeout for ordinary API calls |
| `WEIXIN_GROKBOT_CONFIG_TIMEOUT_MS` | `10000` | Timeout for `getConfig` / typing / notify |
| `WEIXIN_GROKBOT_CDN_TIMEOUT_MS` | `60000` | Timeout for CDN upload/download |
| `WEIXIN_GROKBOT_MAX_UPLOAD_BYTES` | `104857600` (100 MiB) | Local and remote upload size limit |
See `.env.example` for a copy-paste template. Never commit a filled-in `.env`.
## Optional webhook forwarder
`src/forward-poller.js` long-polls inbound messages and `POST`s a JSON summary to a webhook you provide. It is **optional**, not used by the MCP server, and is not tied to any one product’s webhook URL.
```bash
export WEIXIN_FORWARD_WEBHOOK_URL="https://example.invalid/hooks/weixin"
export WEIXIN_FORWARD_WEBHOOK_SECRET="your-shared-secret"
node src/cli.js forward
```
| Variable | Default | Purpose |
| --- | --- | --- |
| `WEIXIN_FORWARD_WEBHOOK_URL` | _(required)_ | Destination URL |
| `WEIXIN_FORWARD_WEBHOOK_SECRET` | _(required)_ | Shared secret |
| `WEIXIN_FORWARD_WEBHOOK_HEADER` | `Authorization` | Header that carries the secret |
| `WEIXIN_FORWARD_WEBHOOK_SCHEME` | `Bearer` | Prefix; set to an empty string to send the secret as the raw header value |
| `WEIXIN_FORWARD_POLL_TIMEOUT_MS` | `35000` | Long-poll timeout |
| `WEIXIN_FORWARD_RETRY_MS` | `5000` | Delay after a poll or POST error |
The JSON body looks like `{ "source": "weixin-grokbot-adapter", "count": N, "messages": [...] }`. Message text and media paths are included; bot tokens are not.
## Protocol coverage
Default API host: `https://ilinkai.weixin.qq.com`. Authenticated requests send `AuthorizationType: ilink_bot_token`, `Authorization: Bearer <bot_token>`, `iLink-App-Id: bot`, plus `base_info.channel_version` (`3.0.0-beta-1/grokbot-adapter-1.1.0`) and `bot_agent`.
| Capability | Method / path | Status |
| --- | --- | --- |
| Bind QR code | `POST /ilink/bot/get_bot_qrcode?bot_type=3` | Implemented (CLI login) |
| Bind status / PIN | `GET /ilink/bot/get_qrcode_status` | Implemented (CLI login) |
| Receive messages | `POST /ilink/bot/getupdates` | Implemented |
| Send text | `POST /ilink/bot/sendmessage` | Implemented |
| Send image / video / file | `POST /ilink/bot/getuploadurl` + CDN POST + `sendmessage` | Implemented |
| Inbound media download | CDN GET + AES-128-ECB | Implemented |
| Typing ticket | `POST /ilink/bot/getconfig` | Implemented |
| Typing indicator | `POST /ilink/bot/sendtyping` | Implemented |
| Lifecycle | `POST /ilink/bot/msg/notifystart` / `notifystop` | Implemented (CLI `notify`) |
Outbound media follows the official upload flow: hash the plaintext, generate a 16-byte AES key and file key, call `getuploadurl` with `no_need_thumb: true`, encrypt with AES-128-ECB and PKCS#7 padding, `POST` `application/octet-stream` to `upload_full_url` (or a URL built from `upload_param` + `filekey`), read `x-encrypted-param`, and put that download parameter plus the AES key into the media item. HTTP 4xx is not retried; other CDN failures are retried up to three times. An optional caption is sent as a separate text message first.
## Out of scope
- Installing or depending on OpenClaw, the official OpenClaw plugin SDK, or `@tencent-weixin/openclaw-weixin-cli`
- Outbound voice messages (SILK encoding) and thumbnail (`thumb_media`) uploads
- Other-owner bind and real-name bind flows documented by the official plugin
- Automatic markdown filtering, quote-reply caches, or tool-call item types
- Acting as a public hosted WeChat gateway (this is a local client)
## Security
- `credentials.json` contains `bot_token` and account identifiers. It is written with mode `600`. Never commit, paste, or share that file, the state directory, or `.env`.
- `publicStatus` / `weixin_status` report only whether a token exists, never the token itself.
- Login must be started by a person in a terminal, not by the MCP server.
- The optional forwarder sends inbound message text to whatever URL you configure. Treat that webhook and its secret like credentials.
- `weixin_send_media` can read a local path or fetch an `http(s)` URL on the machine running the adapter. Only give that tool to hosts you trust.
## Library use
```js
import { pollOnce, sendText, sendMedia } from "weixin-grokbot-adapter";
const inbound = await pollOnce();
await sendText({ text: "hi" });
await sendMedia({ filePath: "./photo.jpg", caption: "look" });
```
Set `WEIXIN_GROKBOT_STATE_DIR` (or log in first) so the library can find credentials.
## Development
```bash
node --test test/*.test.js
# or
npm test
```
See [CONTRIBUTING.md](./CONTRIBUTING.md) and [CHANGELOG.md](./CHANGELOG.md). A Chinese guide is in [README.zh-CN.md](./README.zh-CN.md).
## License
MIT. See [LICENSE](./LICENSE). Portions implement the Weixin iLink HTTP JSON protocol documented and implemented in [Tencent/openclaw-weixin](https://github.com/Tencent/openclaw-weixin), Copyright (c) Tencent, licensed under the MIT License.
TDQS
Scored across 5 tools
Each tool targets a distinct operation: status inspection, message polling, text sending, media sending, and typing indicators. The only adjacent pair, weixin_send and weixin_send_media, is clearly separated by text vs. file/URL payloads.
All tools share the same weixin_ prefix and snake_case convention, making the namespace predictable. However, the suffix mixes states/nouns (status, typing) with actions (poll, send), so the verb pattern is not perfectly uniform.
Five tools is a well-scoped surface for a Weixin bot adapter, covering the essential receive, send, media, and presence operations without bloat. Each tool earns its place.
The core messaging lifecycle is covered: status, inbound polling, outbound text, media attachments, and typing indicators. Minor gaps exist around login/QR initiation and richer message or contact operations, but those appear intentionally delegated to the CLI.