diff --git a/integrations/slack/src/handlers/events.ts b/integrations/slack/src/handlers/events.ts index 324657745..872639129 100644 --- a/integrations/slack/src/handlers/events.ts +++ b/integrations/slack/src/handlers/events.ts @@ -1,7 +1,7 @@ import { FetchEventCallback } from '@gitbook/runtime'; import { SlackRuntimeContext } from '../configuration'; -import { parseEventPayload } from '../utils'; +import { isAllowedToRespond, parseEventPayload } from '../utils'; /** * Handle an event from Slack. @@ -16,7 +16,7 @@ export function createSlackEventsHandler( const eventPayload = await parseEventPayload(request); // url_verification doesn't have an event object - const { type, bot_id } = eventPayload.event ?? eventPayload; + const { type } = eventPayload.event ?? eventPayload; const handler = handlers[type]; @@ -31,11 +31,11 @@ export function createSlackEventsHandler( }); } - const isExternalChannel = eventPayload.is_ext_shared_channel; - // check for bot_id so that the bot doesn't trigger itself // check whether this was triggered from an external channel - if (bot_id || isExternalChannel) { + + // if (bot_id || isExternalChannel) { + if (!isAllowedToRespond(eventPayload)) { return new Response(null, { status: 200, }); diff --git a/integrations/slack/src/handlers/handlers.ts b/integrations/slack/src/handlers/handlers.ts index 7b55ad819..33c918081 100644 --- a/integrations/slack/src/handlers/handlers.ts +++ b/integrations/slack/src/handlers/handlers.ts @@ -3,7 +3,7 @@ import { Logger } from '@gitbook/runtime'; import type { SlashEvent } from './commands'; import { notifyOnlySupportedThreads, queryLens, saveThread } from '../actions'; import { SlackRuntimeContext } from '../configuration'; -import { isSaveThreadMessage, stripBotName } from '../utils'; +import { isAllowedToRespond, isSaveThreadMessage, stripBotName } from '../utils'; const logger = Logger('slack:api'); @@ -39,11 +39,10 @@ export async function queryLensSlashHandler(slashEvent: SlashEvent, context: Sla */ export async function messageEventHandler(eventPayload: any, context: SlackRuntimeContext) { // pull out required params from the event for queryLens - const { type, text, bot_id, thread_ts, channel, user, team } = eventPayload.event; - const isExternalChannel = eventPayload.is_ext_shared_channel; + const { type, text, thread_ts, channel, user, team } = eventPayload.event; // check for bot_id so that the bot doesn't trigger itself - if (['message', 'app_mention'].includes(type) && !bot_id && !isExternalChannel) { + if (['message', 'app_mention'].includes(type) && isAllowedToRespond(eventPayload)) { // strip out the bot-name in the mention and account for user mentions within the query // @ts-ignore const parsedQuery = stripBotName(text, eventPayload.authorizations[0]?.user_id); @@ -73,11 +72,10 @@ export async function messageEventHandler(eventPayload: any, context: SlackRunti */ export async function appMentionEventHandler(eventPayload: any, context: SlackRuntimeContext) { // pull out required params from the slashEvent for queryLens - const { type, text, bot_id, thread_ts, channel, user, team } = eventPayload.event; - const isExternalChannel = eventPayload.is_ext_shared_channel; + const { type, text, thread_ts, channel, user, team } = eventPayload.event; // check for bot_id so that the bot doesn't trigger itself - if (['message', 'app_mention'].includes(type) && !bot_id && !isExternalChannel) { + if (['message', 'app_mention'].includes(type) && isAllowedToRespond(eventPayload)) { // strip out the bot-name in the mention and account for user mentions within the query // @ts-ignore const parsedMessage = stripBotName(text, eventPayload.authorizations[0]?.user_id); diff --git a/integrations/slack/src/utils.ts b/integrations/slack/src/utils.ts index 479bebca4..38fb7adf4 100644 --- a/integrations/slack/src/utils.ts +++ b/integrations/slack/src/utils.ts @@ -122,3 +122,11 @@ export function isSaveThreadMessage(message: string) { return false; } + +// Checks whether we should respond to a slack event +export function isAllowedToRespond(eventPayload: any) { + const { bot_id } = eventPayload.event; + const isExternalChannel = eventPayload.is_ext_shared_channel; + + return !bot_id && !isExternalChannel; +}