BasePersistence

class telegram.ext.BasePersistence(store_data=None, update_interval=60)[source]

Bases: typing.Generic, ABC

Interface class for adding persistence to your bot. Subclass this object for different implementations of a persistent bot.

Attention

The interface provided by this class is intended to be accessed exclusively by Application. Calling any of the methods below manually might interfere with the integration of persistence into Application.

All relevant methods must be overwritten. This includes:

If you don’t actually need one of those methods, a simple pass is enough. For example, if you don’t store bot_data, you don’t need get_bot_data(), update_bot_data() or refresh_bot_data().

Note

You should avoid saving telegram.Bot instances. This is because if you change e.g. the bots token, this won’t propagate to the serialized instances and may lead to exceptions.

To prevent this, the implementation may use bot to replace bot instances with a placeholder before serialization and insert bot back when loading the data. Since bot will be set when the process starts, this will be the up-to-date bot instance.

If the persistence implementation does not take care of this, you should make sure not to store any bot instances in the data that will be persisted. E.g. in case of telegram.TelegramObject, one may call set_bot() to ensure that shortcuts like telegram.Message.reply_text() are available.

This class is a Generic class and accepts three type variables:

  1. The type of the second argument of update_user_data(), which must coincide with the type of the second argument of refresh_user_data() and the values in the dictionary returned by get_user_data().

  2. The type of the second argument of update_chat_data(), which must coincide with the type of the second argument of refresh_chat_data() and the values in the dictionary returned by get_chat_data().

  3. The type of the argument of update_bot_data(), which must coincide with the type of the argument of refresh_bot_data() and the return value of get_bot_data().

Changed in version 20.0:

  • The parameters and attributes store_*_data were replaced by store_data.

  • insert/replace_bot was dropped. Serialization of bot instances now needs to be handled by the specific implementation - see above note.

Parameters:
  • store_data (PersistenceInput, optional) – Specifies which kinds of data will be saved by this persistence instance. By default, all available kinds of data will be saved.

  • update_interval (int | float, optional) –

    The Application will update the persistence in regular intervals. This parameter specifies the time (in seconds) to wait between two consecutive runs of updating the persistence. Defaults to 60 seconds.

    Added in version 20.0.

store_data[source]

Specifies which kinds of data will be saved by this persistence instance.

Type:

PersistenceInput

bot[source]

The bot associated with the persistence.

Type:

telegram.Bot

abstract async drop_chat_data(chat_id)[source]

Will be called by the telegram.ext.Application, when using drop_chat_data().

Added in version 20.0.

Parameters:

chat_id (int) – The chat id to delete from the persistence.

abstract async drop_user_data(user_id)[source]

Will be called by the telegram.ext.Application, when using drop_user_data().

Added in version 20.0.

Parameters:

user_id (int) – The user id to delete from the persistence.

abstract async flush()[source]

Will be called by telegram.ext.Application.stop(). Gives the persistence a chance to finish up saving or close a database connection gracefully.

Changed in version 20.0: Changed this method into an abstractmethod().

abstract async get_bot_data()[source]

Will be called by telegram.ext.Application upon creation with a persistence object. It should return the bot_data if stored, or an empty dict. In the latter case, the dict should produce values corresponding to one of the following:

Returns:

The restored bot data.

Return type:

dict[int, dict | telegram.ext.ContextTypes.bot_data]

abstract async get_callback_data()[source]

Will be called by telegram.ext.Application upon creation with a persistence object. If callback data was stored, it should be returned.

Added in version 13.6.

Changed in version 20.0: Changed this method into an abstractmethod().

Returns:

tuple[list[tuple[str, float, dict[str, object]]], dict[str, str]] | None: The restored metadata or None, if no data was stored.

abstract async get_chat_data()[source]

Will be called by telegram.ext.Application upon creation with a persistence object. It should return the chat_data if stored, or an empty dict. In the latter case, the dictionary should produce values corresponding to one of the following:

