telegram.ext.BasePersistence¶
-
class
telegram.ext.
BasePersistence
(store_user_data=True, store_chat_data=True, store_bot_data=True)¶ Bases:
abc.ABC
Interface class for adding persistence to your bot. Subclass this object for different implementations of a persistent bot.
All relevant methods must be overwritten. This means:
- If
store_bot_data
isTrue
you must overwriteget_bot_data()
andupdate_bot_data()
. - If
store_chat_data
isTrue
you must overwriteget_chat_data()
andupdate_chat_data()
. - If
store_user_data
isTrue
you must overwriteget_user_data()
andupdate_user_data()
. - If you want to store conversation data with
telegram.ext.ConversationHandler
, you must overwriteget_conversations()
andupdate_conversation()
. flush()
will be called when the bot is shutdown.
-
store_user_data
¶ Optional, Whether user_data should be saved by this persistence class.
Type: bool
-
store_chat_data
¶ Optional. Whether chat_data should be saved by this persistence class.
Type: bool
-
store_bot_data
¶ Optional. Whether bot_data should be saved by this persistence class.
Type: bool
Parameters: - store_user_data (
bool
, optional) – Whether user_data should be saved by this persistence class. Default isTrue
. - store_chat_data (
bool
, optional) – Whether chat_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
.
-
flush
()¶ Will be called by
telegram.ext.Updater
upon receiving a stop signal. Gives the persistence a chance to finish up saving or close a database connection gracefully. If this is not of any importance just pass will be sufficient.
-
get_bot_data
()¶ “Will be called by
telegram.ext.Dispatcher
upon creation with a persistence object. It should return the bot_data if stored, or an emptydict
.Returns: The restored bot data. Return type: defaultdict
-
get_chat_data
()¶ “Will be called by
telegram.ext.Dispatcher
upon creation with a persistence object. It should return the chat_data if stored, or an emptydefaultdict(dict)
.Returns: The restored chat data. Return type: defaultdict
-
get_conversations
(name)¶ “Will be called by
telegram.ext.Dispatcher
when atelegram.ext.ConversationHandler
is added iftelegram.ext.ConversationHandler.persistent
isTrue
. It should return the conversations for the handler with name or an emptydict
Parameters: name ( str
) – The handlers name.Returns: The restored conversations for the handler. Return type: dict
-
get_user_data
()¶ “Will be called by
telegram.ext.Dispatcher
upon creation with a persistence object. It should return the user_data if stored, or an emptydefaultdict(dict)
.Returns: The restored user data. Return type: defaultdict
-
update_bot_data
(data)¶ Will be called by the
telegram.ext.Dispatcher
after a handler has handled an update.Parameters: data ( dict
) – Thetelegram.ext.dispatcher.bot_data
.
-
update_chat_data
(chat_id, data)¶ Will be called by the
telegram.ext.Dispatcher
after a handler has handled an update.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, key, new_state)¶ Will be called when a
telegram.ext.ConversationHandler.update_state
is called. this allows the storeage of the new state in the persistence.Parameters: - name (
str
) – The handlers 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, data)¶ Will be called by the
telegram.ext.Dispatcher
after a handler has handled an update.Parameters: - user_id (
int
) – The user the data might have been changed for. - data (
dict
) – Thetelegram.ext.dispatcher.user_data
[user_id].
- user_id (
- If