-
Notifications
You must be signed in to change notification settings - Fork 0
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
526af7a
commit 4788b13
Showing
5 changed files
with
127 additions
and
120 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,36 @@ | ||
import emojis from "../helpers/files/emojis.json"; | ||
|
||
export function getEmojisToReactWith(text: string): Array<string> { | ||
const lowerCaseText = text.toLowerCase(); | ||
|
||
const keywords = Object.values(emojis).flat(); | ||
|
||
// search for each keyword in the text | ||
const emojisToReactWith = findMatchingEmojiKeywords( | ||
keywords, | ||
lowerCaseText, | ||
emojis | ||
); | ||
|
||
return Array.from(new Set(emojisToReactWith)); | ||
} | ||
|
||
function findMatchingEmojiKeywords( | ||
keywords: string[], | ||
lowerCaseText: string, | ||
emojis: Record<string, string[]> | ||
): Array<string> { | ||
const matchingEmojiKeywords: Array<string> = []; | ||
|
||
for (const keyword of keywords) { | ||
//this is too deep too | ||
if (lowerCaseText.includes(keyword)) { | ||
matchingEmojiKeywords.push( | ||
Object.keys(emojis).find((key) => | ||
emojis[key].includes(keyword) | ||
)! | ||
); | ||
} | ||
} | ||
return matchingEmojiKeywords; | ||
} |
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,16 @@ | ||
import { SlackEvent } from "../helpers/interfaces"; | ||
import { SlackWebClient } from "../helpers/types"; | ||
import { strategies } from "./slackEventStrategies"; | ||
|
||
export async function handleSlackMessageEvent( | ||
slackWebClient: SlackWebClient, | ||
slackEvent: SlackEvent | ||
): Promise<void> { | ||
const strategyName: string = slackEvent.subtype ?? slackEvent.type; | ||
let strategy = strategies[strategyName]; | ||
if (strategy) { | ||
await strategy.handle(slackWebClient, slackEvent); | ||
return; | ||
} | ||
console.log(`No strategy found for event type ${slackEvent.type}`); | ||
} |
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,50 @@ | ||
import { | ||
SlackChannelJoinEvent, | ||
SlackEvent, | ||
SlackMessageChangedEvent, | ||
SlackMessageEvent, | ||
} from "../helpers/interfaces"; | ||
import { SlackWebClient } from "../helpers/types"; | ||
import { handleSlackJoinEvent, reactToSlackPost } from "./slackInteraction"; | ||
|
||
interface SlackEventStrategy { | ||
handle(slackWebClient: SlackWebClient, slackEvent: SlackEvent): void; | ||
} | ||
|
||
class ChannelJoinStrategy implements SlackEventStrategy { | ||
async handle( | ||
slackWebClient: SlackWebClient, | ||
slackEvent: SlackChannelJoinEvent | ||
) { | ||
const userId = slackEvent.user; | ||
const slackChannel = slackEvent.channel; | ||
await handleSlackJoinEvent(slackWebClient, slackChannel, userId); | ||
} | ||
} | ||
|
||
class MessageStrategy implements SlackEventStrategy { | ||
async handle( | ||
slackWebClient: SlackWebClient, | ||
slackEvent: SlackMessageEvent | ||
): Promise<void> { | ||
const { text, channel, ts } = slackEvent; | ||
await reactToSlackPost(slackWebClient, text, channel, ts); | ||
} | ||
} | ||
|
||
export class MessageChangedStrategy implements SlackEventStrategy { | ||
async handle( | ||
slackWebClient: SlackWebClient, | ||
slackEvent: SlackMessageChangedEvent | ||
): Promise<void> { | ||
const { channel } = slackEvent; | ||
const { text, ts } = slackEvent.message; | ||
await reactToSlackPost(slackWebClient, text, channel, ts); | ||
} | ||
} | ||
|
||
export const strategies: Record<string, SlackEventStrategy> = { | ||
channel_join: new ChannelJoinStrategy(), | ||
message: new MessageStrategy(), | ||
message_changed: new MessageChangedStrategy(), | ||
}; |
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