-
Notifications
You must be signed in to change notification settings - Fork 0
Transition guide to Version 4.0
All changes can also be reviewed in our official documentation!
For users coming from RC release predating 26th of April, 2016: Please note the subtle changes in "filters" syntax (upper/lower case).
The Dispatcher
class has now a cleaner interface and more precise Message filtering. Instead of many methods with long names like Dispatcher.addTelegramMessageHandler(handler)
, we now only have two of those methods:
Register a handler. A handler must be an instance of a subclass of
telegram.ext.Handler
. All handlers are organized in groups, the default group isint(0)
, but any object can identify a group. Every update will be tested against each handler in each group from first-added to last-added. If the update has been handled in one group, it will not be tested against other handlers in that group. That means an update can only be handled 0 or 1 times per group, but multiple times across all groups.
handler (Handler)
– A Handler instance
group (optional[object])
– The group identifier. Default is 0
This method remains unchanged.
So, the addHandler
method is accepting an object of a subclass of telegram.ext.Handler
. Let's see how that looks in real life:
from telegram.ext import MessageHandler, Filters
def text_callback(bot, update):
print("New text message: " + update.message.text)
dispatcher.addHandler(MessageHandler([Filters.text], text_callback))
As you can see here, the MessageHandler
class is one of the included Handler
subclasses. All that was possible before is still possible, but now more organized and more explicit. Lets take a quick look at another handler class, the RegexHandler
:
from telegram.ext import RegexHandler
def name_callback(bot, update, groupdict):
print("The name of the user is: " + groupdict['name'])
name_regex = r'My name is (?P<name>.*)'
dispatcher.addHandler(RegexHandler(name_regex, text_callback, pass_groupdict=True))
Here you can see the optional argument groupdict
passed to the handler callback function. Note that it is necessary to specify this explicitly when creating the Handler
object.
- You can easily implement your own handlers. Just subclass
telegram.ext.handler
and take a look at the implementation of the provided handlers. - Instead of
addTelegramInlineHandler
there are nowInlineHandler
,ChosenInlineResultHandler
andCallbackQueryHandler
- There is no replacement for
addUnknownTelegramCommandHandler
. Instead, it is recommended to useRegexHandler(r'/.*', ...)
and add it as the last handler - The
UpdateQueue
class andcontext
parameters have been removed
Please read the documentation of the Telegram Bot API to learn about all the new things in version 2 of the bot API. This section covers only those changes that are not backwards compatible and not listed in the Recent Changes list.
-
new_chat_participant
andleft_chat_participant
of theMessage
class are nownew_chat_member
andleft_chat_member
- The following parameters on
InlineResult
andInlineQueryResult
objects are removed in favor ofInlineMessageContent
:
- message_text
- parse_mode
- disable_web_page_preview
- In
InlineQueryResultPhoto
the parametermime_type
has been removed. JPEG is now required. -
ReplyKeyboardMarkup
now takes a list of a list ofKeyboardButton
instead of strings.
The classes Updater
, Dispatcher
and JobQueue
that were previously available for import directly from telegram
are now located in telegram.ext
.
- Wiki of
python-telegram-bot
© Copyright 2015-2025 – Licensed by Creative 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