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

feat: implement ai chatbot reaction response 🤖 #598

Merged
merged 5 commits into from
Dec 7, 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
2 changes: 1 addition & 1 deletion apps/api/src/routers/slack.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ slackEventRouter.post('/slack/events', async (req: RawBodyRequest, res) => {
return;
}

job('slack.reaction.add', {
job('slack.reaction.added', {
channelId: event.item.channel,
messageId: event.item.ts,
reaction: event.reaction,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/infrastructure/bull/bull.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@ export const SlackBullJob = z.discriminatedUnion('name', [
}),
z.object({
name: z.literal('slack.reaction.add'),
data: SlackReaction.pick({
channelId: true,
messageId: true,
reaction: true,
}),
}),
z.object({
name: z.literal('slack.reaction.added'),
data: SlackReaction.pick({
channelId: true,
messageId: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ErrorWithContext } from '@/shared/errors';
import { retryWithBackoff } from '@/shared/utils/core.utils';
import { getSlackMessage } from '../services/slack-message.service';

export async function addSlackReaction(
data: GetBullJobData<'slack.reaction.add'>
export async function onSlackReactionAdded(
data: GetBullJobData<'slack.reaction.added'>
) {
await ensureMessageExists(data);

Expand Down Expand Up @@ -40,7 +40,9 @@ export async function addSlackReaction(
}
}

async function ensureMessageExists(data: GetBullJobData<'slack.reaction.add'>) {
async function ensureMessageExists(
data: GetBullJobData<'slack.reaction.added'>
) {
let waiting = false;

await retryWithBackoff(
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/modules/slack/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ type AnswerPublicQuestionInput = {

/**
* Answers a question asked in a public Slack message by linking to relevant
* threads in our Slack workspace.
* threads in our Slack workspace. After the answer has been sent, the bot then
* reacts to the original message with the ColorStack logo.
*
* @param input - The message (public question) to answer.
* @returns The result of the answer.
Expand Down Expand Up @@ -416,6 +417,12 @@ export async function answerPublicQuestion({
workspace: 'regular',
});

job('slack.reaction.add', {
channelId,
messageId: threadId,
reaction: 'goldicon',
});

await db.transaction().execute(async (trx) => {
await trx
.updateTable('slackMessages')
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/modules/slack/slack.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { match } from 'ts-pattern';
import { SlackBullJob } from '@/infrastructure/bull/bull.types';
import { registerWorker } from '@/infrastructure/bull/use-cases/register-worker';
import { onSlackUserInvited } from '@/modules/slack/events/slack-user-invited';
import { slack } from '@/modules/slack/instances';
import {
answerChatbotQuestion,
answerPublicQuestion,
Expand All @@ -11,9 +12,9 @@ import {
} from '@/modules/slack/slack';
import { updateBirthdatesFromSlack } from '@/modules/slack/use-cases/update-birthdates-from-slack';
import { onSlackProfilePictureChanged } from './events/slack-profile-picture-changed';
import { onSlackReactionAdded } from './events/slack-reaction-added';
import { onSlackWorkspaceJoined } from './events/slack-workspace-joined';
import { addSlackMessage } from './use-cases/add-slack-message';
import { addSlackReaction } from './use-cases/add-slack-reaction';
import { archiveSlackChannel } from './use-cases/archive-slack-channel';
import { changeSlackMessage } from './use-cases/change-slack-message';
import { createSlackChannel } from './use-cases/create-slack-channel';
Expand Down Expand Up @@ -95,7 +96,14 @@ export const slackWorker = registerWorker(
return result.data;
})
.with({ name: 'slack.reaction.add' }, async ({ data }) => {
return addSlackReaction(data);
return slack.reactions.add({
channel: data.channelId,
name: data.reaction,
timestamp: data.messageId,
});
})
.with({ name: 'slack.reaction.added' }, async ({ data }) => {
return onSlackReactionAdded(data);
})
.with({ name: 'slack.reaction.remove' }, async ({ data }) => {
return removeSlackReaction(data);
Expand Down
Loading