Skip to content

Commit ad7276f

Browse files
committed
extract to function
1 parent c90c3db commit ad7276f

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

integrations/slack/src/handlers/events.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FetchEventCallback } from '@gitbook/runtime';
22

33
import { SlackRuntimeContext } from '../configuration';
4-
import { parseEventPayload } from '../utils';
4+
import { isAllowedToRespond, parseEventPayload } from '../utils';
55

66
/**
77
* Handle an event from Slack.
@@ -16,7 +16,7 @@ export function createSlackEventsHandler(
1616
const eventPayload = await parseEventPayload(request);
1717

1818
// url_verification doesn't have an event object
19-
const { type, bot_id } = eventPayload.event ?? eventPayload;
19+
const { type } = eventPayload.event ?? eventPayload;
2020

2121
const handler = handlers[type];
2222

@@ -31,11 +31,11 @@ export function createSlackEventsHandler(
3131
});
3232
}
3333

34-
const isExternalChannel = eventPayload.is_ext_shared_channel;
35-
3634
// check for bot_id so that the bot doesn't trigger itself
3735
// check whether this was triggered from an external channel
38-
if (bot_id || isExternalChannel) {
36+
37+
// if (bot_id || isExternalChannel) {
38+
if (!isAllowedToRespond(eventPayload)) {
3939
return new Response(null, {
4040
status: 200,
4141
});

integrations/slack/src/handlers/handlers.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Logger } from '@gitbook/runtime';
33
import type { SlashEvent } from './commands';
44
import { notifyOnlySupportedThreads, queryLens, saveThread } from '../actions';
55
import { SlackRuntimeContext } from '../configuration';
6-
import { isSaveThreadMessage, stripBotName } from '../utils';
6+
import { isAllowedToRespond, isSaveThreadMessage, stripBotName } from '../utils';
77

88
const logger = Logger('slack:api');
99

@@ -39,11 +39,10 @@ export async function queryLensSlashHandler(slashEvent: SlashEvent, context: Sla
3939
*/
4040
export async function messageEventHandler(eventPayload: any, context: SlackRuntimeContext) {
4141
// pull out required params from the event for queryLens
42-
const { type, text, bot_id, thread_ts, channel, user, team } = eventPayload.event;
43-
const isExternalChannel = eventPayload.is_ext_shared_channel;
42+
const { type, text, thread_ts, channel, user, team } = eventPayload.event;
4443

4544
// check for bot_id so that the bot doesn't trigger itself
46-
if (['message', 'app_mention'].includes(type) && !bot_id && !isExternalChannel) {
45+
if (['message', 'app_mention'].includes(type) && isAllowedToRespond(eventPayload)) {
4746
// strip out the bot-name in the mention and account for user mentions within the query
4847
// @ts-ignore
4948
const parsedQuery = stripBotName(text, eventPayload.authorizations[0]?.user_id);
@@ -73,11 +72,10 @@ export async function messageEventHandler(eventPayload: any, context: SlackRunti
7372
*/
7473
export async function appMentionEventHandler(eventPayload: any, context: SlackRuntimeContext) {
7574
// pull out required params from the slashEvent for queryLens
76-
const { type, text, bot_id, thread_ts, channel, user, team } = eventPayload.event;
77-
const isExternalChannel = eventPayload.is_ext_shared_channel;
75+
const { type, text, thread_ts, channel, user, team } = eventPayload.event;
7876

7977
// check for bot_id so that the bot doesn't trigger itself
80-
if (['message', 'app_mention'].includes(type) && !bot_id && !isExternalChannel) {
78+
if (['message', 'app_mention'].includes(type) && isAllowedToRespond(eventPayload)) {
8179
// strip out the bot-name in the mention and account for user mentions within the query
8280
// @ts-ignore
8381
const parsedMessage = stripBotName(text, eventPayload.authorizations[0]?.user_id);

integrations/slack/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,11 @@ export function isSaveThreadMessage(message: string) {
122122

123123
return false;
124124
}
125+
126+
// Checks whether we should respond to a slack event
127+
export function isAllowedToRespond(eventPayload: any) {
128+
const { bot_id } = eventPayload.event;
129+
const isExternalChannel = eventPayload.is_ext_shared_channel;
130+
131+
return !bot_id && !isExternalChannel;
132+
}

0 commit comments

Comments
 (0)