PicklePersistence¶
- class telegram.ext.PicklePersistence(filepath, store_data=None, single_file=True, on_flush=False, update_interval=60, context_types=None)[source]¶
Bases:
telegram.ext.BasePersistenceUsing python’s builtin
picklefor 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 intoApplication.Note
This implementation of
BasePersistenceuses the functionality of the pickle module to support serialization of bot instances. Specifically any reference tobotwill be replaced by a placeholder before pickling andbotwill be inserted back when loading the data.Examples
See also
Available In
Changed in version 20.0:
The parameters and attributes
store_*_datawere replaced bystore_data.The parameter and attribute
filenamewere replaced byfilepath.filepathnow also acceptspathlib.Pathas argument.
- Parameters:
filepath (
str|pathlib.Path) – The filepath for storing the pickle files. Whensingle_fileisFalsethis will be used as a prefix.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.single_file (
bool, optional) – WhenFalsewill store 5 separate files of filename_user_data, filename_bot_data, filename_chat_data, filename_callback_data and filename_conversations. Default isTrue.on_flush (
bool, optional) – WhenTruewill only save to file whenflush()is called and keep data in memory until that happens. WhenFalsewill store data on any transaction and on call toflush(). Default isFalse.context_types (
telegram.ext.ContextTypes, optional) –Pass an instance of
telegram.ext.ContextTypesto customize the types used in thecontextinterface. If not passed, the defaults documented intelegram.ext.ContextTypeswill be used.Added in version 13.6.
update_interval (
int|float, optional) –The
Applicationwill 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.
- filepath[source]¶
The filepath for storing the pickle files. When
single_fileisFalsethis will be used as a prefix.- Type:
str|pathlib.Path
- single_file[source]¶
Optional. When
Falsewill store 5 separate files of filename_user_data, filename_bot_data, filename_chat_data, filename_callback_data and filename_conversations. Default isTrue.- Type:
bool
- on_flush[source]¶
Optional. When
Truewill only save to file whenflush()is called and keep data in memory until that happens. WhenFalsewill store data on any transaction and on call toflush(). Default isFalse.- Type:
bool
- context_types[source]¶
Container for the types used in the
contextinterface.Added in version 13.6.
- async drop_chat_data(chat_id)[source]¶
Will delete the specified key from the
chat_dataand depending onon_flushsave the pickle file.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_dataand depending onon_flushsave the pickle file.Added in version 20.0.
- Parameters:
user_id (
int) – The user id to delete from the persistence.
- async get_bot_data()[source]¶
Returns the bot_data from the pickle file if it exists or an empty object of type
dict|telegram.ext.ContextTypes.bot_data.- Returns:
The restored bot data.
- Return type:
- async get_callback_data()[source]¶
Returns the callback data from the pickle file if it exists or
None.Added in version 13.6.
- Returns:
tuple[list[tuple[
str,float, dict[str,object]]], dict[str,str]] |None: The restored metadata orNone, if no data was stored.
- async get_chat_data()[source]¶
Returns the chat_data from the pickle file if it exists or an empty
dict.- Returns:
The restored chat data.
- Return type:
dict[
int,dict]
- async get_conversations(name)[source]¶
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
- async get_user_data()[source]¶
Returns the user_data from the pickle file if it exists or an empty
dict.- Returns:
The restored user data.
- Return type:
dict[
int,dict]
- async update_bot_data(data)[source]¶
Will update the bot_data and depending on
on_flushsave the pickle file.- Parameters:
data (
dict|telegram.ext.ContextTypes.bot_data) – Thetelegram.ext.Application.bot_data.
- async update_callback_data(data)[source]¶
Will update the callback_data (if changed) and depending on
on_flushsave the pickle file.Added in version 13.6.
- Parameters:
data (tuple[list[tuple[
str,float, dict[str,object]]], dict[str,str]]) – The relevant data to restoretelegram.ext.CallbackDataCache.
- async update_chat_data(chat_id, data)[source]¶
Will update the chat_data and depending on
on_flushsave the pickle file.- Parameters:
chat_id (
int) – The chat the data might have been changed for.data (
dict) – Thetelegram.ext.Application.chat_data[chat_id].