Telegram bot api - multiple instances of same bot
Asked Answered
I

2

5

I would like to make a telegram bot that sends error messages from a software. This software would run on 20 different pcs. Each log would be like this:

"pc name - Date time - error text".

The bot does not accept any comands. The idea would be to run the same exe on multiple pc's having multiple insances of the same bot, and they all write in the same bot-chat. since there is no command being sent, there is no risk that one instance steals the command from the others.

Is this possible? or is there a smarter way to do it using telegram? I do not want to have a new bot for each machine, as it would become a mess to handle quickly and would require setup for each machine.

thank you

Impressionable answered 14/2, 2022 at 6:39 Comment(0)
D
8

I did a quick test by running two instances of a node app with telegraf, sending messages to me every 5 seconds using bot.telegram.sendMessage.

const { Telegraf } = require('telegraf')

const bot = new Telegraf("<MY_BOT_TOKEN>")

bot.launch().then(
    setInterval(() => {
        bot.telegram.sendMessage('<MY_CHAT_ID>', `hello from ${process.env.INSTANCE_NUMBER}`)
    }, 5000)
)

It worked properly, but I also got this error

Error: 409: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running

About which I found this. Anyway, both the instances kept sending me messages without any problem.

I would say that this depends on the library, which besides sending these messages is also polling the telegram API for updates, even if I didn't set up any callback to manage them. Indeed if you check the sendMessage documentation of Telegram Bot API, it doesn't mention such an error.

This means that you should be able to send such sendMessage calls from multiple instances of the same bot without any problem, just pay attention to the library you are using and what it's doing under the hood. You may even want to implement your own calls instead of relying on existing apps if they don't suit your use case.

update

I tried to send messages without launching the bot (which makes it fetch updates i guess) and i didn't get the errors

const { Telegraf } = require('telegraf')

const bot = new Telegraf("<MY_BOT_TOKEN>")

setInterval(() => {
  bot.telegram.sendMessage('<MY_CHAT_ID>', `hello from ${process.env.INSTANCE_NUMBER}`)
}, 5000))

at this point, I'm quite confident you can do it.

Duct answered 14/3, 2022 at 10:46 Comment(3)
thank you! will send you the reward as soon as it is unlocked tomorrow.Impressionable
This seems to work perfectly for me too. Did you find any disadvantages by no launching the bot?.Cultus
@Cultus if the bot doesn't need to receive input from Telegram users and just needs to post messages - then there are no disadvantages whatsoever, AFAIK.Rogation
N
2

Yes, multiple instances of your 'bot' can use the same bot token to send a message. The issue you can run into is a rate limit if your machines start sending too many messages at the same time.

Nomination answered 2/5, 2022 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.