| 56 | // --------------------------------------------------------------------------- |
| 57 | |
| 58 | export class SlackAdapter implements PlatformAdapter, MessageSender { |
| 59 | readonly name = "slack"; |
| 60 | |
| 61 | private app: App | null = null; |
| 62 | private agent: IPackAgent | null = null; |
| 63 | private readonly options: SlackAdapterOptions; |
| 64 | private botUserId: string | null = null; |
| 65 | private lastThreadByChannel = new Map<string, string>(); |
| 66 | private rootDir = ""; |
| 67 | private ipcBroadcaster: IpcBroadcaster | null = null; |
| 68 | |
| 69 | constructor(options: SlackAdapterOptions) { |
| 70 | this.options = options; |
| 71 | } |
| 72 | |
| 73 | async start(ctx: AdapterContext): Promise<void> { |
| 74 | this.agent = ctx.agent; |
| 75 | this.rootDir = ctx.rootDir; |
| 76 | this.ipcBroadcaster = ctx.ipcBroadcaster ?? null; |
| 77 | |
| 78 | this.app = new App({ |
| 79 | token: this.options.botToken, |
| 80 | appToken: this.options.appToken, |
| 81 | socketMode: true, |
| 82 | ignoreSelf: true, |
| 83 | logLevel: LogLevel.INFO, |
| 84 | }); |
| 85 | |
| 86 | const auth = await this.app.client.auth.test({ |
| 87 | token: this.options.botToken, |
| 88 | }); |
| 89 | this.botUserId = |
| 90 | typeof auth.user_id === "string" ? auth.user_id : null; |
| 91 | |
| 92 | this.registerListeners(this.app); |
| 93 | await this.app.start(); |
| 94 | |
| 95 | const identity = this.botUserId ? `<@${this.botUserId}>` : "Slack bot"; |
| 96 | console.log(`[SlackAdapter] Started as ${identity}`); |
| 97 | } |
| 98 | |
| 99 | async stop(): Promise<void> { |
| 100 | if (this.app) { |
| 101 | await this.app.stop(); |
| 102 | this.app = null; |
| 103 | } |
| 104 | console.log("[SlackAdapter] Stopped"); |
| 105 | } |
| 106 | |
| 107 | // ------------------------------------------------------------------------- |
| 108 | // MessageSender – proactive message sending |
| 109 | // ------------------------------------------------------------------------- |
| 110 | |
| 111 | /** |
| 112 | * Public method: send a message to a specific Slack channel/DM. |
| 113 | * channelId formats: |
| 114 | * - slack-dm-<teamId>-<channelId> |
| 115 | * - slack-thread-<teamId>-<channel>-<threadTs> |
nothing calls this directly
no outgoing calls
no test coverage detected