openclawwechat 1.0.4

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 ADDED
@@ -0,0 +1,373 @@
1
+ # OpenClawWeChat
2
+
3
+ OpenClawWeChat 可通过 ClawChat 的微信小程序实现 OpneClaw 与个人微信之间进行通讯会话。
4
+ ## ✨ 功能特性
5
+
6
+ - ✅ **文本消息**:支持发送和接收文本消息
7
+ - ✅ **媒体消息**:支持图片等媒体消息的发送
8
+ - ✅ **消息回复**:支持消息回复功能
9
+ - ✅ **Telegram Bot API 兼容**:使用 Telegram Bot API 兼容格式
10
+ - ✅ **错误处理**:完整的错误处理和日志记录
11
+ - ✅ **状态管理**:支持账户状态查询和管理
12
+
13
+ ## 📋 前置要求
14
+
15
+ - OpenClaw Gateway 已安装并运行
16
+ - 有效的 API Key(格式:`bot_id:secret`)
17
+ - 💡 **获取方式:** 打开微信小程序 **ClawChat**,在我的页面 APIKey管理 复制你的 API Key
18
+
19
+ ## 🚀 快速开始
20
+
21
+ ### 方法一:从 GitHub 安装(手动安装)
22
+
23
+ #### 步骤 1:进入插件目录
24
+
25
+ **macOS / Linux:**
26
+ ```bash
27
+ cd ~/.openclaw/extensions
28
+ ```
29
+
30
+ **Windows:**
31
+ ```powershell
32
+ # PowerShell
33
+ cd $env:USERPROFILE\.openclaw\extensions
34
+ ```
35
+
36
+ ```cmd
37
+ # CMD
38
+ cd %USERPROFILE%\.openclaw\extensions
39
+ ```
40
+
41
+ #### 步骤 2:克隆仓库
42
+
43
+ ```bash
44
+ git clone https://github.com/hillghost86/OpenClawWeChat.git
45
+ cd OpenClawWeChat
46
+ ```
47
+
48
+ #### 步骤 3:修改配置
49
+
50
+ 编辑 OpenClaw 配置文件,添加插件配置(见下方"配置插件"部分)。
51
+
52
+ ### 方法二:从 NPM 安装
53
+
54
+ ```bash
55
+ # 安装最新版本
56
+ openclaw plugins install openclawwechat
57
+
58
+ # 安装特定版本
59
+ openclaw plugins install openclawwechat@1.0.0
60
+ ```
61
+
62
+ ### 配置插件
63
+
64
+ #### 使用配置脚本(推荐)
65
+
66
+ **macOS / Linux:**
67
+ ```bash
68
+ cd ~/.openclaw/extensions/openclawwechat
69
+ npm run config-init
70
+ ```
71
+
72
+ **Windows:**
73
+ ```powershell
74
+ # PowerShell
75
+ cd $env:USERPROFILE\.openclaw\extensions\openclawwechat
76
+ npm run config-init
77
+ ```
78
+
79
+ ```cmd
80
+ # CMD
81
+ cd %USERPROFILE%\.openclaw\extensions\openclawwechat
82
+ npm run config-init
83
+ ```
84
+
85
+ #### 手动编辑配置文件
86
+
87
+ **macOS / Linux:** `~/.openclaw/openclaw.json`
88
+
89
+ **Windows:** `%USERPROFILE%\.openclaw\openclaw.json` 或 `C:\Users\<用户名>\.openclaw\openclaw.json`
90
+
91
+ 添加以下配置:
92
+
93
+ ```json
94
+ {
95
+ "plugins": {
96
+ "entries": {
97
+ "openclawwechat": {
98
+ "enabled": true,
99
+ "config": {
100
+ "apiKey": "your_bot_id:your_secret"
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
108
+ ### 重启 Gateway
109
+
110
+ ```bash
111
+ openclaw gateway restart
112
+ ```
113
+
114
+ ### 验证安装
115
+
116
+ ```bash
117
+ # 查看插件状态
118
+ openclaw plugins list
119
+
120
+ # 查看日志
121
+ openclaw logs --follow | grep "openclawwechat"
122
+ ```
123
+
124
+ ## ⚙️ 配置说明
125
+
126
+ ### 配置项
127
+
128
+ | 配置项 | 类型 | 必需 | 默认值 | 说明 |
129
+ |--------|------|------|--------|------|
130
+ | `apiKey` | string | ✅ | - | API Key(格式:`bot_id:secret`) |
131
+ | `pollIntervalMs` | number | ❌ | `2000` | 轮询间隔(毫秒) |
132
+ | `sessionKeyPrefix` | string | ❌ | `agent:main:wechat:miniprogram:` | Session Key 前缀 |
133
+ | `debug` | boolean | ❌ | `false` | 是否启用调试日志 |
134
+
135
+ ### 配置示例
136
+
137
+ #### 最小配置
138
+
139
+ > 💡 **获取 API Key:** 打开微信小程序 ClawChat,在设置或账户页面可以找到你的 API Key。
140
+
141
+ ```json
142
+ {
143
+ "apiKey": "20231227:EXAMPLE_SECRET_KEY_35_CHARS_LONG_12345"
144
+ }
145
+ ```
146
+
147
+ #### 完整配置
148
+
149
+ ```json
150
+ {
151
+ "apiKey": "20231227:EXAMPLE_SECRET_KEY_35_CHARS_LONG_12345",
152
+ "pollIntervalMs": 2000,
153
+ "sessionKeyPrefix": "agent:main:wechat:miniprogram:",
154
+ "debug": false
155
+ }
156
+ ```
157
+
158
+ ## 📁 项目结构
159
+
160
+ ```
161
+ OpenClawWeChat/
162
+ ├── README.md # 本文件
163
+ ├── CONFIG.md # 详细配置说明
164
+ ├── EXAMPLE.md # 使用示例
165
+ ├── install.sh # Bash 安装脚本
166
+ ├── install.py # Python 安装脚本(跨平台)
167
+ ├── openclaw.plugin.json # 插件清单文件(必需)
168
+ ├── package.json # NPM 包配置
169
+ ├── tsconfig.json # TypeScript 配置
170
+ ├── index.ts # 插件入口文件(必需)
171
+ └── src/
172
+ ├── channel.ts # Channel Plugin 核心实现
173
+ ├── runtime.ts # Runtime 管理
174
+ ├── polling.ts # 轮询服务实现
175
+ ├── message-parser.ts # 消息解析器
176
+ ├── message-injector.ts # 消息注入器
177
+ ├── reply-sender.ts # 回复发送器
178
+ ├── media-handler.ts # 媒体消息处理
179
+ ├── config.ts # 配置管理
180
+ └── constants.ts # 常量定义
181
+ ```
182
+
183
+ ## 🔧 开发指南
184
+
185
+ ### 构建项目
186
+
187
+ ```bash
188
+ # 安装依赖
189
+ npm install
190
+
191
+ # 编译 TypeScript
192
+ npm run build
193
+
194
+ # 开发模式(监听文件变化)
195
+ npm run dev
196
+ ```
197
+
198
+ ### 测试 npm 包(发布前)
199
+
200
+ 在发布到 npm 之前,建议先进行本地测试:
201
+
202
+ ```bash
203
+ # 1. 预览打包内容
204
+ ./test-pack.sh
205
+
206
+ # 2. 本地完整测试(打包、安装、验证)
207
+ ./test-local.sh
208
+
209
+ # 3. 手动测试安装
210
+ npm pack
211
+ openclaw plugins install ./openclaw-openclawwechat-1.0.0.tgz
212
+ ```
213
+
214
+ 详细测试说明请查看 [TEST.md](./TEST.md)
215
+
216
+ ### 核心实现
217
+
218
+ #### 1. 插件入口 (index.ts)
219
+
220
+ ```typescript
221
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
222
+ import { emptyPluginConfigSchema } from "openclaw/plugin-sdk";
223
+ import { wechatMiniprogramPlugin } from "./src/channel.js";
224
+ import { setWechatMiniprogramRuntime } from "./src/runtime.js";
225
+ import { PLUGIN_ID, PLUGIN_VERSION } from "./src/constants.js";
226
+
227
+ const plugin = {
228
+ id: PLUGIN_ID,
229
+ name: "OpenClawWeChat",
230
+ description: "OpenClawWeChat - WeChat MiniProgram channel plugin for OpenClaw",
231
+ version: PLUGIN_VERSION,
232
+ configSchema: emptyPluginConfigSchema(),
233
+
234
+ register(api: OpenClawPluginApi) {
235
+ setWechatMiniprogramRuntime(api.runtime);
236
+ api.registerChannel({ plugin: wechatMiniprogramPlugin });
237
+ },
238
+ };
239
+
240
+ export default plugin;
241
+ ```
242
+
243
+ #### 2. Channel Plugin (src/channel.ts)
244
+
245
+ 实现 `ChannelPlugin` 接口,包括:
246
+ - `config` - 配置管理和验证
247
+ - `inbound` - 接收消息处理
248
+ - `outbound` - 发送消息(支持文本和媒体)
249
+ - `status` - 账户状态查询
250
+ - `gateway` - Gateway 生命周期管理
251
+
252
+ #### 3. Runtime 管理 (src/runtime.ts)
253
+
254
+ ```typescript
255
+ import type { PluginRuntime } from "openclaw/plugin-sdk";
256
+
257
+ let runtime: PluginRuntime | null = null;
258
+
259
+ export function setWechatMiniprogramRuntime(next: PluginRuntime) {
260
+ runtime = next;
261
+ }
262
+
263
+ export function getWechatMiniprogramRuntime(): PluginRuntime {
264
+ if (!runtime) {
265
+ throw new Error("WeChat MiniProgram runtime not initialized");
266
+ }
267
+ return runtime;
268
+ }
269
+ ```
270
+
271
+ ## 📚 使用示例
272
+
273
+ ### 发送文本消息
274
+
275
+ ```typescript
276
+ // 通过 OpenClaw API 发送消息
277
+ const target = "openclawwechat:openid123";
278
+ const message = {
279
+ content: "Hello, WeChat MiniProgram!",
280
+ target: target
281
+ };
282
+
283
+ // 使用 OpenClaw 的 sendMessage API
284
+ await openclaw.sendMessage(message);
285
+ ```
286
+
287
+ ### 发送媒体消息
288
+
289
+ ```typescript
290
+ const mediaMessage = {
291
+ content: "图片消息",
292
+ media_type: "image",
293
+ media_url: "https://example.com/image.jpg",
294
+ target: "openclawwechat:openid123"
295
+ };
296
+
297
+ await openclaw.sendMessage(mediaMessage);
298
+ ```
299
+
300
+ ### 回复消息
301
+
302
+ ```typescript
303
+ const replyMessage = {
304
+ content: "这是回复",
305
+ reply_to_message_id: 12345,
306
+ target: "openclawwechat:openid123"
307
+ };
308
+
309
+ await openclaw.sendMessage(replyMessage);
310
+ ```
311
+
312
+ ## 🔍 故障排查
313
+
314
+ ### 常见问题
315
+
316
+ 1. **插件未加载**
317
+ - 检查插件是否已启用:`openclaw plugins list`
318
+ - 检查配置文件格式是否正确
319
+ - 查看日志:`openclaw logs | grep "openclawwechat"`
320
+
321
+ 2. **消息发送失败**
322
+ - 检查 API Key 是否正确
323
+ - 确认中转服务器是否正常运行
324
+ - 检查网络连接
325
+
326
+ 3. **轮询未工作**
327
+ - 检查 `pollIntervalMs` 配置
328
+ - 查看轮询服务日志
329
+ - 确认中转服务器 API 端点可访问
330
+
331
+ ### 调试模式
332
+
333
+ 启用调试日志:
334
+
335
+ ```json
336
+ {
337
+ "config": {
338
+ "apiKey": "your_api_key",
339
+ "debug": true
340
+ }
341
+ }
342
+ ```
343
+
344
+ ## 📖 相关文档
345
+
346
+ - [详细配置说明](./CONFIG.md)
347
+ - [使用示例](./EXAMPLE.md)
348
+ - [OpenClaw 插件开发指南](https://docs.openclaw.ai/plugins)
349
+
350
+
351
+ ## 📝 版本历史
352
+
353
+ ### v1.0.0
354
+
355
+ - 初始版本发布
356
+ - 支持文本和媒体消息
357
+ - 支持消息回复功能
358
+ - HTTP 轮询服务
359
+ - 完整的错误处理
360
+ - NPM 发布支持
361
+
362
+ ## 📄 许可证
363
+
364
+ MIT License
365
+
366
+ ## 🤝 贡献
367
+
368
+ 欢迎提交 Issue 和 Pull Request!
369
+
370
+ ## 📮 联系方式
371
+
372
+ - GitHub: [hillghost86/OpenClawWeChat](https://github.com/hillghost86/OpenClawWeChat)
373
+ - Issues: [GitHub Issues](https://github.com/hillghost86/OpenClawWeChat/issues)
package/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * OpenClaw Channel Plugin 入口文件
3
+ *
4
+ * 这是插件的入口点,负责:
5
+ * 1. 定义插件元数据
6
+ * 2. 注册 Channel Plugin
7
+ * 3. 初始化 Runtime
8
+ */
9
+
10
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
11
+ import { emptyPluginConfigSchema } from "openclaw/plugin-sdk";
12
+ import { wechatMiniprogramPlugin } from "./src/channel.js";
13
+ import { setWechatMiniprogramRuntime } from "./src/runtime.js";
14
+ import { PLUGIN_ID, PLUGIN_VERSION } from "./src/constants.js";
15
+
16
+ const plugin = {
17
+ id: PLUGIN_ID,
18
+ name: "OpenClawWeChat",
19
+ description: "OpenClawWeChat - WeChat MiniProgram channel plugin for OpenClaw",
20
+ version: PLUGIN_VERSION,
21
+
22
+ // 配置 Schema
23
+ // 如果不需要配置,使用 emptyPluginConfigSchema()
24
+ // 如果需要配置,定义 JSON Schema
25
+ configSchema: emptyPluginConfigSchema(),
26
+
27
+ /**
28
+ * 插件注册函数
29
+ * OpenClaw 加载插件时会调用此函数
30
+ *
31
+ * @param api - OpenClaw Plugin API
32
+ */
33
+ register(api: OpenClawPluginApi) {
34
+ // 1. 设置 Runtime(用于访问 OpenClaw 运行时)
35
+ setWechatMiniprogramRuntime(api.runtime);
36
+
37
+ // 2. 注册 Channel Plugin
38
+ // Channel Plugin 的消息处理通过 inbound 和 outbound 接口完成
39
+ // 不需要手动监听 chat 事件
40
+ api.registerChannel({ plugin: wechatMiniprogramPlugin });
41
+ },
42
+ };
43
+
44
+ export default plugin;
@@ -0,0 +1,60 @@
1
+ {
2
+ "id": "openclawwechat",
3
+ "name": "OpenClawWeChat",
4
+ "description": "OpenClawWeChat - WeChat MiniProgram channel plugin for OpenClaw",
5
+ "version": "1.0.4",
6
+ "type": "module",
7
+ "main": "index.ts",
8
+ "author": "OpenClaw Plugin Development",
9
+ "license": "MIT",
10
+ "configSchema": {
11
+ "type": "object",
12
+ "additionalProperties": false,
13
+ "properties": {
14
+ "apiKey": {
15
+ "type": "string",
16
+ "default": "YOUR_API_KEY_HERE",
17
+ "description": "API Key(格式:bot_id:secret)"
18
+ },
19
+ "pollIntervalMs": {
20
+ "type": "number",
21
+ "default": 2000,
22
+ "description": "轮询间隔(毫秒)"
23
+ },
24
+ "sessionKeyPrefix": {
25
+ "type": "string",
26
+ "default": "agent:main:wechat:miniprogram:",
27
+ "description": "Session Key 前缀"
28
+ },
29
+ "debug": {
30
+ "type": "boolean",
31
+ "default": false,
32
+ "description": "是否启用调试日志"
33
+ }
34
+ },
35
+ "required": [
36
+ "apiKey"
37
+ ]
38
+ },
39
+ "openclaw": {
40
+ "channel": {
41
+ "id": "openclawwechat",
42
+ "label": "OpenClawWeChat",
43
+ "selectionLabel": "OpenClawWeChat",
44
+ "blurb": "Bridge for OpenClaw to WeChat MiniProgram via HTTP polling",
45
+ "docsPath": "/channels/openclawwechat",
46
+ "docsLabel": "openclawwechat",
47
+ "systemImage": "bubble.left.and.text.bubble.right",
48
+ "aliases": [
49
+ "wechat",
50
+ "miniprogram",
51
+ "wx"
52
+ ]
53
+ },
54
+ "install": {
55
+ "npmSpec": "openclawwechat",
56
+ "localPath": "extensions/OpenClawWeChat",
57
+ "defaultChoice": "local"
58
+ }
59
+ }
60
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "openclawwechat",
3
+ "version": "1.0.4",
4
+ "description": "OpenClawWeChat - OpenClaw WeChat MiniProgram channel plugin",
5
+ "keywords": [
6
+ "openclaw",
7
+ "plugin",
8
+ "wechat",
9
+ "miniprogram",
10
+ "channel",
11
+ "telegram-bot-api"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "OpenClaw Plugin Development",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/hillghost86/OpenClawWeChat.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/hillghost86/OpenClawWeChat/issues"
21
+ },
22
+ "homepage": "https://github.com/hillghost86/OpenClawWeChat#readme",
23
+ "type": "module",
24
+ "main": "index.ts",
25
+ "files": [
26
+ "index.ts",
27
+ "src/**/*.ts",
28
+ "scripts/**/*.js",
29
+ "openclaw.plugin.json",
30
+ "README.md",
31
+ "CONFIG.md",
32
+ "EXAMPLE.md",
33
+ "INSTALL.md"
34
+ ],
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "dev": "tsc --watch",
38
+ "pack": "npm pack --dry-run",
39
+ "publish:public": "npm publish --access public",
40
+ "publish:beta": "npm publish --access public --tag beta",
41
+ "version:patch": "npm version patch",
42
+ "version:minor": "npm version minor",
43
+ "version:major": "npm version major",
44
+ "config-init": "node scripts/config-init.js"
45
+ },
46
+ "devDependencies": {
47
+ "openclaw": "workspace:*",
48
+ "@types/node": "^20.0.0",
49
+ "typescript": "^5.0.0"
50
+ },
51
+ "openclaw": {
52
+ "extensions": [
53
+ "./index.ts"
54
+ ]
55
+ }
56
+ }