telegram.ext.DictPersistence

class telegram.ext.DictPersistence(*args, **kwargs)

Bases: Generic[telegram.ext.utils.types.UD, telegram.ext.utils.types.CD, telegram.ext.utils.types.BD], abc.ABC

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

Note

This class does not implement a flush() method, meaning that 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.

Warning

DictPersistence will try to replace telegram.Bot instances by REPLACED_BOT and insert the bot set with telegram.ext.BasePersistence.set_bot() upon loading of the data. This is to ensure that changes to the bot apply to the saved objects, too. If you change the bots token, this may lead to e.g. Chat not found errors. For the limitations on replacing bots see telegram.ext.BasePersistence.replace_bot() and telegram.ext.BasePersistence.insert_bot().

Parameters
  • store_user_data (bool, optional) – Whether user_data should be saved by this persistence class. Default is True.

  • store_chat_data (bool, optional) – Whether chat_data should be saved by this persistence class. Default is True.

  • store_bot_data (bool, optional) – Whether bot_data should be saved by this persistence class. Default is True.

  • store_callback_data (bool, optional) –

    Whether callback_data should be saved by this persistence class. Default is False.

    New in version 13.6.

  • 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 "".

  • callback_data_json (str, optional) –

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

    New in version 13.6.

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

store_user_data

Whether user_data should be saved by this persistence class.

Type

bool

store_chat_data

Whether chat_data should be saved by this persistence class.

Type

bool

store_bot_data

Whether bot_data should be saved by this persistence class.

Type

bool

store_callback_data

Whether callback_data be saved by this persistence class.

New in version 13.6.

Type

bool

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 meta data on the stored callback data.

New in version 13.6.

Type

telegram.ext.utils.types.CDCData

property callback_data_json

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

New 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

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

get_callback_data()

Returns the callback_data created from the callback_data_json or None.

New in version 13.6.

Returns

The restored meta data or None, if no data was stored.

Return type

Optional[telegram.ext.utils.types.CDCData]

get_chat_data()

Returns the chat_data created from the chat_data_json or an empty defaultdict.

Returns

The restored chat data.

Return type

defaultdict

get_conversations(name)

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

Returns

The restored conversations data.

Return type

dict

get_user_data()

Returns the user_data created from the user_data_json or an empty defaultdict.

Returns

The restored user data.

Return type

defaultdict

refresh_bot_data(bot_data)

Does nothing.

New in version 13.6.

refresh_chat_data(chat_id, chat_data)

Does nothing.

New in version 13.6.

refresh_user_data(user_id, user_data)

Does nothing.

New in version 13.6.

update_bot_data(data)

Will update the bot_data (if changed).

Parameters

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

update_callback_data(data)

Will update the callback_data (if changed).

New in version 13.6.

Parameters

data (telegram.ext.utils.types.CDCData) – The relevant data to restore telegram.ext.CallbackDataCache.

update_chat_data(chat_id, data)

Will update the chat_data (if changed).

Parameters
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 | any) – The new state for the given key.

update_user_data(user_id, data)

Will update the user_data (if changed).

Parameters
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