This adapter is for personal WeChat accounts (微信). If you need enterprise/corporate WeChat, see the WeCom adapter instead.
Prerequisites
- A personal WeChat account
- Python packages:
aiohttpandcryptography - Terminal QR rendering is included when Mibyan is installed with the
messagingextra
Setup
1. Run the Setup Wizard
The easiest way to connect your WeChat account is through the interactive setup:- Request a QR code from the iLink Bot API
- Display the QR code in your terminal (or provide a URL)
- Wait for you to scan the QR code with the WeChat mobile app
- Prompt you to confirm the login on your phone
- Save the account credentials automatically to
~/.mibyan/weixin/accounts/
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
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 inconfig.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:
- Pair Mibyan once with
mibyan gateway setupand note the connected iLink bot account. - Have each allowed user send a direct message to that bot/contact.
- Read the sender/user ID from the gateway logs or the inbound event payload.
- Add those IDs to
WEIXIN_ALLOWED_USERS, then restart the gateway.
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_paramURLs, 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
cryptographyPython package.
Outbound (sending)
All outbound media goes through the encrypted CDN upload flow:
- Generate a random AES-128 key
- Encrypt the file with AES-128-ECB + PKCS#7 padding
- Request an upload URL from the iLink API (
getuploadurl) - Upload the ciphertext to the CDN
- Send the message with the encrypted media reference
Context Token Persistence
The iLink Bot API requires acontext_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
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:- When a message arrives, the adapter fetches a
typing_ticketvia thegetconfigAPI - Typing tickets are cached for 10 minutes per user
send_typingsends a typing-start signal;stop_typingsends a typing-stop signal- 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
- Connect: Validates credentials and starts the poll loop
- Poll: Calls
getupdateswith a 35-second timeout; the server holds the request until messages arrive or the timeout expires - Dispatch: Inbound messages are dispatched concurrently via
asyncio.create_task - Sync buffer: A persistent sync cursor (
get_updates_buf) is saved to disk so the adapter resumes from the correct position after restarts

