telegram.ext.DictPersistence¶
-
class
telegram.ext.
DictPersistence
(*args, **kwargs)¶ Bases:
telegram.ext.basepersistence.BasePersistence
Using python’s dicts and json for making your bot persistent.
Note
This class does not implement a
flush()
method, meaning that data managed byDictPersistence
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.Warning
DictPersistence
will try to replacetelegram.Bot
instances byREPLACED_BOT
and insert the bot set withtelegram.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 seetelegram.ext.BasePersistence.replace_bot()
andtelegram.ext.BasePersistence.insert_bot()
.- Parameters
store_user_data (
bool
, optional) – Whether user_data should be saved by this persistence class. Default isTrue
.store_chat_data (
bool
, optional) – Whether user_data should be saved by this persistence class. Default isTrue
.store_bot_data (
bool
, optional) – Whether bot_data should be saved by this persistence class. Default isTrue
.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""
.
-
store_user_data
¶ Whether user_data should be saved by this persistence class.
- Type
bool
-
store_chat_data
¶ Whether chat_data should be saved by this persistence class.
- Type
bool
-
store_bot_data
¶ Whether bot_data should be saved by this persistence class.
- Type
bool
-
property
bot_data
¶ The bot_data as a dict.
- Type
dict
-
property
bot_data_json
¶ The bot_data serialized as a JSON-string.
- Type
str
-
property
chat_data
¶ The chat_data as a dict.
- Type
dict
-
property
chat_data_json
¶ The chat_data serialized as a JSON-string.
- Type
str
-
property
conversations
¶ The conversations as a dict.
- Type
dict
-
property
conversations_json
¶ The conversations serialized as a JSON-string.
- Type
str
-
get_bot_data
()¶ Returns the bot_data created from the
bot_data_json
or an emptydict
.- Returns
The restored bot data.
- Return type
dict
-
get_chat_data
()¶ Returns the chat_data created from the
chat_data_json
or an emptydefaultdict
.- Returns
The restored chat data.
- Return type
defaultdict
-
get_conversations
(name)¶ Returns the conversations created from the
conversations_json
or an emptydict
.- Returns
The restored conversations data.
- Return type
dict
-
get_user_data
()¶ Returns the user_data created from the
user_data_json
or an emptydefaultdict
.- Returns
The restored user data.
- Return type
defaultdict
-
update_bot_data
(data)¶ Will update the bot_data (if changed).
- Parameters
data (
dict
) – Thetelegram.ext.dispatcher.bot_data
.
-
update_chat_data
(chat_id, data)¶ Will update the chat_data (if changed).
- Parameters
chat_id (
int
) – The chat the data might have been changed for.data (
dict
) – Thetelegram.ext.dispatcher.chat_data
[chat_id].
-
update_conversation
(name, key, new_state)¶ Will update the conversations for the given handler.
- 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 (if changed).
- Parameters
user_id (
int
) – The user the data might have been changed for.data (
dict
) – Thetelegram.ext.dispatcher.user_data
[user_id].
-
property
user_data
¶ The user_data as a dict.
- Type
dict
-
property
user_data_json
¶ The user_data serialized as a JSON-string.
- Type
str