Skip to content

Commit

Permalink
add caption to notification messages
Browse files Browse the repository at this point in the history
  • Loading branch information
devadathanmb committed Oct 31, 2023
1 parent ede0f72 commit a993209
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
24 changes: 19 additions & 5 deletions src/cron/notifyUserCron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Announcement, Attachment } from "../types/types";

async function notifyUserCron(db: Firestore, bot: Telegraf<CustomContext>) {
console.log("Cron job created");
cron.schedule("*/30 * * * *", async () => {
cron.schedule("*/20 * * * *", async () => {
console.log("Running cron job");
readFile("data.json", "utf8", async (err, data) => {
if (err?.code == "ENOENT") {
Expand Down Expand Up @@ -48,6 +48,16 @@ async function notifyUserCron(db: Firestore, bot: Telegraf<CustomContext>) {
);

for (const announcement of diff) {
const captionMsg = `
<b>Subject:</b> ${announcement.subject}
<b>Date:</b> ${announcement.date}
<b>Message:</b> ${announcement.message}
`;

const attachments = announcement.attachments.map(
(attachment: Attachment) => ({
name: attachment.name,
Expand All @@ -65,10 +75,14 @@ async function notifyUserCron(db: Firestore, bot: Telegraf<CustomContext>) {
const chatId = doc.data().chatId;
sendMessagePromises.push(
bot.telegram
.sendDocument(chatId, {
source: fileBuffer,
filename: attachment.name,
})
.sendDocument(
chatId,
{
source: fileBuffer,
filename: attachment.name,
},
{ caption: captionMsg, parse_mode: "HTML" },
)
.catch((err) => {
console.error(
`Error sending message to chatId ${chatId}:`,
Expand Down
47 changes: 34 additions & 13 deletions src/scenes/announcementWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Markup, Scenes } from "telegraf";
import { CustomContext } from "../types/customContext.type";
import fetchAnnouncements from "../services/fetchAnnouncements";
import fetchAttachment from "../services/fetchAttachment";
import { Attachment } from "../types/types";
import { Announcement, Attachment } from "../types/types";
import deleteMessage from "../utils/deleteMessage";
import handleError from "../utils/handleError";
import announcement from "../commands/notifications";

const handleCancelCommand = async (ctx: CustomContext) => {
await deleteMessage(ctx, ctx.scene.session.waitingMsgId);
Expand Down Expand Up @@ -33,32 +34,52 @@ const announcementWizard = new Scenes.WizardScene<CustomContext>(
);
}
try {
const chosenAnnoucement = Number.parseInt(
const chosenAnnouncementId = Number.parseInt(
(ctx.callbackQuery as any)?.data?.split("_")[1],
);
const attachments = ctx.scene.session.announcements
.find((announcement: any) => announcement.id == chosenAnnoucement)
.attachments.map((attachment: any) => ({
const chosenAnnouncement: Announcement =
ctx.scene.session.announcements.find(
(announcement: any) => announcement.id == chosenAnnouncementId,
);

const attachments: Attachment[] = chosenAnnouncement.attachments.map(
(attachment: Attachment) => ({
name: attachment.name,
encryptId: attachment.encryptId,
}));
}),
);

await ctx.deleteMessage(ctx.scene.session.announcementMsgId);
const waitingMsg = await ctx.reply(
"Fetching notification.. Please wait..",
);
ctx.scene.session.waitingMsgId = waitingMsg.message_id;

if (attachments.length == 0) {
await ctx.reply("No attachments found.");
}

attachments.forEach(async (attachment: Attachment) => {
const file = await fetchAttachment(attachment.encryptId);
const fileBuffer = Buffer.from(file, "base64");
await ctx.replyWithDocument({
source: fileBuffer,
filename: attachment.name,
});
const captionMsg = `
<b>Subject:</b> ${chosenAnnouncement.subject}
<b>Date:</b> ${chosenAnnouncement.date}
<b>Message:</b> ${chosenAnnouncement.message}
`;
await ctx.replyWithDocument(
{
source: fileBuffer,
filename: attachment.name,
},
{ caption: captionMsg, parse_mode: "HTML" },
);
});
if (attachments.length == 0) {
await ctx.reply("No attachments found.");
}

await deleteMessage(ctx, ctx.scene.session.waitingMsgId);
return await ctx.scene.leave();
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions src/services/fetchAnnouncements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function fetchAnnouncements(
const relevantData = response.data.content.map((obj: any) => ({
id: obj.id,
subject: obj.subject,
message: obj.message,
date: obj.announcementDate,
attachments: obj.attachmentList.map((attachment: any) => ({
name: attachment.attachmentName,
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface Announcement {
id: number;
subject: string;
date: string;
message: string;
attachments: Attachment[];
}

Expand Down

0 comments on commit a993209

Please sign in to comment.