Skip to content

Commit

Permalink
Merge pull request #11 from after-ephemera/jkj/dm
Browse files Browse the repository at this point in the history
[dm] add dm functionality
  • Loading branch information
after-ephemera authored Mar 14, 2024
2 parents 70f958f + 3abe18b commit 70506f7
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 21 deletions.
18 changes: 18 additions & 0 deletions src/listeners/common/help.ts
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions src/listeners/common/ls.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions src/listeners/common/version.ts
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 8 additions & 18 deletions src/listeners/events/app-mentioned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -13,27 +13,17 @@ const LS_REGEX = new RegExp(`${USER_MENTION_REGEX} ls`);
const appMentionedCallback = async ({
event,
say,
}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"app_mention">): Promise<void> => {
}: AllMiddlewareArgs &
SlackEventMiddlewareArgs<"app_mention">): Promise<void> => {
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);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/listeners/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
events.register(app);
messages.register(app);
await messages.register(app);
};

export default registerListeners;
29 changes: 29 additions & 0 deletions src/listeners/messages/app-mentioned-dm.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
console.log("channel is ", event.channel_type);
handleVersionCommand(event.ts, say);
};

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

export const appMentionedDmLsCallback = async ({
event,
say,
}: AllMiddlewareArgs & SlackEventMiddlewareArgs<"message">): Promise<void> => {
handleLsCommand(event.ts, say);
};
20 changes: 19 additions & 1 deletion src/listeners/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
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<void> => {
// 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(
`.*@(${Object.keys(oncallMap).join("|")})\\b.*`
);
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 };

0 comments on commit 70506f7

Please sign in to comment.