-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix duplicate handler attachment for new cron jobs
- Loading branch information
1 parent
a379426
commit efed838
Showing
5 changed files
with
116 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Telegraf, TelegramError } from "telegraf"; | ||
import { CustomContext } from "../types/customContext.type"; | ||
import Bull = require("bull"); | ||
import { JobData } from "../types/types"; | ||
|
||
function createJobQueue(bot: Telegraf<CustomContext>) { | ||
// Create queue | ||
const queue = new Bull<JobData>("notify-user-queue"); | ||
|
||
// Job completed event | ||
queue.on("completed", async (job) => { | ||
console.log(`✅ Message sent to ${job.data.chatId}`); | ||
await job.remove(); | ||
}); | ||
|
||
// Consumer | ||
queue.process(async (job) => { | ||
const { chatId, file, captionMsg, fileName } = job.data; | ||
|
||
try { | ||
if (!file || !fileName) { | ||
await bot.telegram.sendMessage(chatId, captionMsg, { | ||
parse_mode: "HTML", | ||
}); | ||
} else { | ||
const fileBuffer = Buffer.from(file as string, "base64"); | ||
await bot.telegram.sendDocument( | ||
chatId, | ||
{ | ||
source: fileBuffer, | ||
filename: fileName, | ||
}, | ||
{ caption: captionMsg, parse_mode: "HTML" } | ||
); | ||
} | ||
} catch (error: any) { | ||
console.log(error); | ||
if (error instanceof TelegramError) { | ||
if (error.code === 429) { | ||
const retryAfter = error.parameters?.retry_after!; | ||
await new Promise((resolve) => | ||
setTimeout(resolve, retryAfter * 1000 + 2000) | ||
); | ||
await job.retry(); | ||
} else if (error.code === 403) { | ||
try { | ||
/* await usersRef.doc(chatId.toString()).delete(); */ | ||
await job.remove(); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return queue; | ||
} | ||
|
||
export default createJobQueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Utility function to get the caption message from the message object | ||
import { Announcement } from "../types/types"; | ||
|
||
function getCaptionMsg(announcement: Announcement) { | ||
const captionMsg = ` | ||
<b>Subject:</b> ${announcement.subject ? announcement.subject : "N/A"} | ||
<b>Date:</b> ${announcement.date ? announcement.date : "N/A"} | ||
<b>Message:</b> ${announcement.message ? announcement.message : "N/A"} | ||
`; | ||
|
||
return captionMsg; | ||
} | ||
|
||
export default getCaptionMsg; |