DictPersistence¶
- class telegram.ext.DictPersistence(store_data=None, user_data_json='', chat_data_json='', bot_data_json='', conversations_json='', callback_data_json='', update_interval=60)[source]¶
Bases:
telegram.ext.BasePersistence
Using Python’s
dict
andjson
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
Data managed by
DictPersistence
is in-memory only and will be lost when the bot shuts down. This is, becauseDictPersistence
is mainly intended as starting point for custom persistence classes that need to JSON-serialize the stored data before writing them to file/database.This implementation of
BasePersistence
does not handle data that cannot be serialized byjson.dumps()
.
Available In
See also
Changed in version 20.0: The parameters and attributes
store_*_data
were replaced bystore_data
.- Parameters:
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.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""
.conversations_json (
str
, optional) – JSON string that will be used to reconstruct conversation 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
""
.Added 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.Added in version 20.0.
- property callback_data_json[source]¶
The metadata on the stored callback data as a JSON-string.
Added in version 13.6.
- Type:
- async drop_chat_data(chat_id)[source]¶
Will delete the specified key from the
chat_data
.Added in version 20.0.
- async drop_user_data(user_id)[source]¶
Will delete the specified key from the
user_data
.Added in version 20.0.
- async get_bot_data()[source]¶
Returns the bot_data created from the
bot_data_json
or an emptydict
.- Returns:
The restored bot data.
- Return type:
- async get_callback_data()[source]¶
Returns the callback_data created from the
callback_data_json
orNone
.Added in version 13.6.
- async get_chat_data()[source]¶
Returns the chat_data created from the
chat_data_json
or an emptydict
.- Returns:
The restored chat data.
- Return type:
- async get_conversations(name)[source]¶
Returns the conversations created from the
conversations_json
or an emptydict
.- Returns:
The restored conversations data.
- Return type:
- async get_user_data()[source]¶
Returns the user_data created from the
user_data_json
or an emptydict
.- Returns:
The restored user data.
- Return type:
- async update_bot_data(data)[source]¶
Will update the bot_data (if changed).
- Parameters:
data (
dict
) – Thetelegram.ext.Application.bot_data
.
- async update_callback_data(data)[source]¶
Will update the callback_data (if changed).
Added in version 13.6.
- async update_chat_data(chat_id, data)[source]¶
Will update the chat_data (if changed).
- Parameters:
chat_id (
int
) – The chat the data might have been changed for.data (
dict
) – Thetelegram.ext.Application.chat_data
[chat_id]
.
- async update_conversation(name, key, new_state)[source]¶
Will update the conversations for the given handler.