telegram.ext.CallbackDataCache¶
-
class
telegram.ext.CallbackDataCache(bot, maxsize=1024, persistent_data=None)¶ Bases:
objectA custom cache for storing the callback data of a
telegram.ext.ExtBot. Internally, it keeps two mappings with fixed maximum size:One for mapping the data received in callback queries to the cached objects
One for mapping the IDs of received callback queries to the cached objects
The second mapping allows to manually drop data that has been cached for keyboards of messages sent via inline mode. If necessary, will drop the least recently used items.
New in version 13.6.
- Parameters
bot (
telegram.ext.ExtBot) – The bot this cache is for.maxsize (
int, optional) – Maximum number of items in each of the internal mappings. Defaults to 1024.persistent_data (
telegram.ext.utils.types.CDCData, optional) – Data to initialize the cache with, as returned bytelegram.ext.BasePersistence.get_callback_data().
-
bot¶ The bot this cache is for.
- Type
-
maxsize¶ maximum size of the cache.
- Type
int
-
clear_callback_data(time_cutoff=None)¶ Clears the stored callback data.
- Parameters
time_cutoff (
float|datetime.datetime, optional) – Pass a UNIX timestamp or adatetime.datetimeto clear only entries which are older. For timezone naivedatetime.datetimeobjects, the default timezone of the bot will be used.
-
clear_callback_queries()¶ Clears the stored callback query IDs.
-
drop_data(callback_query)¶ Deletes the data for the specified callback query.
Note
Will not raise exceptions in case the callback data is not found in the cache. Will raise
KeyErrorin case the callback query can not be found in the cache.- Parameters
callback_query (
telegram.CallbackQuery) – The callback query.- Raises
KeyError – If the callback query can not be found in the cache
-
static
extract_uuids(callback_data)¶ Extracts the keyboard uuid and the button uuid from the given
callback_data.- Parameters
callback_data (
str) – Thecallback_dataas present in the button.- Returns
Tuple of keyboard and button uuid
- Return type
(
str,str)
-
property
persistence_data¶ The data that needs to be persisted to allow caching callback data across bot reboots.
-
process_callback_query(callback_query)¶ Replaces the data in the callback query and the attached messages keyboard with the cached objects, if necessary. If the data could not be found,
telegram.ext.InvalidCallbackDatawill be inserted. Ifcallback_query.dataorcallback_query.messageis present, this also saves the callback queries ID in order to be able to resolve it to the stored data.Note
Also considers inserts data into the buttons of
telegram.Message.reply_to_messageandtelegram.Message.pinned_messageif necessary.Warning
In place, i.e. the passed
telegram.CallbackQuerywill be changed!- Parameters
callback_query (
telegram.CallbackQuery) – The callback query.
-
process_keyboard(reply_markup)¶ Registers the reply markup to the cache. If any of the buttons have
callback_data, stores that data and builds a new keyboard with the correspondingly replaced buttons. Otherwise does nothing and returns the original reply markup.- Parameters
reply_markup (
telegram.InlineKeyboardMarkup) – The keyboard.- Returns
The keyboard to be passed to Telegram.
- Return type
-
process_message(message)¶ Replaces the data in the inline keyboard attached to the message with the cached objects, if necessary. If the data could not be found,
telegram.ext.InvalidCallbackDatawill be inserted.Note
Checks
telegram.Message.via_botandtelegram.Message.from_userto check if the reply markup (if any) was actually sent by this caches bot. If it was not, the message will be returned unchanged.Note that this will fail for channel posts, as
telegram.Message.from_userisNonefor those! In the corresponding reply markups the callback data will be replaced bytelegram.ext.InvalidCallbackData.Warning
Does not consider
telegram.Message.reply_to_messageandtelegram.Message.pinned_message. Pass them to these method separately.In place, i.e. the passed
telegram.Messagewill be changed!
- Parameters
message (
telegram.Message) – The message.