telegram_libs 0.1.15__py3-none-any.whl → 0.1.17__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
telegram_libs/mongo.py CHANGED
@@ -1,6 +1,54 @@
1
1
  from pymongo.mongo_client import MongoClient
2
2
  from pymongo.server_api import ServerApi
3
- from telegram_libs.constants import MONGO_URI
3
+ from telegram_libs.constants import MONGO_URI, DEBUG
4
4
 
5
- # Create a new client and connect to the server
6
- mongo_client = MongoClient(MONGO_URI, server_api=ServerApi("1"))
5
+ mongo_client = MongoClient(MONGO_URI, server_api=ServerApi("1"))
6
+
7
+
8
+ class MongoManager:
9
+ def __init__(self, mongo_database_name: str, **kwargs):
10
+ self.client = kwargs.get("client") or mongo_client
11
+ self.db = self.client[mongo_database_name]
12
+ self.users_collection = self.db["users_test"] if DEBUG else self.db["users"]
13
+ self.payments_collection = self.db["order_test"] if DEBUG else self.db["order"]
14
+ self.user_schema = {"user_id": None, **kwargs.get("user_schema")}
15
+
16
+ def create_user(self, user_id: int) -> None:
17
+ """Create a new user in the database."""
18
+ user_data = self.user_schema.copy()
19
+ user_data["user_id"] = user_id
20
+ self.users_collection.insert_one(user_data)
21
+ return user_data
22
+
23
+ def get_user_data(self, user_id: int) -> dict:
24
+ """Retrieve user data from the database."""
25
+ user_data = self.users_collection.find_one({"user_id": user_id})
26
+ if not user_data:
27
+ # Initialize user data if not found
28
+ return self.create_user(user_id)
29
+ return user_data
30
+
31
+ def update_user_data(self, user_id: int, updates: dict) -> None:
32
+ """Update user data in the database."""
33
+ result = self.users_collection.update_one({"user_id": user_id}, {"$set": updates})
34
+ if result.matched_count == 0:
35
+ # If no document was matched, create a new user
36
+ self.create_user(user_id)
37
+ self.users_collection.update_one({"user_id": user_id}, {"$set": updates})
38
+
39
+ def add_order(self, user_id: int, order: dict) -> None:
40
+ """Add an order to the user's data."""
41
+ self.payments_collection.insert_one({"user_id": user_id, **order})
42
+
43
+
44
+ def get_orders(self, user_id: int) -> list:
45
+ """Get all orders for a user."""
46
+ orders = self.payments_collection.find({"user_id": user_id})
47
+ return list(orders)
48
+
49
+
50
+ def update_order(self, user_id: int, order_id: int, updates: dict) -> None:
51
+ """Update an order for a user."""
52
+ self.payments_collection.update_one(
53
+ {"user_id": user_id, "order_id": order_id}, {"$set": updates}
54
+ )
@@ -4,6 +4,7 @@ from telegram import Update
4
4
  from telegram.ext import ContextTypes, Application, CommandHandler, MessageHandler, filters
5
5
  from telegram.ext.filters import BaseFilter
6
6
  from telegram_libs.mongo import mongo_client
7
+ from telegram_libs.constants import DEBUG
7
8
  from telegram_libs.translation import t
8
9
 
9
10
 
@@ -22,7 +23,7 @@ async def _handle_user_response(update: Update, context: ContextTypes.DEFAULT_TY
22
23
  """Handle user's support message"""
23
24
  if context.user_data.get(SUPPORT_WAITING):
24
25
  db_name = "support"
25
- collection_name = "support"
26
+ collection_name = "support" if not DEBUG else "support_test"
26
27
  message_key = "support.response"
27
28
  doc_field_name = "message"
28
29
  context_key = SUPPORT_WAITING
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: telegram_libs
3
- Version: 0.1.15
3
+ Version: 0.1.17
4
4
  Summary: Common libraries for Telegram bots
5
5
  Author: Andrey Gritsaenko gricaenko.95a@gmail.com
6
6
  Requires-Python: >=3.11,<4.0
@@ -2,12 +2,12 @@ telegram_libs/__init__.py,sha256=xrsD5r6ZiJxPapHf1UhQ61z2gHtCCWrzW0CZHvlvXRc,82
2
2
  telegram_libs/constants.py,sha256=7-L1uK9UKCVAZNKYnqmM4B18JDxfxHMpZgTIjh0rATg,606
3
3
  telegram_libs/locales/en.json,sha256=TDueYazTaytYCs8_6Z7-HeulGyqz5ze3Cm4mXYKFX8Q,549
4
4
  telegram_libs/locales/ru.json,sha256=bHIuq9MFIhqIEvbf5j5HM1E9egtsjrTTIx50s6C9PpY,785
5
- telegram_libs/mongo.py,sha256=7UOy_cE0ofIbH7QiiirAjOgo_FM9JImtgxQ8ouEsFeo,245
5
+ telegram_libs/mongo.py,sha256=TzCOB4rXiYQ6Y0ptDXqrJmoGcnZuxYPRPdC7GkDZiDs,2278
6
6
  telegram_libs/subscription.py,sha256=d7xmzplUrm1nNlWlkqW6dddOYa3t_7PAM3iPme0K5F0,1690
7
- telegram_libs/support_handlers.py,sha256=T0ukR2gak0XknkA5RZ8Kjd1KeZAxH1BftQNg6bub5kw,2165
7
+ telegram_libs/support_handlers.py,sha256=SrJGmP9WJp6vpsCE6k0lgs5ftlZR_mRBf-NQThIXkmU,2240
8
8
  telegram_libs/translation.py,sha256=8Kb2cgqKKZH4X_i2Le0V_K1imZdoaCzYAca831DOBig,2049
9
9
  telegram_libs/utils.py,sha256=0eBy7psB0XJQL9dPUeq5c2Ymg7ZPA54sdYJW3x2fI4M,2231
10
- telegram_libs-0.1.15.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
11
- telegram_libs-0.1.15.dist-info/METADATA,sha256=vwa1lBhRpsx6KwevbYqQ-kXyaTudqR3FPZvSE4J0FyM,804
12
- telegram_libs-0.1.15.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
13
- telegram_libs-0.1.15.dist-info/RECORD,,
10
+ telegram_libs-0.1.17.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
11
+ telegram_libs-0.1.17.dist-info/METADATA,sha256=fSyfAOLoJ2DeWIDWDdLW7y9P5HCyKolPbJGf3nhCJFE,804
12
+ telegram_libs-0.1.17.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
13
+ telegram_libs-0.1.17.dist-info/RECORD,,