- Notifications
You must be signed in to change notification settings - Fork5.7k
Architecture
Thetelegram
andtelegram.ext
packages contain several classes that make writing (chat)bots easy.You have met most of them in thetutorial.Because all of that can be a bit overwhelming, the below diagram gives you an overview of how the different components interact with each other.
flowchart TD; AppBuilder(" ext.ApplicationBuilder builder pattern for ext.Application "); App(" ext.Application • entry point for the whole application</li> • provides convenience methods for running the whole app via run_polling/webhook()</li> • administers handlers and error handlers</li> • administers user/chat/bot_data</li> "); BaseHandler(" ext.BaseHandler specifies if and how it handles updates "); BaseRequest(" request.BaseRequest interface for handling the networking backend "); BasePersistence(" ext.BasePersistence interface for persisting data from ext.Application across restarts "); BaseUpdateProcessor(" ext.BaseUpdateProcessor interface for processing updates concurrently "); Bot(" (ext.Ext)Bot Telegram Bot API client used to make requests to the API "); CallbackContext(" ext.CallbackContext Convenience class for unified access to different objects within handler/job/error callbacks "); BaseRateLimiter(" ext.BaseRateLimiter interface for rate limiting API requests "); Defaults(" ext.Defaults gathers default values for frequently used parameters of (ext.Ext)Bot, Ext.JobQueue and ext.BaseHandler "); CallbackDataCache(" ext.CallbackDataCache in-memory LRU-cache for arbitrary callback_data "); ContextTypes(" ext.ContextTypes specifies types of the context argument "); JobQueue(" ext.JobQueue schedules tasks to run at specific times "); Updater(" ext.Updater fetches updates from Telegram and puts them into the update_queue "); AppBuilder -- builds --> App; App -- accesses to build context --> ContextTypes; App -- "provides arguments for<br>handler callbacks, processes exceptions<br>raised in handler callbacks" --> BaseHandler; App -- processes exceptions<br>raised in jobs --> JobQueue; App -- fetches data<br>and passes it to<br>ext.BasePersistence --> CallbackDataCache; App -- fetches updates from<br>the update_queue --> Updater; App -- updates in<br>regular intervals --> BasePersistence; App -- gets default values<br>for parameters --> Defaults; App -- dispatches updates --> BaseUpdateProcessor; BaseUpdateProcessor -- processes updates<br>in specified<br>concurrency scheme--> BaseHandler; ContextTypes -- specifies types --> CallbackContext; BasePersistence -- holds a reference --> Bot; BaseRateLimiter -- rate limits requests<br>to the API --> BaseRequest; Bot -- dispatches requests<br>to the API --> BaseRateLimiter; Bot -- stores arbitrary<br>callback_data --> CallbackDataCache; Bot -- gets default values<br>for parameters --> Defaults; JobQueue -- accesses to<br>build context --> ContextTypes; Updater -- calls get_updates<br>& set/delete_webhook --> Bot;
python-telegram-bot
is designed such that you can adjust it to many use cases.In particular, many components/features are optional and some can even be fully customized.Moreover, the design allows the library to be used alongside otherasyncio
frameworks in the same Python script. We have a dedicated section on this in theover here.
TheUpdater
class is there to fetch updates from the Bot API for your bot.But you don't need to use it.If you want to implement a custom mechanism to fetch updates (e.g. acustom webhook setup, you can just put your updates into theApplication.update_queue
or even manually callApplication.process_update
.To build anApplication
that doesn't use anUpdater
, simply passNone
toApplicationBuilder.updater
.
By default, theApplication
handles updates sequentially, i.e. one by one.However, you can also process updates concurrently, i.e. multiple updates at the same time.It is also possible to customize which updates should be processed concurrently and which should be processed sequentially by providing a custom implementation of theBaseUpdateProcessor
interface class.Please have a look atthis wiki page for more info.
TheJobQueue
class integrates scheduling logic into the setup oftelegram.ext
.This feature is optional.If you don't need/want to use aJobQueue
, simply passNone
toApplicationBuilder.job_queue
.
TheCallbackDataCache
is the backend for caching arbitrarycallback_data
.This feature must be explicitly activated to be used.Please seethis wiki page for more info.
TheDefaults
class allows you to specify default values for parameters that appear quite often, e.g. theparse_mode
parameter.This is an opt-in feature.Please have a look atthis wiki page for more info.
By default, data likeApplication.{chat, bot, user}_data
is stored in-memory and is lost when the bot shuts down.PTB includes an optional functionality to persist data across restarts, which hasBasePersistence
as interface class at it's core.Head tothis page for more info.
By default, all requests to the bot API, i.e. callingBot.send_message
,Message.reply_text
or any other API method, are immediately forwarded to Telegram.Since Telegram imposes rate limits, this can lead to flood errors if you're making to many requests in a short time span.PTB includes an optional functionality to "throttle" the outgoing requests, which hasBaseRateLimiter
as interface class at it's core.Head tothis page for more info.
By default, PTB will use thehttpx
library for the networking backend, i.e. making requests to the Bot API.However, you are free to use a custom backend implementation as well.For this, you'll have to implement theBaseRequest
interface class and pass two instances of your custom networking class toApplicationBuilder.request
andApplicationBuilder.get_updates_request
.
TheHandler
interface class is the most important class when it comes to the question of how theApplication
processes updates.PTB comes with a number ofbuilt-in handler which cover most important use cases.However, if you want to implement a custom logic of when an update should be handled, you can also write a custom implementation ofHandler
and use that.
As mentioned above, PTBs persistence functionality is based on the interface classBasePersistence
.To use persistence in PTB, you use an implementation of this class.PTB comes already ships two implementations (seethis page), but you are very welcome to implement a persistence class for your own serialization backend.
The classCallbackContext
is a central part of the handler/job callbacks in PTB and more advanced users may want to add custom behavior to this class.You can do so by passing an instance of theContextTypes
toApplicationBuilder.context_types
.Have a look atthis example for example use cases.
Wiki ofpython-telegram-bot
© Copyright 2015-2025 – Licensed byCreative Commons
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests