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)[source]

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

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[source]

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

Type:

PersistenceInput

property bot_data[source]

The bot_data as a dict.

Type:

dict

property bot_data_json[source]

The bot_data serialized as a JSON-string.

Type:

str

property callback_data[source]

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[source]

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

Added in version 13.6.

Type:

str

property chat_data[source]

The chat_data as a dict.

Type:

dict

property chat_data_json[source]

The chat_data serialized as a JSON-string.

Type:

str

property conversations[source]

The conversations as a dict.

Type:

dict

property conversations_json[source]

The conversations serialized as a JSON-string.

Type:

str

async drop_chat_data(chat_id)[source]

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)[source]

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()[source]

Does nothing.

Added in version 20.0.

async get_bot_data()[source]

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()[source]

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()[source]

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)[source]

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

Returns:

The restored conversations data.

Return type:

dict

async get_user_data()[source]

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)[source]

Does nothing.

Added in version 13.6.

async refresh_chat_data(chat_id, chat_data)[source]

Does nothing.

Added in version 13.6.

async refresh_user_data(user_id, user_data)[source]

Does nothing.

Added in version 13.6.

async update_bot_data(data)[source]

Will update the bot_data (if changed).

Parameters:

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

async update_callback_data(data)[source]

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)[source]

Will update the chat_data (if changed).

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

Will update the conversations for the given handler.

Parameters:
async update_user_data(user_id, data)[source]

Will update the user_data (if changed).

Parameters:
property user_data[source]

The user_data as a dict.

Type:

dict

property user_data_json[source]

The user_data serialized as a JSON-string.

Type:

str