-
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 is set up to help you understand and set up persistence for your bots.
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.
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.
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 be of use to others (e.g. a general one covering all types of data). Please add it below.
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.
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