diff --git a/src/index.js b/src/index.js index cc7fcd7..a262288 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ require ('dotenv').config(); const { Telegraf } = require('telegraf'); const { upsertGroup, setGroupActive, getActiveGroups } = require('./groups-store'); const { sundayMessage } = require('./tasks/weekly-message'); +const { coachesMessage } = require('./tasks/biweekly-message'); const BOT_TOKEN = process.env.BOT_TOKEN; @@ -51,6 +52,16 @@ bot.on('my_chat_member', async (ctx) => { // Initialize the weekly message task sundayMessage(bot); +coachesMessage(bot); + +bot.telegram.setMyCommands([ + { + command: 'addgroup', + description: 'Register this group in the bot.', + } +]).then(() => { + console.log("📋 Command menu updated successfully!"); +}); // Launch the bot bot.launch().then(() => { diff --git a/src/tasks/biweekly-message.js b/src/tasks/biweekly-message.js new file mode 100644 index 0000000..1dc0ade --- /dev/null +++ b/src/tasks/biweekly-message.js @@ -0,0 +1,42 @@ +const cron = require("node-cron"); +function coachesMessage(bot) { + const timezone = process.env.TIMEZONE || "Asia/Damascus"; + const coachesGroupId = process.env.COACHED_GROUP_ID; + + cron.schedule('0 19 * * 0', async () => { + if(!coachesGroupId){ + console.error( + "❌ [Biweekly Message]: No BIWEEKLY_GROUP_ID found in .env file.", + ); + return; + } + + const epoch = new Date('2026-07-05'); + const today = new Date(); + const weeksSinceEpoch = Math.floor((today - epoch) / (7 * 24 * 60 * 60 * 1000)); + + if(weeksSinceEpoch % 2 !== 0){ + return; + } + console.log( + `⏳ [Biweekly Message]: Preparing to send to specific group ${coachesGroupId}...` + ); + const messageText = + "This is your special bi-weekly message!\n\nIt only appears once every 14 days."; + try{ + await bot.teleram.sendMessage(coachesGroupId, messageText); + conseole.log("✅ [Biweekly Message]: Successfully sent!"); + } + catch (error){ + console.error( + `❌ [Biweekly Message]: Failed to send to group ${coachesGroupId}:`, + error.description || error.message, + ); + } + }, { + timezone: timezone, + }); + console.log("📅 Bi-weekly message task scheduled successfully."); +} + +module.export = { coachesMessage }; \ No newline at end of file diff --git a/src/tasks/weekly-message.js b/src/tasks/weekly-message.js index 5712d21..779510b 100644 --- a/src/tasks/weekly-message.js +++ b/src/tasks/weekly-message.js @@ -4,43 +4,49 @@ const { getActiveGroups, setGroupActive } = require("../groups-store"); function sundayMessage(bot) { const timezone = process.env.TIMEZONE || "Asia/Damascus"; - cron.schedule("* * * * *", async () => { + cron.schedule( + "0 19 * * 0", + async () => { const activeGroups = getActiveGroups(); - if(activeGroups.length === 0){ - console.log("⏳ [Weekly Message]: No active groups found to send messages to."); - return; + if (activeGroups.length === 0) { + console.log( + "⏳ [Weekly Message]: No active groups found to send messages to.", + ); + return; } console.log( `⏳ [Weekly Message]: Preparing to send messages to ${activeGroups.length} group(s)...`, ); -const messageText = - "تذكير أسبوعي 📅\nيرجى إتمام تعبئة استمارة المتابعة الأسبوعية عبر الرابط التالي:\nhttps://docs.google.com/forms/d/e/1FAIpQLSflv4lrXW3-ZEv3P3hegpwKXvXjXeIwQTNUJ8vjhafJdPGkyA/viewform\n\nيعطيكم العافية جميعاً!"; + const messageText = + "تذكير أسبوعي 📅\nيرجى إتمام تعبئة استمارة المتابعة الأسبوعية عبر الرابط التالي:\nhttps://docs.google.com/forms/d/e/1FAIpQLSflv4lrXW3-ZEv3P3hegpwKXvXjXeIwQTNUJ8vjhafJdPGkyA/viewform\n\nيعطيكم العافية جميعاً!"; - for(const group of activeGroups){ - try{ - await bot.telegram.sendMessage(group.id, messageText); - console.log( - `✅ [Weekly Message]: Successfully sent to group "${group.title}"`, - ); - }catch(error){ - console.error( - `❌ [Weekly Message]: Failed to send to "${group.title}" (${group.id}):`, - error.description || error.message, - ); - const errorCode = error.response && error.response.error_code; - if(errorCode === 403 || errorCode === 400){ - setGroupActive(group.id, false); - console.log( - `🚫 Auto-disabled group "${group.title}" due to permission error.`, - ); - } + for (const group of activeGroups) { + try { + await bot.telegram.sendMessage(group.id, messageText); + console.log( + `✅ [Weekly Message]: Successfully sent to group "${group.title}"`, + ); + } catch (error) { + console.error( + `❌ [Weekly Message]: Failed to send to "${group.title}" (${group.id}):`, + error.description || error.message, + ); + const errorCode = error.response && error.response.error_code; + if (errorCode === 403 || errorCode === 400) { + setGroupActive(group.id, false); + console.log( + `🚫 Auto-disabled group "${group.title}" due to permission error.`, + ); } + } } - }, { - timezone: timezone - }); + }, + { + timezone: timezone, + }, + ); console.log("📅 Weekly message task scheduled successfully."); }