agent-wechatbot
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAgent WeChatBot
Agent WeChatBot
A TypeScript CLI tool that enables AI agents and humans to send messages through WeChat Official Account API. Designed for customer engagement, template notifications, and CI/CD integrations using App ID + App Secret authentication.
一款TypeScript CLI工具,支持AI Agent和人类通过微信公众号API发送消息。基于App ID + App Secret认证,适用于客户互动、模板通知以及CI/CD集成场景。
Key Concepts
核心概念
Before diving in, a few things about WeChat Official Account API:
- Send-only — The Official Account API delivers inbound messages via webhooks only. This CLI cannot list or read received messages.
- Customer service messages — You can send free-form text, image, and news (article) messages to users who have interacted with your account within the last 48 hours.
- Template messages — Pre-approved message templates can be sent at any time. Templates must be created and approved in the WeChat Official Account admin panel.
- App ID — Your Official Account's unique application identifier. Found in the WeChat Official Account admin panel under Development > Basic Configuration.
- App Secret — Your application's secret key, paired with the App ID. Found in the same location.
- OpenID — Each follower has a unique OpenID scoped to your Official Account. Use to retrieve follower OpenIDs.
user list - IP Whitelist — Your server IP must be added to the Official Account's IP whitelist, or API calls will fail with error .
40164 - Rate limits — WeChat enforces API call frequency limits. Customer service messages are limited per account per day.
开始使用前,先了解微信公众号API的几个关键点:
- 仅支持发送 —— 微信公众号API仅通过webhook接收消息,本CLI无法查看或读取收到的消息。
- 客服消息 —— 对于过去48小时内与公众号互动过的用户,你可以发送自由格式的文本、图片和图文(文章)消息。
- 模板消息 —— 预先审核通过的消息模板可随时发送。模板需在微信公众号后台创建并审核。
- App ID —— 公众号的唯一应用标识,可在微信公众号后台「开发 > 基本配置」中找到。
- App Secret —— 与App ID配对的应用密钥,同样在上述位置获取。
- OpenID —— 每个粉丝对应一个针对当前公众号的唯一OpenID,可使用命令获取粉丝的OpenID。
user list - IP白名单 —— 服务器IP必须添加到公众号的IP白名单中,否则API调用会返回错误。
40164 - 调用限制 —— 微信对API调用频率有限制,客服消息有每日账号调用上限。
Quick Start
快速开始
bash
undefinedbash
undefinedSet your API credentials
设置API凭证
agent-wechatbot auth set your-app-id your-app-secret
agent-wechatbot auth set your-app-id your-app-secret
Verify authentication
验证认证状态
agent-wechatbot auth status
agent-wechatbot auth status
Send a text message (recipient must have interacted within 48h)
发送文本消息(接收者需在48小时内有互动)
agent-wechatbot message send oXXXXXXXXXXXXXXX "Hello from the CLI!"
agent-wechatbot message send oXXXXXXXXXXXXXXX "Hello from the CLI!"
List available templates
查看可用模板
agent-wechatbot template list --pretty
agent-wechatbot template list --pretty
List followers
查看粉丝列表
agent-wechatbot user list --pretty
undefinedagent-wechatbot user list --pretty
undefinedAuthentication
认证
API Credential Setup
API凭证设置
agent-wechatbot uses App ID + App Secret pairs from the WeChat Official Account admin panel:
bash
undefinedagent-wechatbot使用微信公众号后台获取的App ID + App Secret配对:
bash
undefinedSet credentials (validates against WeChat API before saving)
设置凭证(保存前会验证微信API)
agent-wechatbot auth set your-app-id your-app-secret
agent-wechatbot auth set your-app-id your-app-secret
Check auth status
检查认证状态
agent-wechatbot auth status
agent-wechatbot auth status
Clear stored credentials
清除已存储的凭证
agent-wechatbot auth clear
undefinedagent-wechatbot auth clear
undefinedMulti-Account Management
多账号管理
bash
undefinedbash
undefinedList stored accounts
查看已存储的账号
agent-wechatbot auth list
agent-wechatbot auth list
Switch active account
切换活跃账号
agent-wechatbot auth use <account-id>
agent-wechatbot auth use <account-id>
Remove a stored account
删除已存储的账号
agent-wechatbot auth remove <account-id>
undefinedagent-wechatbot auth remove <account-id>
undefinedMemory
记忆功能
The agent maintains a file as persistent memory across sessions. This is agent-managed, the CLI does not read or write this file. Use the and tools to manage your memory file.
~/.config/agent-messenger/MEMORY.mdReadWriteAgent会在文件中保存跨会话的持久化记忆。该文件由Agent管理,CLI不会直接读写,需使用和工具管理。
~/.config/agent-messenger/MEMORY.mdReadWriteReading Memory
读取记忆
At the start of every task, read using the tool to load any previously discovered account IDs, template names, follower OpenIDs, and preferences.
~/.config/agent-messenger/MEMORY.mdRead- If the file doesn't exist yet, that's fine. Proceed without it and create it when you first have useful information to store.
- If the file can't be read (permissions, missing directory), proceed without memory. Don't error out.
在每个任务开始时,使用工具读取,加载之前记录的账号ID、模板名称、粉丝OpenID和偏好设置。
Read~/.config/agent-messenger/MEMORY.md- 如果文件不存在,无需处理,后续有有用信息时再创建即可。
- 如果无法读取文件(权限问题、目录缺失),跳过记忆功能继续执行,不要抛出错误。
Writing Memory
写入记忆
After discovering useful information, update using the tool. Write triggers include:
~/.config/agent-messenger/MEMORY.mdWrite- After discovering account IDs and App IDs (from ,
auth list, etc.)auth status - After discovering template names and their parameters (from , etc.)
template list - After discovering follower OpenIDs (from ,
user list, etc.)user get - After the user gives you an alias or preference ("call this the notifications account", "my main template is X")
When writing, include the complete file content. The tool overwrites the entire file.
Write发现有用信息后,使用工具更新。触发写入的场景包括:
Write~/.config/agent-messenger/MEMORY.md- 获取到账号ID和App ID后(来自、
auth list等命令)auth status - 获取到模板名称及其参数后(来自等命令)
template list - 获取到粉丝OpenID后(来自、
user list等命令)user get - 用户设置别名或偏好后(如"将这个账号称为通知账号"、"我的主要模板是X")
写入时需包含完整的文件内容,工具会覆盖整个文件。
WriteWhat to Store
存储内容
- Account IDs (App IDs) with names
- Template IDs with their required parameters
- Frequently used follower OpenIDs with context
- User-given aliases ("notifications account", "marketing account")
- Any user preference expressed during interaction
- 带名称的账号ID(App ID)
- 带必填参数的模板ID
- 带上下文的常用粉丝OpenID
- 用户设置的别名(如"通知账号"、"营销账号")
- 交互过程中用户表达的任何偏好
What NOT to Store
禁止存储内容
Never store App Secrets or any credentials. Never store full message content (just context). Never store personal user data.
切勿存储App Secret或任何凭证,切勿存储完整消息内容(仅保留上下文),切勿存储用户个人数据。
Handling Stale Data
处理过期数据
If a memorized template returns an error (template not found, account invalid), remove it from . Don't blindly trust memorized data. Verify when something seems off. Prefer re-listing over using a memorized value that might be stale.
MEMORY.md如果记忆中的模板返回错误(模板不存在、账号无效),请从中删除该条目。不要盲目信任记忆数据,当出现异常时进行验证,优先重新查询而非使用可能过期的记忆值。
MEMORY.mdFormat / Example
格式示例
markdown
undefinedmarkdown
undefinedAgent Messenger Memory
Agent Messenger Memory
WeChat Accounts
WeChat Accounts
- - Acme Notifications
wx1234567890
- - Acme Notifications
wx1234567890
Templates (Acme Notifications)
Templates (Acme Notifications)
- - Order confirmation, params: [order_id, customer_name]
TM00001 - - Shipping update, params: [tracking_number]
TM00002
- - Order confirmation, params: [order_id, customer_name]
TM00001 - - Shipping update, params: [tracking_number]
TM00002
Frequent Recipients
Frequent Recipients
- - Test user (internal QA)
oABCD1234 - - VIP customer
oEFGH5678
- - Test user (internal QA)
oABCD1234 - - VIP customer
oEFGH5678
Aliases
Aliases
- "notifications" -> (Acme Notifications)
wx1234567890
- "notifications" -> (Acme Notifications)
wx1234567890
Notes
Notes
- IP whitelist configured for 203.0.113.10
- Customer service messages limited to 48h interaction window
> Memory lets you skip repeated `template list` calls. When you already know a template ID from a previous session, use it directly.- IP whitelist configured for 203.0.113.10
- Customer service messages limited to 48h interaction window
> 记忆功能可避免重复调用`template list`命令。如果之前会话中已获取模板ID,可直接使用。Commands
命令
Auth Commands
认证命令
bash
undefinedbash
undefinedSet account credentials (validates against API)
设置账号凭证(会通过API验证)
agent-wechatbot auth set <app-id> <app-secret>
agent-wechatbot auth set <app-id> <app-secret>
Check auth status
检查认证状态
agent-wechatbot auth status
agent-wechatbot auth status --account <account-id>
agent-wechatbot auth status
agent-wechatbot auth status --account <account-id>
List stored accounts
查看已存储的账号
agent-wechatbot auth list
agent-wechatbot auth list
Switch active account
切换活跃账号
agent-wechatbot auth use <account-id>
agent-wechatbot auth use <account-id>
Remove a stored account
删除已存储的账号
agent-wechatbot auth remove <account-id>
agent-wechatbot auth remove <account-id>
Clear all credentials
清除所有凭证
agent-wechatbot auth clear
undefinedagent-wechatbot auth clear
undefinedWhoami Command
Whoami命令
bash
undefinedbash
undefinedShow current authenticated bot
显示当前已认证的Bot
agent-wechatbot whoami
agent-wechatbot whoami --pretty
agent-wechatbot whoami --account <account-id>
undefinedagent-wechatbot whoami
agent-wechatbot whoami --pretty
agent-wechatbot whoami --account <account-id>
undefinedMessage Commands
消息命令
bash
undefinedbash
undefinedSend a text message (customer service, within 48h window)
发送文本消息(客服消息,需在48小时互动窗口内)
agent-wechatbot message send <open-id> <text>
agent-wechatbot message send oABCD1234 "Your order has shipped!"
agent-wechatbot message send <open-id> <text>
agent-wechatbot message send oABCD1234 "Your order has shipped!"
Send an image message (customer service, within 48h window)
发送图片消息(客服消息,需在48小时互动窗口内)
agent-wechatbot message send-image <open-id> <media-id>
agent-wechatbot message send-image oABCD1234 MEDIA_ID_HERE
agent-wechatbot message send-image <open-id> <media-id>
agent-wechatbot message send-image oABCD1234 MEDIA_ID_HERE
Send a news/article message (customer service, within 48h window)
发送图文/文章消息(客服消息,需在48小时互动窗口内)
agent-wechatbot message send-news <open-id> --title "Title" --description "Desc" --url "https://..." --picurl "https://..."
undefinedagent-wechatbot message send-news <open-id> --title "Title" --description "Desc" --url "https://..." --picurl "https://..."
undefinedTemplate Commands
模板命令
bash
undefinedbash
undefinedList message templates
查看消息模板
agent-wechatbot template list
agent-wechatbot template list
Send a template message
发送模板消息
agent-wechatbot template send <open-id> <template-id>
agent-wechatbot template send oABCD1234 TM00001 --data '{"order_id":{"value":"ORD-9876"},"customer_name":{"value":"Alice"}}' --url "https://example.com/order/9876"
agent-wechatbot template send <open-id> <template-id>
agent-wechatbot template send oABCD1234 TM00001 --data '{"order_id":{"value":"ORD-9876"},"customer_name":{"value":"Alice"}}' --url "https://example.com/order/9876"
Delete a template
删除模板
agent-wechatbot template delete <template-id>
undefinedagent-wechatbot template delete <template-id>
undefinedUser Commands
用户命令
bash
undefinedbash
undefinedList followers (paginated)
查看粉丝列表(分页)
agent-wechatbot user list
agent-wechatbot user list --next-openid oLAST_OPENID
agent-wechatbot user list
agent-wechatbot user list --next-openid oLAST_OPENID
Get user info by OpenID
通过OpenID获取用户信息
agent-wechatbot user get <open-id>
agent-wechatbot user get oABCD1234 --lang en
undefinedagent-wechatbot user get <open-id>
agent-wechatbot user get oABCD1234 --lang en
undefinedOutput Format
输出格式
JSON (Default)
JSON(默认)
All commands output JSON by default for AI consumption:
json
{
"success": true,
"app_id": "wx1234567890",
"account_name": "wx1234567890"
}所有命令默认输出JSON格式,方便AI处理:
json
{
"success": true,
"app_id": "wx1234567890",
"account_name": "wx1234567890"
}Pretty (Human-Readable)
格式化输出(人类可读)
Use flag for formatted output:
--prettybash
agent-wechatbot template list --pretty使用参数获取格式化输出:
--prettybash
agent-wechatbot template list --prettyGlobal Options
全局参数
| Option | Description |
|---|---|
| Human-readable output instead of JSON |
| Use a specific account for this command |
| 参数 | 描述 |
|---|---|
| 输出人类可读格式而非JSON |
| 为当前命令指定特定账号 |
Common Patterns
常见使用场景
See for additional workflows.
references/common-patterns.md更多工作流请查看。
references/common-patterns.mdSend a customer service message within 48h window
在48小时窗口内发送客服消息
Customer service messages can be sent to users who have interacted with your account within the last 48 hours:
bash
undefined可向过去48小时内与公众号互动过的用户发送客服消息:
bash
undefinedSend a text reply
发送文本回复
agent-wechatbot message send oABCD1234 "Thanks for reaching out! We'll look into this right away."
agent-wechatbot message send oABCD1234 "Thanks for reaching out! We'll look into this right away."
Send a news article
发送图文文章
agent-wechatbot message send-news oABCD1234
--title "Your Order Update"
--description "Your order #12345 has been shipped"
--url "https://example.com/orders/12345"
--picurl "https://example.com/images/shipping.jpg"
--title "Your Order Update"
--description "Your order #12345 has been shipped"
--url "https://example.com/orders/12345"
--picurl "https://example.com/images/shipping.jpg"
undefinedagent-wechatbot message send-news oABCD1234
--title "Your Order Update"
--description "Your order #12345 has been shipped"
--url "https://example.com/orders/12345"
--picurl "https://example.com/images/shipping.jpg"
--title "Your Order Update"
--description "Your order #12345 has been shipped"
--url "https://example.com/orders/12345"
--picurl "https://example.com/images/shipping.jpg"
undefinedSend a template notification (anytime)
发送模板通知(随时可发)
Template messages can be sent at any time, regardless of the 48h window:
bash
undefined模板消息不受48小时窗口限制,可随时发送:
bash
undefinedList templates to find the right one
查看模板找到合适的模板
agent-wechatbot template list --pretty
agent-wechatbot template list --pretty
Send a template message with data
发送带参数的模板消息
agent-wechatbot template send oABCD1234 TM00001
--data '{"order_id":{"value":"ORD-9876"},"status":{"value":"Shipped"}}'
--url "https://example.com/orders/9876"
--data '{"order_id":{"value":"ORD-9876"},"status":{"value":"Shipped"}}'
--url "https://example.com/orders/9876"
undefinedagent-wechatbot template send oABCD1234 TM00001
--data '{"order_id":{"value":"ORD-9876"},"status":{"value":"Shipped"}}'
--url "https://example.com/orders/9876"
--data '{"order_id":{"value":"ORD-9876"},"status":{"value":"Shipped"}}'
--url "https://example.com/orders/9876"
undefinedList and inspect followers
查看并检查粉丝信息
bash
undefinedbash
undefinedGet first page of followers
获取第一页粉丝列表
agent-wechatbot user list --pretty
agent-wechatbot user list --pretty
Get next page
获取下一页粉丝列表
agent-wechatbot user list --next-openid oLAST_OPENID --pretty
agent-wechatbot user list --next-openid oLAST_OPENID --pretty
Get details for a specific follower
获取特定粉丝的详细信息
agent-wechatbot user get oABCD1234 --pretty
undefinedagent-wechatbot user get oABCD1234 --pretty
undefinedCI/CD deployment notification
CI/CD部署通知
bash
agent-wechatbot template send oABCD1234 deployment_alert \
--data '{"version":{"value":"v2.1.0"},"environment":{"value":"production"},"status":{"value":"success"}}'bash
agent-wechatbot template send oABCD1234 deployment_alert \
--data '{"version":{"value":"v2.1.0"},"environment":{"value":"production"},"status":{"value":"success"}}'Templates
模板示例
See directory for runnable examples:
templates/- - Send a customer service message with error handling and retries
post-message.sh - - Generate account, template, and follower summary
account-summary.sh - - Send a template message with parameters
send-template.sh
templates/- - 带错误处理和重试机制的客服消息发送脚本
post-message.sh - - 生成账号、模板和粉丝汇总信息
account-summary.sh - - 发送带参数的模板消息
send-template.sh
Error Handling
错误处理
All commands return consistent error format:
json
{
"error": "No credentials. Run \"auth set <app-id> <app-secret>\" first."
}Common errors: , , , , , .
No credentialsAccount not foundInvalid credentialsWeChat API error (errcode: 40001)IP not in whitelist (errcode: 40164)Rate limit exceeded (errcode: 45009)所有命令返回统一的错误格式:
json
{
"error": "No credentials. Run \"auth set <app-id> <app-secret>\" first."
}常见错误:、、、、、。
No credentialsAccount not foundInvalid credentialsWeChat API error (errcode: 40001)IP not in whitelist (errcode: 40164)Rate limit exceeded (errcode: 45009)Configuration
配置
Credentials stored in (0600 permissions).
~/.config/agent-messenger/wechatbot-credentials.jsonConfig format:
json
{
"current": { "account_id": "wx1234567890" },
"accounts": {
"wx1234567890": {
"app_id": "wx1234567890",
"app_secret": "...",
"account_name": "wx1234567890"
}
}
}凭证存储在(权限为0600)。
~/.config/agent-messenger/wechatbot-credentials.json配置格式:
json
{
"current": { "account_id": "wx1234567890" },
"accounts": {
"wx1234567890": {
"app_id": "wx1234567890",
"app_secret": "...",
"account_name": "wx1234567890"
}
}
}Limitations
限制
- Cannot list or read received messages — WeChat Official Account API delivers inbound messages via webhooks only. This CLI is send-only.
- Customer service messages require 48h window — Free-form text, image, and news messages only work within 48 hours of the user's last interaction.
- Template messages require pre-approval — Templates must be created and approved in the WeChat admin panel before use.
- IP whitelist required — Your server's IP must be added to the Official Account's whitelist, or you'll get error .
40164 - Media IDs required for images — Images must be uploaded to WeChat's media platform first. The CLI accepts media IDs, not URLs.
- No group chat support — Official Account API communicates with individual followers only.
- No real-time events / WebSocket connection — Inbound messages require a separate webhook server.
- No message editing or deletion
- No voice or video calls
- Access tokens expire — Tokens are valid for 7200 seconds. The CLI handles automatic refresh.
- 无法查看或读取收到的消息 —— 微信公众号API仅通过webhook接收消息,本CLI仅支持发送。
- 客服消息需在48小时窗口内发送 —— 自由格式的文本、图片和图文消息仅在用户最后互动的48小时内有效。
- 模板消息需预先审核 —— 模板需在微信后台创建并审核通过后才能使用。
- 需配置IP白名单 —— 服务器IP必须添加到公众号白名单,否则会返回错误。
40164 - 图片需使用Media ID —— 图片需先上传到微信媒体平台,CLI仅接受Media ID,不支持URL。
- 不支持群聊 —— 公众号API仅支持与单个粉丝通信。
- 无实时事件/WebSocket连接 —— 接收消息需单独部署webhook服务器。
- 无法编辑或删除消息
- 不支持语音或视频通话
- Access Token会过期 —— Token有效期为7200秒,CLI会自动处理刷新。
Troubleshooting
故障排查
agent-wechatbot: command not found
agent-wechatbot: command not foundagent-wechatbot: command not found
agent-wechatbot: command not foundagent-wechatbotagent-messengerIf the package is installed globally, use directly:
agent-wechatbotbash
agent-wechatbot message send oABCD1234 "Hello"If the package is NOT installed, run it directly with :
npx -ybash
npx -y agent-messenger wechatbot message send oABCD1234 "Hello"Note: If the user prefers a different package runner (e.g.,,bunx,pnpx), use that instead.pnpm dlx
NEVER run , , or . It will fail or install a wrong package since is not the npm package name.
npx agent-wechatbotbunx agent-wechatbotpnpm dlx agent-wechatbotagent-wechatbotagent-wechatbotagent-messenger如果已全局安装包,可直接使用:
agent-wechatbotbash
agent-wechatbot message send oABCD1234 "Hello"如果未安装包,可使用直接运行:
npx -ybash
npx -y agent-messenger wechatbot message send oABCD1234 "Hello"注意:如果用户偏好其他包运行工具(如、bunx、pnpx),可替换使用。pnpm dlx
切勿运行、或,由于不是npm包名称,这会导致运行失败或安装错误的包。
npx agent-wechatbotbunx agent-wechatbotpnpm dlx agent-wechatbotagent-wechatbotHow to get API credentials
如何获取API凭证
- Log in to the WeChat Official Account admin panel
- Navigate to Development > Basic Configuration
- Copy your App ID and App Secret (you may need to reset the secret if you don't have it saved)
- Add your server's IP to the IP Whitelist
- Run
agent-wechatbot auth set <app-id> <app-secret>
- 登录微信公众号后台
- 进入「开发 > 基本配置」
- 复制App ID和App Secret(如果未保存,可能需要重置密钥)
- 将服务器IP添加到IP白名单
- 运行
agent-wechatbot auth set <app-id> <app-secret>
IP whitelist errors (40164)
IP白名单错误(40164)
If you get error , your server's IP is not in the Official Account's whitelist. Add it in the admin panel under Development > Basic Configuration > IP Whitelist.
40164如果返回错误,说明服务器IP未添加到公众号白名单,需在后台「开发 > 基本配置 > IP白名单」中添加。
40164Token errors (40001, 42001)
Token错误(40001、42001)
These indicate an expired or invalid access token. The CLI handles automatic token refresh, but if you see persistent errors:
- Verify your App Secret hasn't been reset in the admin panel
- Re-run with the current credentials
agent-wechatbot auth set <app-id> <app-secret>
这些错误表示Access Token过期或无效。CLI会自动处理Token刷新,但如果持续出现错误:
- 验证App Secret是否在后台被重置
- 使用当前凭证重新运行
agent-wechatbot auth set <app-id> <app-secret>
Rate limiting (45009)
调用频率限制(45009)
WeChat enforces API call frequency limits. If you hit error , wait before retrying. For bulk operations, add delays between requests.
45009微信对API调用频率有限制,如果返回错误,请等待后重试。批量操作时,请在请求之间添加延迟。
45009References
参考文档
- Authentication Guide
- Common Patterns
- 认证指南
- 常见使用场景