telegram.ext.PicklePersistence

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

Bases: telegram.ext.basepersistence.BasePersistence[telegram.ext.utils.types.UD, telegram.ext.utils.types.CD, telegram.ext.utils.types.BD]

Using python’s builtin pickle for making your bot persistent.

Warning

PicklePersistence 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
  • filename (str) – The filename for storing the pickle files. When single_file is False this will be used as a prefix.

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

  • single_file (bool, optional) – When False will store 5 separate files of filename_user_data, filename_bot_data, filename_chat_data, filename_callback_data and filename_conversations. Default is True.

  • on_flush (bool, optional) – When True will only save to file when flush() is called and keep data in memory until that happens. When False will store data on any transaction and on call to flush(). Default is False.

  • context_types (telegram.ext.ContextTypes, optional) –

    Pass an instance of telegram.ext.ContextTypes to customize the types used in the context interface. If not passed, the defaults documented in telegram.ext.ContextTypes will be used.

    New in version 13.6.

filename

The filename for storing the pickle files. When single_file is False this will be used as a prefix.

Type

str

store_user_data

Optional. Whether user_data should be saved by this persistence class.

Type

bool

store_chat_data

Optional. Whether chat_data should be saved by this persistence class.

Type

bool

store_bot_data

Optional. Whether bot_data should be saved by this persistence class.

Type

bool

store_callback_data

Optional. Whether callback_data be saved by this persistence class.

New in version 13.6.

Type

bool

single_file

Optional. When False will store 5 separate files of filename_user_data, filename_bot_data, filename_chat_data, filename_callback_data and filename_conversations. Default is True.

Type

bool

on_flush

When True will only save to file when flush() is called and keep data in memory until that happens. When False will store data on any transaction and on call to flush(). Default is False.

Type

bool, optional

context_types

Container for the types used in the context interface.

New in version 13.6.

Type

telegram.ext.ContextTypes

flush()

Will save all data in memory to pickle file(s).

get_bot_data()

Returns the bot_data from the pickle file if it exists or an empty object of type telegram.ext.utils.types.BD.

Returns

The restored bot data.

Return type

telegram.ext.utils.types.BD

get_callback_data()

Returns the callback data from the pickle file if it exists 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 from the pickle file if it exists or an empty defaultdict.

Returns

The restored chat data.

Return type

DefaultDict[int, telegram.ext.utils.types.CD]

get_conversations(name)

Returns the conversations from the pickle file if it exists or an empty dict.

Parameters

name (str) – The handlers name.

Returns

The restored conversations for the handler.

Return type

dict

get_user_data()

Returns the user_data from the pickle file if it exists or an empty defaultdict.

Returns

The restored user data.

Return type

DefaultDict[int, telegram.ext.utils.types.UD]

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 and depending on on_flush save the pickle file.

Parameters

data (telegram.ext.utils.types.BD) – The telegram.ext.Dispatcher.bot_data.

update_callback_data(data)

Will update the callback_data (if changed) and depending on on_flush save the pickle file.

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 and depending on on_flush save the pickle file.

Parameters
update_conversation(name, key, new_state)

Will update the conversations for the given handler and depending on on_flush save the pickle file.

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 and depending on on_flush save the pickle file.

Parameters