wechat-cli 0.1.0

A CLI tool to interact with a Wechat iLink bot.
wechat-cli-0.1.0 is not a library.

wechat-cli

License Crates.io Docs

wechat-cli is a command-line client for the WeChat iLink Bot API.

It supports:

  • QR code login
  • Listing local accounts
  • Waiting for the next incoming message to print context_token
  • Sending text, images, and files

Requirements

  • Rust
  • Access to https://ilinkai.weixin.qq.com
  • A WeChat client that can scan the login QR code

Build

cargo build

Binary:

target/debug/wechat-cli

Quick Start

Login:

wechat-cli login

List local accounts:

wechat-cli account list

Wait for the next incoming user message and print context_token:

wechat-cli get-context-token --user-id <user_id>

Send a text message:

wechat-cli send --user-id <user_id> --context-token <token> --text "hello"

Send a file:

wechat-cli send --user-id <user_id> --context-token <token> --file ./demo.pdf

Send an image:

wechat-cli send --user-id <user_id> --context-token <token> --file ./demo.png

Send an image or file with a caption:

wechat-cli send --user-id <user_id> --context-token <token> --file ./demo.png --caption "this is an image"

Commands

login

wechat-cli login [--base-url <base_url>]

Default base_url:

https://ilinkai.weixin.qq.com

account list

wechat-cli account list

get-context-token

wechat-cli get-context-token [--user-id <user_id>]

send

wechat-cli send [OPTIONS] <--text <TEXT>|--file <FILE>>

Options:

  • --account <INDEX>
  • --user-id <USER_ID>
  • --token <TOKEN>
  • --base-url <BASE_URL> optional in explicit credential mode
  • --route-tag <ROUTE_TAG>
  • --context-token <CONTEXT_TOKEN> required
  • --text <TEXT>
  • --file <FILE>
  • --caption <CAPTION>

Rules:

  • --account <index> selects a saved account
  • if --account and --user-id are both omitted, saved account index 0 is used
  • --token switches send into explicit credential mode
  • in explicit credential mode, --user-id is required and --base-url defaults to https://ilinkai.weixin.qq.com
  • --text and --file are mutually exclusive
  • --caption can only be used with --file
  • Image files are sent as image messages automatically
  • Other files are sent as file messages

Workflow

Recommended flow:

  1. Run login
  2. Run get-context-token --user-id <user_id>
  3. Send one message from the bound WeChat user to the bot
  4. Copy the printed token
  5. Run send --context-token <token>

Example:

wechat-cli send --account 0 --context-token <token> --text "hello"

IDs

  • user_id ends with @im.wechat
  • bot_id ends with @im.bot
  • Re-login for the same user_id changes the bound bot_id
  • The bot can only send messages to its currently bound user_id

Message direction:

  • user -> bot: from_user_id = user_id, to_user_id = bot_id
  • bot -> user: to_user_id = user_id

Storage

Local files are stored under:

~/.config/wechat-cli/

Main files:

~/.config/wechat-cli/accounts.json

Notes:

  • accounts.json stores all accounts
  • context_token is not stored locally
  • get_updates_buf is not stored locally
  • Old storage layouts are not read for compatibility

Limitations

  • Sending requires explicitly passing a valid --context-token
  • If the server returns Session expired, you must log in again
  • The bot cannot proactively message arbitrary users
  • Voice and video sending are not supported yet

Help

wechat-cli --help
wechat-cli login --help
wechat-cli account --help
wechat-cli get-context-token --help
wechat-cli send --help