Skip to main content
Python dependency commands on this page use a PM-prepared source checkout. After a dependency change, reactivate the checkout and restart Mibyan. Connect Mibyan to WeChat (微信), Tencent’s personal messaging platform. The adapter uses Tencent’s iLink Bot API for personal WeChat accounts — this is distinct from WeCom (Enterprise WeChat). Messages are delivered via long-polling, so no public endpoint or webhook is required.
This adapter is for personal WeChat accounts (微信). If you need enterprise/corporate WeChat, see the WeCom adapter instead.
iLink bot identity — ordinary WeChat groups may not workQR login connects Mibyan to an iLink bot identity (e.g. a5ace6fd482e@im.bot), not a fully scriptable ordinary personal WeChat account. Consequences:
  • The iLink bot identity generally cannot be invited into ordinary WeChat groups the way a normal contact can.
  • iLink typically does not deliver ordinary WeChat group events (including @-mentions of the personal account used for QR login) to the gateway for most bot-type accounts.
  • @-mentioning the personal WeChat account used to scan the QR code is not the same as @-mentioning the iLink bot — the bot is a separate identity.
  • The WEIXIN_GROUP_POLICY / WEIXIN_GROUP_ALLOWED_USERS settings below only take effect when iLink actually returns group events for your account type. If it doesn’t, group messages will never reach Mibyan regardless of policy.
In practice, most deployments only get DMs to the iLink bot working reliably. If group delivery doesn’t work after configuration, the limitation is on the iLink side, not in Mibyan. The gateway logs a WARNING at startup whenever WEIXIN_GROUP_POLICY is set to anything other than disabled.

Prerequisites

  • A personal WeChat account
  • Python packages: aiohttp and cryptography
  • Terminal QR rendering is included when Mibyan is installed with the messaging extra
Install the required dependencies:

Setup

1. Run the Setup Wizard

The easiest way to connect your WeChat account is through the interactive setup:
Select Weixin when prompted. The wizard will:
  1. Request a QR code from the iLink Bot API
  2. Display the QR code in your terminal (or provide a URL)
  3. Wait for you to scan the QR code with the WeChat mobile app
  4. Prompt you to confirm the login on your phone
  5. Save the account credentials automatically to ~/.mibyan/weixin/accounts/
Once confirmed, you’ll see a message like:
The wizard stores the account_id, token, and base_url so you don’t need to configure them manually.

2. Configure Environment Variables

After initial QR login, set at minimum the account ID in ~/.mibyan/.env:

3. Start the Gateway

The adapter will restore saved credentials, connect to the iLink API, and begin long-polling for messages.

Features

  • Long-poll transport — no public endpoint, webhook, or WebSocket needed
  • QR code login — scan-to-connect setup via mibyan gateway setup
  • DM messaging — configurable access policies; group messaging depends on iLink actually delivering group events for the connected identity (often not the case for iLink bot accounts — see the warning above)
  • Media support — images, video, files, and voice messages
  • AES-128-ECB encrypted CDN — automatic encryption/decryption for all media transfers
  • Context token persistence — disk-backed reply continuity across restarts
  • Markdown formatting — preserves Markdown, including headers, tables, and code blocks, so WeChat clients that support Markdown can render it natively
  • Smart message chunking — messages stay as a single bubble when under the limit; only oversized payloads split at logical boundaries
  • Typing indicators — shows “typing…” status in the WeChat client while the agent processes
  • SSRF protection — outbound media URLs are validated before download
  • Message deduplication — 5-minute sliding window prevents double-processing
  • Automatic retry with backoff — recovers from transient API errors

Configuration Options

Set these in config.yaml under platforms.weixin.extra:

Access Policies

DM Policy

Controls who can send direct messages to the bot:
WEIXIN_ALLOWED_USERS is an inbound filter, not an invitation system. QR login connects one iLink bot identity to Mibyan. Other people do not scan the Mibyan QR code with their own accounts; they must message the connected iLink bot/contact through WeChat, and Mibyan will process the DM only if the sender’s Weixin user ID is present in WEIXIN_ALLOWED_USERS. A practical setup flow is:
  1. Pair Mibyan once with mibyan gateway setup and note the connected iLink bot account.
  2. Have each allowed user send a direct message to that bot/contact.
  3. Read the sender/user ID from the gateway logs or the inbound event payload.
  4. Add those IDs to WEIXIN_ALLOWED_USERS, then restart the gateway.
If only the account that scanned the QR code can talk to Mibyan, verify that the other users are messaging the iLink bot identity itself, not the personal WeChat account that performed the QR login. The iLink bot is a separate identity, and ordinary WeChat contact/group routing can be limited by Tencent’s iLink behavior.

