Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dm] add dm functionality #11

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";

Check failure on line 1 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'version' is defined but never used

Check failure on line 1 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'packageName' is defined but never used

Check failure on line 1 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'version' is defined but never used

Check failure on line 1 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'packageName' is defined but never used


Check failure on line 3 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

More than 1 blank line not allowed

Check failure on line 3 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

More than 1 blank line not allowed
const handleHelpCommand = async (
threadTs: string,
say:Function,

Check failure on line 6 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected a space after the ':'

Check failure on line 6 in src/listeners/common/help.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected a space after the ':'
) => {
// 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";


Check failure on line 4 in src/listeners/common/ls.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

More than 1 blank line not allowed

Check failure on line 4 in src/listeners/common/ls.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

More than 1 blank line not allowed
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 };
Loading