qidao-openclaw-plugin 1.1.0 → 1.2.1
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/package.json +1 -1
- package/src/index.js +70 -104
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { QidaoChannel } from './qidao-channel.js';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
const accountConnections = new Map();
|
|
7
|
+
let connection = null;
|
|
9
8
|
|
|
10
9
|
const qidaoChannel = {
|
|
11
10
|
id: 'qidao',
|
|
@@ -14,38 +13,25 @@ const qidaoChannel = {
|
|
|
14
13
|
id: 'qidao',
|
|
15
14
|
label: '栖岛聊天',
|
|
16
15
|
selectionLabel: '栖岛聊天 (Qidao Chat)',
|
|
17
|
-
blurb: '
|
|
16
|
+
blurb: '连接到栖岛聊天服务',
|
|
18
17
|
},
|
|
19
18
|
|
|
20
19
|
capabilities: {
|
|
21
20
|
chatTypes: ['direct', 'group'],
|
|
22
|
-
supportsMedia: true,
|
|
23
|
-
supportsReactions: false,
|
|
24
|
-
supportsThreads: false,
|
|
25
21
|
},
|
|
26
22
|
|
|
27
23
|
config: {
|
|
28
|
-
|
|
29
|
-
listAccountIds: (cfg) => {
|
|
30
|
-
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
31
|
-
return Object.keys(accounts);
|
|
32
|
-
},
|
|
24
|
+
listAccountIds: () => ['default'],
|
|
33
25
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
37
|
-
const account = accounts[accountId ?? 'default'];
|
|
38
|
-
|
|
39
|
-
if (!account) {
|
|
40
|
-
return { accountId };
|
|
41
|
-
}
|
|
26
|
+
resolveAccount: (cfg) => {
|
|
27
|
+
const qidaoConfig = cfg.channels?.qidao ?? {};
|
|
42
28
|
|
|
43
29
|
return {
|
|
44
|
-
accountId:
|
|
45
|
-
enabled:
|
|
46
|
-
chatId:
|
|
47
|
-
userId:
|
|
48
|
-
serverUrl:
|
|
30
|
+
accountId: 'default',
|
|
31
|
+
enabled: qidaoConfig.enabled ?? false,
|
|
32
|
+
chatId: qidaoConfig.chatId,
|
|
33
|
+
userId: qidaoConfig.userId,
|
|
34
|
+
serverUrl: qidaoConfig.serverUrl ?? 'wss://oc.qidao.chat/ws',
|
|
49
35
|
};
|
|
50
36
|
},
|
|
51
37
|
},
|
|
@@ -53,106 +39,86 @@ const qidaoChannel = {
|
|
|
53
39
|
outbound: {
|
|
54
40
|
deliveryMode: 'direct',
|
|
55
41
|
|
|
56
|
-
|
|
57
|
-
sendText: async ({ text, accountId, chatId }) => {
|
|
58
|
-
const connection = accountConnections.get(accountId ?? 'default');
|
|
59
|
-
|
|
42
|
+
sendText: async ({ text, chatId }) => {
|
|
60
43
|
if (!connection || !connection.isConnected()) {
|
|
61
|
-
return {
|
|
62
|
-
ok: false,
|
|
63
|
-
error: '未连接到栖岛服务'
|
|
64
|
-
};
|
|
44
|
+
return { ok: false, error: '未连接到栖岛服务' };
|
|
65
45
|
}
|
|
66
46
|
|
|
67
47
|
if (!chatId) {
|
|
68
|
-
return {
|
|
69
|
-
ok: false,
|
|
70
|
-
error: '未指定chatId'
|
|
71
|
-
};
|
|
48
|
+
return { ok: false, error: '未指定chatId' };
|
|
72
49
|
}
|
|
73
50
|
|
|
74
51
|
try {
|
|
75
52
|
await connection.sendMessage(parseInt(chatId), text);
|
|
76
|
-
return { ok: true };
|
|
77
|
-
} catch (error) {
|
|
78
|
-
return {
|
|
79
|
-
ok: false,
|
|
80
|
-
error: error.message
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
|
|
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
|
-
},
|
|
114
|
-
|
|
115
|
-
// 添加 gateway 配置,用于启动和停止连接
|
|
116
|
-
gateway: {
|
|
117
|
-
start: async (cfg, accountId) => {
|
|
118
|
-
const account = qidaoChannel.config.resolveAccount(cfg, accountId);
|
|
119
|
-
|
|
120
|
-
if (!account.enabled || !account.chatId) {
|
|
121
|
-
return { ok: false, error: '账户未配置或已禁用' };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
const connection = new QidaoChannel({
|
|
126
|
-
serverUrl: account.serverUrl,
|
|
127
|
-
chatId: account.chatId,
|
|
128
|
-
userId: account.userId,
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
await connection.connect();
|
|
132
|
-
accountConnections.set(accountId, connection);
|
|
133
|
-
|
|
134
53
|
return { ok: true };
|
|
135
54
|
} catch (error) {
|
|
136
55
|
return { ok: false, error: error.message };
|
|
137
56
|
}
|
|
138
57
|
},
|
|
139
|
-
|
|
140
|
-
stop: async (cfg, accountId) => {
|
|
141
|
-
const connection = accountConnections.get(accountId);
|
|
142
|
-
if (connection) {
|
|
143
|
-
connection.disconnect();
|
|
144
|
-
accountConnections.delete(accountId);
|
|
145
|
-
}
|
|
146
|
-
return { ok: true };
|
|
147
|
-
},
|
|
148
58
|
},
|
|
149
59
|
};
|
|
150
60
|
|
|
151
61
|
export default function register(api) {
|
|
152
62
|
api.logger.info('栖岛聊天插件加载中...');
|
|
153
63
|
|
|
154
|
-
// 注册Channel
|
|
155
64
|
api.registerChannel({ plugin: qidaoChannel });
|
|
156
65
|
|
|
66
|
+
setImmediate(async () => {
|
|
67
|
+
try {
|
|
68
|
+
const cfg = api.getGatewayConfig?.() || {};
|
|
69
|
+
const account = qidaoChannel.config.resolveAccount(cfg);
|
|
70
|
+
|
|
71
|
+
if (!account.enabled) {
|
|
72
|
+
api.logger.info('栖岛聊天未启用');
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!account.chatId) {
|
|
77
|
+
api.logger.warn('栖岛聊天缺少chatId配置');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
api.logger.info(`连接栖岛聊天 (chatId: ${account.chatId})`);
|
|
82
|
+
|
|
83
|
+
connection = new QidaoChannel({
|
|
84
|
+
serverUrl: account.serverUrl,
|
|
85
|
+
chatId: account.chatId,
|
|
86
|
+
userId: account.userId,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
connection.on('connect', () => {
|
|
90
|
+
api.logger.info('栖岛聊天已连接');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
connection.on('disconnect', () => {
|
|
94
|
+
api.logger.warn('栖岛聊天已断开');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
connection.on('error', (error) => {
|
|
98
|
+
api.logger.error(`栖岛聊天错误: ${error.message}`);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
connection.on('message', (message) => {
|
|
102
|
+
if (message.type === 'new_message') {
|
|
103
|
+
api.ingestMessage({
|
|
104
|
+
channelId: 'qidao',
|
|
105
|
+
accountId: 'default',
|
|
106
|
+
senderId: message.senderId.toString(),
|
|
107
|
+
senderName: message.senderName,
|
|
108
|
+
text: message.messageText,
|
|
109
|
+
timestamp: message.timestamp,
|
|
110
|
+
chatType: message.chatType === 0 ? 'direct' : 'group',
|
|
111
|
+
chatId: message.chatId.toString(),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
await connection.connect();
|
|
117
|
+
|
|
118
|
+
} catch (error) {
|
|
119
|
+
api.logger.error(`栖岛聊天插件初始化失败: ${error.message}`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
157
123
|
api.logger.info('栖岛聊天插件加载完成');
|
|
158
124
|
}
|