From b9f5b010e3a453b452e35de4ddb2a0c794151c1a Mon Sep 17 00:00:00 2001 From: Jk Jensen Date: Thu, 27 Jun 2024 12:56:50 -0700 Subject: [PATCH] [feature] add support for attachments such as from datadog complex messages --- src/listeners/messages/index.ts | 36 +++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/listeners/messages/index.ts b/src/listeners/messages/index.ts index 2f19a84..60d8d9b 100644 --- a/src/listeners/messages/index.ts +++ b/src/listeners/messages/index.ts @@ -1,11 +1,11 @@ -import { type App } from "@slack/bolt"; -import oncallMentionedCallback from "./oncall-mentioned"; +import { type App, KnownEventFromType, subtype } from '@slack/bolt'; +import oncallMentionedCallback from './oncall-mentioned'; import { appMentionedDmHelpCallback, appMentionedDmLsCallback, appMentionedDmVersionCallback, -} from "@src/listeners/messages/app-mentioned-dm"; -import { oncallMap } from "@api/pd"; +} from '@src/listeners/messages/app-mentioned-dm'; +import { oncallMap } from '@api/pd'; // These regexes allow for DM messages to the bot without the mention const VERSION_REGEX = new RegExp(`^version$`); @@ -16,10 +16,34 @@ const register = async (app: App): Promise => { // This regex matches any string that contains a mention of any of the oncall shortnames. // Updating the config requires a restart of the service. const allShortnamesRegex = new RegExp( - `.*@(${Object.keys(oncallMap).join("|")})\\b.*` + `.*@(${Object.keys(oncallMap).join('|')})\\b.*` ); - console.log("**** allShortnamesRegex", allShortnamesRegex); + console.log('**** allShortnamesRegex', allShortnamesRegex); app.message(allShortnamesRegex, oncallMentionedCallback); + app.message( + async ({ + context, + message, + next, + }: { + context: any; + message: KnownEventFromType<'message'>; + next: any; + }) => { + // check for edge case where the "attachments" in the message contains the shortname + if ( + 'attachments' in message && + allShortnamesRegex.test(JSON.stringify(message.attachments)) + ) { + const matches = JSON.stringify(message.attachments).match( + allShortnamesRegex + ); + context.matches = matches; + await next(); + } + }, + oncallMentionedCallback + ); app.message(VERSION_REGEX, appMentionedDmVersionCallback); app.message(LS_REGEX, appMentionedDmLsCallback);