https://github.com/hk-mattew/ptb-persistence
Python data persistence library for Telegram bot developed using PTB.
https://github.com/hk-mattew/ptb-persistence
basepersistence mongodb ptb-persistence python-telegram-bot telegram-bot telegram-bot-api
Last synced: over 1 year ago
JSON representation
Python data persistence library for Telegram bot developed using PTB.
- Host: GitHub
- URL: https://github.com/hk-mattew/ptb-persistence
- Owner: HK-Mattew
- License: mit
- Created: 2024-08-30T12:54:30.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-11T12:21:50.000Z (almost 2 years ago)
- Last Synced: 2025-01-29T18:15:49.784Z (over 1 year ago)
- Topics: basepersistence, mongodb, ptb-persistence, python-telegram-bot, telegram-bot, telegram-bot-api
- Language: Python
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Persistent data for lib PTB
PTBPersistence is a Python library to deal with developed bot data persistence using lib [PTB](https://github.com/python-telegram-bot/python-telegram-bot).
Developed to support multiple data storage.
Data store supported at the moment:
- MongoDB
## Installation
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install.
```bash
pip install git+https://github.com/HK-Mattew/ptb-persistence.git
```
Use Poetry to install.
```bash
poetry add git+https://github.com/HK-Mattew/ptb-persistence.git
```
## Usage
```python
from ptb_persistence import PTBPersistence
from ptb_persistence.datastores.mongodb import MongoDBDataStore
from telegram.ext import Application
data_store = MongoDBDataStore(
...
)
ptb_persistence = PTBPersistence(
data_store=data_store
)
application = (
Application
.builder()
.token("")
.persistence(ptb_persistence).build()
)
```
## Support for multiple Workers/Processes
This library is designed to work well with multi-worker bots. Typically bots that run in webhook mode and use load balancers between multiple instances of the same bot.
### Conversational data persistence
Currently, PTB does not have a method like refresh_conversation_data(...). And this prevents us from working with persistent conversations across multiple workers/processes.
Here I work around this (Famous Hack) 😄,
I created a custom Application class where I implement some logic to update conversation data. And this allows us to work with conversation data shared between multiple workers/processes.
See the custom CustomApplication class (and also more details): [Here](https://github.com/HK-Mattew/ptb-persistence/blob/main/ptb_persistence/utils/ptb.py)
## Contributing
Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.
Please make sure to update tests as appropriate.
## License
[MIT](https://choosealicense.com/licenses/mit/)