@xgjktech/xg_cwork_im 1.0.8 → 1.0.9
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 +367 -360
- package/package.json +1 -1
- package/src/channel.ts +137 -12
- package/src/connection.ts +362 -359
- package/src/inbound-media-local.ts +159 -0
- package/src/types.ts +301 -287
package/README.md
CHANGED
|
@@ -1,360 +1,367 @@
|
|
|
1
|
-
# XG CWork IM Channel for OpenClaw
|
|
2
|
-
|
|
3
|
-
工作说说 IM 机器人 Channel 插件,通过 WebSocket 长连接接收 `@` 消息,由 OpenClaw AI 处理后自动回复。
|
|
4
|
-
|
|
5
|
-
## 工作原理
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
IM 系统 → WebSocket → 插件(本项目) → OpenClaw AI → IM 发送接口 → IM 系统
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
1. 插件启动时用 `appKey` 换取 `access_token` 和 `userId`
|
|
12
|
-
2. 建立 WebSocket 长连接,接收 `robotMention` 事件
|
|
13
|
-
3. 将收到的消息转发给 OpenClaw 处理
|
|
14
|
-
4. OpenClaw AI 回复后,插件:
|
|
15
|
-
- 先通过 WebSocket `START` 让 IM 端显示一条「思考中」占位消息;
|
|
16
|
-
- 当 AI 产出第一条完整回复时,用 HTTP `/im/message/send` 覆盖这条占位消息;
|
|
17
|
-
- 若有多条回复,则后续每条都通过 HTTP 作为**独立消息**发送;
|
|
18
|
-
- 首条回复若在配置的超时时间内一直未产生,会自动将占位消息更新为「当前请求处理超时,请稍后重试」。
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## 前置条件
|
|
23
|
-
|
|
24
|
-
- 已安装 [OpenClaw](https://github.com/openclaw) (`>=2026.2.13`)
|
|
25
|
-
- 已在 IM 后台注册机器人,获取到 `appKey`
|
|
26
|
-
|
|
27
|
-
---
|
|
28
|
-
|
|
29
|
-
## 安装
|
|
30
|
-
|
|
31
|
-
推荐同事通过 **npm** 安装,无需访问内部 Git;需要改源码时再从内部仓库克隆(方法 B)。
|
|
32
|
-
|
|
33
|
-
### 方法 A:通过 npm 包安装(推荐)
|
|
34
|
-
|
|
35
|
-
一条命令完成安装,依赖由 OpenClaw 自动处理:
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
openclaw plugins install @xgjktech/xg_cwork_im
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
运行 `openclaw plugins list` 确认列表中有 `xg_cwork_im` 即可。
|
|
42
|
-
|
|
43
|
-
### 方法 B:通过本地源码安装
|
|
44
|
-
|
|
45
|
-
如需二次开发或无法使用 npm 安装时,可从内部 Git 克隆后以链接模式安装:
|
|
46
|
-
|
|
47
|
-
```bash
|
|
48
|
-
git clone http://192.168.3.200/openclaw/openclaw-channel-xg-cwork-im.git
|
|
49
|
-
cd openclaw-channel-xg-cwork-im
|
|
50
|
-
npm install
|
|
51
|
-
openclaw plugins install -l .
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### 方法 C:手动安装
|
|
55
|
-
|
|
56
|
-
1. 将本仓库下载或复制到 `~/.openclaw/extensions/xg_cwork_im`(Windows 为 `%USERPROFILE%\.openclaw\extensions\xg_cwork_im`)。
|
|
57
|
-
2. 确保目录内包含 `index.ts`、`openclaw.plugin.json`、`package.json`。
|
|
58
|
-
3. 在该目录执行 `npm install --omit=dev` 或 `npm run install:prod` 安装依赖。
|
|
59
|
-
4. 运行 `openclaw plugins list` 确认 `xg_cwork_im` 已显示。
|
|
60
|
-
|
|
61
|
-
### 方法 D:国内网络环境(npm 镜像源)
|
|
62
|
-
|
|
63
|
-
若执行 `openclaw plugins install @xgjktech/xg_cwork_im` 时卡在「Installing plugin dependencies...」或出现 `npm install failed`,可临时指定国内镜像源:
|
|
64
|
-
|
|
65
|
-
```bash
|
|
66
|
-
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com openclaw plugins install @xgjktech/xg_cwork_im
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
若插件已半安装(扩展目录存在但依赖未装全),可进入插件目录手动补装:
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
cd ~/.openclaw/extensions/xg_cwork_im
|
|
73
|
-
# Windows: cd %USERPROFILE%\.openclaw\extensions\xg_cwork_im
|
|
74
|
-
rm -rf node_modules package-lock.json
|
|
75
|
-
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com npm install
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
---
|
|
79
|
-
|
|
80
|
-
## 更新已安装的插件
|
|
81
|
-
|
|
82
|
-
当插件发布新版本后,已安装的用户可按以下方式更新。
|
|
83
|
-
|
|
84
|
-
### 通过 npm 安装的(方法 A)
|
|
85
|
-
|
|
86
|
-
使用**插件 id**(不是 npm 包名)执行更新:
|
|
87
|
-
|
|
88
|
-
```bash
|
|
89
|
-
openclaw plugins update xg_cwork_im
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
更新完成后执行 `openclaw gateway restart` 使新版本生效。
|
|
93
|
-
|
|
94
|
-
国内网络可临时指定镜像源后再更新:
|
|
95
|
-
|
|
96
|
-
```bash
|
|
97
|
-
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com openclaw plugins update xg_cwork_im
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
### 通过本地源码 / 链接安装的(方法 B)
|
|
101
|
-
|
|
102
|
-
在插件目录拉取最新代码后重启网关:
|
|
103
|
-
|
|
104
|
-
```bash
|
|
105
|
-
cd openclaw-channel-xg-cwork-im # 进入当时克隆的目录
|
|
106
|
-
git pull
|
|
107
|
-
npm install
|
|
108
|
-
openclaw gateway restart
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### 手动安装的(方法 C)
|
|
112
|
-
|
|
113
|
-
重新从内部 Git 下载或复制最新代码到 `~/.openclaw/extensions/xg_cwork_im`,在该目录执行 `npm install --omit=dev` 或 `npm run install:prod`,再执行 `openclaw gateway restart`。
|
|
114
|
-
|
|
115
|
-
---
|
|
116
|
-
|
|
117
|
-
## 配置
|
|
118
|
-
|
|
119
|
-
### 步骤 1:启用并信任插件
|
|
120
|
-
|
|
121
|
-
执行以下命令,将 `xg_cwork_im` 插件添加到 OpenClaw 的信任白名单中:
|
|
122
|
-
|
|
123
|
-
```bash
|
|
124
|
-
openclaw plugins enable xg_cwork_im
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### 步骤 2:配置 Channel 账户信息
|
|
128
|
-
|
|
129
|
-
编辑 `~/.openclaw/openclaw.json`,添加以下配置(推荐 `accounts` 使用对象 map,key 即 `accountId`):
|
|
130
|
-
|
|
131
|
-
```json
|
|
132
|
-
{
|
|
133
|
-
"channels": {
|
|
134
|
-
"xg_cwork_im": {
|
|
135
|
-
"baseUrl": "https://cwork-web-test.xgjktech.com.cn",
|
|
136
|
-
"wsBaseUrl": "wss://cwork-web-test.xgjktech.com.cn",
|
|
137
|
-
"accounts": {
|
|
138
|
-
"main": { "appKey": "你的机器人 appKey", "agentId": "main", "name": "个人助手" }
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
> **多账户**:如有多个机器人,在 `accounts` 中继续追加即可(对象 map:key 为 `accountId`)。每个账户可指定不同的 `agentId` 对应不同的 OpenClaw Agent。
|
|
146
|
-
|
|
147
|
-
> **accounts.default(可选)**:OpenClaw 的 `doctor --fix` 可能会自动生成 `accounts.default`,用于存放默认项(例如 `groupPolicy` 等)。本插件会把 `accounts.default` 作为“默认覆盖”合并到每个真实账号上,但 **不会**把它当作一个需要启动连接的账号。
|
|
148
|
-
|
|
149
|
-
### 步骤 3:重启 Gateway
|
|
150
|
-
|
|
151
|
-
配置完成后,重启网关使配置生效:
|
|
152
|
-
|
|
153
|
-
```bash
|
|
154
|
-
openclaw gateway restart
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
---
|
|
158
|
-
|
|
159
|
-
## 完整配置参考
|
|
160
|
-
|
|
161
|
-
`~/.openclaw/openclaw.json` 参考结构(示例使用 CWork 测试/生产域名,详细请参考 `docs/im服务接口说明.md`):
|
|
162
|
-
|
|
163
|
-
```json5
|
|
164
|
-
{
|
|
165
|
-
"plugins": {
|
|
166
|
-
"enabled": true,
|
|
167
|
-
"allow": ["xg_cwork_im"]
|
|
168
|
-
},
|
|
169
|
-
"channels": {
|
|
170
|
-
"xg_cwork_im": {
|
|
171
|
-
"baseUrl": "https://cwork-web-test.xgjktech.com.cn",
|
|
172
|
-
"wsBaseUrl": "wss://cwork-web-test.xgjktech.com.cn",
|
|
173
|
-
"groupPolicy": "mention",
|
|
174
|
-
"debug": false,
|
|
175
|
-
// 可选:首条 AI 回复超时时间(毫秒),默认 30 分钟
|
|
176
|
-
// 未配置时:30 * 60_000 = 1800000
|
|
177
|
-
"firstReplyTimeoutMs": 1800000,
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
"
|
|
200
|
-
"
|
|
201
|
-
"
|
|
202
|
-
"
|
|
203
|
-
|
|
204
|
-
"
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
|
224
|
-
|
|
225
|
-
| `
|
|
226
|
-
| `
|
|
227
|
-
| `
|
|
228
|
-
| `
|
|
229
|
-
| `
|
|
230
|
-
| `
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
-
|
|
253
|
-
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
│
|
|
274
|
-
├──
|
|
275
|
-
├──
|
|
276
|
-
├──
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
- 在
|
|
313
|
-
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
1
|
+
# XG CWork IM Channel for OpenClaw
|
|
2
|
+
|
|
3
|
+
工作说说 IM 机器人 Channel 插件,通过 WebSocket 长连接接收 `@` 消息,由 OpenClaw AI 处理后自动回复。
|
|
4
|
+
|
|
5
|
+
## 工作原理
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
IM 系统 → WebSocket → 插件(本项目) → OpenClaw AI → IM 发送接口 → IM 系统
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
1. 插件启动时用 `appKey` 换取 `access_token` 和 `userId`
|
|
12
|
+
2. 建立 WebSocket 长连接,接收 `robotMention` 事件
|
|
13
|
+
3. 将收到的消息转发给 OpenClaw 处理
|
|
14
|
+
4. OpenClaw AI 回复后,插件:
|
|
15
|
+
- 先通过 WebSocket `START` 让 IM 端显示一条「思考中」占位消息;
|
|
16
|
+
- 当 AI 产出第一条完整回复时,用 HTTP `/im/message/send` 覆盖这条占位消息;
|
|
17
|
+
- 若有多条回复,则后续每条都通过 HTTP 作为**独立消息**发送;
|
|
18
|
+
- 首条回复若在配置的超时时间内一直未产生,会自动将占位消息更新为「当前请求处理超时,请稍后重试」。
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 前置条件
|
|
23
|
+
|
|
24
|
+
- 已安装 [OpenClaw](https://github.com/openclaw) (`>=2026.2.13`)
|
|
25
|
+
- 已在 IM 后台注册机器人,获取到 `appKey`
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 安装
|
|
30
|
+
|
|
31
|
+
推荐同事通过 **npm** 安装,无需访问内部 Git;需要改源码时再从内部仓库克隆(方法 B)。
|
|
32
|
+
|
|
33
|
+
### 方法 A:通过 npm 包安装(推荐)
|
|
34
|
+
|
|
35
|
+
一条命令完成安装,依赖由 OpenClaw 自动处理:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
openclaw plugins install @xgjktech/xg_cwork_im
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
运行 `openclaw plugins list` 确认列表中有 `xg_cwork_im` 即可。
|
|
42
|
+
|
|
43
|
+
### 方法 B:通过本地源码安装
|
|
44
|
+
|
|
45
|
+
如需二次开发或无法使用 npm 安装时,可从内部 Git 克隆后以链接模式安装:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
git clone http://192.168.3.200/openclaw/openclaw-channel-xg-cwork-im.git
|
|
49
|
+
cd openclaw-channel-xg-cwork-im
|
|
50
|
+
npm install
|
|
51
|
+
openclaw plugins install -l .
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 方法 C:手动安装
|
|
55
|
+
|
|
56
|
+
1. 将本仓库下载或复制到 `~/.openclaw/extensions/xg_cwork_im`(Windows 为 `%USERPROFILE%\.openclaw\extensions\xg_cwork_im`)。
|
|
57
|
+
2. 确保目录内包含 `index.ts`、`openclaw.plugin.json`、`package.json`。
|
|
58
|
+
3. 在该目录执行 `npm install --omit=dev` 或 `npm run install:prod` 安装依赖。
|
|
59
|
+
4. 运行 `openclaw plugins list` 确认 `xg_cwork_im` 已显示。
|
|
60
|
+
|
|
61
|
+
### 方法 D:国内网络环境(npm 镜像源)
|
|
62
|
+
|
|
63
|
+
若执行 `openclaw plugins install @xgjktech/xg_cwork_im` 时卡在「Installing plugin dependencies...」或出现 `npm install failed`,可临时指定国内镜像源:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com openclaw plugins install @xgjktech/xg_cwork_im
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
若插件已半安装(扩展目录存在但依赖未装全),可进入插件目录手动补装:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
cd ~/.openclaw/extensions/xg_cwork_im
|
|
73
|
+
# Windows: cd %USERPROFILE%\.openclaw\extensions\xg_cwork_im
|
|
74
|
+
rm -rf node_modules package-lock.json
|
|
75
|
+
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com npm install
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 更新已安装的插件
|
|
81
|
+
|
|
82
|
+
当插件发布新版本后,已安装的用户可按以下方式更新。
|
|
83
|
+
|
|
84
|
+
### 通过 npm 安装的(方法 A)
|
|
85
|
+
|
|
86
|
+
使用**插件 id**(不是 npm 包名)执行更新:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
openclaw plugins update xg_cwork_im
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
更新完成后执行 `openclaw gateway restart` 使新版本生效。
|
|
93
|
+
|
|
94
|
+
国内网络可临时指定镜像源后再更新:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com openclaw plugins update xg_cwork_im
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 通过本地源码 / 链接安装的(方法 B)
|
|
101
|
+
|
|
102
|
+
在插件目录拉取最新代码后重启网关:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
cd openclaw-channel-xg-cwork-im # 进入当时克隆的目录
|
|
106
|
+
git pull
|
|
107
|
+
npm install
|
|
108
|
+
openclaw gateway restart
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 手动安装的(方法 C)
|
|
112
|
+
|
|
113
|
+
重新从内部 Git 下载或复制最新代码到 `~/.openclaw/extensions/xg_cwork_im`,在该目录执行 `npm install --omit=dev` 或 `npm run install:prod`,再执行 `openclaw gateway restart`。
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 配置
|
|
118
|
+
|
|
119
|
+
### 步骤 1:启用并信任插件
|
|
120
|
+
|
|
121
|
+
执行以下命令,将 `xg_cwork_im` 插件添加到 OpenClaw 的信任白名单中:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
openclaw plugins enable xg_cwork_im
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### 步骤 2:配置 Channel 账户信息
|
|
128
|
+
|
|
129
|
+
编辑 `~/.openclaw/openclaw.json`,添加以下配置(推荐 `accounts` 使用对象 map,key 即 `accountId`):
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"channels": {
|
|
134
|
+
"xg_cwork_im": {
|
|
135
|
+
"baseUrl": "https://cwork-web-test.xgjktech.com.cn",
|
|
136
|
+
"wsBaseUrl": "wss://cwork-web-test.xgjktech.com.cn",
|
|
137
|
+
"accounts": {
|
|
138
|
+
"main": { "appKey": "你的机器人 appKey", "agentId": "main", "name": "个人助手" }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
> **多账户**:如有多个机器人,在 `accounts` 中继续追加即可(对象 map:key 为 `accountId`)。每个账户可指定不同的 `agentId` 对应不同的 OpenClaw Agent。
|
|
146
|
+
|
|
147
|
+
> **accounts.default(可选)**:OpenClaw 的 `doctor --fix` 可能会自动生成 `accounts.default`,用于存放默认项(例如 `groupPolicy` 等)。本插件会把 `accounts.default` 作为“默认覆盖”合并到每个真实账号上,但 **不会**把它当作一个需要启动连接的账号。
|
|
148
|
+
|
|
149
|
+
### 步骤 3:重启 Gateway
|
|
150
|
+
|
|
151
|
+
配置完成后,重启网关使配置生效:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
openclaw gateway restart
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## 完整配置参考
|
|
160
|
+
|
|
161
|
+
`~/.openclaw/openclaw.json` 参考结构(示例使用 CWork 测试/生产域名,详细请参考 `docs/im服务接口说明.md`):
|
|
162
|
+
|
|
163
|
+
```json5
|
|
164
|
+
{
|
|
165
|
+
"plugins": {
|
|
166
|
+
"enabled": true,
|
|
167
|
+
"allow": ["xg_cwork_im"]
|
|
168
|
+
},
|
|
169
|
+
"channels": {
|
|
170
|
+
"xg_cwork_im": {
|
|
171
|
+
"baseUrl": "https://cwork-web-test.xgjktech.com.cn",
|
|
172
|
+
"wsBaseUrl": "wss://cwork-web-test.xgjktech.com.cn",
|
|
173
|
+
"groupPolicy": "mention",
|
|
174
|
+
"debug": false,
|
|
175
|
+
// 可选:首条 AI 回复超时时间(毫秒),默认 30 分钟
|
|
176
|
+
// 未配置时:30 * 60_000 = 1800000
|
|
177
|
+
"firstReplyTimeoutMs": 1800000,
|
|
178
|
+
// 可选:入站附件先下载到当前 Agent workspace 下该子目录,再以 file:// 交给 OpenClaw
|
|
179
|
+
"inboundMediaWorkspaceSubdir": "xg_im_inbound",
|
|
180
|
+
"accounts": {
|
|
181
|
+
"main": { "appKey": "appKey_机器人A", "agentId": "main", "name": "个人助手" },
|
|
182
|
+
"sales": { "appKey": "appKey_机器人B", "agentId": "sales", "name": "销售助手" }
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
"bindings": [
|
|
187
|
+
{ "type": "route", "agentId": "main", "match": { "channel": "xg_cwork_im", "accountId": "main" } },
|
|
188
|
+
{ "type": "route", "agentId": "sales", "match": { "channel": "xg_cwork_im", "accountId": "sales" } }
|
|
189
|
+
]
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### 生产环境配置示例
|
|
194
|
+
|
|
195
|
+
```json5
|
|
196
|
+
{
|
|
197
|
+
"channels": {
|
|
198
|
+
"xg_cwork_im": {
|
|
199
|
+
"baseUrl": "https://sg-al-cwork-web.mediportal.com.cn",
|
|
200
|
+
"wsBaseUrl": "wss://sg-al-cwork-web.mediportal.com.cn",
|
|
201
|
+
"groupPolicy": "mention",
|
|
202
|
+
"debug": false,
|
|
203
|
+
"maxConnectionAttempts": 200,
|
|
204
|
+
"maxReconnectDelay": 120000,
|
|
205
|
+
// 生产环境可根据需要调整首回复超时时间(毫秒)
|
|
206
|
+
"firstReplyTimeoutMs": 300000,
|
|
207
|
+
// 按需启用:入站附件落盘到 workspace,见配置表 inboundMediaWorkspaceSubdir
|
|
208
|
+
// "inboundMediaWorkspaceSubdir": "xg_im_inbound",
|
|
209
|
+
"accounts": {
|
|
210
|
+
"main": { "appKey": "你的生产 appKey", "agentId": "main", "name": "个人助手" }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## 配置项说明
|
|
220
|
+
|
|
221
|
+
### 顶层配置(`channels.xg_cwork_im`)
|
|
222
|
+
|
|
223
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
224
|
+
|---|---|---|---|
|
|
225
|
+
| `baseUrl` | string | **必填** | IM 服务域名(API 接口地址) |
|
|
226
|
+
| `wsBaseUrl` | string | — | **可选**。WebSocket 独立域名。若不填,插件将尝试自动映射。 |
|
|
227
|
+
| `groupPolicy` | string | `"mention"` | `open`=收全部消息;`mention`=仅 @ 机器人触发 |
|
|
228
|
+
| `enabled` | boolean | `true` | 是否启用 |
|
|
229
|
+
| `debug` | boolean | `false` | 开启调试日志 |
|
|
230
|
+
| `maxConnectionAttempts` | number | `10` | WebSocket 最大重连次数 |
|
|
231
|
+
| `initialReconnectDelay` | number | `1000` | 初始重连延迟(ms) |
|
|
232
|
+
| `maxReconnectDelay` | number | `60000` | 最大重连延迟(ms) |
|
|
233
|
+
| `reconnectJitter` | number | `0.3` | 重连抖动因子(0-1) |
|
|
234
|
+
| `firstReplyTimeoutMs` | number | `300000` | **首条 AI 回复超时时间(毫秒)**。用于保护「思考中」占位消息:若在该时间窗口内 AI 没有任何回复,则自动将占位消息更新为「当前请求处理超时,请稍后重试」。|
|
|
235
|
+
| `inboundMediaWorkspaceSubdir` | string | — | **可选**。非空时,将入站附件先下载到**当前 Agent workspace** 下该**相对子目录**(如 `xg_im_inbound`),路径约定:`{子目录}/{发送者 userId}/{YYYY-MM-DD}/{文件名}_{HHmmssmmm}.ext`(时间为本机写入时刻),再向 OpenClaw 传入本地 `file://` 路径;未配置则仍使用 IM 返回的 URL。需 OpenClaw runtime 提供 `resolveAgentWorkspaceDir`。 |
|
|
236
|
+
|
|
237
|
+
### 账户配置(`accounts.<accountId>` / `accounts[n]`)
|
|
238
|
+
|
|
239
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
240
|
+
|---|---|---|---|
|
|
241
|
+
| `appKey` | string | **必填** | 机器人 appKey(IM 后台注册获取) |
|
|
242
|
+
| `agentId` | string | `"main"` | 对应的 OpenClaw Agent ID |
|
|
243
|
+
| `name` | string | — | 账户显示名称(仅用于日志标识) |
|
|
244
|
+
| `groupPolicy` | string | 继承顶层 | 可覆盖顶层的 groupPolicy |
|
|
245
|
+
| `inboundMediaWorkspaceSubdir` | string | 继承顶层 | 可覆盖顶层的入站附件落盘子目录 |
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## 故障排除
|
|
250
|
+
|
|
251
|
+
### 收不到消息
|
|
252
|
+
- 确认 `appKey` 正确,且机器人已在 IM 后台激活
|
|
253
|
+
- 查看 OpenClaw 日志,确认 WebSocket 连接成功(`WebSocket connected successfully`)
|
|
254
|
+
- 群聊中确认已 `@` 机器人
|
|
255
|
+
|
|
256
|
+
### WebSocket 频繁断连
|
|
257
|
+
- 检查网络稳定性
|
|
258
|
+
- 适当增大 `maxConnectionAttempts` 和 `maxReconnectDelay`
|
|
259
|
+
- 开启 `debug: true` 查看详细日志
|
|
260
|
+
|
|
261
|
+
### token 相关报错
|
|
262
|
+
- 确认 `baseUrl` 域名可达
|
|
263
|
+
- 确认 `appKey` 有效(可在 IM 后台查看机器人状态)
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## 项目结构
|
|
268
|
+
|
|
269
|
+
```
|
|
270
|
+
openclaw-channel-xg-cwork-im/
|
|
271
|
+
├── index.ts # 插件入口
|
|
272
|
+
├── src/
|
|
273
|
+
│ ├── types.ts # 类型定义
|
|
274
|
+
│ ├── auth.ts # 认证(appKey → token)
|
|
275
|
+
│ ├── connection.ts # WebSocket 连接管理
|
|
276
|
+
│ ├── send-service.ts # IM 消息发送
|
|
277
|
+
│ ├── group-history-tool.ts # 拉取群消息历史的 Agent Tool
|
|
278
|
+
│ ├── send-group-message-tool.ts # 发送群消息的 Agent Tool
|
|
279
|
+
│ ├── inbound-media-local.ts # 可选:入站附件落盘到 workspace 子目录
|
|
280
|
+
│ └── channel.ts # Channel Plugin 主定义
|
|
281
|
+
├── package.json
|
|
282
|
+
├── tsconfig.json
|
|
283
|
+
├── openclaw.plugin.json
|
|
284
|
+
└── README.md
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
## 代码仓库与发布流程说明
|
|
290
|
+
|
|
291
|
+
- **代码仓库**:本项目在**内部 Git** 管理,地址:`http://192.168.3.200/openclaw/openclaw-channel-xg-cwork-im`,不推送到 GitHub。
|
|
292
|
+
- **安装方式**:同事安装请优先使用 **npm**(方法 A);需要改源码时从内部 Git 克隆(方法 B)。
|
|
293
|
+
- **发布流程**:在内部 Git 上开发、提交后,由维护者**发布到 npm**;同事通过 `openclaw plugins install @xgjktech/xg_cwork_im` 安装,无需访问 Git。
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## 维护者:如何发布到 npm(详细步骤)
|
|
298
|
+
|
|
299
|
+
代码在内部 Git 上维护,发布到 npm 后同事即可一条命令安装。按下列步骤操作。
|
|
300
|
+
|
|
301
|
+
### 步骤 1:在 npm 注册 / 登录
|
|
302
|
+
|
|
303
|
+
1. 无 npm 账号:打开 [https://www.npmjs.com/signup](https://www.npmjs.com/signup) 注册。
|
|
304
|
+
2. 在项目根目录执行:
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
npm login
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
3. 按提示输入 **Username**、**Password**、**Email**。
|
|
311
|
+
4. 若账号开启了**双因素认证(2FA)**,发布时必须使用:
|
|
312
|
+
- 在 npm 网站 **Access Tokens** 里创建 **Granular Access Token**,勾选 **Publish packages**,并勾选 **Bypass 2FA**;或
|
|
313
|
+
- 在命令行登录时使用 **OTP**(一次性密码)。
|
|
314
|
+
否则会报错:`Two-factor authentication or granular access token with bypass 2fa enabled is required to publish packages`。
|
|
315
|
+
|
|
316
|
+
### 步骤 2:确认 scope 与权限
|
|
317
|
+
|
|
318
|
+
- 包名为 `@xgjktech/xg_cwork_im`,属于 **scope** `@xgjktech`。
|
|
319
|
+
- 在 [npm 官网](https://www.npmjs.com/) 登录 → **Organizations** → **Create**,创建组织名为 **xgjktech**,确保你的账号有该组织的发布权限。
|
|
320
|
+
- 若改用个人 scope(如 `@你的用户名/xg_cwork_im`),需修改 `package.json` 的 `"name"` 并同步改 README 中的安装命令。
|
|
321
|
+
|
|
322
|
+
### 步骤 3:发布前检查
|
|
323
|
+
|
|
324
|
+
在项目根目录执行:
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
npm pack --dry-run
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
确认列出的文件无敏感内容(如 `.env`、密钥)。发布内容由 `package.json` 的 `"files"` 控制。
|
|
331
|
+
|
|
332
|
+
### 步骤 4:执行发布
|
|
333
|
+
|
|
334
|
+
**scoped 包必须加 `--access public`**,否则会按私有包计费并可能报 402:
|
|
335
|
+
|
|
336
|
+
```bash
|
|
337
|
+
npm publish --access public
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
成功后可在此查看:
|
|
341
|
+
[https://www.npmjs.com/package/@xgjktech/xg_cwork_im](https://www.npmjs.com/package/@xgjktech/xg_cwork_im)
|
|
342
|
+
|
|
343
|
+
### 步骤 5:后续更新(重新推送到 npm)
|
|
344
|
+
|
|
345
|
+
每次发新版本都按下面做一遍即可:
|
|
346
|
+
|
|
347
|
+
1. 在内部 Git 上改代码、提交、推送。
|
|
348
|
+
2. 在 **package.json** 里把 **version** 升高(如 `1.0.0` → `1.0.1`),保存。
|
|
349
|
+
3. 在项目根目录执行:
|
|
350
|
+
|
|
351
|
+
```bash
|
|
352
|
+
npm publish --access public
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
4. 通知同事执行 **更新已安装的插件**(见上文「更新已安装的插件」),例如:
|
|
356
|
+
`openclaw plugins update xg_cwork_im` → `openclaw gateway restart`。
|
|
357
|
+
|
|
358
|
+
(若该 scope 已发过公开包,有时可只执行 `npm publish`,建议仍带 `--access public` 避免误发成私有。)
|
|
359
|
+
|
|
360
|
+
### 常见问题
|
|
361
|
+
|
|
362
|
+
| 报错或现象 | 原因 | 处理 |
|
|
363
|
+
|-----------|------|------|
|
|
364
|
+
| **403 Forbidden**,且提示需 2FA 或 token | 账号开启 2FA 后,普通登录无法发布 | 使用 **Granular Access Token**(勾选 Publish + Bypass 2FA),或在发布时提供 OTP |
|
|
365
|
+
| **403 Forbidden**,无 2FA 提示 | 当前账号无 `@xgjktech` 发布权限 | 在 npm 创建/加入组织 `xgjktech`,或将包名改为个人 scope |
|
|
366
|
+
| **402 Payment Required** | 未加 `--access public`,scoped 包被视为私有 | 执行 `npm publish --access public` |
|
|
367
|
+
| 国内网络登录/发布慢或超时 | 默认 registry 在国外 | 登录:`npm login --registry=https://registry.npmjs.org/`;发布时也可加 `--registry=https://registry.npmjs.org/`,不推荐长期用第三方镜像发布 |
|