telegram.ext.BasePersistence¶
-
class
telegram.ext.BasePersistence(store_user_data: bool = True, store_chat_data: bool = True, store_bot_data: bool = True)¶ Bases:
abc.ABCInterface class for adding persistence to your bot. Subclass this object for different implementations of a persistent bot.
All relevant methods must be overwritten. This includes:
get_bot_data()update_bot_data()get_chat_data()update_chat_data()get_user_data()update_user_data()get_conversations()update_conversation()flush()
If you don’t actually need one of those methods, a simple
passis enough. For example, ifstore_bot_data=False, you don’t needget_bot_data()andupdate_bot_data().Warning
Persistence will try to replace
telegram.Botinstances byREPLACED_BOTand insert the bot set withset_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 founderrors. For the limitations on replacing bots seereplace_bot()andinsert_bot().Note
replace_bot()andinsert_bot()are used independently of the implementation of theupdate/get_*()methods, i.e. you don’t need to worry about it while implementing a custom persistence subclass.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.
-
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
-
REPLACED_BOT= 'bot_instance_replaced_by_ptb_persistence'¶ Placeholder for
telegram.Botinstances replaced in saved data.Type: str
-
flush() → None¶ Will be called by
telegram.ext.Updaterupon receiving a stop signal. Gives the persistence a chance to finish up saving or close a database connection gracefully.
-
get_bot_data() → Dict[object, object]¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. It should return thebot_dataif stored, or an emptydict.Returns: The restored bot data. Return type: dict
-
get_chat_data() → DefaultDict[int, Dict[object, object]]¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. It should return thechat_dataif stored, or an emptydefaultdict(dict).Returns: The restored chat data. Return type: defaultdict
-
get_conversations(name: str) → Dict[Tuple[int, ...], Optional[object]]¶ Will be called by
telegram.ext.Dispatcherwhen atelegram.ext.ConversationHandleris added iftelegram.ext.ConversationHandler.persistentisTrue. It should return the conversations for the handler with name or an emptydictParameters: name ( str) – The handlers name.Returns: The restored conversations for the handler. Return type: dict
-
get_user_data() → DefaultDict[int, Dict[object, object]]¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. It should return theuser_dataif stored, or an emptydefaultdict(dict).Returns: The restored user data. Return type: defaultdict
-
insert_bot(obj: object) → object¶ Replaces all instances of
REPLACED_BOTthat occur within the passed object withbot. Currently, this handles objects of typelist,tuple,set,frozenset,dict,defaultdictand objects that have a__dict__or__slot__attribute, excluding objects that can’t be copied with copy.copy.Parameters: obj ( object) – The objectReturns: Copy of the object with Bot instances inserted. Return type: obj
-
classmethod
replace_bot(obj: object) → object¶ Replaces all instances of
telegram.Botthat occur within the passed object withREPLACED_BOT. Currently, this handles objects of typelist,tuple,set,frozenset,dict,defaultdictand objects that have a__dict__or__slot__attribute, excluding objects that can’t be copied with copy.copy.Parameters: obj ( object) – The objectReturns: Copy of the object with Bot instances replaced. Return type: obj
-
set_bot(bot: telegram.bot.Bot) → None¶ Set the Bot to be used by this persistence instance.
Parameters: bot ( telegram.Bot) – The bot.
-
update_bot_data(data: Dict[KT, VT]) → None¶ Will be called by the
telegram.ext.Dispatcherafter a handler has handled an update.Parameters: data ( dict) – Thetelegram.ext.dispatcher.bot_data.
-
update_chat_data(chat_id: int, data: Dict[KT, VT]) → None¶ Will be called by the
telegram.ext.Dispatcherafter 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: str, key: Tuple[int, ...], new_state: Optional[object]) → None¶ Will be called when a
telegram.ext.ConversationHandler.update_stateis called. This allows the storage of the new state in the persistence.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 be called by the
telegram.ext.Dispatcherafter 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 (