Group Policy

Controls which groups the bot responds in when iLink delivers group events for the connected identity. For QR-login iLink bot identities (e.g. ...@im.bot), group events are typically not delivered at all, so this policy may have no effect — see the iLink bot limitation warning at the top of the page.
The default group policy is disabled for Weixin (unlike WeCom where it defaults to open). This is intentional — personal WeChat accounts may be in many groups, and iLink bot identities typically can’t receive ordinary WeChat group messages at all. The gateway logs a WARNING at startup if you set WEIXIN_GROUP_POLICY to anything other than disabled.

Media Support

Inbound (receiving)

The adapter receives media attachments from users, downloads them from the WeChat CDN, decrypts them, and caches them locally for agent processing: Quoted messages: Media from quoted (replied-to) messages is also extracted, so the agent has context about what the user is replying to.

AES-128-ECB Encrypted CDN

WeChat media files are transferred through an encrypted CDN. The adapter handles this transparently:
  • Inbound: Encrypted media is downloaded from the CDN using encrypted_query_param URLs, then decrypted with AES-128-ECB using the per-file key provided in the message payload.
  • Outbound: Files are encrypted locally with a random AES-128-ECB key, uploaded to the CDN, and the encrypted reference is included in the outbound message.
  • The AES key is 16 bytes (128-bit). Keys may arrive as raw base64 or hex-encoded — the adapter handles both formats.
  • This requires the cryptography Python package.
No configuration is needed — encryption and decryption happen automatically.

Outbound (sending)

All outbound media goes through the encrypted CDN upload flow:
  1. Generate a random AES-128 key
  2. Encrypt the file with AES-128-ECB + PKCS#7 padding
  3. Request an upload URL from the iLink API (getuploadurl)
  4. Upload the ciphertext to the CDN
  5. Send the message with the encrypted media reference

Context Token Persistence

The iLink Bot API requires a context_token to be echoed back with each outbound message for a given peer. The adapter maintains a disk-backed context token store:
  • Tokens are saved per account+peer to ~/.mibyan/weixin/accounts/<account_id>.context-tokens.json
  • On startup, previously saved tokens are restored
  • Every inbound message updates the stored token for that sender
  • Outbound messages automatically include the latest context token
This ensures reply continuity even after gateway restarts.

Markdown Formatting

WeChat clients connected through the iLink Bot API can render Markdown directly, so the adapter preserves Markdown instead of rewriting it:
  • Headers stay as Markdown headings (#, ##, …)
  • Tables stay as Markdown tables
  • Code fences stay as fenced code blocks
  • Excessive blank lines are collapsed to double newlines outside fenced code blocks

Message Chunking

Messages are delivered as a single chat message whenever they fit within the platform limit. Only oversized payloads are split for delivery:
  • Maximum message length: 4000 characters
  • Messages under the limit stay intact even when they contain multiple paragraphs or line breaks
  • Oversized messages split at logical boundaries (paragraphs, blank lines, code fences)
  • Code fences are kept intact whenever possible (never split mid-block unless the fence itself exceeds the limit)
  • Oversized individual blocks fall back to the base adapter’s truncation logic
  • A 0.3 s inter-chunk delay prevents WeChat rate-limit drops when multiple chunks are sent

Typing Indicators

The adapter shows typing status in the WeChat client:
  1. When a message arrives, the adapter fetches a typing_ticket via the getconfig API
  2. Typing tickets are cached for 10 minutes per user
  3. send_typing sends a typing-start signal; stop_typing sends a typing-stop signal
  4. The gateway automatically triggers typing indicators while the agent processes a message

Long-Poll Connection

The adapter uses HTTP long-polling (not WebSocket) to receive messages:

How It Works

  1. Connect: Validates credentials and starts the poll loop
  2. Poll: Calls getupdates with a 35-second timeout; the server holds the request until messages arrive or the timeout expires
  3. Dispatch: Inbound messages are dispatched concurrently via asyncio.create_task
  4. Sync buffer: A persistent sync cursor (get_updates_buf) is saved to disk so the adapter resumes from the correct position after restarts

Retry Behavior

On API errors, the adapter uses a simple retry strategy:

Deduplication

Inbound messages are deduplicated using message IDs with a 5-minute window. This prevents double-processing during network hiccups or overlapping poll responses.

Token Lock

Only one Weixin gateway instance can use a given token at a time. The adapter acquires a scoped lock on startup and releases it on shutdown. If another gateway is already using the same token, startup fails with an informative error message.

All Environment Variables

Troubleshooting