telegram.ext.BasePersistence¶
-
class
telegram.ext.BasePersistence(*args, **kwargs)¶ Bases:
Generic[telegram.ext.utils.types.UD,telegram.ext.utils.types.CD,telegram.ext.utils.types.BD],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:
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(),update_bot_data()orrefresh_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_callback_data (
bool, optional) –Whether callback_data should be saved by this persistence class. Default is
False.New in version 13.6.
-
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
-
store_callback_data¶ Optional. Whether callback_data should be saved by this persistence class.
New in version 13.6.
- Type
bool
-
REPLACED_BOT: ClassVar[str] = 'bot_instance_replaced_by_ptb_persistence'¶ Placeholder for
telegram.Botinstances replaced in saved data.- Type
str
-
flush()¶ 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.
-
abstract
get_bot_data()¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. It should return thebot_dataif stored, or an emptytelegram.ext.utils.types.BD.- Returns
The restored bot data.
- Return type
-
get_callback_data()¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. If callback data was stored, it should be returned.New in version 13.6.
- Returns
The restored meta data or
None, if no data was stored.- Return type
Optional[
telegram.ext.utils.types.CDCData]
-
abstract
get_chat_data()¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. It should return thechat_dataif stored, or an emptydefaultdict(telegram.ext.utils.types.CD)with integer keys.- Returns
The restored chat data.
- Return type
DefaultDict[
int,telegram.ext.utils.types.CD]
-
abstract
get_conversations(name)¶ 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 emptydict- Parameters
name (
str) – The handlers name.- Returns
The restored conversations for the handler.
- Return type
dict
-
abstract
get_user_data()¶ Will be called by
telegram.ext.Dispatcherupon creation with a persistence object. It should return theuser_dataif stored, or an emptydefaultdict(telegram.ext.utils.types.UD)with integer keys.- Returns
The restored user data.
- Return type
DefaultDict[
int,telegram.ext.utils.types.UD]
-
insert_bot(obj)¶ 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__slots__attribute, excluding classes and objects that can’t be copied withcopy.copy. If the parsing of an object fails, the object will be returned unchanged and the error will be logged.- Parameters
obj (
object) – The object- Returns
Copy of the object with Bot instances inserted.
- Return type
obj
-
refresh_bot_data(bot_data)¶ Will be called by the
telegram.ext.Dispatcherbefore passing thebot_datato a callback. Can be used to update data stored inbot_datafrom an external source.New in version 13.6.
- Parameters
bot_data (
telegram.ext.utils.types.BD) – Thebot_data.
-
refresh_chat_data(chat_id, chat_data)¶ Will be called by the
telegram.ext.Dispatcherbefore passing thechat_datato a callback. Can be used to update data stored inchat_datafrom an external source.New in version 13.6.
- Parameters
chat_id (
int) – The chat ID thischat_datais associated with.chat_data (
telegram.ext.utils.types.CD) – Thechat_dataof a single chat.
-
refresh_user_data(user_id, user_data)¶ Will be called by the
telegram.ext.Dispatcherbefore passing theuser_datato a callback. Can be used to update data stored inuser_datafrom an external source.New in version 13.6.
- Parameters
user_id (
int) – The user ID thisuser_datais associated with.user_data (
telegram.ext.utils.types.UD) – Theuser_dataof a single user.
-
classmethod
replace_bot(obj)¶ 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__slots__attribute, excluding classes and objects that can’t be copied withcopy.copy. If the parsing of an object fails, the object will be returned unchanged and the error will be logged.- Parameters
obj (
object) – The object- Returns
Copy of the object with Bot instances replaced.
- Return type
obj
-
set_bot(bot)¶ Set the Bot to be used by this persistence instance.
- Parameters
bot (
telegram.Bot) – The bot.
-
abstract
update_bot_data(data)¶ Will be called by the
telegram.ext.Dispatcherafter a handler has handled an update.- Parameters
data (
telegram.ext.utils.types.BD) – Thetelegram.ext.Dispatcher.bot_data.
-
update_callback_data(data)¶ Will be called by the
telegram.ext.Dispatcherafter a handler has handled an update.New in version 13.6.
- Parameters
data (
telegram.ext.utils.types.CDCData) – The relevant data to restoretelegram.ext.CallbackDataCache.
-
abstract
update_chat_data(chat_id, data)¶ 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 (
telegram.ext.utils.types.CD) – Thetelegram.ext.Dispatcher.chat_data[chat_id].
-
abstract
update_conversation(name, key, new_state)¶ Will be called when a
telegram.ext.ConversationHandlerchanges states. 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.
-
abstract
update_user_data(user_id, data)¶ 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 (
telegram.ext.utils.types.UD) – Thetelegram.ext.Dispatcher.user_data[user_id].