'Telegram bot api - multiple instances of same bot
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
Solution 1:[1]
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.
Solution 2:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Koen |