Skip to content

Commit

Permalink
[dm] improve dms
Browse files Browse the repository at this point in the history
  • Loading branch information
after-ephemera committed Mar 14, 2024
1 parent f924365 commit 44f71ea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
44 changes: 40 additions & 4 deletions src/listeners/messages/app-mentioned-dm.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
import {
KnownEventFromType,
type AllMiddlewareArgs,
type SlackEventMiddlewareArgs,
GenericMessageEvent,
MessageChangedEvent,
} from "@slack/bolt";
import handleVersionCommand from "@srclisteners/common/version";
import handleHelpCommand from "@srclisteners/common/help";
import handleLsCommand from "@srclisteners/common/ls";

const USER_MENTION_REGEX = "<@U[A-Z0-9]{8,10}>.*";

const shouldHandleCommand = (
channelType: string,
messageText: string
): boolean => {
if (channelType == "im") {
// always handle direct messages because the bot only sees its own DMs
return true;
}
// outside of DMs ignore
return false;
};

const getMessageFrom = (messageEvent: KnownEventFromType<"message">) => {
// we have to handle the MessageChangedEvent case so we need to do some type
// hackery to make sure we get the message text out.
return (
(messageEvent as GenericMessageEvent)?.text ||
((messageEvent as MessageChangedEvent)?.message as GenericMessageEvent)
?.text ||
""
);
};

export const appMentionedDmVersionCallback = async ({
message,
event,
say,
}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise<void> => {
console.log("channel is ", event.channel_type);
handleVersionCommand(event.ts, say);
if (shouldHandleCommand(event.channel_type, getMessageFrom(message))) {
handleVersionCommand(event.ts, say);
}
};

export const appMentionedDmHelpCallback = async ({
message,
event,
say,
}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise<void> => {
handleHelpCommand(event.ts, say);
if (shouldHandleCommand(event.channel_type, getMessageFrom(message))) {
handleHelpCommand(event.ts, say);
}
};

export const appMentionedDmLsCallback = async ({
message,
event,
say,
}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise<void> => {
handleLsCommand(event.ts, say);
if (shouldHandleCommand(event.channel_type, getMessageFrom(message))) {
handleLsCommand(event.ts, say);
}
};
12 changes: 4 additions & 8 deletions src/listeners/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import {
} from "@src/listeners/messages/app-mentioned-dm";
import { oncallMap } from "@api/pd";

const USER_MENTION_REGEX = "<@U[A-Z0-9]{8,10}>";
// These regexes optionally include the bot user mention, allowing for
// DM messages to the bot without the mention
const VERSION_REGEX = new RegExp(`(${USER_MENTION_REGEX} )?version`);
const LS_REGEX = new RegExp(`(${USER_MENTION_REGEX} )?ls`);
const HELP_REGEX = new RegExp(`(${USER_MENTION_REGEX} )?help`);
// These regexes allow for DM messages to the bot without the mention
const VERSION_REGEX = new RegExp(`^version$`);
const LS_REGEX = new RegExp(`^ls$`);
const HELP_REGEX = new RegExp(`^help$`);

const register = async (app: App): Promise<void> => {
// This regex matches any string that contains a mention of any of the oncall shortnames.
Expand All @@ -23,8 +21,6 @@ const register = async (app: App): Promise<void> => {
console.log("**** allShortnamesRegex", allShortnamesRegex);
app.message(allShortnamesRegex, oncallMentionedCallback);

const result = await app.client.auth.test();
let bot_id = result.user_id;
app.message(VERSION_REGEX, appMentionedDmVersionCallback);
app.message(LS_REGEX, appMentionedDmLsCallback);
app.message(HELP_REGEX, appMentionedDmHelpCallback);
Expand Down

0 comments on commit 44f71ea

Please sign in to comment.