rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.7k stars 1.19k forks source link

How to run multiple webhook bots on a server #323

Open armin37 opened 6 years ago

armin37 commented 6 years ago

I started a bot on webhook mode on port 8443 and it works fine. but i need to run multiple bots on a VPS. how can i do that?

Clevero commented 6 years ago

Just register multiple bots in your main

like in the example from the README

// Example taken from https://github.com/rubenlagus/TelegramBotsExample
    public class Main {
        public static void main(String[] args) {
            ApiContextInitializer.init();
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
            try {
                telegramBotsApi.registerBot(new ChannelHandlers());
                telegramBotsApi.registerBot(new DirectionsHandlers());
                telegramBotsApi.registerBot(new RaeHandlers());
                telegramBotsApi.registerBot(new WeatherHandlers());
                telegramBotsApi.registerBot(new TransifexHandlers());
                telegramBotsApi.registerBot(new FilesHandlers());
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

https://github.com/rubenlagus/TelegramBots/blob/master/README.md

armin37 commented 6 years ago

It will be too messy with this method after a while and the program will be heavy including lots of libraries. Some robots may use MySQL and some use PostgreSQL. And also by this method I have to stop them all to add a new bot. It should be a way to do such a thing. https://stackoverflow.com/a/44856332/5894095

Or something like having a central file which gets the requests and pass them to a bot running on port 1337 https://example.com/userbot.php?port=1337 I'm sure it's possible, because I found a NodeJs sample before.

But the bot would starts only on the ports which Telegram asked to set with this library.

rubenlagus commented 6 years ago

@armin37 You can set up port forwarding on your own outside this library. This library will allow you to set up also the url path foir each one of the bots.

You don't have to stop anything to add a new bot, you can directly call registerBot at any time to add a new one.

Regarding heavy, if you don't want to build a program with all the libraries, you can build separate projects that listen to different ports and then build your own port forwarding method in your server.

armin37 commented 6 years ago

@rubenlagus thanks for your answer. can you explain a little more? let's assume i have a file like this that gets port number as parameter and forwards request. https://example.com/userbot.php?port=1337 then how should be external and internal url?

something like this?

final String EXTERNALWEBHOOKURL = "https://example.com:"+ 8443 ; final String INTERNALWEBHOOKURL = "https://example.com/userbot.php?port=1337";

actually i don't get what is the difference between these two and exactly how telegram use them