Scalable Production-Ready Telegram bot on Django

There are a lot of articles on Habré on how to create the simplest Telegram bot with menu buttons and logic, there are instructions on how to deploy it all. In this article, I'll show you how to make production bots that hundreds of thousands of users can use.





Hello everyone! My name is Daniil Okhlopkov and I have a swarm of telegram bots used by over 300,000 people in total. While developing them over the past year, I came to a stack of technologies and approaches that provide me with not only comfortable development, but also scalability. In this article, I will share all the knowledge I have gained.





About my experience

, "" , - stackoverflow. , *, , . , , . , !





GitHub , . , python-telegram-bot, Django, Celery, Postgres, Redis, Dokku, GitHub Actions . , .





Django?

: Django - ! ? :





  1. Django , . YCombinator , , - .





  2. - , . , : " ".





  3. Django . . .





, , hello-world, , , (, Telegram Bot API ). , - - , .





, background tasks. , . Django Celery. Redis, Django-celery-beat ( , ).





:

, . . "" ,





  1. ( , sqlite, in memory). , Postgres.





    1. , .





  2. - , .





    1. , , Sentry.





  3. CI, .





    1. GitHub Actions, Dokku git pull , - . .





  4. . - .





    1. polling , - ⬇️.





Telegram Bot API ( ): .





  1. Polling - , .





  2. Webhook - : URL .





, -, handlers, . , HTTPS , , , IP DNS . ngrok , , , .





polling - , Webhook : , , load balancer ( docker-compose, PaaS, Heroku, Google App Engine Digital Ocean App Platform).





Django Telegram bot

Django? view ( ), ( handlers - ) ( ).





Django Model . , - /start. , , . @classmethod



, User -.





, . Redis load balancers .... .





, , - . , , , . , - , ? .





Postgres Redis. HTTPs. load balancer . , , zero-downtime ?





. , (Google App Engine, Digital Ocean App Platform, Heroku). self-hosted (Dokku, CapRover). Self-hosted , - (, Digital Ocean), apt-get install 



.





Dokku?

Heroku . GitHub , . , , . , , , , , . .





, stackoverflow " ", " ", " https" . , .





.

: . , , . ! .





Heroku , , : , . ?





, Open Source self-hosted . Dokku. , UI one-click github integration, .





Heroku, Dokku , open-source Buildpacks, 2 HTTPS .





Dokku + Django

buildpacks , , ) ) . requirements.txt Procfile.





Dokku DOKKU_SCALE, . , worker=4, : Procfile worker ( , Celery worker), 4 , load balancer. ? .





Dokku , "" Postgres Redis, , DATABASE_URL



REDIS_URL



, settings.py, .





. CI , Main ssh git pull ( Dokku). GitHub Actions, .





, Dokku , , HTTPs, GitHub Actions , Wiki .






, - . , - , , . , .





? ? .








All Articles