CallbackQueryHandler¶
- class telegram.ext.CallbackQueryHandler(callback, pattern=None, block=True)[source]¶
Bases:
telegram.ext.BaseHandlerHandler class to handle Telegram
callback queries. Optionally based on a regex.Read the documentation of the
remodule for more information.Note
If your bot allows arbitrary objects as
callback_data, it may happen that the originalcallback_datafor the incomingtelegram.CallbackQuerycan not be found. This is the case when either a malicious client tempered with thetelegram.CallbackQuery.dataor the data was simply dropped from cache or not persisted. In these cases, an instance oftelegram.ext.InvalidCallbackDatawill be set astelegram.CallbackQuery.data.New in version 13.6.
Warning
When setting
blocktoFalse, you cannot rely on adding custom attributes totelegram.ext.CallbackContext. See its docs for more info.- Parameters:
callback (coroutine function) –
The callback function for this handler. Will be called when
check_update()has determined that an update should be processed by this handler. Callback signature:async def callback(update: Update, context: CallbackContext)
The return value of the callback is usually ignored except for the special case of
telegram.ext.ConversationHandler.pattern (
str|re.Pattern|callable|type, optional) –Pattern to test
telegram.CallbackQuery.dataagainst. If a string or a regex pattern is passed,re.match()is used ontelegram.CallbackQuery.datato determine if an update should be handled by this handler. If your bot allows arbitrary objects ascallback_data, non-strings will be accepted. To filter arbitrary objects you may pass:a callable, accepting exactly one argument, namely the
telegram.CallbackQuery.data. It must returnTrueorFalse/Noneto indicate, whether the update should be handled.a
type. Iftelegram.CallbackQuery.datais an instance of that type (or a subclass), the update will be handled.
If
telegram.CallbackQuery.dataisNone, thetelegram.CallbackQueryupdate will not be handled.See also
Changed in version 13.6: Added support for arbitrary callback data.
block (
bool, optional) –Determines whether the return value of the callback should be awaited before processing the next handler in
telegram.ext.Application.process_update(). Defaults toTrue.See also
- pattern[source]¶
Optional. Regex pattern, callback or type to test
telegram.CallbackQuery.dataagainst.Changed in version 13.6: Added support for arbitrary callback data.
- Type:
re.Pattern|callable|type
- block[source]¶
Determines whether the return value of the callback should be awaited before processing the next handler in
telegram.ext.Application.process_update().- Type:
bool
Available In
- check_update(update)[source]¶
Determines whether an update should be passed to this handler’s
callback.- Parameters:
update (
telegram.Update|object) – Incoming update.- Returns:
bool
- collect_additional_context(context, update, application, check_result)[source]¶
Add the result of
re.match(pattern, update.callback_query.data)toCallbackContext.matchesas list with one element.