Package to add persistence to your telegram bot using pymongo
Project description
MongoPersistence
Package to add persistence to your telegram bot in a mongodb database.
ATTENTION: MongoPersistence is a new project, it should work, but if you encounter any bugs, please report them in the Issues section and if you have an idea how to fix it, please consider opening a PR.
Usage
First, make sure you've created a database using mongodb. If you haven't already, click here to get started.
Then create a collection in your database for each type of data you want to make persistent.
from telegram.ext import Application
from mongopersistence import MongoPersistence
persistence = MongoPersistence(
mongo_url="mongodb://username:password@your-ip:27017/",
db_name="your-database-name",
name_col_user_data="my-collection-for-user-data", # optional
name_col_chat_data="my-collection-for-chat-data", # optional
name_col_bot_data="my-collection-for-bot-data", # optional
name_col_conversations_data="my-collection-for-conversations", # optional
create_col_if_not_exist=True, # optional
ignore_general_data=["cache"],
ignore_user_data=["foo", "bar"],
)
application = (
Application
.builder()
.token("your-token")
.persistence(persistence).build()
)
With this code you will add persistence only to user_data and since the load_on_flush attribute has not been specified
then the data will be loaded only when the bot is shut down.
If you want the data to be loaded continuously instead, define:
MongoPersistence(..., load_on_flush=False, update_interval=60)
update_interval can also be undefined and the default is 60.
One of the advantages of setting load_on_flush = False is that if you modify the database (either directly from the
site or through the code of your other app) then it will automatically be updated to the modified data type inside your
bot, without having to shut it down first !
The last thing to specify is to be CAREFUL in adding permission to your IP on the mongodb site. Otherwise, you will encounter an error running the bot.
Installation
Install mongopersistence with pip
pip install mongopersistence
Roadmap
Search all the TODOs in this repo to see how you can contribute to this package, but in general:
-
Add support for make persistent callback data.
-
Create tests
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongopersistence-0.3.2.tar.gz.
File metadata
- Download URL: mongopersistence-0.3.2.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
053f2693022803e108c1d1594cf126bb748be36d9f6195f33e146fbad8ee756f
|
|
| MD5 |
13347692b3643e9aaf9b86b2779f70cc
|
|
| BLAKE2b-256 |
546bfb650425f86e724548570c0783d8760c6adc26f56d13193f33491da9974f
|
File details
Details for the file mongopersistence-0.3.2-py3-none-any.whl.
File metadata
- Download URL: mongopersistence-0.3.2-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.5 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e983b1cc2bd4f27da7959d1da877edcf7b1d698d2964f63d375d3a1ce860373
|
|
| MD5 |
7d763f44faf507fa628ea09c2688e35f
|
|
| BLAKE2b-256 |
d57db4957cceb1c08f6e6746d8b0f9f40f77e938426cf95e749ffec30d2b7776
|