From 80c923bf1c59960714bf781296900babc13e7253 Mon Sep 17 00:00:00 2001 From: adithyagenie Date: Sun, 16 Jun 2024 00:52:00 +0530 Subject: [PATCH] Add multiple group broadcast support Signed-off-by: adithyagenie --- README.md | 4 ++-- src/bot/commands/contestRem.ts | 21 +++++++++++++++------ src/index.ts | 5 +++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d850eb9..a75228c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ variables. WEBHOOK_URL="your_webhook_url" WEBHOOK_SECRET="your_webhook_secret" PORT=your_port_number - CHAT_ID="your_chat_id" + CHAT_ID="your_chat_ids seperated by commas" ``` 4. Run the bot: @@ -44,7 +44,7 @@ variables. - **WEBHOOK_URL**: The URL for the webhook (required if using webhook mode). - **WEBHOOK_SECRET**: The secret for the webhook (required if using webhook mode). - **PORT**: The port number to run the webhook server on (required if using webhook mode). -- **CHAT_ID**: The chat ID of the group or person where the bot will send notifications. +- **CHAT_ID**: The chat IDs of the group or person where the bot will send notifications, seperated by commas. ## Usage diff --git a/src/bot/commands/contestRem.ts b/src/bot/commands/contestRem.ts index 949811f..50dfe5b 100644 --- a/src/bot/commands/contestRem.ts +++ b/src/bot/commands/contestRem.ts @@ -1,15 +1,21 @@ import { bot } from "../bot"; export async function reminderContestLC(contestName: string, contestKey: string): Promise { - await bot.api.sendMessage(parseInt(process.env.CHAT_ID as string), - `LeetCode ${contestName} is in ~10 minutes.\n + const chatids = (process.env.CHAT_ID as string).split(",").map(i => parseInt(i)); + for (const id of chatids) { + await bot.api.sendMessage(id, + `LeetCode ${contestName} is in ~10 minutes.\n Attend the contest here: https://leetcode.com/contest/${contestKey}/`); + } } export async function reminderContestCodeChef(): Promise { - await bot.api.sendMessage(parseInt(process.env.CHAT_ID as string), - `CodeChef contest is in ~10 minutes.\n + const chatids = (process.env.CHAT_ID as string).split(",").map(i => parseInt(i)); + for (const id of chatids) { + await bot.api.sendMessage(id, + `CodeChef contest is in ~10 minutes.\n Attend the contest here: https://www.codechef.com/contests/`); + } } export async function reminderContestCF(contestName: string, contestID: number, contestTime: Date) { @@ -17,7 +23,10 @@ export async function reminderContestCF(contestName: string, contestID: number, timeZone: 'Asia/Calcutta', timeStyle: "short" }; - await bot.api.sendMessage(parseInt(process.env.CHAT_ID as string), - `${contestName} at ${new Date(contestTime).toLocaleTimeString('en-US', options)} IST starts in ~10 minutes. + const chatids = (process.env.CHAT_ID as string).split(",").map(i => parseInt(i)); + for (const id of chatids) { + await bot.api.sendMessage(id, + `${contestName} at ${new Date(contestTime).toLocaleTimeString('en-US', options)} IST starts in ~10 minutes. Attend the contest here: https://codeforces.com/contests/${contestID}`); + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 7c1b588..e54626d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,12 @@ import { config } from "dotenv"; + +config(); + import { FastifyInstance } from "fastify"; import { startserver } from "./api/server"; import { botInit } from "./bot/bot"; import { contestScheduler } from "./helpers/scheduler"; -config(); - if (process.env.BOT_TOKEN === undefined || process.env.RUN_METHOD === undefined || (process.env.RUN_METHOD !== "POLLING" && process.env.RUN_METHOD !== "WEBHOOK") ||