forked from MadisonReed/oncall-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from after-ephemera/jkj/dm
[dm] add dm functionality
- Loading branch information
Showing
7 changed files
with
109 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |