Skip to content
Back to top
View this page

# DictPersistence

class telegram.ext.DictPersistence(store_data=None, user_data_json='', chat_data_json='', bot_data_json='', conversations_json='', callback_data_json='', update_interval=60)

  Bases: `telegram.ext.BasePersistence`

  Using Python’s `dict` and `json` for making your bot persistent.

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

  Note

  - Data managed by `DictPersistence` is in-memory only and will be lost when the bot shuts down. This is, because `DictPersistence` is mainly intended as starting point for custom persistence classes that need to JSON-serialize the stored data before writing them to file/database.

  - This implementation of `BasePersistence` does not handle data that cannot be serialized by `json.dumps()`.

  See also

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

  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.

    - user_data_json (`str`, optional) – JSON string that will be used to reconstruct user_data on creating this persistence. Default is `""`.

    - chat_data_json (`str`, optional) – JSON string that will be used to reconstruct chat_data on creating this persistence. Default is `""`.

    - bot_data_json (`str`, optional) – JSON string that will be used to reconstruct bot_data on creating this persistence. Default is `""`.

    - conversations_json (`str`, optional) – JSON string that will be used to reconstruct conversation on creating this persistence. Default is `""`.

    - callback_data_json (`str`, optional) –

      JSON string that will be used to reconstruct callback_data on creating this persistence. Default is `""`.

      Added in version 13.6.

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

  property bot_data

    The bot_data as a dict.

    Type:

      `dict`

  property bot_data_json

    The bot_data serialized as a JSON-string.

    Type:

      `str`

  property callback_data

    The metadata on the stored callback data.

    Added in version 13.6.

    Type:

      tuple[list[tuple[`str`, `float`, dict[`str`, `object`]]], dict[`str`, `str`]]

  property callback_data_json

    The metadata on the stored callback data as a JSON-string.

    Added in version 13.6.

    Type:

      `str`

  property chat_data

    The chat_data as a dict.

    Type:

      `dict`

  property chat_data_json

    The chat_data serialized as a JSON-string.

    Type:

      `str`

  property conversations

    The conversations as a dict.

    Type:

      `dict`

  property conversations_json

    The conversations serialized as a JSON-string.

    Type:

      `str`

  async drop_chat_data(chat_id)

    Will delete the specified key from the `chat_data`.

    Added in version 20.0.

    Parameters:

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

  async drop_user_data(user_id)

    Will delete the specified key from the `user_data`.

    Added in version 20.0.

    Parameters:

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

  async flush()

    Does nothing.

    Added in version 20.0.

    See also

    `telegram.ext.BasePersistence.flush()`

  async get_bot_data()

    Returns the bot_data created from the `bot_data_json` or an empty `dict`.

    Returns:

      The restored bot data.

    Return type:

      `dict`

  async get_callback_data()

    Returns the callback_data created from the `callback_data_json` or `None`.

    Added in version 13.6.

    Returns:

      The restored metadata or `None`, if no data was stored.

    Return type:

      tuple[list[tuple[`str`, `float`, dict[`str`, `object`]]], dict[`str`, `str`]]

  async get_chat_data()

    Returns the chat_data created from the `chat_data_json` or an empty `dict`.

    Returns:

      The restored chat data.

    Return type:

      `dict`

  async get_conversations(name)

    Returns the conversations created from the `conversations_json` or an empty `dict`.

    Returns:

      The restored conversations data.

    Return type:

      `dict`

  async get_user_data()

    Returns the user_data created from the `user_data_json` or an empty `dict`.

    Returns:

      The restored user data.

    Return type:

      `dict`

  async refresh_bot_data(bot_data)

    Does nothing.

    Added in version 13.6.

    See also

    `telegram.ext.BasePersistence.refresh_bot_data()`

  async refresh_chat_data(chat_id, chat_data)

    Does nothing.

    Added in version 13.6.

    See also

    `telegram.ext.BasePersistence.refresh_chat_data()`

  async refresh_user_data(user_id, user_data)

    Does nothing.

    Added in version 13.6.

    See also

    `telegram.ext.BasePersistence.refresh_user_data()`

  async update_bot_data(data)

    Will update the bot_data (if changed).

    Parameters:

      data (`dict`) – The `telegram.ext.Application.bot_data`.

  async update_callback_data(data)

    Will update the callback_data (if changed).

    Added in version 13.6.

    Parameters:

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

  async update_chat_data(chat_id, data)

    Will update the chat_data (if changed).

    Parameters:

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

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

  async update_conversation(name, key, new_state)

    Will update the conversations for the given handler.

    Parameters:

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

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

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

  async update_user_data(user_id, data)

    Will update the user_data (if changed).

    Parameters:

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

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

  property user_data

    The user_data as a dict.

    Type:

      `dict`

  property user_data_json

    The user_data serialized as a JSON-string.

    Type:

      `str`