I have 6 bots and i want to run them on the same server, But when i run the second bot, it throws this error:
address already in use :::8443
I know i only can use 4 ports(80,88,443,8443) for webhook, But i have 6 bots. Actually, I'm trying to run all the bots on the same port.
Is there anyway to do it?
I'm using telegraf framework. I made a directory for every bot because i think it is the way to do it. maybe i'm wrong.
This is the code of the bots in every directory:
const Telegraf = require('telegraf');
const fs = require('fs');
let token = 'botID:botToken';
const bot = new Telegraf(token);
const tlsOptions = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem'),
ca: []
}
bot.on('message', (ctx) => ctx.reply('Hey'));
bot.telegram.setWebhook('https://myAddress.com:8443/test/botID');
bot.startWebhook('/test/botID', tlsOptions, 8443);
I must admit I have not written a Telegram bot so some of what I might say next may not apply. Edit - I did some reading and have updated my answer based upon my reading.
The Telegram Bot service provides webhook access, so, you can simply register different URL's per Bot and then use routing to have the appropriate Bot code answer the requests. You can do this all in a mono-repository or set it up as a microservice. Up to you.
I'm not going to provide code but basically you would have a single repository that exposes a web service on one of the ports accepted by the Telegram Bot service for the webhook (80, 88, 443, 8443). Within this web service you will receive a request on a particular URL that will then route that request to the appropriate handler which in this case will be your bot code.
E.g. web service exposed on port 8443
Express, Koa, etc... can all provide this type of code. (Telegraf even provides an example for Koa integration in their documentation.)
This option, believe it or not, may actually require less coding on your part BUT will require more configuration and orchestration. In this configuration you simply need to setup a reverse proxy (nginx works great) that will receive all of your inbound Telegram bot requests and then forwards them to your local Telegraf based bots.
So you would build and run your bots on all separate ports (e.g. 3000, 3001, 3002, etc). Then configure the reverse proxy to route inbound requests to the appropriate bot handler.
E.g. reverse proxy listening on port 8443
For reverse proxy I mentioned nginx, you can also use services like AWS or Azure's API Gateways. You could also run the Telegraf bot as a serverless app on AWS (through Lambda) or Azure (through Functions).
Either way will work. Personally I would deploy it using the microservice method because then you can scale each bot independently as needed.
A port is simply a "number" on a TCP or UDP packet that tells the IP stack what application should receive the packet. There are 65536 ports available per IP address, per protocol (TCP or UDP - HTTPS uses TCP). So, technically, there are no limitations to which ports you can use to have your application receive its packets. Therefore, excluding any other limitations (inbound firewalls, framework limitations, etc) you could simply start your 6 bots on ports: 8443, 8444, 8445, and so on.
To answer your question about running six bots on a single port, again I can address this with generic server techniques. Backing up a little, when packets are incoming to a computer they find they right computer first with the IP address, then locally the MAC address, then finally the port on the computer to get it to the right application. If you need multiple applications to receive data on the same port then you need to perform that addressing yourself in the application protocol. Therefore, if in the Telegraf bot application protocol there is an indication which bot should receive the data then you could simply have routing code that directs incoming data to the proper bot. In this case you would NOT have each BOT start by listening on a port (e.g. in your case the same port) because that will, as you have experienced, generate the error that the port is already in use. You would instead have your routing code listen on the port you wish to listen on, and then route the incoming data to the proper bot code.
If there are limitations to which ports you can receive information on, and there is no way within the Telegraf bot protocol to determine which bot should receive information, then your only way to run six bots will be to have more than a single IP address and spread the bots out across the ports available and then across multiple IP addresses when you run out of ports.
E.g. ports available 80, 88, 443, 8443
Need to run 6 bots.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With