xiaozuoassistant 0.2.17 → 0.2.18
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/dist/server/channels/feishu.js +68 -55
- package/package.json +1 -1
|
@@ -8,65 +8,78 @@ export class FeishuChannel extends BaseChannel {
|
|
|
8
8
|
this.bots = [];
|
|
9
9
|
}
|
|
10
10
|
async start() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// 为每个机器人初始化客户端
|
|
21
|
-
for (const botConfig of feishuConfig) {
|
|
22
|
-
const { name, appId, appSecret } = botConfig;
|
|
23
|
-
if (!name || !appId || !appSecret) {
|
|
24
|
-
console.warn(`Feishu bot ${name || 'unknown'} missing required config, skipping.`);
|
|
25
|
-
continue;
|
|
11
|
+
try {
|
|
12
|
+
// 每次 start 时重新读取最新配置,避免使用旧缓存
|
|
13
|
+
const feishuConfig = config.channels?.feishu;
|
|
14
|
+
// 清空现有机器人
|
|
15
|
+
this.bots = [];
|
|
16
|
+
// 检查配置是否为数组
|
|
17
|
+
if (!Array.isArray(feishuConfig) || feishuConfig.length === 0) {
|
|
18
|
+
console.log('Feishu bots not configured, skipping Feishu channel.');
|
|
19
|
+
return;
|
|
26
20
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
21
|
+
console.log(`Found ${feishuConfig.length} Feishu bot(s) in config`);
|
|
22
|
+
// 为每个机器人初始化客户端
|
|
23
|
+
for (const botConfig of feishuConfig) {
|
|
24
|
+
const { name, appId, appSecret } = botConfig;
|
|
25
|
+
if (!name || !appId || !appSecret) {
|
|
26
|
+
console.warn(`Feishu bot ${name || 'unknown'} missing required config, skipping.`);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
console.log(`Initializing Feishu Bot: ${name}`);
|
|
31
|
+
// Initialize Lark Client for API calls
|
|
32
|
+
const client = new lark.Client({
|
|
33
|
+
appId,
|
|
34
|
+
appSecret,
|
|
35
|
+
appType: lark.AppType.SelfBuild,
|
|
36
|
+
domain: lark.Domain.Feishu,
|
|
37
|
+
});
|
|
38
|
+
// Initialize WebSocket Client for receiving events
|
|
39
|
+
const wsClient = new lark.WSClient({
|
|
40
|
+
appId,
|
|
41
|
+
appSecret,
|
|
42
|
+
});
|
|
43
|
+
// Start WebSocket client
|
|
44
|
+
console.log(`Starting WebSocket client for Feishu Bot: ${name}`);
|
|
45
|
+
await wsClient.start({
|
|
46
|
+
eventDispatcher: new lark.EventDispatcher({}).register({
|
|
47
|
+
'im.message.receive_v1': async (data) => {
|
|
48
|
+
console.log(`[Feishu-${name}] Received message event:`, data);
|
|
49
|
+
const event = data.message;
|
|
50
|
+
if (!event || !data.sender)
|
|
51
|
+
return;
|
|
52
|
+
// Ignore bot messages
|
|
53
|
+
if (data.sender.sender_type !== 'user') {
|
|
54
|
+
console.log(`[Feishu-${name}] Ignoring bot message`);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const content = JSON.parse(event.content).text;
|
|
59
|
+
console.log(`[Feishu-${name}] Received message: ${content}`);
|
|
60
|
+
// Use chat_id for group chats, open_id/user_id for p2p?
|
|
61
|
+
// Actually message.chat_id is universal for where the message comes from.
|
|
62
|
+
const sessionId = `feishu:${name}:${event.chat_id}`;
|
|
63
|
+
console.log(`[Feishu-${name}] Emitting message with sessionId: ${sessionId}`);
|
|
64
|
+
this.emitMessage(sessionId, content);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
console.error(`[Feishu-${name}] Failed to parse message content:`, e);
|
|
68
|
+
}
|
|
56
69
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
console.error(`[Feishu-${name}] Failed to start WebSocket client:`, error);
|
|
70
|
+
})
|
|
71
|
+
});
|
|
72
|
+
this.bots.push({ name, client, wsClient });
|
|
73
|
+
console.log(`Feishu Bot ${name} Started (WebSocket Mode)`);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error(`[Feishu-${name}] Failed to start WebSocket client:`, error);
|
|
77
|
+
}
|
|
68
78
|
}
|
|
69
79
|
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
console.error('Feishu channel start failed:', error);
|
|
82
|
+
}
|
|
70
83
|
}
|
|
71
84
|
async stop() {
|
|
72
85
|
// There is no explicit stop method for WSClient in the current SDK version exposed clearly,
|