mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
Updated Arbitrary callback_data (markdown)
parent
ffc8353309
commit
cd0d6ee538
1 changed files with 3 additions and 3 deletions
|
@ -4,9 +4,6 @@ The Telegrams Bot API only accepts strings with length up to 64 bytes as `callba
|
|||
|
||||
With PTB, you are able to pass *any* object as `callback_data`. This is achieved by storing the object in a cache and passing a unique identifier for that object to Telegram. When a `CallbackQuery` is received, the id in the `callback_data` is replaced with the stored object. To use this feature, set [`Application.arbitrary_callback_data`](https://docs.python-telegram-bot.org/telegram.ext.applicationbuilder.html#telegram.ext.ApplicationBuilder.arbitrary_callback_data) to `True`. The cache that holds the stored data has limited size (more details on memory usage below). If the cache is full and objects from a new `InlineKeyboardMarkup` need to be stored, it will discard the data for the least recently used keyboard.
|
||||
|
||||
> [!Note]
|
||||
> You can of course also manually implement the idea of storing the object in a cache and passing a unique identifier for that object to Telegram, e.g. with the help of PTB [[storing data functionality|Storing-bot,-user-and-chat-related-data]]. PTBs built-in "Arbitrary `callback_data`" provides this mechanism in a way that requires minimal additional implementation effort on your end and that ties in well with the overall PTB framework.
|
||||
|
||||
This means two things for you:
|
||||
|
||||
1. If you don't use [persistence](../wiki/Making-your-bot-persistent), buttons won't work after restarting your bot, as the stored updates are lost. More precisely, the `callback_data` you will receive is an instance of `telegram.ext.InvalidCallbackData`. If you don't need persistence otherwise, you can set `store_callback_data` to `True` and all the others to `False`.
|
||||
|
@ -17,6 +14,9 @@ This means two things for you:
|
|||
* a callable accepting the `callback_data` as only argument. You can perform any kinds of tests on the `callback_data` and return `True` or `False` accordingly
|
||||
* a type. In that case the `CallbackQuery` will be handled, if the `callback_data` is an instance of that type. Btw: This allows you to inform users, when a buttons' data has been dropped from cache. With `CallbackQueryHandler(callback, pattern=InvalidCallbackData)` you can e.g., call `await update.callback_query.answer(text='Button is no longer valid', show_alert=True)` to inform the user.
|
||||
|
||||
> [!Note]
|
||||
> You can of course also manually implement the idea of storing the object in a cache and passing a unique identifier for that object to Telegram, e.g. with the help of PTB [[storing data functionality|Storing-bot,-user-and-chat-related-data]]. PTBs built-in "Arbitrary `callback_data`" provides this mechanism in a way that requires minimal additional implementation effort on your end and that ties in well with the overall PTB framework.
|
||||
|
||||
## Memory Usage
|
||||
|
||||
PTB stores the callback data objects in memory. Additionally, to that, it stores a mapping of `CallbackQuery.id` to the corresponding UUID. By default, both storages contain a maximum number of 1024 items. You can change the size by passing an integer to the `arbitrary_callback_data` argument of `ApplicationBuilder`/`ExtBot`.
|
||||
|
|
Loading…
Reference in a new issue