Skip to content

Making your bot persistent

tobiaswicker edited this page Sep 18, 2019 · 43 revisions

In V12.0b1 we added a persistence mechanism to telegram.ext. This wiki is set up to help you understand and set up persistence for your bots.

What can become persistent?

The persistence structure is designed to make chat_data, user_data and ConversationHandler's states persistent. Job's and the job_queue is not supported because the serialization of callbacks is too unstable to reliably make persistent for broad user-cases. For a snippet on how to save and restore a basic job_queue see here.

Included persistence classes

Three classes concerning persistence in bots have been added.
BasePersistence - Is an interface class for persistence classes. If you create your own persistence classes to maintain a database-connection for example, you must inherit from BasePersistence
PicklePersistence - Uses pickle files to make the bot persistent.
DictPersistence - Uses in memory dicts and easy conversion to and from JSON to make the bot persistent.

3rd party persistence classes

If you want to create your own persistence class, please carefully read the docs on BasePersistence. It will tell you what methods you need to overwrite.

If you've written a persistence class that could benefit others (e.g. a general one covering all types of data), please add it below.

What do I need to change?

To make your bot persistent you need to do the following.

  • Create a persistence object (e.g. my_persistence = PicklePersistence(filename='my_file'))
  • Construct Updater with the persistence (Updater('TOKEN', persistence=my_persistence, use_context=True))

This is enough to make user_data and chat_data persistent. To make a conversationhandler persistent (save states between bot restarts) ConversationHandler(<no change>, persistent=True, name='my_name') If you want a conversationhandler to be persistent you MUST NAME IT. persistent is False by default. Adding these arguments and adding the conversationhandler to a persistence-aware updater/dispatcher will make it persistent.

Clone this wiki locally