telegram.ext.PicklePersistence

class telegram.ext.PicklePersistence(filename: str, store_user_data: bool = True, store_chat_data: bool = True, store_bot_data: bool = True, single_file: bool = True, on_flush: bool = False)

Bases: telegram.ext.basepersistence.BasePersistence

Using python’s builtin pickle for making you 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().

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 user_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
single_file

Optional. When False will store 3 separate files of filename_user_data, filename_chat_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
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 user_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 .
  • single_file (bool, optional) – When False will store 3 separate files of filename_user_data, filename_chat_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.
flush() → None

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

get_bot_data() → Dict[Any, Any]

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

Returns:The restored bot data.
Return type:dict
get_chat_data() → DefaultDict[int, Dict[Any, Any]]

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

Returns:The restored chat data.
Return type:defaultdict
get_conversations(name: str) → Dict[Tuple[int, ...], Optional[object]]

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

Parameters:name (str) – The handlers name.
Returns:The restored conversations for the handler.
Return type:dict
get_user_data() → DefaultDict[int, Dict[Any, Any]]

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

Returns:The restored user data.
Return type:defaultdict
update_bot_data(data: Dict[KT, VT]) → None

Will update the bot_data and depending on on_flush save the pickle file.

Parameters:data (dict) – The telegram.ext.dispatcher.bot_data.
update_chat_data(chat_id: int, data: Dict[KT, VT]) → None

Will update the chat_data and depending on on_flush save the pickle file.

Parameters:
  • chat_id (int) – The chat the data might have been changed for.
  • data (dict) – The telegram.ext.dispatcher.chat_data [chat_id].
update_conversation(name: str, key: Tuple[int, ...], new_state: Optional[object]) → None

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: int, data: Dict[KT, VT]) → None

Will update the user_data and depending on on_flush save the pickle file.

Parameters:
  • user_id (int) – The user the data might have been changed for.
  • data (dict) – The telegram.ext.dispatcher.user_data [user_id].