@zhin.js/adapter-lark 1.0.55 → 1.0.57

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/src/bot.ts ADDED
@@ -0,0 +1,714 @@
1
+ /**
2
+ * 飞书/Lark Bot 实现
3
+ */
4
+ import type { Context } from "koa";
5
+ import axios, { type AxiosInstance } from "axios";
6
+ import { createHash } from "crypto";
7
+ import {
8
+ Bot,
9
+ Message,
10
+ SendOptions,
11
+ SendContent,
12
+ MessageSegment,
13
+ segment,
14
+ } from "zhin.js";
15
+ import type { LarkBotConfig, LarkMessage, LarkEvent, AccessToken } from "./types.js";
16
+ import type { LarkAdapter } from "./adapter.js";
17
+
18
+ export class LarkBot implements Bot<LarkBotConfig, LarkMessage> {
19
+ $connected: boolean
20
+ private router: any
21
+ private accessToken: AccessToken
22
+ private axiosInstance: AxiosInstance
23
+
24
+ get logger() {
25
+ return this.adapter.plugin.logger;
26
+ }
27
+
28
+ get $id() {
29
+ return this.$config.name;
30
+ }
31
+
32
+ constructor(public adapter: LarkAdapter, router: any, public $config: LarkBotConfig) {
33
+ this.router = router;
34
+ this.$connected = false;
35
+ this.accessToken = { token: '', expires_in: 0, timestamp: 0 };
36
+
37
+ // 设置 API 基础 URL
38
+ const baseURL = $config.apiBaseUrl || ($config.isFeishu ?
39
+ 'https://open.feishu.cn/open-apis' :
40
+ 'https://open.larksuite.com/open-apis'
41
+ );
42
+
43
+ this.axiosInstance = axios.create({
44
+ baseURL,
45
+ timeout: 30000,
46
+ headers: {
47
+ 'Content-Type': 'application/json; charset=utf-8'
48
+ }
49
+ });
50
+
51
+ // 设置请求拦截器,自动添加 access_token
52
+ this.axiosInstance.interceptors.request.use(async (config) => {
53
+ await this.ensureAccessToken();
54
+ config.headers = config.headers;
55
+ config.headers['Authorization'] = `Bearer ${this.accessToken.token}`;
56
+ return config;
57
+ });
58
+
59
+ // 设置 webhook 路由
60
+ this.setupWebhookRoute();
61
+ }
62
+
63
+ private setupWebhookRoute(): void {
64
+ this.router.post(this.$config.webhookPath, (ctx: Context) => {
65
+ this.handleWebhook(ctx);
66
+ });
67
+ }
68
+
69
+ private async handleWebhook(ctx: Context): Promise<void> {
70
+ try {
71
+ const body = (ctx.request as any).body;
72
+ const headers = ctx.request.headers;
73
+
74
+ // 验证请求(如果配置了验证令牌)
75
+ if (this.$config.verificationToken) {
76
+ const token = headers['x-lark-request-token'] as string;
77
+ if (token !== this.$config.verificationToken) {
78
+ this.logger.warn('Invalid verification token in webhook');
79
+ ctx.status = 403;
80
+ ctx.body = 'Forbidden';
81
+ return;
82
+ }
83
+ }
84
+
85
+ // 签名验证(如果配置了加密密钥)
86
+ if (this.$config.encryptKey) {
87
+ const timestamp = headers['x-lark-request-timestamp'] as string;
88
+ const nonce = headers['x-lark-request-nonce'] as string;
89
+ const signature = headers['x-lark-signature'] as string;
90
+ const bodyStr = JSON.stringify(body);
91
+
92
+ if (!this.verifySignature(timestamp, nonce, bodyStr, signature)) {
93
+ this.logger.warn('Invalid signature in webhook');
94
+ ctx.status = 403;
95
+ ctx.body = 'Forbidden';
96
+ return;
97
+ }
98
+ }
99
+
100
+ const event: LarkEvent = body;
101
+
102
+ // URL 验证挑战(首次配置 webhook 时)
103
+ if (event.type === 'url_verification') {
104
+ ctx.body = { challenge: (event as any).challenge };
105
+ return;
106
+ }
107
+
108
+ // 处理消息事件
109
+ if (event.type === 'event_callback' && event.event) {
110
+ await this.handleEvent(event.event);
111
+ }
112
+
113
+ ctx.status = 200;
114
+ ctx.body = { code: 0, msg: 'success' };
115
+
116
+ } catch (error) {
117
+ this.logger.error('Webhook error:', error);
118
+ ctx.status = 500;
119
+ ctx.body = { code: -1, msg: 'Internal Server Error' };
120
+ }
121
+ }
122
+
123
+ private verifySignature(timestamp: string, nonce: string, body: string, signature: string): boolean {
124
+ if (!this.$config.encryptKey) return true;
125
+
126
+ try {
127
+ const stringToSign = `${timestamp}${nonce}${this.$config.encryptKey}${body}`;
128
+ const calculatedSignature = createHash('sha256').update(stringToSign).digest('hex');
129
+ return calculatedSignature === signature;
130
+ } catch (error) {
131
+ this.logger.error('Signature verification error:', error);
132
+ return false;
133
+ }
134
+ }
135
+
136
+ private async handleEvent(event: any): Promise<void> {
137
+ // 处理消息事件
138
+ if (event.message) {
139
+ const message = this.$formatMessage(event.message, event);
140
+ this.adapter.emit('message.receive', message);
141
+ this.logger.info(`${this.$config.name} recv ${message.$channel.type}(${message.$channel.id}): ${segment.raw(message.$content)}`);
142
+ }
143
+ }
144
+
145
+ // ================================================================================================
146
+ // Token 管理
147
+ // ================================================================================================
148
+
149
+ private async ensureAccessToken(): Promise<void> {
150
+ const now = Date.now();
151
+ // 提前 5 分钟刷新 token
152
+ if (this.accessToken.token && now < (this.accessToken.timestamp + (this.accessToken.expires_in - 300) * 1000)) {
153
+ return;
154
+ }
155
+
156
+ await this.refreshAccessToken();
157
+ }
158
+
159
+ private async refreshAccessToken(): Promise<void> {
160
+ try {
161
+ const response = await axios.post(
162
+ `${this.$config.apiBaseUrl || (this.$config.isFeishu ?
163
+ 'https://open.feishu.cn/open-apis' :
164
+ 'https://open.larksuite.com/open-apis'
165
+ )}/auth/v3/tenant_access_token/internal`,
166
+ {
167
+ app_id: this.$config.appId,
168
+ app_secret: this.$config.appSecret
169
+ }
170
+ );
171
+
172
+ if (response.data.code === 0) {
173
+ this.accessToken = {
174
+ token: response.data.tenant_access_token,
175
+ expires_in: response.data.expire,
176
+ timestamp: Date.now()
177
+ };
178
+ this.logger.debug('Access token refreshed successfully');
179
+ } else {
180
+ throw new Error(`Failed to get access token: ${response.data.msg}`);
181
+ }
182
+ } catch (error) {
183
+ this.logger.error('Failed to refresh access token:', error);
184
+ throw error;
185
+ }
186
+ }
187
+
188
+ // ================================================================================================
189
+ // 消息格式化
190
+ // ================================================================================================
191
+
192
+ $formatMessage(msg: LarkMessage, event?: any): Message<LarkMessage> {
193
+ const content = this.parseMessageContent(msg);
194
+
195
+ // 确定聊天类型
196
+ const chatType = msg.chat_id?.startsWith('oc_') ? 'group' : 'private';
197
+
198
+ return Message.from(msg, {
199
+ $id: msg.message_id || Date.now().toString(),
200
+ $adapter: 'lark',
201
+ $bot: this.$config.name,
202
+ $sender: {
203
+ id: msg.sender?.sender_id?.open_id || 'unknown',
204
+ name: msg.sender?.sender_id?.user_id || msg.sender?.sender_id?.open_id || 'Unknown User'
205
+ },
206
+ $channel: {
207
+ id: msg.chat_id || 'unknown',
208
+ type: chatType as any
209
+ },
210
+ $content: content,
211
+ $raw: JSON.stringify(msg),
212
+ $timestamp: msg.create_time ? parseInt(msg.create_time) : Date.now(),
213
+ $recall: async () => {
214
+ await this.$recallMessage(msg.message_id || '');
215
+ },
216
+ $reply: async (content: SendContent): Promise<string> => {
217
+ return await this.adapter.sendMessage({
218
+ context: 'lark',
219
+ bot: this.$config.name,
220
+ id: msg.chat_id || 'unknown',
221
+ type: chatType,
222
+ content: content
223
+ });
224
+ }
225
+ });
226
+ }
227
+
228
+ private parseMessageContent(msg: LarkMessage): MessageSegment[] {
229
+ const content: MessageSegment[] = [];
230
+
231
+ if (!msg.content || !msg.message_type) {
232
+ return content;
233
+ }
234
+
235
+ try {
236
+ const messageContent = JSON.parse(msg.content);
237
+
238
+ switch (msg.message_type) {
239
+ case 'text':
240
+ if (messageContent.text) {
241
+ content.push(segment('text', { content: messageContent.text }));
242
+
243
+ // 处理 @提及
244
+ if (msg.mentions) {
245
+ for (const mention of msg.mentions) {
246
+ if (mention.key && messageContent.text.includes(mention.key)) {
247
+ content.push(segment('at', {
248
+ id: mention.id?.open_id,
249
+ name: mention.name
250
+ }));
251
+ }
252
+ }
253
+ }
254
+ }
255
+ break;
256
+
257
+ case 'image':
258
+ content.push(segment('image', {
259
+ file_key: messageContent.image_key,
260
+ url: `https://open.feishu.cn/open-apis/im/v1/messages/${msg.message_id}/resources/${messageContent.image_key}`
261
+ }));
262
+ break;
263
+
264
+ case 'file':
265
+ content.push(segment('file', {
266
+ file_key: messageContent.file_key,
267
+ file_name: messageContent.file_name,
268
+ file_size: messageContent.file_size
269
+ }));
270
+ break;
271
+
272
+ case 'audio':
273
+ content.push(segment('audio', {
274
+ file_key: messageContent.file_key,
275
+ duration: messageContent.duration
276
+ }));
277
+ break;
278
+
279
+ case 'video':
280
+ content.push(segment('video', {
281
+ file_key: messageContent.file_key,
282
+ duration: messageContent.duration,
283
+ width: messageContent.width,
284
+ height: messageContent.height
285
+ }));
286
+ break;
287
+
288
+ case 'sticker':
289
+ content.push(segment('sticker', {
290
+ file_key: messageContent.file_key
291
+ }));
292
+ break;
293
+
294
+ case 'rich_text':
295
+ // 富文本消息处理(简化)
296
+ if (messageContent.content) {
297
+ this.parseRichTextContent(messageContent.content, content);
298
+ }
299
+ break;
300
+
301
+ case 'post':
302
+ // 卡片消息处理
303
+ content.push(segment('card', messageContent));
304
+ break;
305
+
306
+ default:
307
+ content.push(segment('text', { content: `[不支持的消息类型: ${msg.message_type}]` }));
308
+ break;
309
+ }
310
+ } catch (error) {
311
+ this.logger.error('Failed to parse message content:', error);
312
+ content.push(segment('text', { content: '[消息解析失败]' }));
313
+ }
314
+
315
+ return content;
316
+ }
317
+
318
+ private parseRichTextContent(richContent: any, content: MessageSegment[]): void {
319
+ // 简化的富文本解析
320
+ if (Array.isArray(richContent)) {
321
+ for (const block of richContent) {
322
+ if (block.tag === 'text' && block.text) {
323
+ content.push(segment('text', { content: block.text }));
324
+ } else if (block.tag === 'a' && block.href) {
325
+ content.push(segment('link', {
326
+ url: block.href,
327
+ text: block.text || block.href
328
+ }));
329
+ } else if (block.tag === 'at' && block.user_id) {
330
+ content.push(segment('at', {
331
+ id: block.user_id,
332
+ name: block.user_name
333
+ }));
334
+ }
335
+ }
336
+ }
337
+ }
338
+
339
+ // ================================================================================================
340
+ // 消息发送
341
+ // ================================================================================================
342
+
343
+ async $sendMessage(options: SendOptions): Promise<string> {
344
+ const chatId = options.id;
345
+ const content = this.formatSendContent(options.content);
346
+
347
+ try {
348
+ const response = await this.axiosInstance.post('/im/v1/messages', {
349
+ receive_id: chatId,
350
+ receive_id_type: 'chat_id',
351
+ msg_type: content.msg_type,
352
+ content: content.content
353
+ });
354
+
355
+ if (response.data.code !== 0) {
356
+ throw new Error(`Failed to send message: ${response.data.msg}`);
357
+ }
358
+
359
+ this.logger.debug('Message sent successfully:', response.data.data?.message_id);
360
+ return response.data.data?.message_id || '';
361
+ } catch (error) {
362
+ this.logger.error('Failed to send message:', error);
363
+ throw error;
364
+ }
365
+ }
366
+ async $recallMessage(id:string):Promise<void> {
367
+ await this.axiosInstance.post('/im/v1/messages/recall', {
368
+ message_id: id
369
+ });
370
+ }
371
+
372
+ private formatSendContent(content: SendContent): { msg_type: string, content: string } {
373
+ if (typeof content === 'string') {
374
+ return {
375
+ msg_type: 'text',
376
+ content: JSON.stringify({ text: content })
377
+ };
378
+ }
379
+
380
+ if (Array.isArray(content)) {
381
+ const textParts: string[] = [];
382
+ let hasMedia = false;
383
+ let mediaContent: any = null;
384
+
385
+ for (const item of content) {
386
+ if (typeof item === 'string') {
387
+ textParts.push(item);
388
+ } else {
389
+ const segment = item as MessageSegment;
390
+ switch (segment.type) {
391
+ case 'text':
392
+ textParts.push(segment.data.content || segment.data.text || '');
393
+ break;
394
+
395
+ case 'at':
396
+ textParts.push(`<at user_id="${segment.data.id}">${segment.data.name || segment.data.id}</at>`);
397
+ break;
398
+
399
+ case 'image':
400
+ if (!hasMedia) {
401
+ hasMedia = true;
402
+ mediaContent = {
403
+ msg_type: 'image',
404
+ content: JSON.stringify({
405
+ image_key: segment.data.file_key || segment.data.key
406
+ })
407
+ };
408
+ }
409
+ break;
410
+
411
+ case 'file':
412
+ if (!hasMedia) {
413
+ hasMedia = true;
414
+ mediaContent = {
415
+ msg_type: 'file',
416
+ content: JSON.stringify({
417
+ file_key: segment.data.file_key || segment.data.key
418
+ })
419
+ };
420
+ }
421
+ break;
422
+
423
+ case 'card':
424
+ if (!hasMedia) {
425
+ hasMedia = true;
426
+ mediaContent = {
427
+ msg_type: 'interactive',
428
+ content: JSON.stringify(segment.data)
429
+ };
430
+ }
431
+ break;
432
+ }
433
+ }
434
+ }
435
+
436
+ // 优先发送媒体内容
437
+ if (hasMedia && mediaContent) {
438
+ return mediaContent;
439
+ }
440
+
441
+ // 否则发送文本内容
442
+ return {
443
+ msg_type: 'text',
444
+ content: JSON.stringify({ text: textParts.join('') })
445
+ };
446
+ }
447
+
448
+ return {
449
+ msg_type: 'text',
450
+ content: JSON.stringify({ text: String(content) })
451
+ };
452
+ }
453
+
454
+ // ================================================================================================
455
+ // Bot 生命周期
456
+ // ================================================================================================
457
+
458
+ async $connect(): Promise<void> {
459
+ try {
460
+ // 获取 access token
461
+ await this.refreshAccessToken();
462
+
463
+ this.$connected = true;
464
+ this.logger.info(`Lark bot connected: ${this.$config.name}`);
465
+ this.logger.info(`Webhook URL: ${this.$config.webhookPath}`);
466
+
467
+ } catch (error) {
468
+ this.logger.error('Failed to connect Lark bot:', error);
469
+ throw error;
470
+ }
471
+ }
472
+
473
+ async $disconnect(): Promise<void> {
474
+ try {
475
+ this.$connected = false;
476
+ this.logger.info('Lark bot disconnected');
477
+ } catch (error) {
478
+ this.logger.error('Error disconnecting Lark bot:', error);
479
+ }
480
+ }
481
+
482
+ // ================================================================================================
483
+ // 工具方法
484
+ // ================================================================================================
485
+
486
+ // 获取用户信息
487
+ async getUserInfo(userId: string, userIdType: 'open_id' | 'user_id' | 'union_id' = 'open_id'): Promise<any> {
488
+ try {
489
+ const response = await this.axiosInstance.get(`/contact/v3/users/${userId}`, {
490
+ params: { user_id_type: userIdType }
491
+ });
492
+
493
+ return response.data.data?.user;
494
+ } catch (error) {
495
+ this.logger.error('Failed to get user info:', error);
496
+ return null;
497
+ }
498
+ }
499
+
500
+ // 获取群聊信息
501
+ async getChatInfo(chatId: string): Promise<any> {
502
+ try {
503
+ const response = await this.axiosInstance.get(`/im/v1/chats/${chatId}`);
504
+ return response.data.data;
505
+ } catch (error) {
506
+ this.logger.error('Failed to get chat info:', error);
507
+ return null;
508
+ }
509
+ }
510
+
511
+ // 上传文件
512
+ async uploadFile(filePath: string, fileType: 'image' | 'file' | 'video' | 'audio' = 'file'): Promise<string | null> {
513
+ try {
514
+ const FormData = require('form-data');
515
+ const fs = require('fs');
516
+
517
+ const form = new FormData();
518
+ form.append('file', fs.createReadStream(filePath));
519
+ form.append('file_type', fileType);
520
+
521
+ const response = await this.axiosInstance.post('/im/v1/files', form, {
522
+ headers: {
523
+ ...form.getHeaders()
524
+ }
525
+ });
526
+
527
+ if (response.data.code === 0) {
528
+ return response.data.data.file_key;
529
+ }
530
+
531
+ throw new Error(`Upload failed: ${response.data.msg}`);
532
+ } catch (error) {
533
+ this.logger.error('Failed to upload file:', error);
534
+ return null;
535
+ }
536
+ }
537
+
538
+ // ==================== 群组管理 API ====================
539
+
540
+ /**
541
+ * 创建群聊
542
+ * @param name 群名
543
+ * @param userIds 成员 open_id 列表
544
+ * @param ownerId 群主 open_id
545
+ */
546
+ async createChat(name: string, userIds: string[], ownerId?: string): Promise<string | null> {
547
+ try {
548
+ const response = await this.axiosInstance.post('/im/v1/chats', {
549
+ name,
550
+ user_id_list: userIds,
551
+ owner_id: ownerId
552
+ });
553
+
554
+ if (response.data.code === 0) {
555
+ this.logger.info(`创建群聊成功: ${response.data.data.chat_id}`);
556
+ return response.data.data.chat_id;
557
+ }
558
+ throw new Error(`Failed to create chat: ${response.data.msg}`);
559
+ } catch (error) {
560
+ this.logger.error('Failed to create chat:', error);
561
+ return null;
562
+ }
563
+ }
564
+
565
+ /**
566
+ * 更新群信息
567
+ * @param chatId 群聊 ID
568
+ * @param options 更新选项
569
+ */
570
+ async updateChatInfo(chatId: string, options: {
571
+ name?: string;
572
+ description?: string;
573
+ }): Promise<boolean> {
574
+ try {
575
+ const response = await this.axiosInstance.put(`/im/v1/chats/${chatId}`, options);
576
+
577
+ if (response.data.code === 0) {
578
+ this.logger.info(`更新群信息成功: ${chatId}`);
579
+ return true;
580
+ }
581
+ throw new Error(`Failed to update chat: ${response.data.msg}`);
582
+ } catch (error) {
583
+ this.logger.error('Failed to update chat:', error);
584
+ return false;
585
+ }
586
+ }
587
+
588
+ /**
589
+ * 添加群成员
590
+ * @param chatId 群聊 ID
591
+ * @param userIds 用户 ID 列表
592
+ */
593
+ async addChatMembers(chatId: string, userIds: string[]): Promise<boolean> {
594
+ try {
595
+ const response = await this.axiosInstance.post(`/im/v1/chats/${chatId}/members`, {
596
+ id_list: userIds
597
+ });
598
+
599
+ if (response.data.code === 0) {
600
+ this.logger.info(`添加群成员成功: ${chatId}`);
601
+ return true;
602
+ }
603
+ throw new Error(`Failed to add members: ${response.data.msg}`);
604
+ } catch (error) {
605
+ this.logger.error('Failed to add chat members:', error);
606
+ return false;
607
+ }
608
+ }
609
+
610
+ /**
611
+ * 移除群成员
612
+ * @param chatId 群聊 ID
613
+ * @param userIds 用户 ID 列表
614
+ */
615
+ async removeChatMembers(chatId: string, userIds: string[]): Promise<boolean> {
616
+ try {
617
+ const response = await this.axiosInstance.delete(`/im/v1/chats/${chatId}/members`, {
618
+ data: { id_list: userIds }
619
+ });
620
+
621
+ if (response.data.code === 0) {
622
+ this.logger.info(`移除群成员成功: ${chatId}`);
623
+ return true;
624
+ }
625
+ throw new Error(`Failed to remove members: ${response.data.msg}`);
626
+ } catch (error) {
627
+ this.logger.error('Failed to remove chat members:', error);
628
+ return false;
629
+ }
630
+ }
631
+
632
+ /**
633
+ * 获取群成员列表
634
+ * @param chatId 群聊 ID
635
+ */
636
+ async getChatMembers(chatId: string): Promise<any[]> {
637
+ try {
638
+ const response = await this.axiosInstance.get(`/im/v1/chats/${chatId}/members`);
639
+
640
+ if (response.data.code === 0) {
641
+ return response.data.data.items || [];
642
+ }
643
+ throw new Error(`Failed to get members: ${response.data.msg}`);
644
+ } catch (error) {
645
+ this.logger.error('Failed to get chat members:', error);
646
+ return [];
647
+ }
648
+ }
649
+
650
+ /**
651
+ * 解散群聊
652
+ * @param chatId 群聊 ID
653
+ */
654
+ async dissolveChat(chatId: string): Promise<boolean> {
655
+ try {
656
+ const response = await this.axiosInstance.delete(`/im/v1/chats/${chatId}`);
657
+
658
+ if (response.data.code === 0) {
659
+ this.logger.info(`解散群聊成功: ${chatId}`);
660
+ return true;
661
+ }
662
+ throw new Error(`Failed to dissolve chat: ${response.data.msg}`);
663
+ } catch (error) {
664
+ this.logger.error('Failed to dissolve chat:', error);
665
+ return false;
666
+ }
667
+ }
668
+
669
+ /**
670
+ * 设置群管理员
671
+ * @param chatId 群聊 ID
672
+ * @param userIds 用户 ID 列表
673
+ */
674
+ async setChatManagers(chatId: string, userIds: string[]): Promise<boolean> {
675
+ try {
676
+ const response = await this.axiosInstance.post(`/im/v1/chats/${chatId}/managers/add_managers`, {
677
+ manager_ids: userIds
678
+ });
679
+
680
+ if (response.data.code === 0) {
681
+ this.logger.info(`设置群管理员成功: ${chatId}`);
682
+ return true;
683
+ }
684
+ throw new Error(`Failed to set managers: ${response.data.msg}`);
685
+ } catch (error) {
686
+ this.logger.error('Failed to set chat managers:', error);
687
+ return false;
688
+ }
689
+ }
690
+
691
+ /**
692
+ * 移除群管理员
693
+ * @param chatId 群聊 ID
694
+ * @param userIds 用户 ID 列表
695
+ */
696
+ async removeChatManagers(chatId: string, userIds: string[]): Promise<boolean> {
697
+ try {
698
+ const response = await this.axiosInstance.post(`/im/v1/chats/${chatId}/managers/delete_managers`, {
699
+ manager_ids: userIds
700
+ });
701
+
702
+ if (response.data.code === 0) {
703
+ this.logger.info(`移除群管理员成功: ${chatId}`);
704
+ return true;
705
+ }
706
+ throw new Error(`Failed to remove managers: ${response.data.msg}`);
707
+ } catch (error) {
708
+ this.logger.error('Failed to remove chat managers:', error);
709
+ return false;
710
+ }
711
+ }
712
+ }
713
+
714
+ // 定义 Adapter 类