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

[recnet-api] Update slack message template #359

Merged
merged 8 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions apps/recnet-api/src/modules/slack/slack.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SlackBlockDto } from "slack-block-builder";

export type SendSlackResult = {
success: boolean;
skip?: boolean;
userId?: string;
};

export type SlackMessageBlocks = Readonly<SlackBlockDto>[];
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
import groupBy from "lodash.groupby";
import { BlockCollection, Md, Blocks, BlockBuilder } from "slack-block-builder";

import { WeeklyDigestContent } from "@recnet-api/modules/subscription/subscription.type";

import { formatDate } from "@recnet/recnet-date-fns";

import type { SlackMessageBlocks } from "../slack.type";

export const weeklyDigestSlackTemplate = (
cutoff: Date,
content: WeeklyDigestContent,
nodeEnv: string
): string => {
const subject = `${nodeEnv !== "production" && "[DEV] "}📬 Your Weekly Digest for ${formatDate(cutoff)}`;
const unusedInviteCodes = `You have ${content.numUnusedInviteCodes} unused invite codes! Share the love ❤️`;
const latestAnnouncement = content.latestAnnouncement
? `📢 ${content.latestAnnouncement.title} \n ${content.latestAnnouncement.content}`
: "";
const recsUrls = content.recs.map(
(rec) => `[${rec.article.title}](https://recnet.io/rec/${rec.id})`
): SlackMessageBlocks => {
const { recs, numUnusedInviteCodes, latestAnnouncement } = content;

const recsGroupByTitle = groupBy(recs, (rec) => {
const titleLowercase = rec.article.title.toLowerCase();
const words = titleLowercase.split(" ").filter((w) => w.length > 0);
return words.join("");
});
const recSection = Object.values(recsGroupByTitle).map((recs) => {
const article = recs[0].article;
return [
Blocks.Section({
text: `${Md.bold(Md.link(article.link, article.title))}\n${Md.italic(article.author)} - ${article.year}`,
}),
...recs.map((rec) =>
Blocks.Section({
text: `${Md.link(`https://recnet.io/${rec.user.handle}`, rec.user.displayName)}${rec.isSelfRec ? Md.italic("(Self-Rec)") : ""}: ${rec.description} (${Md.link(`https://recnet.io/rec/${rec.id}`, "view")})`,
})
),
Blocks.Divider(),
];
});

const footer: BlockBuilder[] = [];
if (numUnusedInviteCodes > 0) {
footer.push(
Blocks.Section({
text: `❤️ You have ${Md.bold(`${numUnusedInviteCodes}`)} unused invite codes. Share the love!`,
})
);
}
if (latestAnnouncement) {
footer.push(
Blocks.Section({
text: `📢 Announcement - ${latestAnnouncement.title}: ${latestAnnouncement.content}`,
})
);
}

return BlockCollection(
// headers: number of rec
Blocks.Header({
text: `${nodeEnv !== "production" && "[DEV] "}📬 Your Weekly Digest for ${formatDate(cutoff)}`,
}),
Blocks.Section({
text: `You have ${Md.bold(`${recs.length}`)} recommendations this week!`,
}),
Blocks.Section({
text: "Check out these rec'd paper for you from your network!",
}),
Blocks.Divider(),
...recSection.flat(),
...footer,
Blocks.Section({
text: `👀 Any interesting read this week? ${Md.link("https://recnet.io", "Share with your network!")}`,
})
);
return `${subject}\nYou have ${content.recs.length} recommendations this week!\nCheck out these rec'd paper for you from your network!\n${unusedInviteCodes}\n${latestAnnouncement}\n${recsUrls.join("\n")} \n\nAny interesting read this week? 👀\nShare with your network: https://recnet.io/`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SLACK_RETRY_DURATION_MS,
SLACK_RETRY_LIMIT,
} from "../slack.const";
import { SendSlackResult } from "../slack.type";
import { SendSlackResult, SlackMessageBlocks } from "../slack.type";

@Injectable()
export class SlackTransporter {
Expand All @@ -31,7 +31,7 @@ export class SlackTransporter {

public async sendDirectMessage(
user: DbUser,
message: string
message: SlackMessageBlocks
): Promise<SendSlackResult> {
if (
this.appConfig.nodeEnv !== "production" &&
Expand Down Expand Up @@ -82,7 +82,7 @@ export class SlackTransporter {

private async postDirectMessage(
userSlackId: string,
message: string
message: SlackMessageBlocks
): Promise<void> {
// Open a direct message conversation
const conversationResp = await this.client.conversations.open({
Expand All @@ -97,10 +97,12 @@ export class SlackTransporter {
);
}

const notificationText = `📬 Your RecNet weekly digest has arrived!`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This SlackTransporter class is designed to send messages for general purposes. I suggest putting this text into the template to create a better logic boundary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I forget to extract this part.

// Send the message
await this.client.chat.postMessage({
channel: conversationId,
text: message,
text: notificationText,
blocks: message,
});
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"server-only": "^0.0.1",
"slack-block-builder": "^2.8.0",
"sonner": "^1.4.32",
"tailwind-merge": "^2.2.1",
"tailwindcss-radix": "^3.0.3",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading