@sunnoy/wecom 1.1.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,41 +1,69 @@
1
- # OpenClaw WeCom (Enterprise WeChat) AI Bot Plugin
1
+ # OpenClaw 企业微信 (WeCom) AI 机器人插件
2
2
 
3
- [English](https://github.com/sunnoy/openclaw-plugin-wecom/blob/main/README.md) | [简体中文](https://github.com/sunnoy/openclaw-plugin-wecom/blob/main/README_ZH.md)
3
+ `openclaw-plugin-wecom` 是一个专为 [OpenClaw](https://github.com/openclaw/openclaw) 框架开发的企业微信(WeCom)集成插件。它允许你将强大的 AI 能力无缝接入企业微信,支持 AI 机器人模式和自建应用模式,并具备多层消息投递回退机制。
4
4
 
5
- `openclaw-plugin-wecom` is an Enterprise WeChat (WeCom) integration plugin developed for the [OpenClaw](https://github.com/openclaw/openclaw) framework. It enables seamless AI capabilities in Enterprise WeChat with advanced features.
5
+ ## 核心特性
6
6
 
7
- ## ✨ Key Features
7
+ ### 消息模式支持
8
+ - **AI 机器人模式 (Bot Mode)**: 基于企业微信最新的 AI 机器人流式分片机制,实现流畅的打字机式回复体验。支持 JSON 格式的回调消息。
9
+ - **自建应用模式 (Agent Mode)**: 支持企业微信自建应用,可处理 XML 格式的回调消息,支持收发消息、上传下载媒体文件。
10
+ - **Webhook Bot 模式**: 支持通过 Webhook 发送消息到群聊,适用于群通知场景。
8
11
 
9
- - 🌊 **Streaming Output**: Built on WeCom's latest AI bot streaming mechanism for smooth typewriter-style responses.
10
- - 🤖 **Dynamic Agent Management**: Automatically creates isolated agents per direct message user or group chat, with independent workspaces and conversation contexts.
11
- - 👥 **Deep Group Chat Integration**: Supports group message parsing with @mention triggering.
12
- - 🎤 **Voice Message Support**: Automatically processes voice messages transcribed by WeCom into text for AI interaction (direct messages only).
13
- - 🖼️ **Image Support**: Automatic base64 encoding and sending of local images (screenshots, generated images) without requiring additional configuration.
14
- - 🛠️ **Command Enhancement**: Built-in commands (e.g., `/new` for new sessions, `/status` for status) with allowlist configuration.
15
- - 🔒 **Security & Authentication**: Full support for WeCom message encryption/decryption, URL verification, and sender validation.
16
- - **High-Performance Async Processing**: Asynchronous message architecture ensures responsive gateway even during long AI inference.
12
+ ### 智能消息投递
13
+ - **四层投递回退机制**: 确保消息可靠送达
14
+ 1. **流式通道**: 优先通过活跃流式通道发送
15
+ 2. **Response URL 回退**: 流式通道关闭后,使用企业微信 response_url 发送
16
+ 3. **Webhook Bot 回退**: 支持通过 Webhook 发送到指定群聊
17
+ 4. **Agent API 回退**: 通过自建应用 API 主动推送消息
18
+ - **消息防抖合并**: 同一用户在短时间内(2 秒内)连续发送的多条消息自动合并为一次 AI 请求。
19
+ - **内存自动清理**: 定期清理过期的流元数据和响应 URL,防止内存泄漏。
17
20
 
18
- ## 📋 Prerequisites
21
+ ### 动态 Agent 与隔离
22
+ - **动态 Agent 管理**: 默认按"每个私聊用户 / 每个群聊"自动创建独立 Agent。每个 Agent 拥有独立的工作区与对话上下文,实现更强的数据隔离。
23
+ - **群聊深度集成**: 支持群聊消息解析,可通过 @提及(At-mention)精准触发机器人响应。
24
+ - **管理员用户**: 可配置管理员列表,绕过指令白名单和动态 Agent 路由限制。
25
+ - **指令白名单**: 内置常用指令支持(如 `/new`、`/status`),并提供指令白名单配置功能。
19
26
 
20
- - [OpenClaw](https://github.com/openclaw/openclaw) installed (version 2026.1.30+)
21
- - Enterprise WeChat admin access to create intelligent robot applications
22
- - Server address accessible from Enterprise WeChat (HTTP/HTTPS)
27
+ ### 多媒体支持
28
+ - **丰富消息类型**: 支持文本、图片、语音、图文混排、文件、位置、链接等消息类型。
29
+ - **入站媒体处理**: 自动解密企业微信 AES-256-CBC 加密的图片,下载并保存语音、视频、文件等媒体供 AI 分析。
30
+ - **出站图片发送**: 支持通过 `msg_item` API 发送 base64 编码图片,单张最大 2MB,每条消息最多 10 张。
31
+ - **文件上传下载**: Agent 模式下支持上传临时媒体文件和下载用户发送的媒体文件。
23
32
 
24
- ## 🚀 Installation
33
+ ### 安全与扩展
34
+ - **安全与认证**: 完整支持企业微信消息加解密、URL 验证及发送者身份校验。
35
+ - **高性能异步处理**: 采用异步消息处理架构,确保即使在长耗时 AI 推理过程中,企业微信网关也能保持高响应性。
36
+ - **模块化架构**: 清晰的代码组织结构,易于维护和扩展。
37
+
38
+ ## 前置要求
39
+
40
+ - 已安装 [OpenClaw](https://github.com/openclaw/openclaw) (版本 2026.1.30+)
41
+ - 企业微信管理后台权限,可创建智能机器人应用或自建应用
42
+ - 可从企业微信访问的服务器地址(HTTP/HTTPS)
43
+
44
+ ## 安装
25
45
 
26
46
  ```bash
27
47
  openclaw plugins install @sunnoy/wecom
28
48
  ```
29
49
 
30
- This command will automatically:
31
- - Download the plugin from npm
32
- - Install to `~/.openclaw/extensions/`
33
- - Update your OpenClaw configuration
34
- - Register the plugin
50
+ 此命令会自动:
51
+ - npm 下载插件
52
+ - 安装到 `~/.openclaw/extensions/` 目录
53
+ - 更新 OpenClaw 配置
54
+ - 注册插件
55
+
56
+ ### 运行测试
57
+
58
+ ```bash
59
+ npm test
60
+ ```
61
+
62
+ 运行单元测试(使用 Node.js 内置测试运行器)。
35
63
 
36
- ## ⚙️ Configuration
64
+ ## 配置
37
65
 
38
- Add to your OpenClaw configuration file (`~/.openclaw/openclaw.json`):
66
+ OpenClaw 配置文件(`~/.openclaw/openclaw.json`)中添加:
39
67
 
40
68
  ```json
41
69
  {
@@ -49,51 +77,202 @@ Add to your OpenClaw configuration file (`~/.openclaw/openclaw.json`):
49
77
  "channels": {
50
78
  "wecom": {
51
79
  "enabled": true,
52
- "token": "Your Token",
53
- "encodingAesKey": "Your EncodingAESKey",
80
+ "token": "你的 Bot Token",
81
+ "encodingAesKey": "你的 Bot EncodingAESKey",
82
+ "adminUsers": ["管理员userid"],
54
83
  "commands": {
55
84
  "enabled": true,
56
85
  "allowlist": ["/new", "/status", "/help", "/compact"]
86
+ },
87
+ "agent": {
88
+ "corpId": "企业 CorpID",
89
+ "corpSecret": "应用 Secret",
90
+ "agentId": 1000002,
91
+ "token": "回调 Token (Agent 模式)",
92
+ "encodingAesKey": "回调 EncodingAESKey (Agent 模式)"
93
+ },
94
+ "webhooks": {
95
+ "ops-group": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx",
96
+ "dev-group": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=yyy"
57
97
  }
58
98
  }
59
99
  }
60
100
  }
61
101
  ```
62
102
 
63
- ### Configuration Options
103
+ ### 配置说明
104
+
105
+ #### 基础配置
106
+
107
+ | 配置项 | 类型 | 必填 | 说明 |
108
+ |--------|------|------|------|
109
+ | `plugins.entries.wecom.enabled` | boolean | 是 | 启用插件 |
110
+ | `channels.wecom.token` | string | 是* | 企业微信机器人 Token (*Bot 模式必填) |
111
+ | `channels.wecom.encodingAesKey` | string | 是* | 消息加密密钥(43 位)(*Bot 模式必填) |
112
+ | `channels.wecom.adminUsers` | array | 否 | 管理员用户 ID 列表(绕过指令白名单和动态路由) |
113
+ | `channels.wecom.commands.enabled` | boolean | 否 | 是否启用指令白名单过滤(默认 true) |
114
+ | `channels.wecom.commands.allowlist` | array | 否 | 允许的指令白名单 |
115
+
116
+ #### 动态 Agent 配置
117
+
118
+ 配置按人/按群隔离的 Agent 管理:
119
+
120
+ | 配置项 | 类型 | 必填 | 说明 |
121
+ |--------|------|------|------|
122
+ | `channels.wecom.dynamicAgents.enabled` | boolean | 否 | 是否启用动态 Agent(默认 true) |
123
+ | `channels.wecom.dm.createAgentOnFirstMessage` | boolean | 否 | 私聊时为每个用户创建独立 Agent(默认 true) |
124
+ | `channels.wecom.groupChat.enabled` | boolean | 否 | 是否启用群聊处理(默认 true) |
125
+ | `channels.wecom.groupChat.requireMention` | boolean | 否 | 群聊是否必须 @ 提及才响应(默认 true) |
126
+
127
+ #### 工作区模板配置 (可选)
128
+
129
+ 配置工作区模板目录,为动态创建的 Agent 工作区预置初始化文件:
130
+
131
+ | 配置项 | 类型 | 必填 | 说明 |
132
+ |--------|------|------|------|
133
+ | `channels.wecom.workspaceTemplate` | string | 否 | 模板目录路径,支持 AGENTS.md、BOOTSTRAP.md 等 bootstrap 文件 |
134
+
135
+ 当动态 Agent 首次创建时,会自动从模板目录复制 bootstrap 文件到对应的工作区。详细说明请参考[动态 Agent 路由](#动态-agent-路由)章节。
136
+
137
+ #### Agent 模式配置 (可选)
138
+
139
+ 配置自建应用以实现更强大的消息收发能力:
140
+
141
+ | 配置项 | 类型 | 必填 | 说明 |
142
+ |--------|------|------|------|
143
+ | `channels.wecom.agent.corpId` | string | 是 | 企业 CorpID |
144
+ | `channels.wecom.agent.corpSecret` | string | 是 | 应用 Secret |
145
+ | `channels.wecom.agent.agentId` | number | 是 | 应用 Agent ID |
146
+ | `channels.wecom.agent.token` | string | 是 | 回调 Token (用于验证签名) |
147
+ | `channels.wecom.agent.encodingAesKey` | string | 是 | 回调 EncodingAESKey (43 位) |
148
+
149
+ #### Webhook 配置 (可选)
150
+
151
+ 配置 Webhook Bot 用于群通知:
152
+
153
+ | 配置项 | 类型 | 必填 | 说明 |
154
+ |--------|------|------|------|
155
+ | `channels.wecom.webhooks` | object | 否 | Webhook URL 映射 (key: 名称, value: URL) |
156
+
157
+ ## 企业微信后台配置
158
+
159
+ ### 方式一:创建 AI 机器人 (Bot 模式)
160
+
161
+ AI 机器人模式适用于简单的问答场景,支持流式输出。
64
162
 
65
- | Option | Type | Required | Description |
66
- |--------|------|----------|-------------|
67
- | `plugins.entries.wecom.enabled` | boolean | Yes | Enable the plugin |
68
- | `channels.wecom.token` | string | Yes | WeCom bot Token |
69
- | `channels.wecom.encodingAesKey` | string | Yes | WeCom message encryption key (43 chars) |
70
- | `channels.wecom.commands.allowlist` | array | No | Command allowlist |
163
+ > 📖 **官方文档**:[企业微信 AI 机器人开发指南](https://developer.work.weixin.qq.com/document/path/101039)
71
164
 
72
- ## 🔌 Enterprise WeChat Configuration
165
+ **创建步骤:**
73
166
 
74
- 1. Log in to [Enterprise WeChat Admin Console](https://work.weixin.qq.com/)
75
- 2. Navigate to "Application Management" → "Applications" → "Create Application" → Select "Intelligent Robot"
76
- 3. Configure "Receive Messages":
167
+ 1. 登录[企业微信管理后台](https://work.weixin.qq.com/)
168
+ 2. 进入「应用管理」→「应用」→ 下拉找到「智能机器人」→ 点击「创建应用」
169
+ 3. **关键步骤**:在创建页面底部,选择 **「API 模式创建」**,而非「标准模式创建」
170
+ > ⚠️ **必须选择 API 模式**。标准模式下回调消息为 XML 格式,API 模式为 JSON 格式,本插件的 Bot 模式仅支持 JSON。
171
+ 4. 填写机器人名称、头像等基本信息,点击「创建」
172
+ 5. 创建完成后,进入机器人详情页:
173
+ - 复制 `Token`(用于验证消息签名)
174
+ - 复制 `EncodingAESKey`(43位字符,用于消息加解密)
175
+ 6. 点击「接收消息」区域的「设置」:
77
176
  - **URL**: `https://your-domain.com/webhooks/wecom`
78
- - **Token**: Match `channels.wecom.token`
79
- - **EncodingAESKey**: Match `channels.wecom.encodingAesKey`
80
- 4. Save and enable message receiving
177
+ - **Token**: 填入上一步复制的 Token
178
+ - **EncodingAESKey**: 填入上一步复制的 EncodingAESKey
179
+ 7. 保存配置并启用消息接收
180
+
181
+ ### 方式二:创建自建应用 (Agent 模式)
182
+
183
+ 自建应用模式提供更完整的消息收发能力,支持 XML 回调、主动推送、媒体文件处理。
184
+
185
+ > 📖 **官方文档**:[企业微信自建应用开发指南](https://developer.work.weixin.qq.com/document/path/90226)、[接收消息服务器配置](https://developer.work.weixin.qq.com/document/path/90238)
186
+
187
+ **创建步骤:**
188
+
189
+ 1. 登录[企业微信管理后台](https://work.weixin.qq.com/)
190
+ 2. 进入「应用管理」→「应用」→ 点击「创建应用」
191
+ 3. 填写应用信息:
192
+ - 应用名称:如 "AI 助手"
193
+ - 应用头像:上传应用图标
194
+ - 可见成员:选择可使用该应用的成员
195
+ 4. 点击「创建应用」,记录以下信息:
196
+ - `AgentId`:应用 ID(数字)
197
+ - `Secret`:应用凭证(点击「查看」获取)
198
+ 5. 在「接收消息」区域点击「设置 API 接收」:
199
+ - **URL**: `https://your-domain.com/webhooks/app`
200
+ - **Token**: 点击「随机生成」获取
201
+ - **EncodingAESKey**: 点击「随机生成」获取(43位字符)
202
+ - 点击「保存」时,企业微信会发送验证请求到上述 URL 进行域名校验
203
+ > ⚠️ **注意**:保存前请确保服务已部署并可访问,否则校验会失败。如果遇到「回调 URL 校验失败」,请检查:
204
+ > - 服务器是否可以从公网访问
205
+ > - URL 路径是否正确(`/webhooks/app`)
206
+ > - Token 和 EncodingAESKey 是否已正确配置到插件
207
+ > - 防火墙是否放行了企业微信服务器 IP 段
208
+ 6. 获取企业 CorpID:
209
+ - 进入「我的企业」页面
210
+ - 复制页面底部的「企业ID」
211
+ 7. 配置应用可见范围(确保需要使用 AI 助手的成员在可见范围内)
212
+
213
+ ### 方式三:配置群机器人 (Webhook 模式)
214
+
215
+ Webhook Bot 用于向群聊发送通知消息。
216
+
217
+ > 📖 **官方文档**:[企业微信群机器人开发指南](https://developer.work.weixin.qq.com/document/path/99110)
218
+
219
+ **创建步骤:**
220
+
221
+ 1. 在手机或电脑端打开目标群聊
222
+ 2. 点击群聊右上角「···」→「群机器人」→「添加机器人」
223
+ 3. 选择「新建机器人」,填写机器人名称
224
+ 4. 复制 Webhook 地址(格式:`https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx`)
225
+ 5. 将 Webhook 地址配置到 `openclaw.json` 的 `webhooks` 中
226
+
227
+ **注意事项:**
228
+ - Webhook Bot 仅支持发送消息,不支持接收消息
229
+ - 每个群聊可添加多个机器人
230
+ - Webhook 地址请妥善保管,避免泄露
231
+
232
+ ## 支持的消息类型
233
+
234
+ | 类型 | 方向 | 说明 |
235
+ |------|------|------|
236
+ | 文本 (text) | 收/发 | 纯文本消息 |
237
+ | 图片 (image) | 收/发 | 入站图片自动解密;出站通过 `msg_item` base64 发送 |
238
+ | 语音 (voice) | 收 | 企业微信自动转文字后处理(仅限私聊) |
239
+ | 图文混排 (mixed) | 收 | 文本 + 图片混合消息 |
240
+ | 文件 (file) | 收 | 文件附件(下载后传给 AI 分析) |
241
+ | 位置 (location) | 收 | 位置分享(转换为文本描述) |
242
+ | 链接 (link) | 收 | 分享链接(提取标题、描述、URL 为文本) |
243
+
244
+ ## 管理员用户
245
+
246
+ 管理员用户可以绕过指令白名单限制,并跳过动态 Agent 路由(直接路由到主 Agent)。
81
247
 
82
- ## 🤖 Dynamic Agent Routing
248
+ ```json
249
+ {
250
+ "channels": {
251
+ "wecom": {
252
+ "adminUsers": ["user1", "user2"]
253
+ }
254
+ }
255
+ }
256
+ ```
257
+
258
+ 管理员用户 ID 不区分大小写,匹配企业微信的 `userid` 字段。
83
259
 
84
- The plugin implements per-user/per-group agent isolation:
260
+ ## 动态 Agent 路由
85
261
 
86
- ### How It Works
262
+ 本插件实现"按人/按群隔离"的 Agent 管理:
87
263
 
88
- 1. When a WeCom message arrives, the plugin generates a deterministic `agentId`:
89
- - **Direct Messages**: `wecom-dm-<userId>`
90
- - **Group Chats**: `wecom-group-<chatId>`
91
- 2. OpenClaw automatically creates/reuses the corresponding agent workspace
92
- 3. Each user/group has independent conversation history and context
264
+ ### 工作原理
93
265
 
94
- ### Advanced Configuration
266
+ 1. 企业微信消息到达后,插件生成确定性的 `agentId`:
267
+ - **私聊**: `wecom-dm-<userId>`
268
+ - **群聊**: `wecom-group-<chatId>`
269
+ 2. OpenClaw 自动创建/复用对应的 Agent 工作区
270
+ 3. 每个用户/群聊拥有独立的对话历史和上下文
271
+ 4. **管理员用户**跳过动态路由,直接使用主 Agent
95
272
 
96
- Configure under `channels.wecom`:
273
+ ### 高级配置
274
+
275
+ 配置在 `channels.wecom` 下:
97
276
 
98
277
  ```json
99
278
  {
@@ -114,16 +293,16 @@ Configure under `channels.wecom`:
114
293
  }
115
294
  ```
116
295
 
117
- | Option | Type | Default | Description |
118
- |--------|------|---------|-------------|
119
- | `dynamicAgents.enabled` | boolean | `true` | Enable dynamic agents |
120
- | `dm.createAgentOnFirstMessage` | boolean | `true` | Use dynamic agents for DMs |
121
- | `groupChat.enabled` | boolean | `true` | Enable group chat processing |
122
- | `groupChat.requireMention` | boolean | `true` | Require @mention in groups |
296
+ | 配置项 | 类型 | 默认值 | 说明 |
297
+ |--------|------|--------|------|
298
+ | `dynamicAgents.enabled` | boolean | `true` | 是否启用动态 Agent |
299
+ | `dm.createAgentOnFirstMessage` | boolean | `true` | 私聊使用动态 Agent |
300
+ | `groupChat.enabled` | boolean | `true` | 启用群聊处理 |
301
+ | `groupChat.requireMention` | boolean | `true` | 群聊必须 @ 提及才响应 |
123
302
 
124
- ### Disable Dynamic Agents
303
+ ### 禁用动态 Agent
125
304
 
126
- To route all messages to the default agent:
305
+ 如果需要所有消息进入默认 Agent:
127
306
 
128
307
  ```json
129
308
  {
@@ -135,155 +314,297 @@ To route all messages to the default agent:
135
314
  }
136
315
  ```
137
316
 
138
- ## 🛠️ Command Allowlist
317
+ ### 工作区模板
139
318
 
140
- Prevent regular users from executing sensitive Gateway management commands through WeCom messages.
319
+ 可以为动态创建的 Agent 工作区预置初始化文件。当新 Agent 首次创建时,会自动从模板目录复制 bootstrap 文件。
141
320
 
142
321
  ```json
143
322
  {
144
323
  "channels": {
145
324
  "wecom": {
146
- "commands": {
147
- "enabled": true,
148
- "allowlist": ["/new", "/status", "/help", "/compact"]
149
- }
325
+ "workspaceTemplate": "/path/to/template-dir"
150
326
  }
151
327
  }
152
328
  }
153
329
  ```
154
330
 
155
- ### Recommended Allowlist Commands
331
+ **支持的模板文件:**
332
+ - `AGENTS.md` - Agent 列表配置
333
+ - `BOOTSTRAP.md` - 初始化引导文档
334
+ - `CLAUDE.md` - Claude Code 指令集
335
+ - 其他自定义文件
156
336
 
157
- | Command | Description | Safety Level |
158
- |---------|-------------|--------------|
159
- | `/new` | Reset conversation, start new session | ✅ User-level |
160
- | `/compact` | Compress current session context | ✅ User-level |
161
- | `/help` | Show help information | ✅ User-level |
162
- | `/status` | Show Agent status | ✅ User-level |
337
+ 模板目录中的文件会复制到动态 Agent 的工作区(`~/.openclaw/workspace-<agentId>/`),仅当目标文件不存在时才会复制。
163
338
 
164
- > ⚠️ **Security Note**: Do not add `/gateway`, `/plugins`, or other management commands to the allowlist to prevent regular users from gaining Gateway instance admin privileges.
339
+ ## 支持的目标格式
165
340
 
166
- ## FAQ
341
+ 插件支持多种目标格式,用于消息路由和 Webhook 发送:
167
342
 
168
- ### Q: What plugin ID should I use in the configuration file?
343
+ | 格式 | 示例 | 说明 |
344
+ |------|------|------|
345
+ | `webhook:<name>` | `webhook:ops-group` | 发送到配置的 Webhook 群 |
346
+ | `wecom:<userId>` | `wecom:zhangsan` | 企业微信用户 ID |
347
+ | `party:<id>` | `party:2` | 部门 ID(数字) |
348
+ | `tag:<name>` | `tag:Developers` | 标签名称 |
349
+ | `group:<chatId>` | `group:wr123456` | 群聊 ID |
350
+ | `chatId` | `wr123456` | 以 `wr` 或 `wc` 开头的群聊 ID |
169
351
 
170
- **A:** Use the **complete plugin ID** in `plugins.entries`:
352
+ ### 使用示例
171
353
 
172
- ```json
173
- {
174
- "plugins": {
175
- "entries": {
176
- "wecom": { "enabled": true } // ✅ Correct
177
- }
178
- }
179
- }
354
+ 通过 OpenClaw 向企业微信发送消息时,可以使用上述格式指定目标:
355
+
356
+ ```bash
357
+ # 发送给指定用户
358
+ openclaw send "wecom:zhangsan" "Hello!"
359
+
360
+ # 发送到 Webhook 群
361
+ openclaw send "webhook:dev-group" "部署成功!"
362
+
363
+ # 发送给部门
364
+ openclaw send "party:2" "全体员工通知"
180
365
  ```
181
366
 
182
- **Do not** use the channel id:
367
+ ## 指令白名单
368
+
369
+ 为防止普通用户通过企业微信消息执行敏感的 Gateway 管理指令,本插件支持**指令白名单**机制。
370
+
183
371
  ```json
184
372
  {
185
- "plugins": {
186
- "entries": {
187
- "wecom": { "enabled": true } // ❌ Incorrect
373
+ "channels": {
374
+ "wecom": {
375
+ "commands": {
376
+ "enabled": true,
377
+ "allowlist": ["/new", "/status", "/help", "/compact"]
378
+ }
188
379
  }
189
380
  }
190
381
  }
191
382
  ```
192
383
 
193
- ### Q: Why does `openclaw doctor` report warnings?
384
+ ### 推荐白名单指令
194
385
 
195
- **A:** If you see configuration warnings, run:
386
+ | 指令 | 说明 | 安全级别 |
387
+ |------|------|----------|
388
+ | `/new` | 重置当前对话,开启全新会话 | 用户级 |
389
+ | `/compact` | 压缩当前会话上下文 | 用户级 |
390
+ | `/help` | 查看帮助信息 | 用户级 |
391
+ | `/status` | 查看当前 Agent 状态 | 用户级 |
196
392
 
197
- ```bash
198
- openclaw doctor --fix
199
- ```
393
+ > **安全提示**:不要将 `/gateway`、`/plugins` 等管理指令添加到白名单,避免普通用户获得 Gateway 实例的管理权限。配置在 `adminUsers` 中的管理员不受此限制。
200
394
 
201
- This will automatically fix common configuration issues.
395
+ ## 消息防抖合并
202
396
 
203
- ### Q: How does image sending work?
397
+ 当用户在短时间内(2 秒内)连续发送多条消息时,插件会自动将它们合并为一次 AI 请求。这样可以避免同一用户触发多个并发的 LLM 调用,提供更连贯的回复。
204
398
 
205
- **A:** The plugin automatically handles images generated by OpenClaw (such as browser screenshots):
399
+ - 第一条消息的流式通道接收 AI 回复
400
+ - 后续被合并的消息会显示已合并的提示
401
+ - 指令消息(以 `/` 开头)不参与防抖,会立即处理
206
402
 
207
- - **Local images** (from `~/.openclaw/media/`) are automatically encoded to base64 and sent via WeCom's `msg_item` API
208
- - **Image constraints**: Max 2MB per image, supports JPG and PNG formats, up to 10 images per message
209
- - **No configuration needed**: Works out of the box with tools like browser screenshot
210
- - Images appear when the AI completes its response (streaming doesn't support incremental image sending)
403
+ ## 常见问题 (FAQ)
211
404
 
212
- **Example:**
213
- ```
214
- User: "Take a screenshot of GitHub homepage"
215
- AI: [Takes screenshot] → Image displays properly in WeCom ✅
216
- ```
405
+ ### Q: 回调报错 `Unexpected token '<', "..." is not valid JSON` 怎么办?
406
+
407
+ **A:** 这是企业微信机器人**创建模式**选错导致的。企业微信提供两种机器人创建方式:
408
+
409
+ - **标准模式**:回调消息为 **XML 格式**,本插件不支持
410
+ - **API 模式**:回调消息为 **JSON 格式**,本插件所需
411
+
412
+ **解决方法**:删除当前机器人,重新创建时在页面底部选择 **"API 模式创建"**。
413
+
414
+ ### Q: 入站图片是怎么处理的?
415
+
416
+ **A:** 企业微信使用 AES-256-CBC 加密用户发送的图片。插件会自动:
417
+ 1. 从企业微信的 URL 下载加密图片
418
+ 2. 使用配置的 `encodingAesKey` 解密
419
+ 3. 保存到本地并传给 AI 进行视觉分析
420
+
421
+ 图文混排消息也完全支持——文本和图片会一起提取并发送给 AI。
422
+
423
+ ### Q: 出站图片发送是如何工作的?
424
+
425
+ **A:** 插件会自动处理 OpenClaw 生成的图片(如浏览器截图):
426
+
427
+ - **本地图片**(来自 `~/.openclaw/media/`)会自动进行 base64 编码,通过企业微信 `msg_item` API 发送
428
+ - **图片限制**:单张图片最大 2MB,支持 JPG 和 PNG 格式,每条消息最多 10 张图片
429
+ - **无需配置**:开箱即用,配合浏览器截图等工具自动生效
430
+ - 图片会在 AI 完成回复后显示(流式输出不支持增量发送图片)
431
+
432
+ 如果图片处理失败(超出大小限制、格式不支持等),文本回复仍会正常发送,错误信息会记录在日志中。
433
+
434
+ ### Q: 机器人支持语音消息吗?
435
+
436
+ **A:** 支持!私聊中的语音消息会被企业微信自动转录为文字并作为文本处理,无需额外配置。
437
+
438
+ ### Q: 机器人支持文件消息吗?
439
+
440
+ **A:** 支持。用户发送的文件会被下载并作为附件传给 AI。AI 可以分析文件内容(如读取 PDF 或解析代码文件)。MIME 类型根据文件扩展名自动检测。
441
+
442
+ ### Q: 如何配置自建应用 (Agent) 模式?
443
+
444
+ **A:** Agent 模式提供更强大的消息收发能力,包括主动推送消息和接收 XML 格式回调。
445
+
446
+ **配置步骤:**
447
+
448
+ 1. 在企业微信管理后台创建"自建应用"
449
+ 2. 获取应用凭证:
450
+ - `corpId`: 企业 ID(在"我的企业"页面)
451
+ - `agentId`: 应用 ID
452
+ - `corpSecret`: 应用 Secret
453
+ 3. 设置接收消息:
454
+ - 获取 `token` 和 `encodingAesKey`(随机生成)
455
+ - 回调 URL: `https://your-domain.com/webhooks/app`
456
+
457
+ 4. 在 `openclaw.json` 中添加 Agent 配置:
458
+ ```json
459
+ {
460
+ "channels": {
461
+ "wecom": {
462
+ "agent": {
463
+ "corpId": "wwxxxxxxxxxxxxxxxx",
464
+ "corpSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
465
+ "agentId": 1000002,
466
+ "token": "your_callback_token",
467
+ "encodingAesKey": "your_43_char_encoding_aes_key"
468
+ }
469
+ }
470
+ }
471
+ }
472
+ ```
473
+
474
+ **Agent 模式与 Bot 模式的区别:**
475
+
476
+ | 特性 | Bot 模式 | Agent 模式 |
477
+ |------|----------|------------|
478
+ | 创建方式 | 智能机器人 | 自建应用 |
479
+ | 回调格式 | JSON | XML |
480
+ | 主动推送 | 不支持 | 支持 |
481
+ | 媒体下载 | 不支持 | 支持 |
482
+ | 文件消息 | 不支持 | 支持 |
483
+
484
+ ### Q: 如何使用 Webhook Bot 发送群通知?
485
+
486
+ **A:** Webhook Bot 适用于向群聊发送通知消息。
487
+
488
+ **配置步骤:**
489
+
490
+ 1. 在企业微信群聊中添加"群机器人"
491
+ 2. 复制 Webhook URL(包含 key 参数)
492
+ 3. 在配置中添加 webhook 映射:
493
+ ```json
494
+ {
495
+ "channels": {
496
+ "wecom": {
497
+ "webhooks": {
498
+ "ops-group": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx",
499
+ "dev-group": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=yyy"
500
+ }
501
+ }
502
+ }
503
+ }
504
+ ```
505
+
506
+ 4. 使用 `webhook:` 前缀作为目标:
507
+ - 文本:`webhook:ops-group`
508
+ - 支持 Markdown、图片、文件等多种消息类型
217
509
 
218
- If an image fails to process (size limit, invalid format), the text response will still be delivered and an error will be logged.
510
+ ### Q: 四层消息投递回退是如何工作的?
219
511
 
220
- ### Q: Does the bot support voice messages?
512
+ **A:** 插件采用四层回退机制确保消息可靠送达:
221
513
 
222
- **A:** Yes! Voice messages in direct chats are automatically transcribed by WeCom and processed as text. No additional configuration needed.
514
+ | 层级 | 条件 | 说明 |
515
+ |------|------|------|
516
+ | **Layer 1** | 活跃流式通道 | 正常对话时,消息通过流式通道实时推送 |
517
+ | **Layer 2** | response_url | 流式通道关闭后 1 小时内,可通过 response_url 发送 |
518
+ | **Layer 3a** | Webhook Bot | 目标以 `webhook:` 开头时,使用 Webhook 发送 |
519
+ | **Layer 3b** | Agent API | 配置了 Agent 时,通过自建应用 API 主动推送 |
223
520
 
224
- ### Q: How to configure auth token for public-facing OpenClaw with WeCom callbacks?
521
+ 当上一层级不可用时,自动回退到下一层级。这种设计确保了即使在流式通道关闭的情况下,AI 生成的异步消息(如定时任务、子 Agent 输出)仍能送达。
225
522
 
226
- **A:** WeCom bot **does not need** OpenClaw's Gateway Auth Token.
523
+ ### Q: OpenClaw 开放公网需要 auth token,企业微信回调如何配置?
227
524
 
228
- - **Gateway Auth Token** (`gateway.auth.token`) is used for:
229
- - WebUI access authentication
230
- - WebSocket connection authentication
231
- - CLI remote connection authentication
525
+ - **Gateway Auth Token** (`gateway.auth.token`) 主要用于:
526
+ - WebUI 访问认证
527
+ - WebSocket 连接认证
528
+ - CLI 远程连接认证
232
529
 
233
- - **WeCom Webhook** (`/webhooks/wecom`) authentication:
234
- - Uses WeCom's own signature verification (Token + EncodingAESKey)
235
- - Does not require Gateway Auth Token
236
- - OpenClaw plugin system automatically handles webhook routing
530
+ - **企业微信 Webhook** (`/webhooks/wecom`) 的认证机制:
531
+ - 使用企业微信自己的签名验证(Token + EncodingAESKey
532
+ - 不需要 Gateway Auth Token
533
+ - OpenClaw 插件系统会自动处理 webhook 路由
237
534
 
238
- **Deployment suggestions:**
239
- 1. If using a reverse proxy (e.g., Nginx), configure authentication exemption for `/webhooks/wecom` path
240
- 2. Or expose the webhook endpoint on a separate port without Gateway Auth
535
+ **部署建议:**
536
+ 1. 如果使用反向代理(如 Nginx),可以为 `/webhooks/wecom` 路径配置豁免认证
537
+ 2. 或者将 webhook 端点暴露在独立端口,不经过 Gateway Auth
241
538
 
242
- ### Q: How to fix EncodingAESKey length validation failure?
539
+ ### Q: EncodingAESKey 长度验证失败怎么办?
243
540
 
244
- **A:** Common causes and solutions:
541
+ **A:** 常见原因和解决方法:
245
542
 
246
- 1. **Check configuration key name**: Ensure correct key name `encodingAesKey` (case-sensitive)
543
+ 1. **检查配置键名**:确保使用正确的键名 `encodingAesKey`(注意大小写)
247
544
  ```json
248
545
  {
249
546
  "channels": {
250
547
  "wecom": {
251
- "encodingAesKey": "..." // ✅ Correct
548
+ "encodingAesKey": "..."
252
549
  }
253
550
  }
254
551
  }
255
552
  ```
256
553
 
257
- 2. **Check key length**: EncodingAESKey must be exactly 43 characters
554
+ 2. **检查密钥长度**:EncodingAESKey 必须是 43 位字符
258
555
  ```bash
259
- # Check length
260
- echo -n "your-key" | wc -c
556
+ # 检查长度
557
+ echo -n "你的密钥" | wc -c
261
558
  ```
262
559
 
263
- 3. **Check for extra spaces/newlines**: Ensure no leading/trailing whitespace in the key string
560
+ 3. **检查是否有多余空格/换行**:确保密钥字符串前后没有空格或换行符
264
561
 
265
- ## 📂 Project Structure
562
+ ## 项目结构
266
563
 
267
564
  ```
268
565
  openclaw-plugin-wecom/
269
- ├── index.js # Plugin entry point
270
- ├── webhook.js # WeCom HTTP communication handler
271
- ├── dynamic-agent.js # Dynamic agent routing logic
272
- ├── stream-manager.js # Streaming response manager
273
- ├── crypto.js # WeCom encryption algorithms
274
- ├── client.js # Client logic
275
- ├── logger.js # Logging module
276
- ├── utils.js # Utility functions
277
- ├── package.json # npm package config
278
- └── openclaw.plugin.json # OpenClaw plugin manifest
566
+ ├── index.js # 插件入口
567
+ ├── package.json # npm 包配置
568
+ ├── openclaw.plugin.json # OpenClaw 插件清单
569
+ ├── crypto.js # 企业微信加密算法(消息 + 媒体)
570
+ ├── logger.js # 日志模块
571
+ ├── utils.js # 工具函数(TTL 缓存、消息去重)
572
+ ├── stream-manager.js # 流式回复管理
573
+ ├── image-processor.js # 图片编码/校验(msg_item)
574
+ ├── webhook.js # 企业微信 Bot 模式 HTTP 通信处理
575
+ ├── dynamic-agent.js # 动态 Agent 分配逻辑
576
+ ├── wecom/ # 核心模块目录
577
+ │ ├── channel-plugin.js # 主频道插件逻辑
578
+ │ ├── http-handler.js # HTTP 请求处理器
579
+ │ ├── agent-api.js # Agent API 客户端(AccessToken 缓存、消息发送)
580
+ │ ├── agent-inbound.js # Agent 模式入站处理器(XML 回调)
581
+ │ ├── webhook-bot.js # Webhook Bot 客户端
582
+ │ ├── inbound-processor.js # 入站消息处理器
583
+ │ ├── xml-parser.js # XML 解析器(Agent 模式)
584
+ │ ├── target.js # 目标解析器(支持多种目标格式)
585
+ │ ├── commands.js # 命令处理
586
+ │ ├── constants.js # 常量定义
587
+ │ ├── state.js # 状态管理
588
+ │ ├── stream-utils.js # 流式处理工具
589
+ │ ├── response-url.js # response_url 处理
590
+ │ ├── allow-from.js # 权限控制
591
+ │ ├── media.js # 媒体文件处理
592
+ │ ├── webhook-targets.js # Webhook 目标管理
593
+ │ └── workspace-template.js # 工作区模板
594
+ ├── tests/ # 测试目录
595
+ │ ├── target.test.js # 目标解析器测试
596
+ │ └── xml-parser.test.js # XML 解析器测试
597
+ ├── README.md # 本文档
598
+ ├── CONTRIBUTING.md # 贡献指南
599
+ └── LICENSE # 开源协议
279
600
  ```
280
601
 
281
- ## 🤝 Contributing
602
+ ## 贡献规范
282
603
 
283
- We welcome contributions! Please submit Issues or Pull Requests for bugs or feature suggestions.
604
+ 我们非常欢迎开发者参与贡献!如果你发现了 Bug 或有更好的功能建议,请提交 Issue Pull Request。
284
605
 
285
- See [CONTRIBUTING.md](./CONTRIBUTING.md) for details.
606
+ 详见 [CONTRIBUTING.md](./CONTRIBUTING.md)
286
607
 
287
- ## 📄 License
608
+ ## 开源协议
288
609
 
289
- This project is licensed under the [ISC License](./LICENSE).
610
+ 本项目采用 [ISC License](./LICENSE) 协议。