Python Telegram Bot Bootstrap

JM Robles
3 min readAug 16, 2022

TL;DR: This bootstrap enables the easy telegram bot developing in Python with production-grade features.

3, 2, 1… Telegram bot in the moon!

Motivation

Telegram rules. It’s the king if we take in count the number of features… and its awesome bot skills.

I used telegram bots for multiple scenarios: alarm notification, scrapping control, automation tools, …

I find python-telegram-bot library for Python very good.

Telegram exposes a REST API for its bots platforms. It’s a great API but its documentation is a bit ugly.

python-telegram-bot wraps the API and it’s pretty straightforward to create a bot with it… but in my humble opinion, it’s slightly complicated to create a production grade bot.

That is the reason for creating a Python bootstrap project with interesting and very used features: github.com/jmrobles/telegram-bot-bootstrap

Features

The features included since the very moment that you clone the repository are:

  • Dependency injection: You can easy inject dependencies for your bot.
  • Clean Architecture: Approach to a clean architecture.
  • Internationalization (i18n) support: gettext locale ready.
  • Persistence with database support: Database support with SQLAlchemy and pydantic.
  • Bot encapsulation: Use of class instead of methods to interface with python-telegram-bot.
  • Payments ready: If you can use Payments, a template handler for payments with Stripe is already enabled.
  • Execution lifecycle: Use of signals to shutdown the bot properly.
  • Metrics: Prometheus client integration.

Structure

Code is structured in 3 main folders:

  • core: interfaces, abstract classes, …
  • app: business logic (models, controllers, …)
  • infra: infrastructure parts (storage driver, adapters, UI specific, …)

It’s important to highlight the main bot code.

We use a class, TGBot, where we implement the commands that our bot handles.

JM Robles

Backend / Platform engineer #go #python #kubernetes #terraform #devops