telegram.ext.DictPersistence¶
-
class
telegram.ext.
DictPersistence
(store_user_data: bool = True, store_chat_data: bool = True, store_bot_data: bool = True, user_data_json: str = '', chat_data_json: str = '', bot_data_json: str = '', conversations_json: str = '')¶ 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
-
bot_data
¶ The bot_data as a dict.
Type: dict
-
bot_data_json
¶ The bot_data serialized as a JSON-string.
Type: str
-
chat_data
¶ The chat_data as a dict.
Type: dict
-
chat_data_json
¶ The chat_data serialized as a JSON-string.
Type: str
-
conversations
¶ The conversations as a dict.
Type: dict
-
conversations_json
¶ The conversations serialized as a JSON-string.
Type: str
-
get_bot_data
() → Dict[object, object]¶ Returns the bot_data created from the
bot_data_json
or an emptydict
.Returns: The restored bot data. Return type: dict
-
get_chat_data
() → DefaultDict[int, Dict[object, object]]¶ Returns the chat_data created from the
chat_data_json
or an emptydefaultdict
.Returns: The restored chat data. Return type: defaultdict
-
get_conversations
(name: str) → Dict[Tuple[int, ...], Optional[object]]¶ Returns the conversations created from the
conversations_json
or an emptydict
.Returns: The restored conversations data. Return type: dict
-
get_user_data
() → DefaultDict[int, Dict[object, object]]¶ 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: Dict[KT, VT]) → None¶ Will update the bot_data (if changed).
Parameters: data ( dict
) – Thetelegram.ext.dispatcher.bot_data
.
-
update_chat_data
(chat_id: int, data: Dict[KT, VT]) → None¶ 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].
- chat_id (
-
update_conversation
(name: str, key: Tuple[int, ...], new_state: Optional[object]) → None¶ 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.
- name (
-
update_user_data
(user_id: int, data: Dict[KT, VT]) → None¶ 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].
- user_id (
-
user_data
¶ The user_data as a dict.
Type: dict
-
user_data_json
¶ The user_data serialized as a JSON-string.
Type: str
- store_user_data (