diff --git a/src/cron/notifyUserCron.ts b/src/cron/notifyUserCron.ts index 641f584..04963e1 100644 --- a/src/cron/notifyUserCron.ts +++ b/src/cron/notifyUserCron.ts @@ -9,7 +9,7 @@ import { Announcement, Attachment } from "../types/types"; async function notifyUserCron(db: Firestore, bot: Telegraf) { 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") { @@ -48,6 +48,16 @@ async function notifyUserCron(db: Firestore, bot: Telegraf) { ); for (const announcement of diff) { + const captionMsg = ` + +Subject: ${announcement.subject} + +Date: ${announcement.date} + +Message: ${announcement.message} + +`; + const attachments = announcement.attachments.map( (attachment: Attachment) => ({ name: attachment.name, @@ -65,10 +75,14 @@ async function notifyUserCron(db: Firestore, bot: Telegraf) { 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}:`, diff --git a/src/scenes/announcementWizard.ts b/src/scenes/announcementWizard.ts index 1df78ac..5fb2ce9 100644 --- a/src/scenes/announcementWizard.ts +++ b/src/scenes/announcementWizard.ts @@ -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); @@ -33,32 +34,52 @@ const announcementWizard = new Scenes.WizardScene( ); } 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 = ` + +Subject: ${chosenAnnouncement.subject} + +Date: ${chosenAnnouncement.date} + +Message: ${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) { diff --git a/src/services/fetchAnnouncements.ts b/src/services/fetchAnnouncements.ts index d0c6843..3124097 100644 --- a/src/services/fetchAnnouncements.ts +++ b/src/services/fetchAnnouncements.ts @@ -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, diff --git a/src/types/types.ts b/src/types/types.ts index e3971ef..ae7946c 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -37,6 +37,7 @@ interface Announcement { id: number; subject: string; date: string; + message: string; attachments: Attachment[]; }