diff --git a/src/listeners/common/help.ts b/src/listeners/common/help.ts new file mode 100644 index 0000000..1ad365b --- /dev/null +++ b/src/listeners/common/help.ts @@ -0,0 +1,18 @@ +import { version, name as packageName } from "package.json"; + + +const handleHelpCommand = async ( + threadTs: string, + say:Function, +) => { + // we have to handle the MessageChangedEvent case so we need to do some type + // hackery to make sure we get the message text out. + await say({ + text: `You can @ me with the following commands: + - version + - ls`, + thread_ts: threadTs, + }); +}; + +export default handleHelpCommand; diff --git a/src/listeners/common/ls.ts b/src/listeners/common/ls.ts new file mode 100644 index 0000000..1517eff --- /dev/null +++ b/src/listeners/common/ls.ts @@ -0,0 +1,17 @@ +import { getOncallSlackMembers } from "@api/oncall"; +import { makeOncallMappingMessage } from "@api/pd"; + + +const handleLsCommand = async ( + threadTs: string, + say:Function, +) => { + const slackMembers = await getOncallSlackMembers(); + const usersMessage = makeOncallMappingMessage(slackMembers); + await say({ + text: `Current oncall listing:\n ${usersMessage}`, + thread_ts: threadTs, + }); +}; + +export default handleLsCommand; diff --git a/src/listeners/common/version.ts b/src/listeners/common/version.ts new file mode 100644 index 0000000..1125a41 --- /dev/null +++ b/src/listeners/common/version.ts @@ -0,0 +1,16 @@ +import { version, name as packageName } from "package.json"; + + +const handleVersionCommand = async ( + threadTs: string, + say:Function, +) => { + // we have to handle the MessageChangedEvent case so we need to do some type + // hackery to make sure we get the message text out. + await say({ + text: `I am *${packageName}* and running version ${version}.`, + thread_ts: threadTs, + }); +}; + +export default handleVersionCommand; diff --git a/src/listeners/events/app-mentioned.ts b/src/listeners/events/app-mentioned.ts index 3df3ab8..2e666da 100644 --- a/src/listeners/events/app-mentioned.ts +++ b/src/listeners/events/app-mentioned.ts @@ -2,9 +2,9 @@ import { type AllMiddlewareArgs, type SlackEventMiddlewareArgs, } from "@slack/bolt"; -import { getOncallSlackMembers } from "@api/oncall"; -import { version, name as packageName } from "package.json"; -import { makeOncallMappingMessage } from "@api/pd"; +import handleVersionCommand from "@srclisteners/common/version"; +import handleLsCommand from "@srclisteners/common/ls"; +import handleHelpCommand from "@srclisteners/common/help"; const USER_MENTION_REGEX = "^<@U[A-Z0-9]{8,10}>"; const VERSION_REGEX = new RegExp(`${USER_MENTION_REGEX} version`); @@ -13,27 +13,17 @@ const LS_REGEX = new RegExp(`${USER_MENTION_REGEX} ls`); const appMentionedCallback = async ({ event, say, -}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"app_mention">): Promise => { +}: AllMiddlewareArgs & + SlackEventMiddlewareArgs<"app_mention">): Promise => { console.log(`**** bot mentioned ${event.text}`); const threadTs = event.ts; if (event.text.match(VERSION_REGEX) !== null) { - await say({ - text: `I am *${packageName}* and running version ${version}.`, - thread_ts: threadTs, - }); + await handleVersionCommand(threadTs, say); } else if (event.text.match(LS_REGEX) !== null) { - const slackMembers = await getOncallSlackMembers(); - const usersMessage = makeOncallMappingMessage(slackMembers); - await say({ - text: `Current oncall listing:\n ${usersMessage}`, - thread_ts: threadTs, - }); + await handleLsCommand(threadTs, say); } else { // list available commands - await say({ - text: "You can @ me with the following commands:\n- version\n- ls", - thread_ts: threadTs, - }); + handleHelpCommand(threadTs, say); } }; diff --git a/src/listeners/index.ts b/src/listeners/index.ts index b22f103..4ca9f13 100644 --- a/src/listeners/index.ts +++ b/src/listeners/index.ts @@ -2,9 +2,9 @@ import { type App } from "@slack/bolt"; import events from "./events/index"; import messages from "./messages/index"; -const registerListeners = (app: App): void => { +const registerListeners = async (app: App): Promise => { events.register(app); - messages.register(app); + await messages.register(app); }; export default registerListeners; diff --git a/src/listeners/messages/app-mentioned-dm.ts b/src/listeners/messages/app-mentioned-dm.ts new file mode 100644 index 0000000..e33faf0 --- /dev/null +++ b/src/listeners/messages/app-mentioned-dm.ts @@ -0,0 +1,29 @@ +import { + type AllMiddlewareArgs, + type SlackEventMiddlewareArgs, +} from "@slack/bolt"; +import handleVersionCommand from "@srclisteners/common/version"; +import handleHelpCommand from "@srclisteners/common/help"; +import handleLsCommand from "@srclisteners/common/ls"; + +export const appMentionedDmVersionCallback = async ({ + event, + say, +}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise => { + console.log("channel is ", event.channel_type); + handleVersionCommand(event.ts, say); +}; + +export const appMentionedDmHelpCallback = async ({ + event, + say, +}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise => { + handleHelpCommand(event.ts, say); +}; + +export const appMentionedDmLsCallback = async ({ + event, + say, +}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise => { + handleLsCommand(event.ts, say); +}; diff --git a/src/listeners/messages/index.ts b/src/listeners/messages/index.ts index 76e9eec..8144f75 100644 --- a/src/listeners/messages/index.ts +++ b/src/listeners/messages/index.ts @@ -1,8 +1,20 @@ import { type App } from "@slack/bolt"; import oncallMentionedCallback from "./oncall-mentioned"; +import { + appMentionedDmHelpCallback, + appMentionedDmLsCallback, + appMentionedDmVersionCallback, +} from "@src/listeners/messages/app-mentioned-dm"; import { oncallMap } from "@api/pd"; -const register = (app: App): void => { +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`); + +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( @@ -10,6 +22,12 @@ const register = (app: App): 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); }; export default { register };