Skip to content

Commit

Permalink
chore: Register Slack as plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjcsmith committed Dec 13, 2024
1 parent 611907c commit 8a18042
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
8 changes: 3 additions & 5 deletions control-plane/src/modules/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 14 additions & 6 deletions control-plane/src/modules/slack/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8a18042

Please sign in to comment.