How to listen to multiple bots?
Question
Hi. My goal is to listen to multiple telegram bots.
Is it possible to create multiple instances of TelegramBot class in one node.js instance (but this looks like a horrible solution, memory will leak)? Or maybe I could use this library as not a singleton.
If there is interest, I can open a PR for this.
cc @yagop
Would be better to use multiple instances. Memory will grow but because of requests. Think the prototypes are the same.
First of all, thank you for your replies.
@yagop
Would be better to use multiple instances
What if I have 10 000 requests from 10 000 different bots? Sorry, but this is a bad way to solve this problem, imo. Even if I create a new instance for every new bot, where I should store them? Storing state between requests is a bad practice in a node.js app, as far as I know.
I think it should work something like this:
TelegramBot.setBots([botToken1, botToken2, botTokenN])
TelegramBot.on('message', (data) => { console.log(data.bot) }
where data.bot could be different for every new request. Of course, we must specify allowed bots to listen to beforehand (via tokens).
What do you think about this? Is this suitable for node-telegram-bot-api philosophy?
The number of request would be the same (10000) as Telegram doesn't provide multiple bots queries / actions and doesn't seem to have any sense for them to do it. I don't understand what you mean with "Storing state between requests".
If you have 10.000 I think the better would be to pass telegramWebHook a http, https or Express server which is actually not possible. And share that server between clients (with diferent routes for each server).
// Memory is increased but just a few MB for 10k instances.
➜ cat a.js
const TelegramBot = require('node-telegram-bot-api');
console.log(process.memoryUsage());
const arr = new Array(10000)
.fill()
.map(() => new TelegramBot('', {polling: false}))
console.log(process.memoryUsage());
➜ node a.js
{ rss: 51064832,
heapTotal: 32518144,
heapUsed: 18240976,
external: 1037144 }
{ rss: 54038528,
heapTotal: 33042432,
heapUsed: 21282736,
external: 1037144 }
I'm looking to solve this problem too.
This would work well for us:
const arr = new Array(10000)
.fill()
.map(() => new TelegramBot('', {polling: false}))
But we would need to set a new url for each one, but that leads to port conflicts. Any recommendations here? We may look to make a PR to this package unless there's an easier solution.
This is a related item we've seen: https://github.com/yagop/node-telegram-bot-api/issues/219#issuecomment-289619660
Pls share if have working code for that, i am totally confused with nodeJS and multiple bot to listen for single class.
You fail to mention how every instance would require a new proxy to connect to VG or PG.
Hey guys! With my limited knowledge i have created this and working fine for multiple webhooks.
Have a look...
https://gist.github.com/imdkbj/1f20dee8136a573f933a5130d906cc6e
Hey guys! With my limited knowledge i have created this and working fine for multiple webhooks.
Have a look...
https://gist.github.com/imdkbj/1f20dee8136a573f933a5130d906cc6e
@imdkbj I have gone through github link you have shared but i have a doubt. Where setwebhook.js file is being required(referenced) in webhook.js file. As without requiring how it will be called. Please help me in this.
The number of request would be the same (10000) as Telegram doesn't provide multiple bots queries / actions and doesn't seem to have any sense for them to do it. I don't understand what you mean with "Storing state between requests".
If you have 10.000 I think the better would be to pass telegramWebHook a
http,httpsorExpressserver which is actually not possible. And share that server between clients (with diferent routes for each server).// Memory is increased but just a few MB for 10k instances. ➜ cat a.js const TelegramBot = require('node-telegram-bot-api'); console.log(process.memoryUsage()); const arr = new Array(10000) .fill() .map(() => new TelegramBot('', {polling: false})) console.log(process.memoryUsage()); ➜ node a.js { rss: 51064832, heapTotal: 32518144, heapUsed: 18240976, external: 1037144 } { rss: 54038528, heapTotal: 33042432, heapUsed: 21282736, external: 1037144 }
@yagop please share full code on how to integrate multiple telegram bots using this node module
Hey guys! With my limited knowledge i have created this and working fine for multiple webhooks. Have a look... https://gist.github.com/imdkbj/1f20dee8136a573f933a5130d906cc6e
@imdkbj I have gone through github link you have shared but i have a doubt. Where setwebhook.js file is being required(referenced) in webhook.js file. As without requiring how it will be called. Please help me in this.
Hey!! Both the file are independent in nature and not required reference to each other.
webhook.js : this will be always running to listen your incoming webhook.
setwebhook.js : this file u can run as per wish to either standalone or export function to use in somewhere else.
The function is webhookURL to setup the webhook, have a look how this been used.
Pls let me know if any doubts.