Skip to content

Commit

Permalink
extract to function
Browse files Browse the repository at this point in the history
  • Loading branch information
scazan committed Nov 13, 2023
1 parent c90c3db commit ad7276f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
10 changes: 5 additions & 5 deletions integrations/slack/src/handlers/events.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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];

Expand All @@ -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,
});
Expand Down
12 changes: 5 additions & 7 deletions integrations/slack/src/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions integrations/slack/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit ad7276f

Please sign in to comment.