Skip to content
Back to top
View this page

# BasePersistence

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

  Bases: `ABC`, `typing.Generic`

  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:

  - `get_bot_data()`

  - `update_bot_data()`

  - `refresh_bot_data()`

  - `get_chat_data()`

  - `update_chat_data()`

  - `refresh_chat_data()`

  - `drop_chat_data()`

  - `get_user_data()`

  - `update_user_data()`

  - `refresh_user_data()`

  - `drop_user_data()`

  - `get_callback_data()`

  - `update_callback_data()`

  - `get_conversations()`

  - `update_conversation()`

  - `flush()`

  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()`.

  See also

  Architecture Overview, Making Your Bot Persistent

  Use In

  `telegram.ext.ApplicationBuilder.persistence()`

  Available In

  `telegram.ext.Application.persistence`

  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

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

    Type:

      `PersistenceInput`

  bot

    The bot associated with the persistence.

    Type:

      `telegram.Bot`

  abstractmethod async drop_chat_data(chat_id)

    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.

  abstractmethod async drop_user_data(user_id)

    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.

  abstractmethod async flush()

    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()`.

  abstractmethod async get_bot_data()

    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:

    - `dict`

    - The type from `telegram.ext.ContextTypes.bot_data` if `telegram.ext.ContextTypes` are used.

    Returns:

      The restored bot data.

    Return type:

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

  abstractmethod async get_callback_data()

    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.

  abstractmethod async get_chat_data()

    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:

    - `dict`

    - The type from `telegram.ext.ContextTypes.chat_data` if `telegram.ext.ContextTypes` is used.

    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`]

  abstractmethod async get_conversations(name)

    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`

  abstractmethod async get_user_data()

    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:

    - `dict`

    - The type from `telegram.ext.ContextTypes.user_data` if `telegram.ext.ContextTypes` is used.

    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`]

  abstractmethod async refresh_bot_data(bot_data)

    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`.

  abstractmethod async refresh_chat_data(chat_id, chat_data)

    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:

      - chat_id (`int`) – The chat ID this `chat_data` is associated with.

      - chat_data (`dict` | `telegram.ext.ContextTypes.chat_data`) – The `chat_data` of a single chat.

  abstractmethod async refresh_user_data(user_id, user_data)

    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:

      - user_id (`int`) – The user ID this `user_data` is associated with.

      - user_data (`dict` | `telegram.ext.ContextTypes.user_data`) – The `user_data` of a single user.

  set_bot(bot)

    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`.

  abstractmethod async update_bot_data(data)

    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`.

  abstractmethod async update_callback_data(data)

    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`.

  abstractmethod async update_chat_data(chat_id, data)

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

    Parameters:

      - chat_id (`int`) – The chat the data might have been changed for.

      - data (`dict` | `telegram.ext.ContextTypes.chat_data`) – The `telegram.ext.Application.chat_data` `[chat_id]`.

  abstractmethod async update_conversation(name, key, new_state)

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

    Parameters:

      - name (`str`) – The handler’s name.

      - key (`tuple`) – The key the state is changed for.

      - new_state (`object`) – The new state for the given key.

  property update_interval

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

    Added in version 20.0.

    Type:

      `float`

  abstractmethod async update_user_data(user_id, data)

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

    Parameters:

      - user_id (`int`) – The user the data might have been changed for.

      - data (`dict` | `telegram.ext.ContextTypes.user_data`) – The `telegram.ext.Application.user_data` `[user_id]`.