- class telegram.ext.BasePersistence(store_data=None, update_interval=60)[source]¶
Bases:
ABC,typing.GenericInterface class for adding persistence to your bot. Subclass this object for different implementations of a persistent bot.
Attention
The interface provided by this class is intended to be accessed exclusively by
Application. Calling any of the methods below manually might interfere with the integration of persistence intoApplication.All relevant methods must be overwritten. This includes:
If you don’t actually need one of those methods, a simple
passis enough. For example, if you don’t storebot_data, you don’t needget_bot_data(),update_bot_data()orrefresh_bot_data().Note
You should avoid saving
telegram.Botinstances. This is because if you change e.g. the bots token, this won’t propagate to the serialized instances and may lead to exceptions.To prevent this, the implementation may use
botto replace bot instances with a placeholder before serialization and insertbotback when loading the data. Sincebotwill be set when the process starts, this will be the up-to-date bot instance.If the persistence implementation does not take care of this, you should make sure not to store any bot instances in the data that will be persisted. E.g. in case of
telegram.TelegramObject, one may callset_bot()to ensure that shortcuts liketelegram.Message.reply_text()are available.This class is a
Genericclass and accepts three type variables:The type of the second argument of
update_user_data(), which must coincide with the type of the second argument ofrefresh_user_data()and the values in the dictionary returned byget_user_data().The type of the second argument of
update_chat_data(), which must coincide with the type of the second argument ofrefresh_chat_data()and the values in the dictionary returned byget_chat_data().The type of the argument of
update_bot_data(), which must coincide with the type of the argument ofrefresh_bot_data()and the return value ofget_bot_data().
Changed in version 20.0:
The parameters and attributes
store_*_datawere replaced bystore_data.insert/replace_botwas dropped. Serialization of bot instances now needs to be handled by the specific implementation - see above note.
- Parameters:
store_data (
PersistenceInput, optional) – Specifies which kinds of data will be saved by this persistence instance. By default, all available kinds of data will be saved.update_interval (
int|float, optional) –The
Applicationwill update the persistence in regular intervals. This parameter specifies the time (in seconds) to wait between two consecutive runs of updating the persistence. Defaults to60seconds.Added in version 20.0.
- abstractmethod async drop_chat_data(chat_id)[source]¶
Will be called by the
telegram.ext.Application, when usingdrop_chat_data().Added in version 20.0.
- abstractmethod async drop_user_data(user_id)[source]¶
Will be called by the
telegram.ext.Application, when usingdrop_user_data().Added in version 20.0.
- abstractmethod async flush()[source]¶
Will be called by
telegram.ext.Application.stop(). Gives the persistence a chance to finish up saving or close a database connection gracefully.Changed in version 20.0: Changed this method into an
abstractmethod().
- abstractmethod async get_bot_data()[source]¶
Will be called by
telegram.ext.Applicationupon creation with a persistence object. It should return thebot_dataif stored, or an emptydict. In the latter case, thedictshould produce values corresponding to one of the following:The type from
telegram.ext.ContextTypes.bot_dataiftelegram.ext.ContextTypesare used.
- Returns:
The restored bot data.
- Return type:
dict[
int,dict|telegram.ext.ContextTypes.bot_data]
- abstractmethod async get_callback_data()[source]¶
Will be called by
telegram.ext.Applicationupon creation with a persistence object. If callback data was stored, it should be returned.Added in version 13.6.
Changed in version 20.0: Changed this method into an
abstractmethod().
- abstractmethod async get_chat_data()[source]¶
Will be called by
telegram.ext.Applicationupon creation with a persistence object. It should return thechat_dataif stored, or an emptydict. In the latter case, the dictionary should produce values corresponding to one of the following:The type from
telegram.ext.ContextTypes.chat_dataiftelegram.ext.ContextTypesis used.
- Returns:
The restored chat data.
- Return type:
dict[
int,dict|telegram.ext.ContextTypes.chat_data]
- abstractmethod async get_conversations(name)[source]¶
Will be called by
telegram.ext.Applicationwhen atelegram.ext.ConversationHandleris added iftelegram.ext.ConversationHandler.persistentisTrue. It should return the conversations for the handler withnameor an emptydict.
- abstractmethod async get_user_data()[source]¶
Will be called by
telegram.ext.Applicationupon creation with a persistence object. It should return theuser_dataif stored, or an emptydict. In the latter case, the dictionary should produce values corresponding to one of the following:The type from
telegram.ext.ContextTypes.user_dataiftelegram.ext.ContextTypesis used.
- Returns:
The restored user data.
- Return type:
dict[
int,dict|telegram.ext.ContextTypes.user_data]
- abstractmethod async refresh_bot_data(bot_data)[source]¶
Will be called by the
telegram.ext.Applicationbefore passing thebot_datato a callback. Can be used to update data stored inbot_datafrom an external source.Tip
This method is expected to edit the object
bot_datain-place instead of returning a new object.Warning
When using
concurrent_updates(), this method may be called while a handler callback is still running. This might lead to race conditions.Added in version 13.6.
Changed in version 20.0: Changed this method into an
abstractmethod().- Parameters:
bot_data (
dict|telegram.ext.ContextTypes.bot_data) – Thebot_data.
- abstractmethod async refresh_chat_data(chat_id, chat_data)[source]¶
Will be called by the
telegram.ext.Applicationbefore passing thechat_datato a callback. Can be used to update data stored inchat_datafrom an external source.Tip
This method is expected to edit the object
chat_datain-place instead of returning a new object.Warning
When using
concurrent_updates(), this method may be called while a handler callback is still running. This might lead to race conditions.Added in version 13.6.
Changed in version 20.0: Changed this method into an
abstractmethod().
- abstractmethod async refresh_user_data(user_id, user_data)[source]¶
Will be called by the
telegram.ext.Applicationbefore passing theuser_datato a callback. Can be used to update data stored inuser_datafrom an external source.Tip
This method is expected to edit the object
user_datain-place instead of returning a new object.Warning
When using
concurrent_updates(), this method may be called while a handler callback is still running. This might lead to race conditions.Added in version 13.6.
Changed in version 20.0: Changed this method into an
abstractmethod().
- set_bot(bot)[source]¶
Set the Bot to be used by this persistence instance.
- Parameters:
bot (
telegram.Bot) – The bot.- Raises:
TypeError – If
PersistenceInput.callback_dataisTrueand thebotis not an instance oftelegram.ext.ExtBot.
- abstractmethod async update_bot_data(data)[source]¶
Will be called by the
telegram.ext.Applicationafter a handler has handled an update.- Parameters:
data (
dict|telegram.ext.ContextTypes.bot_data) – Thetelegram.ext.Application.bot_data.
- abstractmethod async update_callback_data(data)[source]¶
Will be called by the
telegram.ext.Applicationafter a handler has handled an update.Added in version 13.6.
Changed in version 20.0: Changed this method into an
abstractmethod().
- abstractmethod async update_chat_data(chat_id, data)[source]¶
Will be called by the
telegram.ext.Applicationafter a handler has handled an update.- Parameters:
chat_id (
int) – The chat the data might have been changed for.data (
dict|telegram.ext.ContextTypes.chat_data) – Thetelegram.ext.Application.chat_data[chat_id].
- abstractmethod async update_conversation(name, key, new_state)[source]¶
Will be called when a
telegram.ext.ConversationHandlerchanges states. This allows the storage of the new state in the persistence.
- property update_interval[source]¶
Time (in seconds) that the
Applicationwill wait between two consecutive runs of updating the persistence.Added in version 20.0.
- Type:
- abstractmethod async update_user_data(user_id, data)[source]¶
Will be called by the
telegram.ext.Applicationafter a handler has handled an update.- Parameters:
user_id (
int) – The user the data might have been changed for.data (
dict|telegram.ext.ContextTypes.user_data) – Thetelegram.ext.Application.user_data[user_id].
BasePersistence - python-telegram-bot v22.7