Run multiple Telegram bots on a single host
Telegram offers JSON-based, accessible via a RESTful control webhook API updates to push data to the handler application running as a publicly available https server on one of valid ports (80, 443, 88 or 8443) with self-signed or CA-signed certificate. A frugal build-and-forget methodology is oftentimes preferred to avoid the costs of using fancy off-site CI/CD platforms and no-code services. Popular and well-maintained open source bot libraries widely used by developers (Telebot, Telegraf, etc.) may serve several bot tokens per process, but it comes with a cost of combining multiple bot logic into one source repo.
Running multiple instances of dockerized bot applications on a single host considering a limited number of allowed ports requires nginx reverse proxy to take care of connection dispatch. Configuration of nginx has to be carefully maintained in regard with actual port settings of docker containers; nginx SSL-certificates also have to be consistent with all deployed bot applications. No external container orchestration and no nginx control API are allowed by the conditions of our task since we are about to fit one standalone host. Therefore, the amount of manwork required to set-up and maintain single-host multi-bot dockerized infrastructure is significantly higher than the development process of the bot itself.
To minimise our efforts in bot hosting deployment tgbot-swarm
solves two scopes of tasks:
-
Provide a built-in API that employs port, path and SSL-certificate assignment mechanism and a relevant proxy config generation. In a nutshell it is a node.js-express controller application that runs in the same docker container with nginx, serves RESTful JSON-based API and sends system control signals to local nginx process.
-
Provide Jenkins groovy pipelines to automate build and remote deployment (using ssh in this example) of a controller/proxy container and an arbitrary bot container based on response from the controller.
- Prepare your
tgbot-swarm
host:
> sudo groupadd jenkins
> sudo adduser jenkins
> sudo passwd jenkins [password]
> sudo usermod -aG jenkins jenkins
> loginctl enable-linger jenkins
> mkdir /opt/jenkins
> chown jenkins:jenkins jenkins
> chmod 755 jenkins
> yum install docker
> firewall-cmd --add-port=[external-port]/tcp --permanent
- Make your own fork of this repository.
- Edit
pipeline/infrastructure-dev.conf
according to your host environment. - Create plaintext records in Jenkins secret storage:
swarm-apikey-dev
= SOME-RANDOM-STRINGswarm-hostname-dev
= Domain name of your tgbot host.swarm-sshcred-dev
= SSH password of previously created user Jenkins on the hostswarm-tgtoken-dev
= Telegram API key of your bot
- Edit
pipeline/deploy-controller.jenkinsfile
: set your repo url in theCheckout Code
stage. - Edit
pipeline/deploy-samplebot.jenkinsfile
according to your host environment and set your repo url in theCheckout Code
stage. - Create Jenkins pipelines using
pipeline/deploy-controller.jenkinsfile
andpipeline/deploy-samplebot.jenkinsfile
from your repo. - Run
deploy-controller
anddeploy-samplebot
sequentially.
"request": {
"api-key": "",
"application-id": "",
"description": ""
}
"response": {
"path": "",
"port": "",
"url": "",
"updated": "",
"ssl_certificate": "",
"ssl_key": "",
"docker_container_name": "",
"docker_image_name": ""
}
tgbot-swarm
is licensed under the MIT license for all open source applications.
Please report bugs here on Github. Guidelines for bug reports:
- Use the GitHub issue search — check if the issue has already been reported.
- Check if the issue has been fixed — try to reproduce it using the latest master or development branch in the repository.
- Isolate the problem — create a reduced test case and a live example.
A good bug report shouldn't leave others needing to chase you up for more information. Please try to be as detailed as possible in your report. Feature requests are welcome. Please look for existing ones and use GitHub's "reactions" feature to vote.