qidao-openclaw-plugin 1.0.6 → 1.0.7
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/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/index.js +175 -182
package/openclaw.plugin.json
CHANGED
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -7,198 +7,191 @@ import { QidaoChannel } from './qidao-channel.js';
|
|
|
7
7
|
// 存储所有账户的连接实例
|
|
8
8
|
const accountConnections = new Map();
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
id:
|
|
10
|
+
const qidaoChannel = {
|
|
11
|
+
id: 'qidao',
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
meta: {
|
|
14
|
+
id: 'qidao',
|
|
15
|
+
label: '栖岛聊天',
|
|
16
|
+
selectionLabel: '栖岛聊天 (Qidao Chat)',
|
|
17
|
+
blurb: '连接到栖岛聊天服务,支持实时消息收发',
|
|
16
18
|
},
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
capabilities: {
|
|
21
|
+
chatTypes: ['direct', 'group'],
|
|
22
|
+
supportsMedia: true,
|
|
23
|
+
supportsReactions: false,
|
|
24
|
+
supportsThreads: false,
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
config: {
|
|
28
|
+
// 列出所有配置的账户ID
|
|
29
|
+
listAccountIds: (cfg) => {
|
|
30
|
+
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
31
|
+
return Object.keys(accounts);
|
|
32
|
+
},
|
|
20
33
|
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
34
|
+
// 解析指定账户的配置
|
|
35
|
+
resolveAccount: (cfg, accountId) => {
|
|
36
|
+
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
37
|
+
const account = accounts[accountId ?? 'default'];
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
selectionLabel: '栖岛聊天 (Qidao Chat)',
|
|
29
|
-
blurb: '连接到栖岛聊天服务,支持实时消息收发',
|
|
30
|
-
},
|
|
39
|
+
if (!account) {
|
|
40
|
+
return { accountId };
|
|
41
|
+
}
|
|
31
42
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
return {
|
|
44
|
+
accountId: accountId ?? 'default',
|
|
45
|
+
enabled: account.enabled ?? true,
|
|
46
|
+
chatId: account.chatId,
|
|
47
|
+
userId: account.userId,
|
|
48
|
+
serverUrl: account.serverUrl ?? 'wss://oc.qidao.chat/ws',
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
outbound: {
|
|
54
|
+
deliveryMode: 'direct',
|
|
55
|
+
|
|
56
|
+
// 发送文本消息
|
|
57
|
+
sendText: async ({ text, accountId, chatId }) => {
|
|
58
|
+
const connection = accountConnections.get(accountId ?? 'default');
|
|
38
59
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return { accountId };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
accountId: accountId ?? 'default',
|
|
57
|
-
enabled: account.enabled ?? true,
|
|
58
|
-
chatId: account.chatId,
|
|
59
|
-
userId: account.userId,
|
|
60
|
-
serverUrl: account.serverUrl ?? 'wss://oc.qidao.chat/ws',
|
|
61
|
-
};
|
|
62
|
-
},
|
|
63
|
-
},
|
|
60
|
+
if (!connection || !connection.isConnected()) {
|
|
61
|
+
return {
|
|
62
|
+
ok: false,
|
|
63
|
+
error: '未连接到栖岛服务'
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!chatId) {
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
error: '未指定chatId'
|
|
71
|
+
};
|
|
72
|
+
}
|
|
64
73
|
|
|
65
|
-
outbound: {
|
|
66
|
-
deliveryMode: 'direct',
|
|
67
|
-
|
|
68
|
-
// 发送文本消息
|
|
69
|
-
sendText: async ({ text, accountId, chatId }) => {
|
|
70
|
-
const connection = accountConnections.get(accountId ?? 'default');
|
|
71
|
-
|
|
72
|
-
if (!connection || !connection.isConnected()) {
|
|
73
|
-
return {
|
|
74
|
-
ok: false,
|
|
75
|
-
error: '未连接到栖岛服务'
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!chatId) {
|
|
80
|
-
return {
|
|
81
|
-
ok: false,
|
|
82
|
-
error: '未指定chatId'
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
await connection.sendMessage(parseInt(chatId), text);
|
|
88
|
-
return { ok: true };
|
|
89
|
-
} catch (error) {
|
|
90
|
-
return {
|
|
91
|
-
ok: false,
|
|
92
|
-
error: error.message
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
// 发送媒体消息
|
|
98
|
-
sendMedia: async ({ url, accountId, chatId }) => {
|
|
99
|
-
const connection = accountConnections.get(accountId ?? 'default');
|
|
100
|
-
|
|
101
|
-
if (!connection || !connection.isConnected()) {
|
|
102
|
-
return {
|
|
103
|
-
ok: false,
|
|
104
|
-
error: '未连接到栖岛服务'
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!chatId) {
|
|
109
|
-
return {
|
|
110
|
-
ok: false,
|
|
111
|
-
error: '未指定chatId'
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
await connection.sendImageMessage(parseInt(chatId), url);
|
|
117
|
-
return { ok: true };
|
|
118
|
-
} catch (error) {
|
|
119
|
-
return {
|
|
120
|
-
ok: false,
|
|
121
|
-
error: error.message
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// 异步初始化账户连接
|
|
129
|
-
setImmediate(async () => {
|
|
130
74
|
try {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
// 为每个启用的账户创建连接
|
|
135
|
-
Object.entries(accounts).forEach(([accountId, accountConfig]) => {
|
|
136
|
-
if (accountConfig.enabled === false) {
|
|
137
|
-
api.logger.info(`栖岛账户 ${accountId} 已禁用`);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (!accountConfig.chatId) {
|
|
142
|
-
api.logger.warn(`栖岛账户 ${accountId} 缺少chatId配置`);
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const serverUrl = accountConfig.serverUrl ?? 'wss://oc.qidao.chat/ws';
|
|
147
|
-
|
|
148
|
-
api.logger.info(`连接栖岛账户: ${accountId} (chatId: ${accountConfig.chatId})`);
|
|
149
|
-
|
|
150
|
-
// 创建连接
|
|
151
|
-
const connection = new QidaoChannel({
|
|
152
|
-
serverUrl,
|
|
153
|
-
chatId: accountConfig.chatId,
|
|
154
|
-
userId: accountConfig.userId,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// 监听连接事件
|
|
158
|
-
connection.on('connect', () => {
|
|
159
|
-
api.logger.info(`栖岛账户 ${accountId} 已连接`);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
connection.on('disconnect', () => {
|
|
163
|
-
api.logger.warn(`栖岛账户 ${accountId} 已断开`);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
connection.on('error', (error) => {
|
|
167
|
-
api.logger.error(`栖岛账户 ${accountId} 错误: ${error.message}`);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
// 监听栖岛消息,转发给OpenClaw
|
|
171
|
-
connection.on('message', (message) => {
|
|
172
|
-
api.logger.debug(`栖岛账户 ${accountId} 收到消息:`, message);
|
|
173
|
-
|
|
174
|
-
// 将栖岛消息转换为OpenClaw消息格式
|
|
175
|
-
if (message.type === 'new_message') {
|
|
176
|
-
api.ingestMessage({
|
|
177
|
-
channelId: 'qidao',
|
|
178
|
-
accountId: accountId,
|
|
179
|
-
senderId: message.senderId.toString(),
|
|
180
|
-
senderName: message.senderName,
|
|
181
|
-
text: message.messageText,
|
|
182
|
-
timestamp: message.timestamp,
|
|
183
|
-
chatType: message.chatType === 0 ? 'direct' : 'group',
|
|
184
|
-
chatId: message.chatId.toString(),
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// 连接到服务器
|
|
190
|
-
connection.connect().catch((error) => {
|
|
191
|
-
api.logger.error(`栖岛账户 ${accountId} 连接失败: ${error.message}`);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// 保存连接实例
|
|
195
|
-
accountConnections.set(accountId, connection);
|
|
196
|
-
});
|
|
75
|
+
await connection.sendMessage(parseInt(chatId), text);
|
|
76
|
+
return { ok: true };
|
|
197
77
|
} catch (error) {
|
|
198
|
-
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
error: error.message
|
|
81
|
+
};
|
|
199
82
|
}
|
|
200
|
-
}
|
|
83
|
+
},
|
|
201
84
|
|
|
202
|
-
|
|
203
|
-
|
|
85
|
+
// 发送媒体消息
|
|
86
|
+
sendMedia: async ({ url, accountId, chatId }) => {
|
|
87
|
+
const connection = accountConnections.get(accountId ?? 'default');
|
|
88
|
+
|
|
89
|
+
if (!connection || !connection.isConnected()) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
error: '未连接到栖岛服务'
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!chatId) {
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
error: '未指定chatId'
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await connection.sendImageMessage(parseInt(chatId), url);
|
|
105
|
+
return { ok: true };
|
|
106
|
+
} catch (error) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
error: error.message
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
},
|
|
204
114
|
};
|
|
115
|
+
|
|
116
|
+
export default function register(api) {
|
|
117
|
+
api.logger.info('栖岛聊天插件加载中...');
|
|
118
|
+
|
|
119
|
+
// 注册Channel
|
|
120
|
+
api.registerChannel({ plugin: qidaoChannel });
|
|
121
|
+
|
|
122
|
+
// 异步初始化账户连接
|
|
123
|
+
setImmediate(async () => {
|
|
124
|
+
try {
|
|
125
|
+
const cfg = api.getGatewayConfig?.() || {};
|
|
126
|
+
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
127
|
+
|
|
128
|
+
// 为每个启用的账户创建连接
|
|
129
|
+
Object.entries(accounts).forEach(([accountId, accountConfig]) => {
|
|
130
|
+
if (accountConfig.enabled === false) {
|
|
131
|
+
api.logger.info(`栖岛账户 ${accountId} 已禁用`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!accountConfig.chatId) {
|
|
136
|
+
api.logger.warn(`栖岛账户 ${accountId} 缺少chatId配置`);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const serverUrl = accountConfig.serverUrl ?? 'wss://oc.qidao.chat/ws';
|
|
141
|
+
|
|
142
|
+
api.logger.info(`连接栖岛账户: ${accountId} (chatId: ${accountConfig.chatId})`);
|
|
143
|
+
|
|
144
|
+
// 创建连接
|
|
145
|
+
const connection = new QidaoChannel({
|
|
146
|
+
serverUrl,
|
|
147
|
+
chatId: accountConfig.chatId,
|
|
148
|
+
userId: accountConfig.userId,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// 监听连接事件
|
|
152
|
+
connection.on('connect', () => {
|
|
153
|
+
api.logger.info(`栖岛账户 ${accountId} 已连接`);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
connection.on('disconnect', () => {
|
|
157
|
+
api.logger.warn(`栖岛账户 ${accountId} 已断开`);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
connection.on('error', (error) => {
|
|
161
|
+
api.logger.error(`栖岛账户 ${accountId} 错误: ${error.message}`);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// 监听栖岛消息,转发给OpenClaw
|
|
165
|
+
connection.on('message', (message) => {
|
|
166
|
+
api.logger.debug(`栖岛账户 ${accountId} 收到消息:`, message);
|
|
167
|
+
|
|
168
|
+
// 将栖岛消息转换为OpenClaw消息格式
|
|
169
|
+
if (message.type === 'new_message') {
|
|
170
|
+
api.ingestMessage({
|
|
171
|
+
channelId: 'qidao',
|
|
172
|
+
accountId: accountId,
|
|
173
|
+
senderId: message.senderId.toString(),
|
|
174
|
+
senderName: message.senderName,
|
|
175
|
+
text: message.messageText,
|
|
176
|
+
timestamp: message.timestamp,
|
|
177
|
+
chatType: message.chatType === 0 ? 'direct' : 'group',
|
|
178
|
+
chatId: message.chatId.toString(),
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// 连接到服务器
|
|
184
|
+
connection.connect().catch((error) => {
|
|
185
|
+
api.logger.error(`栖岛账户 ${accountId} 连接失败: ${error.message}`);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// 保存连接实例
|
|
189
|
+
accountConnections.set(accountId, connection);
|
|
190
|
+
});
|
|
191
|
+
} catch (error) {
|
|
192
|
+
api.logger.error(`栖岛聊天插件初始化失败: ${error.message}`);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
api.logger.info('栖岛聊天插件加载完成');
|
|
197
|
+
}
|