( slackAdapter: SlackAdapter, githubAdapter: GitHubAdapter, linearAdapter: LinearAdapter )
| 19 | import { createSlackWebhookHandler } from '@/lib/bot/platforms/slack-webhook'; |
| 20 | |
| 21 | function createKiloBot( |
| 22 | slackAdapter: SlackAdapter, |
| 23 | githubAdapter: GitHubAdapter, |
| 24 | linearAdapter: LinearAdapter |
| 25 | ) { |
| 26 | const chatBot = new Chat({ |
| 27 | userName: process.env.NODE_ENV === 'production' ? 'Kilo' : 'Henk', |
| 28 | adapters: { |
| 29 | github: githubAdapter, |
| 30 | slack: slackAdapter, |
| 31 | linear: linearAdapter, |
| 32 | }, |
| 33 | state: createChatState(), |
| 34 | logger: process.env.NODE_ENV === 'production' ? 'info' : 'debug', |
| 35 | }); |
| 36 | |
| 37 | chatBot.webhooks.slack = createSlackWebhookHandler(chatBot, slackAdapter); |
| 38 | chatBot.webhooks.linear = createLinearWebhookHandler(chatBot, linearAdapter); |
| 39 | |
| 40 | chatBot.onNewMention(async function handleIncomingMessage( |
| 41 | thread: Thread, |
| 42 | message: Message |
| 43 | ): Promise<void> { |
| 44 | const botPlatform = botPlatforms.requireByAdapter(thread.adapter); |
| 45 | const identity = await botPlatform.getIdentity({ thread, message }); |
| 46 | const [platformIntegration, kiloUserId] = await Promise.all([ |
| 47 | getPlatformIntegration(identity), |
| 48 | resolveKiloUserId(chatBot.getState(), identity), |
| 49 | ]); |
| 50 | |
| 51 | if (!platformIntegration) { |
| 52 | captureException(new Error('No active platform integration found'), { |
| 53 | extra: { platform: identity.platform, teamId: identity.teamId }, |
| 54 | }); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (!botPlatform.isEnabledForBot(platformIntegration)) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (!(await botPlatform.canHandleMessage({ thread, message, platformIntegration }))) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if (!kiloUserId) { |
| 67 | await botPlatform.promptLinkAccount({ |
| 68 | thread, |
| 69 | message, |
| 70 | identity, |
| 71 | platformIntegration, |
| 72 | state: chatBot.getState(), |
| 73 | }); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | const user = await findUserById(kiloUserId); |
| 78 |
no test coverage detected