From 8a18042158ac41370003f0c31faf5285fee487ac Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 13 Dec 2024 14:11:59 +1030 Subject: [PATCH] chore: Register Slack as plugin --- control-plane/src/modules/slack/index.ts | 8 +++----- control-plane/src/modules/slack/receiver.ts | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/control-plane/src/modules/slack/index.ts b/control-plane/src/modules/slack/index.ts index de5e183c..2aad784a 100644 --- a/control-plane/src/modules/slack/index.ts +++ b/control-plane/src/modules/slack/index.ts @@ -11,22 +11,20 @@ export const start = async (fastify: FastifyInstance) => { token: env.SLACK_BOT_TOKEN, receiver: new FastifySlackReceiver({ signingSecret: env.SLACK_SIGNING_SECRET, + path: '/triggers/slack', fastify, }) }); - - app.event('app_mention', async ({ event, client, say }) => { + app.event('app_mention', async ({ event }) => { logger.info("Received app_mention event. Skipping.", event); }); // Event listener for direct messages - app.event('message', async ({ event, client, say }) => { + app.event('message', async ({ event, client }) => { logger.info("Received message event. Responding.", event); try { if (event.channel_type === 'im' && event.subtype !== 'bot_message') { - - await say(`Hi there! I'm Inferable, your friendly AI assistant. How can I help you today?`); // Respond to the direct message await client.chat.postMessage({ channel: event.channel, diff --git a/control-plane/src/modules/slack/receiver.ts b/control-plane/src/modules/slack/receiver.ts index d3970170..bd32cb71 100644 --- a/control-plane/src/modules/slack/receiver.ts +++ b/control-plane/src/modules/slack/receiver.ts @@ -8,8 +8,7 @@ import { Logger, LogLevel, } from '@slack/bolt' -import { FastifyInstance } from 'fastify'; -import { createServer } from 'http'; +import { FastifyInstance, FastifyPluginCallback } from 'fastify'; import { logger } from '../observability/logger'; const slackLogger: Logger = { @@ -18,8 +17,8 @@ const slackLogger: Logger = { info: (message: string) => logger.info(message), warn: (message: string) => logger.warn(message), getLevel: () => LogLevel.INFO, - setLevel: (level: string) => void 0, - setName: (name: string) => void 0, + setLevel: () => void 0, + setName: () => void 0, } export class FastifySlackReceiver implements Receiver { @@ -51,8 +50,17 @@ export class FastifySlackReceiver implements Receiver { return new Promise((resolve, reject) => { try { - // Bind request handler - this.fastify.post(this.path, (request, reply) => this.requestHandler(request, reply)); + // Register a seperate plugin and dissable the content type parsers + const slackPlugin: FastifyPluginCallback = async (instance) => { + const contentTypes = ['application/json', 'application/x-www-form-urlencoded']; + + instance.removeContentTypeParser(contentTypes); + instance.addContentTypeParser(contentTypes, { parseAs: 'string' }, instance.defaultTextParser); + + instance.post('', (request, reply) => this.requestHandler(request, reply)); + }; + + this.fastify.register(slackPlugin, { prefix: this.path }); resolve(void 0); } catch (error) { reject(error);