Changed in version 20.0: This method may now return a dict instead of a collections.defaultdict

Returns:

The restored chat data.

Return type:

dict[int, dict | telegram.ext.ContextTypes.chat_data]

abstract async get_conversations(name)[source]

Will be called by telegram.ext.Application when a telegram.ext.ConversationHandler is added if telegram.ext.ConversationHandler.persistent is True. It should return the conversations for the handler with name or an empty dict.

Parameters:

name (str) – The handlers name.

Returns:

The restored conversations for the handler.

Return type:

dict

abstract async get_user_data()[source]

Will be called by telegram.ext.Application upon creation with a persistence object. It should return the user_data if stored, or an empty dict. In the latter case, the dictionary should produce values corresponding to one of the following:

Changed in version 20.0: This method may now return a dict instead of a collections.defaultdict

Returns:

The restored user data.

Return type:

dict[int, dict | telegram.ext.ContextTypes.user_data]

abstract async refresh_bot_data(bot_data)[source]

Will be called by the telegram.ext.Application before passing the bot_data to a callback. Can be used to update data stored in bot_data from an external source.

Tip

This method is expected to edit the object bot_data in-place instead of returning a new object.

Warning

When using concurrent_updates(), this method may be called while a handler callback is still running. This might lead to race conditions.

Added in version 13.6.

Changed in version 20.0: Changed this method into an abstractmethod().

Parameters:

bot_data (dict | telegram.ext.ContextTypes.bot_data) – The bot_data.

abstract async refresh_chat_data(chat_id, chat_data)[source]

Will be called by the telegram.ext.Application before passing the chat_data to a callback. Can be used to update data stored in chat_data from an external source.

Tip

This method is expected to edit the object chat_data in-place instead of returning a new object.

Warning

When using concurrent_updates(), this method may be called while a handler callback is still running. This might lead to race conditions.

Added in version 13.6.

Changed in version 20.0: Changed this method into an abstractmethod().

Parameters:
abstract async refresh_user_data(user_id, user_data)[source]

Will be called by the telegram.ext.Application before passing the user_data to a callback. Can be used to update data stored in user_data from an external source.

Tip

This method is expected to edit the object user_data in-place instead of returning a new object.

Warning

When using concurrent_updates(), this method may be called while a handler callback is still running. This might lead to race conditions.

Added in version 13.6.

Changed in version 20.0: Changed this method into an abstractmethod().

Parameters:
set_bot(bot)[source]

Set the Bot to be used by this persistence instance.

Parameters:

bot (telegram.Bot) – The bot.

Raises:

TypeError – If PersistenceInput.callback_data is True and the bot is not an instance of telegram.ext.ExtBot.

abstract async update_bot_data(data)[source]

Will be called by the telegram.ext.Application after a handler has handled an update.

Parameters:

data (dict | telegram.ext.ContextTypes.bot_data) – The telegram.ext.Application.bot_data.

abstract async update_callback_data(data)[source]

Will be called by the telegram.ext.Application after a handler has handled an update.

Added in version 13.6.

Changed in version 20.0: Changed this method into an abstractmethod().

Parameters:

data (tuple[list[tuple[str, float, dict[str, Any]]], dict[str, str]] | None) – The relevant data to restore telegram.ext.CallbackDataCache.

abstract async update_chat_data(chat_id, data)[source]

Will be called by the telegram.ext.Application after a handler has handled an update.

Parameters:
abstract async update_conversation(name, key, new_state)[source]

Will be called when a telegram.ext.ConversationHandler changes states. This allows the storage of the new state in the persistence.

Parameters:
property update_interval[source]

Time (in seconds) that the Application will wait between two consecutive runs of updating the persistence.

Added in version 20.0.

Type:

float

abstract async update_user_data(user_id, data)[source]

Will be called by the telegram.ext.Application after a handler has handled an update.

Parameters: