-
Notifications
You must be signed in to change notification settings - Fork 5
Making your bot persistent
In V12.0b1 we added a persistence mechanism to telegram.ext. This wiki page is there to help you understand and set up persistence for your bots.
- What can become persistent?
- Included persistence classes
- 3rd party persistence classes
- What do I need to change?
- Storing Bots
- The persistence structure is designed to make
bot_data,chat_data,user_dataandConversationHandler's states persistent. -
Job's and thejob_queueis not supported because the serialization of callbacks is too unstable to reliably make persistent for broad user-cases. However, the currentJobQueuebackend APScheduler has it's own persistence logic that you can leverage. - For a special note about
Botinstances, see below
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. Note that this class is mainly intended as starting point for custom persistence classes that need to JSON-serialize the stored data before writing them to file/database and does not actually write any data to file/database.
Instead of manually handling a database to store data, consider implementing a subclass of BasePersistence. This allows you to simply pass an instance of that subclass to the Updater/Dispatcher and let PTB handle the loading, updating & storing of the data!
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), it would be great if you linked it here or even better made it available in ptbcontrib.
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))
Note that the Updater passes the persistence variable to the Dispatcher, so if you aren't using the Updater, you can set the persistence on your Dispatcher object instead.
This is enough to make user_data, bot_data and chat_data persistent.
To make a conversation handler persistent (save states between bot restarts) you must name it and set persistent to True.
Like ConversationHandler(<no change>, persistent=True, name='my_name'). persistent is False by default.
Adding these arguments and adding the conversation handler to a persistence-aware updater/dispatcher will make it persistent.
As of v13, persistence will automatically try to replace telegram.Bot instances by REPLACED_BOT and
insert the bot set with set_bot upon loading of the data. This is to ensure that
changes to the bot apply to the saved objects, too. For example, you might change the default values used by the bot. If you change the bots token, this may
lead to e.g. Chat not found errors. For the limitations on replacing bots see
replace_bot and insert_bot.
This is relevant e.g. if you store Telegram objects like Message in bot/user/chat_data, as some of them have a bot attribute, which holds a reference to the Dispatchers bot.
Wiki of python-telegram-bot © Copyright 2015-2022 – Licensed by Creative Commons
- Types of Handlers
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Exception Handling
- Job Queue
- Arbitrary
callback_data - Avoiding flood limits
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Webhooks
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests