telegram.ext.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.BasePersistence
Using python’s builtin
pickle
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 intoApplication
.Note
This implementation of
BasePersistence
uses the functionality of the pickle module to support serialization of bot instances. Specifically any reference tobot
will be replaced by a placeholder before pickling andbot
will be inserted back when loading the data.Changed in version 20.0:
The parameters and attributes
store_*_data
were replaced bystore_data
.The parameter and attribute
filename
were replaced byfilepath
.filepath
now also acceptspathlib.Path
as argument.
- Parameters
filepath (
str
|pathlib.Path
) – The filepath for storing the pickle files. Whensingle_file
isFalse
this 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) – WhenFalse
will 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) – WhenTrue
will only save to file whenflush()
is called and keep data in memory until that happens. WhenFalse
will store data on any transaction and on call toflush()
. Default isFalse
.context_types (
telegram.ext.ContextTypes
, optional) –Pass an instance of
telegram.ext.ContextTypes
to customize the types used in thecontext
interface. If not passed, the defaults documented intelegram.ext.ContextTypes
will be used.New 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.New in version 20.0.
- filepath[source]¶
The filepath for storing the pickle files. When
single_file
isFalse
this will be used as a prefix.- Type
- single_file[source]¶
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 isTrue
.- Type
- on_flush[source]¶
When
True
will only save to file whenflush()
is called and keep data in memory until that happens. WhenFalse
will store data on any transaction and on call toflush()
. Default isFalse
.- Type
bool
, optional
- async drop_chat_data(chat_id)[source]¶
Will delete the specified key from the
chat_data
and depending onon_flush
save the pickle file.New in version 20.0.
- async drop_user_data(user_id)[source]¶
Will delete the specified key from the
user_data
and depending onon_flush
save the pickle file.New in version 20.0.
- 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
.New in version 13.6.
- async get_chat_data()[source]¶
Returns the chat_data from the pickle file if it exists or an empty
dict
.
- async get_conversations(name)[source]¶
Returns the conversations from the pickle file if it exists or an empty dict.
- async get_user_data()[source]¶
Returns the user_data from the pickle file if it exists or an empty
dict
.
- async update_bot_data(data)[source]¶
Will update the bot_data and depending on
on_flush
save 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_flush
save the pickle file.New in version 13.6.
- async update_chat_data(chat_id, data)[source]¶
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
) – Thetelegram.ext.Application.chat_data
[chat_id]
.