qidao-openclaw-plugin 1.0.3 → 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/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/index.js +71 -65
package/openclaw.plugin.json
CHANGED
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -15,7 +15,7 @@ export default {
|
|
|
15
15
|
properties: {}
|
|
16
16
|
},
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
register(api) {
|
|
19
19
|
api.logger.info('栖岛聊天插件加载中...');
|
|
20
20
|
|
|
21
21
|
// 注册Channel
|
|
@@ -125,72 +125,78 @@ export default {
|
|
|
125
125
|
},
|
|
126
126
|
});
|
|
127
127
|
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
Object.entries(accounts).forEach(([accountId, accountConfig]) => {
|
|
134
|
-
if (accountConfig.enabled === false) {
|
|
135
|
-
api.logger.info(`栖岛账户 ${accountId} 已禁用`);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (!accountConfig.chatId) {
|
|
140
|
-
api.logger.warn(`栖岛账户 ${accountId} 缺少chatId配置`);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const serverUrl = accountConfig.serverUrl ?? 'wss://oc.qidao.chat/ws';
|
|
145
|
-
|
|
146
|
-
api.logger.info(`连接栖岛账户: ${accountId} (chatId: ${accountConfig.chatId})`);
|
|
147
|
-
|
|
148
|
-
// 创建连接
|
|
149
|
-
const connection = new QidaoChannel({
|
|
150
|
-
serverUrl,
|
|
151
|
-
chatId: accountConfig.chatId,
|
|
152
|
-
userId: accountConfig.userId,
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// 监听连接事件
|
|
156
|
-
connection.on('connect', () => {
|
|
157
|
-
api.logger.info(`栖岛账户 ${accountId} 已连接`);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
connection.on('disconnect', () => {
|
|
161
|
-
api.logger.warn(`栖岛账户 ${accountId} 已断开`);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
connection.on('error', (error) => {
|
|
165
|
-
api.logger.error(`栖岛账户 ${accountId} 错误: ${error.message}`);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
// 监听栖岛消息,转发给OpenClaw
|
|
169
|
-
connection.on('message', (message) => {
|
|
170
|
-
api.logger.debug(`栖岛账户 ${accountId} 收到消息:`, message);
|
|
128
|
+
// 异步初始化账户连接
|
|
129
|
+
setImmediate(async () => {
|
|
130
|
+
try {
|
|
131
|
+
const openclawConfig = api.getGatewayConfig?.() || {};
|
|
132
|
+
const accounts = openclawConfig.channels?.qidao?.accounts ?? {};
|
|
171
133
|
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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,
|
|
183
155
|
});
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
+
});
|
|
197
|
+
} catch (error) {
|
|
198
|
+
api.logger.error(`栖岛聊天插件初始化失败: ${error.message}`);
|
|
199
|
+
}
|
|
194
200
|
});
|
|
195
201
|
|
|
196
202
|
api.logger.info('栖岛聊天插件加载完成');
|