From 702a42952b007f6dd68c3d678e64b046c3891e28 Mon Sep 17 00:00:00 2001 From: Siddharth Sharma <75090218+PanditSiddharth@users.noreply.github.com> Date: Sat, 25 Nov 2023 15:40:46 +0530 Subject: [PATCH 1/3] Create runMultipleBotsByPollingMode Run many bots in one server --- examples/runMultipleBotsByPollingMode | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/runMultipleBotsByPollingMode diff --git a/examples/runMultipleBotsByPollingMode b/examples/runMultipleBotsByPollingMode new file mode 100644 index 0000000..6445c92 --- /dev/null +++ b/examples/runMultipleBotsByPollingMode @@ -0,0 +1,37 @@ +const { Telegraf, Composer } = require('telegraf'); + +// Array of bot tokens after filtering out empty or undefined values +const tokens = [process.env.TOKEN1, process.env.TOKEN2].filter(Boolean); + +const bot = new Composer(); + +// Adding a start listener to the main bot instance +bot.start((ctx) => { + ctx.reply("This bot is working..."); +}); + +async function setupBots() { + for (let token of tokens) { + try { + // New Telegraf bot instance + const newBot = new Telegraf(token); + + // Adding this instance to the main Composer class instance + newBot.use(bot); + + // Running the newly created bot in polling mode + await newBot.launch({ dropPendingUpdates: true }); + + console.log(`Bot with token ${token} has been initialized.`); + } catch (err) { + console.error(`Error initializing bot with token ${token}: ${err.message}`); + // Handle the error here + } + } +} + +// Call the setupBots function +setupBots(); + +// Try this example by forking this REPL +// https://replit.com/@PanditSiddharth/RunMultipleBotsInPollingMode#index.js From 8d4cb1275ced967afb77acf75f2ed582859b93dc Mon Sep 17 00:00:00 2001 From: Siddharth Sharma <75090218+PanditSiddharth@users.noreply.github.com> Date: Sat, 25 Nov 2023 15:42:42 +0530 Subject: [PATCH 2/3] Rename runMultipleBotsByPollingMode to run-many-bots-by-polling-mode changed name as telegraf-doc's other files name --- ...runMultipleBotsByPollingMode => run-many-bots-by-polling-mode} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{runMultipleBotsByPollingMode => run-many-bots-by-polling-mode} (100%) diff --git a/examples/runMultipleBotsByPollingMode b/examples/run-many-bots-by-polling-mode similarity index 100% rename from examples/runMultipleBotsByPollingMode rename to examples/run-many-bots-by-polling-mode From c46d908e01bfe7264546d0f4bf420a9be4bd9b08 Mon Sep 17 00:00:00 2001 From: Siddharth Sharma <75090218+PanditSiddharth@users.noreply.github.com> Date: Sat, 25 Nov 2023 15:46:40 +0530 Subject: [PATCH 3/3] Create run-many-bots-in-one-server run-many-bots-in-one-server by webhook mode --- examples/webhook/run-many-bots-in-one-server | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/webhook/run-many-bots-in-one-server diff --git a/examples/webhook/run-many-bots-in-one-server b/examples/webhook/run-many-bots-in-one-server new file mode 100644 index 0000000..9dcce9c --- /dev/null +++ b/examples/webhook/run-many-bots-in-one-server @@ -0,0 +1,65 @@ +const express = require('express'); +const { Telegraf, Composer } = require('telegraf'); + +const app = express(); +const bots = {}; +const port = process.env.PORT || 3000 +const DOMAIN = process.env.DOMAIN +// Array of bot tokens after filtering out empty or undefined values +const tokens = [process.env.TOKEN1, process.env.TOKEN2].filter(Boolean); +const bot = new Composer(); + +app.get("/", (req, res) => { + res.send("Bot started.."); +}); + +// here you can add more listeners e.g. bot.on("message", ....) +bot.start((ctx) => { + ctx.reply("This bot is working...") +}) + +async function setupBots() { + for (let token of tokens) { + try { + // new Telegraf bot instance + const newBot = new Telegraf(token); + + // adding this instance in Composer class instance + newBot.use(bot); + + // add bot to bots object + bots[token] = newBot; + + // setting webhook endpoint for newlly created telegraf bot + await newBot.telegram.setWebhook(`${DOMAIN}/tg/${token}`, { drop_pending_updates: true }); + + console.log(`Bot with token ${token} has been initialized.`); + } catch (err) { + console.error(`Error initializing bot with token ${token}: ${err.message}`); + } + } +} + +// calling setupBots function +setupBots(); + +// telegrams request converts in json formate before its use +app.use(express.json()); + +// dynamically getting response from each bot +app.post("/tg/:token", async (req, res) => { + try { + const token = req.params.token; + + // giving update to telegraf and response is sent to telegram + await bots[token].handleUpdate(req.body, res); + } catch (err) { + console.error(err); + res.status(500).json({ message: 'Internal server error' }); + } +}); + +// Starting express server +app.listen(port, () => console.log(`Bot running on ${DOMAIN}`)); + +// Try this example fork this repl: https://replit.com/@PanditSiddharth/TelegrafMultipleBotsByWebhookExample#index.js