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.
- Loading branch information
1 parent
f924365
commit 44f71ea
Showing
2 changed files
with
44 additions
and
12 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 |
---|---|---|
@@ -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); | ||
} | ||
}; |
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