BasePersistence¶
- class telegram.ext.BasePersistence(store_data=None, update_interval=60)[source]¶
Bases:
typing.Generic
,ABC
Interface 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
pass
is 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.Bot
instances. 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
bot
to replace bot instances with a placeholder before serialization and insertbot
back when loading the data. Sincebot
will 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
Generic
class 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()
.
Available In
Changed in version 20.0:
The parameters and attributes
store_*_data
were replaced bystore_data
.insert/replace_bot
was 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
Application
will update the persistence in regular intervals. This parameter specifies the time (in seconds) to wait between two consecutive runs of updating the persistence. Defaults to60
seconds.Added in version 20.0.
- abstract async drop_chat_data(chat_id)[source]¶
Will be called by the
telegram.ext.Application
, when usingdrop_chat_data()
.Added in version 20.0.
- abstract async drop_user_data(user_id)[source]¶
Will be called by the
telegram.ext.Application
, when usingdrop_user_data()
.Added in version 20.0.
- abstract 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()
.
- abstract async get_bot_data()[source]¶
Will be called by
telegram.ext.Application
upon creation with a persistence object. It should return thebot_data
if stored, or an emptydict
. In the latter case, thedict
should produce values corresponding to one of the following:The type from
telegram.ext.ContextTypes.bot_data
iftelegram.ext.ContextTypes
are used.
- Returns:
The restored bot data.
- Return type:
dict[
int
,dict
|telegram.ext.ContextTypes.bot_data
]
- abstract async get_callback_data()[source]¶
Will be called by
telegram.ext.Application
upon 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()
.
- abstract async get_chat_data()[source]¶
Will be called by
telegram.ext.Application
upon creation with a persistence object. It should return thechat_data
if 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_data
iftelegram.ext.ContextTypes
is used.
Changed in version 20.0: This method may now return a
dict
instead of acollections.defaultdict
- Returns:
The restored chat data.
- Return type:
dict[
int
,dict
|telegram.ext.ContextTypes.chat_data
]
- abstract async get_conversations(name)[source]¶
Will be called by
telegram.ext.Application
when atelegram.ext.ConversationHandler
is added iftelegram.ext.ConversationHandler.persistent
isTrue
. It should return the conversations for the handler withname
or an emptydict
.
- abstract async get_user_data()[source]¶
Will be called by
telegram.ext.Application
upon creation with a persistence object. It should return theuser_data
if 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_data
iftelegram.ext.ContextTypes
is used.
Changed in version 20.0: This method may now return a
dict
instead of acollections.defaultdict
- Returns:
The restored user data.
- Return type:
dict[
int
,dict
|telegram.ext.ContextTypes.user_data
]
- abstract async refresh_bot_data(bot_data)[source]¶
Will be called by the
telegram.ext.Application
before passing thebot_data
to a callback. Can be used to update data stored inbot_data
from an external source.Tip
This method is expected to edit the object
bot_data
in-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
.
- abstract async refresh_chat_data(chat_id, chat_data)[source]¶
Will be called by the
telegram.ext.Application
before passing thechat_data
to a callback. Can be used to update data stored inchat_data
from an external source.Tip
This method is expected to edit the object
chat_data
in-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()
.
- abstract async refresh_user_data(user_id, user_data)[source]¶
Will be called by the
telegram.ext.Application
before passing theuser_data
to a callback. Can be used to update data stored inuser_data
from an external source.Tip
This method is expected to edit the object
user_data
in-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_data
isTrue
and thebot
is not an instance oftelegram.ext.ExtBot
.
- abstract async update_bot_data(data)[source]¶
Will be called by the
telegram.ext.Application
after a handler has handled an update.- Parameters:
data (
dict
|telegram.ext.ContextTypes.bot_data
) – Thetelegram.ext.Application.bot_data
.
- abstract async update_callback_data(data)[source]¶
Will be called by the
telegram.ext.Application
after a handler has handled an update.Added in version 13.6.
Changed in version 20.0: Changed this method into an
abstractmethod()
.
- abstract async update_chat_data(chat_id, data)[source]¶
Will be called by the
telegram.ext.Application
after 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]
.
- abstract async update_conversation(name, key, new_state)[source]¶
Will be called when a
telegram.ext.ConversationHandler
changes states. This allows the storage of the new state in the persistence.
- property update_interval[source]¶
Time (in seconds) that the
Application
will wait between two consecutive runs of updating the persistence.Added in version 20.0.
- Type:
- abstract async update_user_data(user_id, data)[source]¶
Will be called by the
telegram.ext.Application
after 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]
.