node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

How to listen to multiple bots?

Open veob opened this issue 9 years ago • 11 comments

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.

veob avatar Oct 21 '17 06:10 veob

If there is interest, I can open a PR for this.

cc @yagop

CapacitorSet avatar Oct 24 '17 22:10 CapacitorSet

Would be better to use multiple instances. Memory will grow but because of requests. Think the prototypes are the same.

yagop avatar Oct 24 '17 23:10 yagop

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?

veob avatar Oct 25 '17 18:10 veob

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 }

yagop avatar Oct 25 '17 21:10 yagop

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

elie222 avatar Aug 28 '18 19:08 elie222

Pls share if have working code for that, i am totally confused with nodeJS and multiple bot to listen for single class.

imdkbj avatar Aug 15 '19 09:08 imdkbj

You fail to mention how every instance would require a new proxy to connect to VG or PG.

Samscdev avatar Jan 26 '20 17:01 Samscdev

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 avatar Jan 27 '20 16:01 imdkbj

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.

DineshKumarSathrasala avatar Apr 07 '20 11:04 DineshKumarSathrasala

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 }

@yagop please share full code on how to integrate multiple telegram bots using this node module

DineshKumarSathrasala avatar Apr 07 '20 11:04 DineshKumarSathrasala

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.

imdkbj avatar Apr 08 '20 17:04 imdkbj