Skip to content

Commit

Permalink
chore: refactor slack messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Balint Dolla committed Nov 3, 2023
1 parent 46969db commit bf16059
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 111 deletions.
31 changes: 7 additions & 24 deletions packages/core/services/slack/constructAskBirthdayMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { ChatPostMessageArguments } from "@slack/web-api";
import type { ChatReplaceMessageArguments } from "@/types/ChatReplaceMessageArguments";
import { pickBirthdayActionId } from "@/types/SlackInteractionRequest";

import { makeTextBlock, makeTextBlockWithDatepicker } from "./messageItems";

type Arguments = {
user: string;
name: string;
Expand All @@ -15,30 +17,11 @@ const constructBaseAskBirthdayMessage = ({
}: Arguments): Omit<ChatPostMessageArguments, "channel"> => ({
text: "Please share your birthday with us! 🥳",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Hey ${name}! 👋`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: "Please share your birthday with us! 🥳",
},
accessory: {
type: "datepicker",
initial_date: "1995-01-01",
placeholder: {
type: "plain_text",
text: "Select a date",
emoji: true,
},
action_id: pickBirthdayActionId,
},
},
makeTextBlock(`Hey ${name}! 👋`),
makeTextBlockWithDatepicker(
"Please share your birthday with us! 🥳",
pickBirthdayActionId,
),
],
metadata: {
event_type: "askBirthday",
Expand Down
20 changes: 6 additions & 14 deletions packages/core/services/slack/constructAskPresentIdeasMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ChatPostMessageArguments } from "@slack/web-api";

import { makeTextBlock } from "./messageItems";

type Arguments = {
birthdayPerson: string;
user: string;
Expand All @@ -24,19 +26,9 @@ export const constructAskPresentIdeasMessage = ({
},
text: `Hey, <@${user}>! <@${birthdayPerson}>'s birthday is in 2 months. Do you have any present ideas?`,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Hey ${name}! 👋`,
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: `It's <@${birthdayPerson}>'s birthday is in 2 months. Do you have any present ideas?`,
},
},
makeTextBlock(`Hey ${name}! 👋`),
makeTextBlock(
`It's <@${birthdayPerson}>'s birthday is in 2 months. Do you have any present ideas?`,
),
],
}) satisfies ChatPostMessageArguments;
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import type { ChatReplaceMessageArguments } from "@/types/ChatReplaceMessageArguments";

import { makeTextBlock } from "./messageItems";

export const constructBirthdayConfirmedMessage =
(): ChatReplaceMessageArguments => ({
replace_original: true,
text: "Thanks for submitting your birthday! 🎉",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Thanks for submitting your birthday! 🎉`,
},
},
],
blocks: [makeTextBlock(`Thanks for submitting your birthday! 🎉`)],
});
49 changes: 9 additions & 40 deletions packages/core/services/slack/constructConfirmBirthdayMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,21 @@ import {
birthdayIncorrectActionId,
} from "@/types/SlackInteractionRequest";

import { makeActionsBlock, makeTextBlock } from "./messageItems";

export const constructConfirmBirthdayMessage = (
birthday: string,
): ChatReplaceMessageArguments =>
({
replace_original: true,
text: "Confirm your birthday",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Are you sure your birthday is ${birthday}?`,
},
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
emoji: true,
text: "Yes",
},
style: "primary",
action_id: birthdayConfirmActionId,
value: birthday,
},
{
type: "button",
text: {
type: "plain_text",
emoji: true,
text: "No",
},
style: "danger",
action_id: birthdayIncorrectActionId,
value: birthday,
},
],
},
],
metadata: {
event_type: "confirmBirthday",
event_payload: {
makeTextBlock(`Are you sure your birthday is ${birthday}?`),
makeActionsBlock(
birthdayConfirmActionId,
birthdayIncorrectActionId,
birthday,
},
},
"",
),
],
}) satisfies ChatReplaceMessageArguments;
26 changes: 10 additions & 16 deletions packages/core/services/slack/constructIceBreakerQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ChatPostMessageArguments, KnownBlock } from "@slack/web-api";

import { makeTextBlock } from "./messageItems";

type Arguments = {
users: string[];
channel: string;
Expand All @@ -19,25 +21,17 @@ export const constructIceBreakerQuestion = ({
iceBreakerQuestions[Math.floor(Math.random() * iceBreakerQuestions.length)];

const blocks: KnownBlock[] = [
{
type: "section",
text: {
type: "mrkdwn",
text: `${randomIceBreakerQuestion} Post your picks in the thread! 👇`,
},
},
makeTextBlock(
`${randomIceBreakerQuestion} Post your picks in the thread! 👇`,
),
];

if (users.length) {
blocks.push({
type: "section",
text: {
type: "mrkdwn",
text: `Let's see your ones ${users
.map((user) => `<@${user}>`)
.join(", ")}!`,
},
});
blocks.push(
makeTextBlock(
`Let's see your ones ${users.map((user) => `<@${user}>`).join(", ")}!`,
),
);
}

return {
Expand Down
59 changes: 59 additions & 0 deletions packages/core/services/slack/messageItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { ActionsBlock, SectionBlock } from "@slack/web-api";

export const makeTextBlock = (text: string): SectionBlock => ({
type: "section",
text: {
type: "mrkdwn",
text,
},
});

export const makeTextBlockWithDatepicker = (
text: string,
actionId: string,
): SectionBlock => ({
...makeTextBlock(text),
accessory: {
type: "datepicker",
initial_date: "1995-01-01",
placeholder: {
type: "plain_text",
text: "Select a date",
emoji: true,
},
action_id: actionId,
},
});

export const makeActionsBlock = (
yesActionId: string,
noActionId: string,
yesValue: string,
noValue: string,
): ActionsBlock => ({
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
emoji: true,
text: "Yes",
},
style: "primary",
action_id: yesActionId,
value: yesValue,
},
{
type: "button",
text: {
type: "plain_text",
emoji: true,
text: "No",
},
style: "danger",
action_id: noActionId,
value: noValue,
},
],
});
14 changes: 6 additions & 8 deletions tests/utils/generateIceBreakerTestUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ export const generateIceBreakerTestUsers = async (today?: string) => {
start.add(6, "months"), // random day outside the window
];

await Promise.all(
birthdays.map((birthday, i) =>
testDb.insert(users).values({
id: `U${i + 1}`,
teamId: "T1",
birthday: birthday.toDate(),
}),
),
await testDb.insert(users).values(
birthdays.map((birthday, i) => ({
id: `U${i + 1}`,
teamId: "T1",
birthday: birthday.toDate(),
})),
);
};

Expand Down

0 comments on commit bf16059

Please sign in to comment.