@zhin.js/adapter-slack 1.0.36 → 1.0.38

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/lib/index.js CHANGED
@@ -1,929 +1,13 @@
1
- import { App as SlackApp, LogLevel } from "@slack/bolt";
2
- import { WebClient } from "@slack/web-api";
3
- import { Adapter, Message, segment, usePlugin, createGroupManagementTools, GROUP_MANAGEMENT_SKILL_KEYWORDS, GROUP_MANAGEMENT_SKILL_TAGS, } from "zhin.js";
4
- ;
1
+ /**
2
+ * Slack 适配器入口:类型扩展、导出、注册
3
+ */
4
+ import { usePlugin } from "zhin.js";
5
+ import { SlackAdapter } from "./adapter.js";
6
+ export * from "./types.js";
7
+ export { SlackBot } from "./bot.js";
8
+ export { SlackAdapter } from "./adapter.js";
5
9
  const plugin = usePlugin();
6
- const { provide, useContext } = plugin;
7
- export class SlackBot {
8
- adapter;
9
- $config;
10
- $connected;
11
- app;
12
- client;
13
- get $id() {
14
- return this.$config.name;
15
- }
16
- constructor(adapter, $config) {
17
- this.adapter = adapter;
18
- this.$config = $config;
19
- this.$connected = false;
20
- // Initialize Slack app
21
- if ($config.socketMode && $config.appToken) {
22
- // Socket Mode
23
- this.app = new SlackApp({
24
- token: $config.token,
25
- signingSecret: $config.signingSecret,
26
- appToken: $config.appToken,
27
- socketMode: true,
28
- logLevel: $config.logLevel || LogLevel.INFO,
29
- });
30
- }
31
- else {
32
- // HTTP Mode
33
- this.app = new SlackApp({
34
- token: $config.token,
35
- signingSecret: $config.signingSecret,
36
- socketMode: false,
37
- logLevel: $config.logLevel || LogLevel.INFO,
38
- });
39
- }
40
- this.client = new WebClient($config.token);
41
- }
42
- async $connect() {
43
- try {
44
- // Set up message event handler
45
- this.app.message(async ({ message, say }) => {
46
- await this.handleSlackMessage(message);
47
- });
48
- // Set up app mention handler
49
- this.app.event("app_mention", async ({ event, say }) => {
50
- await this.handleSlackMessage(event);
51
- });
52
- // Start the app
53
- const port = this.$config.port || 3000;
54
- if (this.$config.socketMode) {
55
- await this.app.start();
56
- }
57
- else {
58
- await this.app.start(port);
59
- }
60
- this.$connected = true;
61
- // Get bot info
62
- const authTest = await this.client.auth.test();
63
- plugin.logger.info(`Slack bot ${this.$config.name} connected successfully as @${authTest.user}`);
64
- if (!this.$config.socketMode) {
65
- plugin.logger.info(`Slack bot listening on port ${port}`);
66
- }
67
- }
68
- catch (error) {
69
- plugin.logger.error("Failed to connect Slack bot:", error);
70
- this.$connected = false;
71
- throw error;
72
- }
73
- }
74
- async $disconnect() {
75
- try {
76
- await this.app.stop();
77
- this.$connected = false;
78
- plugin.logger.info(`Slack bot ${this.$config.name} disconnected`);
79
- }
80
- catch (error) {
81
- plugin.logger.error("Error disconnecting Slack bot:", error);
82
- throw error;
83
- }
84
- }
85
- async handleSlackMessage(msg) {
86
- // Ignore bot messages and message changes
87
- if ("subtype" in msg && (msg.subtype === "bot_message" || msg.subtype === "message_changed")) {
88
- return;
89
- }
90
- const message = this.$formatMessage(msg);
91
- this.adapter.emit("message.receive", message);
92
- plugin.logger.debug(`${this.$config.name} recv ${message.$channel.type}(${message.$channel.id}): ${segment.raw(message.$content)}`);
93
- }
94
- $formatMessage(msg) {
95
- // Determine channel type based on channel ID
96
- const channelType = "channel_type" in msg && msg.channel_type === "im" ? "private" : "group";
97
- const channelId = msg.channel;
98
- // Parse message content
99
- const content = this.parseMessageContent(msg);
100
- // Extract user info safely
101
- const userId = ("user" in msg ? msg.user : "") || "";
102
- const userName = ("username" in msg ? msg.username : null) || userId || "Unknown";
103
- const messageText = ("text" in msg ? msg.text : "") || "";
104
- const result = Message.from(msg, {
105
- $id: msg.ts,
106
- $adapter: "slack",
107
- $bot: this.$config.name,
108
- $sender: {
109
- id: userId,
110
- name: userName,
111
- },
112
- $channel: {
113
- id: channelId,
114
- type: channelType,
115
- },
116
- $content: content,
117
- $raw: messageText,
118
- $timestamp: parseFloat(msg.ts) * 1000,
119
- $recall: async () => {
120
- try {
121
- await this.client.chat.delete({
122
- channel: channelId,
123
- ts: result.$id,
124
- });
125
- }
126
- catch (error) {
127
- plugin.logger.error("Error recalling Slack message:", error);
128
- throw error;
129
- }
130
- },
131
- $reply: async (content, quote) => {
132
- if (!Array.isArray(content))
133
- content = [content];
134
- const sendOptions = {
135
- channel: channelId,
136
- };
137
- // Handle thread reply
138
- if (quote) {
139
- const threadTs = typeof quote === "boolean" ? result.$id : quote;
140
- sendOptions.thread_ts = threadTs;
141
- }
142
- return await this.adapter.sendMessage({
143
- context: "slack",
144
- bot: this.$config.name,
145
- id: channelId,
146
- type: "channel",
147
- content: content,
148
- });
149
- },
150
- });
151
- return result;
152
- }
153
- parseMessageContent(msg) {
154
- const segments = [];
155
- // Handle text
156
- if ("text" in msg && msg.text) {
157
- // Parse Slack formatting
158
- segments.push(...this.parseSlackText(msg.text));
159
- }
160
- // Handle files
161
- if ("files" in msg && msg.files) {
162
- for (const file of msg.files) {
163
- if (file.mimetype?.startsWith("image/")) {
164
- segments.push({
165
- type: "image",
166
- data: {
167
- id: file.id,
168
- name: file.name,
169
- url: file.url_private || file.permalink,
170
- size: file.size,
171
- mimetype: file.mimetype,
172
- },
173
- });
174
- }
175
- else if (file.mimetype?.startsWith("video/")) {
176
- segments.push({
177
- type: "video",
178
- data: {
179
- id: file.id,
180
- name: file.name,
181
- url: file.url_private || file.permalink,
182
- size: file.size,
183
- mimetype: file.mimetype,
184
- },
185
- });
186
- }
187
- else if (file.mimetype?.startsWith("audio/")) {
188
- segments.push({
189
- type: "audio",
190
- data: {
191
- id: file.id,
192
- name: file.name,
193
- url: file.url_private || file.permalink,
194
- size: file.size,
195
- mimetype: file.mimetype,
196
- },
197
- });
198
- }
199
- else {
200
- segments.push({
201
- type: "file",
202
- data: {
203
- id: file.id,
204
- name: file.name,
205
- url: file.url_private || file.permalink,
206
- size: file.size,
207
- mimetype: file.mimetype,
208
- },
209
- });
210
- }
211
- }
212
- }
213
- // Handle attachments
214
- if ("attachments" in msg && msg.attachments) {
215
- for (const attachment of msg.attachments) {
216
- if (attachment.image_url) {
217
- segments.push({
218
- type: "image",
219
- data: {
220
- url: attachment.image_url,
221
- title: attachment.title,
222
- text: attachment.text,
223
- },
224
- });
225
- }
226
- }
227
- }
228
- return segments.length > 0
229
- ? segments
230
- : [{ type: "text", data: { text: "" } }];
231
- }
232
- parseSlackText(text) {
233
- const segments = [];
234
- let lastIndex = 0;
235
- // Match user mentions <@U12345678>
236
- const userMentionRegex = /<@([UW][A-Z0-9]+)(?:\|([^>]+))?>/g;
237
- // Match channel mentions <#C12345678|general>
238
- const channelMentionRegex = /<#([C][A-Z0-9]+)(?:\|([^>]+))?>/g;
239
- // Match links <http://example.com|Example>
240
- const linkRegex = /<(https?:\/\/[^|>]+)(?:\|([^>]+))?>/g;
241
- const allMatches = [];
242
- // Collect all matches
243
- let match;
244
- while ((match = userMentionRegex.exec(text)) !== null) {
245
- allMatches.push({ match, type: "user" });
246
- }
247
- while ((match = channelMentionRegex.exec(text)) !== null) {
248
- allMatches.push({ match, type: "channel" });
249
- }
250
- while ((match = linkRegex.exec(text)) !== null) {
251
- allMatches.push({ match, type: "link" });
252
- }
253
- // Sort by position
254
- allMatches.sort((a, b) => a.match.index - b.match.index);
255
- // Process matches
256
- for (const { match, type } of allMatches) {
257
- const matchStart = match.index;
258
- const matchEnd = matchStart + match[0].length;
259
- // Add text before match
260
- if (matchStart > lastIndex) {
261
- const beforeText = text.slice(lastIndex, matchStart);
262
- if (beforeText.trim()) {
263
- segments.push({ type: "text", data: { text: beforeText } });
264
- }
265
- }
266
- // Add special segment
267
- switch (type) {
268
- case "user":
269
- segments.push({
270
- type: "at",
271
- data: {
272
- id: match[1],
273
- name: match[2] || match[1],
274
- text: match[0],
275
- },
276
- });
277
- break;
278
- case "channel":
279
- segments.push({
280
- type: "channel_mention",
281
- data: {
282
- id: match[1],
283
- name: match[2] || match[1],
284
- text: match[0],
285
- },
286
- });
287
- break;
288
- case "link":
289
- segments.push({
290
- type: "link",
291
- data: {
292
- url: match[1],
293
- text: match[2] || match[1],
294
- },
295
- });
296
- break;
297
- }
298
- lastIndex = matchEnd;
299
- }
300
- // Add remaining text
301
- if (lastIndex < text.length) {
302
- const remainingText = text.slice(lastIndex);
303
- if (remainingText.trim()) {
304
- segments.push({ type: "text", data: { text: remainingText } });
305
- }
306
- }
307
- return segments.length > 0
308
- ? segments
309
- : [{ type: "text", data: { text } }];
310
- }
311
- async $sendMessage(options) {
312
- try {
313
- const result = await this.sendContentToChannel(options.id, options.content);
314
- plugin.logger.debug(`${this.$config.name} send ${options.type}(${options.id}): ${segment.raw(options.content)}`);
315
- return result.ts || "";
316
- }
317
- catch (error) {
318
- plugin.logger.error("Failed to send Slack message:", error);
319
- throw error;
320
- }
321
- }
322
- async sendContentToChannel(channel, content, extraOptions = {}) {
323
- if (!Array.isArray(content))
324
- content = [content];
325
- let textContent = "";
326
- const attachments = [];
327
- for (const segment of content) {
328
- if (typeof segment === "string") {
329
- textContent += segment;
330
- continue;
331
- }
332
- const { type, data } = segment;
333
- switch (type) {
334
- case "text":
335
- textContent += data.text || "";
336
- break;
337
- case "at":
338
- textContent += `<@${data.id}>`;
339
- break;
340
- case "channel_mention":
341
- textContent += `<#${data.id}>`;
342
- break;
343
- case "link":
344
- if (data.text && data.text !== data.url) {
345
- textContent += `<${data.url}|${data.text}>`;
346
- }
347
- else {
348
- textContent += `<${data.url}>`;
349
- }
350
- break;
351
- case "image":
352
- if (data.url) {
353
- attachments.push({
354
- image_url: data.url,
355
- title: data.name || data.title,
356
- });
357
- }
358
- break;
359
- case "file":
360
- // Files need to be uploaded separately
361
- if (data.file) {
362
- try {
363
- await this.client.files.upload({
364
- channels: channel,
365
- file: data.file,
366
- filename: data.name,
367
- });
368
- }
369
- catch (error) {
370
- plugin.logger.error("Failed to upload file:", error);
371
- }
372
- }
373
- break;
374
- default:
375
- textContent += data.text || `[${type}]`;
376
- }
377
- }
378
- // Send message
379
- const messageOptions = {
380
- channel,
381
- text: textContent.trim() || "Message",
382
- ...extraOptions,
383
- };
384
- if (attachments.length > 0) {
385
- messageOptions.attachments = attachments;
386
- }
387
- const result = await this.client.chat.postMessage(messageOptions);
388
- return result.message || {};
389
- }
390
- async $recallMessage(id) {
391
- // Slack requires both channel and ts (timestamp) to delete a message
392
- // The Bot interface only provides message ID (ts), making recall impossible
393
- // Users should use message.$recall() instead, which has the full context
394
- throw new Error("SlackBot.$recallMessage: Message recall not supported without channel information. " +
395
- "Use message.$recall() method instead, which contains the required context.");
396
- }
397
- // ==================== 工作区管理 API ====================
398
- /**
399
- * 邀请用户到频道
400
- * @param channel 频道 ID
401
- * @param users 用户 ID 列表
402
- */
403
- async inviteToChannel(channel, users) {
404
- try {
405
- await this.client.conversations.invite({ channel, users: users.join(',') });
406
- plugin.logger.info(`Slack Bot ${this.$id} 邀请用户 ${users.join(',')} 到频道 ${channel}`);
407
- return true;
408
- }
409
- catch (error) {
410
- plugin.logger.error(`Slack Bot ${this.$id} 邀请用户失败:`, error);
411
- throw error;
412
- }
413
- }
414
- /**
415
- * 从频道踢出用户
416
- * @param channel 频道 ID
417
- * @param user 用户 ID
418
- */
419
- async kickFromChannel(channel, user) {
420
- try {
421
- await this.client.conversations.kick({ channel, user });
422
- plugin.logger.info(`Slack Bot ${this.$id} 将用户 ${user} 从频道 ${channel} 踢出`);
423
- return true;
424
- }
425
- catch (error) {
426
- plugin.logger.error(`Slack Bot ${this.$id} 踢出用户失败:`, error);
427
- throw error;
428
- }
429
- }
430
- /**
431
- * 设置频道话题
432
- * @param channel 频道 ID
433
- * @param topic 话题
434
- */
435
- async setChannelTopic(channel, topic) {
436
- try {
437
- await this.client.conversations.setTopic({ channel, topic });
438
- plugin.logger.info(`Slack Bot ${this.$id} 设置频道 ${channel} 话题为 "${topic}"`);
439
- return true;
440
- }
441
- catch (error) {
442
- plugin.logger.error(`Slack Bot ${this.$id} 设置话题失败:`, error);
443
- throw error;
444
- }
445
- }
446
- /**
447
- * 设置频道目的
448
- * @param channel 频道 ID
449
- * @param purpose 目的
450
- */
451
- async setChannelPurpose(channel, purpose) {
452
- try {
453
- await this.client.conversations.setPurpose({ channel, purpose });
454
- plugin.logger.info(`Slack Bot ${this.$id} 设置频道 ${channel} 目的`);
455
- return true;
456
- }
457
- catch (error) {
458
- plugin.logger.error(`Slack Bot ${this.$id} 设置目的失败:`, error);
459
- throw error;
460
- }
461
- }
462
- /**
463
- * 归档频道
464
- * @param channel 频道 ID
465
- */
466
- async archiveChannel(channel) {
467
- try {
468
- await this.client.conversations.archive({ channel });
469
- plugin.logger.info(`Slack Bot ${this.$id} 归档频道 ${channel}`);
470
- return true;
471
- }
472
- catch (error) {
473
- plugin.logger.error(`Slack Bot ${this.$id} 归档频道失败:`, error);
474
- throw error;
475
- }
476
- }
477
- /**
478
- * 取消归档频道
479
- * @param channel 频道 ID
480
- */
481
- async unarchiveChannel(channel) {
482
- try {
483
- await this.client.conversations.unarchive({ channel });
484
- plugin.logger.info(`Slack Bot ${this.$id} 取消归档频道 ${channel}`);
485
- return true;
486
- }
487
- catch (error) {
488
- plugin.logger.error(`Slack Bot ${this.$id} 取消归档失败:`, error);
489
- throw error;
490
- }
491
- }
492
- /**
493
- * 重命名频道
494
- * @param channel 频道 ID
495
- * @param name 新名称
496
- */
497
- async renameChannel(channel, name) {
498
- try {
499
- await this.client.conversations.rename({ channel, name });
500
- plugin.logger.info(`Slack Bot ${this.$id} 重命名频道 ${channel} 为 "${name}"`);
501
- return true;
502
- }
503
- catch (error) {
504
- plugin.logger.error(`Slack Bot ${this.$id} 重命名频道失败:`, error);
505
- throw error;
506
- }
507
- }
508
- /**
509
- * 获取频道成员列表
510
- * @param channel 频道 ID
511
- */
512
- async getChannelMembers(channel) {
513
- try {
514
- const result = await this.client.conversations.members({ channel });
515
- return result.members || [];
516
- }
517
- catch (error) {
518
- plugin.logger.error(`Slack Bot ${this.$id} 获取成员列表失败:`, error);
519
- throw error;
520
- }
521
- }
522
- /**
523
- * 获取频道信息
524
- * @param channel 频道 ID
525
- */
526
- async getChannelInfo(channel) {
527
- try {
528
- const result = await this.client.conversations.info({ channel });
529
- return result.channel;
530
- }
531
- catch (error) {
532
- plugin.logger.error(`Slack Bot ${this.$id} 获取频道信息失败:`, error);
533
- throw error;
534
- }
535
- }
536
- /**
537
- * 获取用户信息
538
- * @param user 用户 ID
539
- */
540
- async getUserInfo(user) {
541
- try {
542
- const result = await this.client.users.info({ user });
543
- return result.user;
544
- }
545
- catch (error) {
546
- plugin.logger.error(`Slack Bot ${this.$id} 获取用户信息失败:`, error);
547
- throw error;
548
- }
549
- }
550
- /**
551
- * 添加消息反应
552
- * @param channel 频道 ID
553
- * @param timestamp 消息时间戳
554
- * @param name 表情名称
555
- */
556
- async addReaction(channel, timestamp, name) {
557
- try {
558
- await this.client.reactions.add({ channel, timestamp, name });
559
- plugin.logger.info(`Slack Bot ${this.$id} 添加反应 :${name}: 到消息`);
560
- return true;
561
- }
562
- catch (error) {
563
- plugin.logger.error(`Slack Bot ${this.$id} 添加反应失败:`, error);
564
- throw error;
565
- }
566
- }
567
- /**
568
- * 移除消息反应
569
- * @param channel 频道 ID
570
- * @param timestamp 消息时间戳
571
- * @param name 表情名称
572
- */
573
- async removeReaction(channel, timestamp, name) {
574
- try {
575
- await this.client.reactions.remove({ channel, timestamp, name });
576
- plugin.logger.info(`Slack Bot ${this.$id} 移除反应 :${name}:`);
577
- return true;
578
- }
579
- catch (error) {
580
- plugin.logger.error(`Slack Bot ${this.$id} 移除反应失败:`, error);
581
- throw error;
582
- }
583
- }
584
- /**
585
- * 置顶消息
586
- * @param channel 频道 ID
587
- * @param timestamp 消息时间戳
588
- */
589
- async pinMessage(channel, timestamp) {
590
- try {
591
- await this.client.pins.add({ channel, timestamp });
592
- plugin.logger.info(`Slack Bot ${this.$id} 置顶消息(频道 ${channel})`);
593
- return true;
594
- }
595
- catch (error) {
596
- plugin.logger.error(`Slack Bot ${this.$id} 置顶消息失败:`, error);
597
- throw error;
598
- }
599
- }
600
- /**
601
- * 取消置顶消息
602
- * @param channel 频道 ID
603
- * @param timestamp 消息时间戳
604
- */
605
- async unpinMessage(channel, timestamp) {
606
- try {
607
- await this.client.pins.remove({ channel, timestamp });
608
- plugin.logger.info(`Slack Bot ${this.$id} 取消置顶消息`);
609
- return true;
610
- }
611
- catch (error) {
612
- plugin.logger.error(`Slack Bot ${this.$id} 取消置顶失败:`, error);
613
- throw error;
614
- }
615
- }
616
- }
617
- class SlackAdapter extends Adapter {
618
- constructor(plugin) {
619
- super(plugin, "slack", []);
620
- }
621
- createBot(config) {
622
- return new SlackBot(this, config);
623
- }
624
- // ── IGroupManagement 标准群管方法 ──────────────────────────────────
625
- async kickMember(botId, sceneId, userId) {
626
- const bot = this.bots.get(botId);
627
- if (!bot)
628
- throw new Error(`Bot ${botId} 不存在`);
629
- return bot.kickFromChannel(sceneId, userId);
630
- }
631
- async setGroupName(botId, sceneId, name) {
632
- const bot = this.bots.get(botId);
633
- if (!bot)
634
- throw new Error(`Bot ${botId} 不存在`);
635
- return bot.renameChannel(sceneId, name);
636
- }
637
- async listMembers(botId, sceneId) {
638
- const bot = this.bots.get(botId);
639
- if (!bot)
640
- throw new Error(`Bot ${botId} 不存在`);
641
- return bot.getChannelMembers(sceneId);
642
- }
643
- async getGroupInfo(botId, sceneId) {
644
- const bot = this.bots.get(botId);
645
- if (!bot)
646
- throw new Error(`Bot ${botId} 不存在`);
647
- return bot.getChannelInfo(sceneId);
648
- }
649
- // ── 生命周期 ───────────────────────────────────────────────────────
650
- async start() {
651
- this.registerSlackPlatformTools();
652
- const groupTools = createGroupManagementTools(this, this.name);
653
- groupTools.forEach((t) => this.addTool(t));
654
- this.declareSkill({
655
- description: 'Slack 工作区/频道管理:踢人、禁言、改频道名、查成员等。需先 list_members 获取用户 ID 再执行管理操作。',
656
- keywords: GROUP_MANAGEMENT_SKILL_KEYWORDS,
657
- tags: GROUP_MANAGEMENT_SKILL_TAGS,
658
- });
659
- await super.start();
660
- }
661
- /**
662
- * 注册 Slack 平台特有工具(邀请到频道等)
663
- */
664
- registerSlackPlatformTools() {
665
- // 邀请用户到频道
666
- this.addTool({
667
- name: 'slack_invite_to_channel',
668
- description: '邀请用户加入 Slack 频道',
669
- parameters: {
670
- type: 'object',
671
- properties: {
672
- bot: { type: 'string', description: 'Bot 名称' },
673
- channel: { type: 'string', description: '频道 ID' },
674
- users: { type: 'array', items: { type: 'string' }, description: '用户 ID 列表' },
675
- },
676
- required: ['bot', 'channel', 'users'],
677
- },
678
- platforms: ['slack'],
679
- scopes: ['group'],
680
- permissionLevel: 'group_admin',
681
- execute: async (args) => {
682
- const { bot: botId, channel, users } = args;
683
- const bot = this.bots.get(botId);
684
- if (!bot)
685
- throw new Error(`Bot ${botId} 不存在`);
686
- const success = await bot.inviteToChannel(channel, users);
687
- return { success, message: success ? `已邀请用户加入频道` : '操作失败' };
688
- },
689
- });
690
- // 设置频道话题
691
- this.addTool({
692
- name: 'slack_set_topic',
693
- description: '设置 Slack 频道话题',
694
- parameters: {
695
- type: 'object',
696
- properties: {
697
- bot: { type: 'string', description: 'Bot 名称' },
698
- channel: { type: 'string', description: '频道 ID' },
699
- topic: { type: 'string', description: '新话题' },
700
- },
701
- required: ['bot', 'channel', 'topic'],
702
- },
703
- platforms: ['slack'],
704
- scopes: ['group'],
705
- permissionLevel: 'group_admin',
706
- execute: async (args) => {
707
- const { bot: botId, channel, topic } = args;
708
- const bot = this.bots.get(botId);
709
- if (!bot)
710
- throw new Error(`Bot ${botId} 不存在`);
711
- const success = await bot.setChannelTopic(channel, topic);
712
- return { success, message: success ? `已设置频道话题` : '操作失败' };
713
- },
714
- });
715
- // 归档频道
716
- this.addTool({
717
- name: 'slack_archive_channel',
718
- description: '归档 Slack 频道',
719
- parameters: {
720
- type: 'object',
721
- properties: {
722
- bot: { type: 'string', description: 'Bot 名称' },
723
- channel: { type: 'string', description: '频道 ID' },
724
- },
725
- required: ['bot', 'channel'],
726
- },
727
- platforms: ['slack'],
728
- scopes: ['group'],
729
- permissionLevel: 'group_admin',
730
- execute: async (args) => {
731
- const { bot: botId, channel } = args;
732
- const bot = this.bots.get(botId);
733
- if (!bot)
734
- throw new Error(`Bot ${botId} 不存在`);
735
- const success = await bot.archiveChannel(channel);
736
- return { success, message: success ? `已归档频道` : '操作失败' };
737
- },
738
- });
739
- // 置顶消息
740
- this.addTool({
741
- name: 'slack_pin_message',
742
- description: '置顶 Slack 消息',
743
- parameters: {
744
- type: 'object',
745
- properties: {
746
- bot: { type: 'string', description: 'Bot 名称' },
747
- channel: { type: 'string', description: '频道 ID' },
748
- timestamp: { type: 'string', description: '消息时间戳' },
749
- },
750
- required: ['bot', 'channel', 'timestamp'],
751
- },
752
- platforms: ['slack'],
753
- scopes: ['group'],
754
- permissionLevel: 'group_admin',
755
- execute: async (args) => {
756
- const { bot: botId, channel, timestamp } = args;
757
- const bot = this.bots.get(botId);
758
- if (!bot)
759
- throw new Error(`Bot ${botId} 不存在`);
760
- const success = await bot.pinMessage(channel, timestamp);
761
- return { success, message: success ? `已置顶消息` : '操作失败' };
762
- },
763
- });
764
- // 添加反应
765
- this.addTool({
766
- name: 'slack_add_reaction',
767
- description: '给 Slack 消息添加表情反应',
768
- parameters: {
769
- type: 'object',
770
- properties: {
771
- bot: { type: 'string', description: 'Bot 名称' },
772
- channel: { type: 'string', description: '频道 ID' },
773
- timestamp: { type: 'string', description: '消息时间戳' },
774
- emoji: { type: 'string', description: '表情名称(不含冒号)' },
775
- },
776
- required: ['bot', 'channel', 'timestamp', 'emoji'],
777
- },
778
- platforms: ['slack'],
779
- scopes: ['group', 'private'],
780
- permissionLevel: 'user',
781
- execute: async (args) => {
782
- const { bot: botId, channel, timestamp, emoji } = args;
783
- const bot = this.bots.get(botId);
784
- if (!bot)
785
- throw new Error(`Bot ${botId} 不存在`);
786
- const success = await bot.addReaction(channel, timestamp, emoji);
787
- return { success, message: success ? `已添加反应 :${emoji}:` : '操作失败' };
788
- },
789
- });
790
- // 移除表情反应
791
- this.addTool({
792
- name: 'slack_remove_reaction',
793
- description: '移除 Slack 消息上的表情反应',
794
- parameters: {
795
- type: 'object',
796
- properties: {
797
- bot: { type: 'string', description: 'Bot 名称' },
798
- channel_id: { type: 'string', description: '频道 ID' },
799
- timestamp: { type: 'string', description: '消息时间戳' },
800
- name: { type: 'string', description: '表情名称(如 thumbsup、heart)' },
801
- },
802
- required: ['bot', 'channel_id', 'timestamp', 'name'],
803
- },
804
- platforms: ['slack'],
805
- scopes: ['group'],
806
- permissionLevel: 'user',
807
- execute: async (args) => {
808
- const { bot: botId, channel_id, timestamp, name } = args;
809
- const bot = this.bots.get(botId);
810
- if (!bot)
811
- throw new Error(`Bot ${botId} 不存在`);
812
- const success = await bot.removeReaction(channel_id, timestamp, name);
813
- return { success, message: success ? `已移除反应 :${name}:` : '操作失败' };
814
- },
815
- });
816
- // 取消置顶消息
817
- this.addTool({
818
- name: 'slack_unpin_message',
819
- description: '取消 Slack 频道中消息的置顶',
820
- parameters: {
821
- type: 'object',
822
- properties: {
823
- bot: { type: 'string', description: 'Bot 名称' },
824
- channel_id: { type: 'string', description: '频道 ID' },
825
- timestamp: { type: 'string', description: '消息时间戳' },
826
- },
827
- required: ['bot', 'channel_id', 'timestamp'],
828
- },
829
- platforms: ['slack'],
830
- scopes: ['group'],
831
- permissionLevel: 'group_admin',
832
- execute: async (args) => {
833
- const { bot: botId, channel_id, timestamp } = args;
834
- const bot = this.bots.get(botId);
835
- if (!bot)
836
- throw new Error(`Bot ${botId} 不存在`);
837
- const success = await bot.unpinMessage(channel_id, timestamp);
838
- return { success, message: success ? '已取消置顶' : '操作失败' };
839
- },
840
- });
841
- // 查询用户信息
842
- this.addTool({
843
- name: 'slack_user_info',
844
- description: '查询 Slack 用户详细信息',
845
- parameters: {
846
- type: 'object',
847
- properties: {
848
- bot: { type: 'string', description: 'Bot 名称' },
849
- user_id: { type: 'string', description: '用户 ID' },
850
- },
851
- required: ['bot', 'user_id'],
852
- },
853
- platforms: ['slack'],
854
- scopes: ['group', 'private'],
855
- permissionLevel: 'user',
856
- execute: async (args) => {
857
- const { bot: botId, user_id } = args;
858
- const bot = this.bots.get(botId);
859
- if (!bot)
860
- throw new Error(`Bot ${botId} 不存在`);
861
- const user = await bot.getUserInfo(user_id);
862
- return {
863
- id: user.id,
864
- name: user.name,
865
- real_name: user.real_name,
866
- display_name: user.profile?.display_name,
867
- email: user.profile?.email,
868
- is_admin: user.is_admin,
869
- is_bot: user.is_bot,
870
- status_text: user.profile?.status_text,
871
- };
872
- },
873
- });
874
- // 设置频道用途
875
- this.addTool({
876
- name: 'slack_set_purpose',
877
- description: '设置 Slack 频道的用途/目的',
878
- parameters: {
879
- type: 'object',
880
- properties: {
881
- bot: { type: 'string', description: 'Bot 名称' },
882
- channel_id: { type: 'string', description: '频道 ID' },
883
- purpose: { type: 'string', description: '频道用途描述' },
884
- },
885
- required: ['bot', 'channel_id', 'purpose'],
886
- },
887
- platforms: ['slack'],
888
- scopes: ['group'],
889
- permissionLevel: 'group_admin',
890
- execute: async (args) => {
891
- const { bot: botId, channel_id, purpose } = args;
892
- const bot = this.bots.get(botId);
893
- if (!bot)
894
- throw new Error(`Bot ${botId} 不存在`);
895
- const success = await bot.setChannelPurpose(channel_id, purpose);
896
- return { success, message: success ? '频道用途已更新' : '操作失败' };
897
- },
898
- });
899
- // 恢复归档频道
900
- this.addTool({
901
- name: 'slack_unarchive',
902
- description: '恢复已归档的 Slack 频道',
903
- parameters: {
904
- type: 'object',
905
- properties: {
906
- bot: { type: 'string', description: 'Bot 名称' },
907
- channel_id: { type: 'string', description: '频道 ID' },
908
- },
909
- required: ['bot', 'channel_id'],
910
- },
911
- platforms: ['slack'],
912
- scopes: ['group'],
913
- permissionLevel: 'group_admin',
914
- execute: async (args) => {
915
- const { bot: botId, channel_id } = args;
916
- const bot = this.bots.get(botId);
917
- if (!bot)
918
- throw new Error(`Bot ${botId} 不存在`);
919
- const success = await bot.unarchiveChannel(channel_id);
920
- return { success, message: success ? '频道已恢复' : '操作失败' };
921
- },
922
- });
923
- plugin.logger.debug('已注册 Slack 平台工作区管理工具');
924
- }
925
- }
926
- // 使用新的 provide() API 注册适配器
10
+ const { provide } = plugin;
927
11
  provide({
928
12
  name: "slack",
929
13
  description: "Slack Bot Adapter",