telegram_libs 0.1.27__py3-none-any.whl → 0.1.29__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.
@@ -14,5 +14,8 @@
14
14
  "support": {
15
15
  "message": "Write down any questions, issues or suggestions you have, and we will resolve them as soon as possible 👇 ",
16
16
  "response": "Thank you! Our support team will contact you soon."
17
+ },
18
+ "rate_limit": {
19
+ "exceeded": "You have exceeded the free rate limit for today. Please try again tomorrow."
17
20
  }
18
21
  }
@@ -14,5 +14,8 @@
14
14
  "support": {
15
15
  "message": "Напишите любые вопросы, проблемы, или предложения и мы решим их как можно скорее 👇 ",
16
16
  "response": "Спасибо! Наша служба поддержки свяжется с вами в ближайшее время."
17
+ },
18
+ "rate_limit": {
19
+ "exceeded": "Вы превысили лимит бесплатных запросов на сегодня. Пожалуйста, попробуйте снова завтра."
17
20
  }
18
21
  }
telegram_libs/utils.py CHANGED
@@ -68,12 +68,8 @@ class RateLimitManager:
68
68
  self.mongo_manager = mongo_manager
69
69
  self.rate_limit = rate_limit
70
70
 
71
- def check_limit(self, user_id: int) -> bool:
71
+ def check_limit(self, user_id: int) -> tuple[bool, dict]:
72
72
  """Check if user has exceeded the daily rate limit."""
73
- # Premium users have no limits
74
- if self.mongo_manager.check_subscription_status(user_id):
75
- return True
76
-
77
73
  # Get today's date and reset time to midnight
78
74
  today = datetime.now().date()
79
75
 
@@ -90,18 +86,38 @@ class RateLimitManager:
90
86
  "last_action_date": datetime.now().isoformat(),
91
87
  },
92
88
  )
93
- return True
89
+ return True, user_data
94
90
 
95
91
  # Check if user has exceeded the limit
96
92
  actions_today = user_data.get("actions_today", 0)
97
93
  if actions_today >= self.rate_limit:
98
- return False
94
+ return False, user_data
95
+
96
+ return True, user_data
97
+
98
+ def check_and_increment(self, user_id: int) -> bool:
99
+ """Check if user can perform an action and increment the count if allowed."""
100
+ if self.mongo_manager.check_subscription_status(user_id):
101
+ return True
99
102
 
100
- return True
103
+ can_perform, user_data = self.check_limit(user_id)
104
+ if can_perform:
105
+ self.increment_action_count(user_id, user_data)
106
+ return True
107
+ return False
108
+
109
+ async def check_limit_with_response(self, update: Update, context: ContextTypes.DEFAULT_TYPE, user_id: int) -> None:
110
+ """Check if user can perform an action and handle the response."""
111
+ if not self.check_and_increment(user_id):
112
+ lang = self.mongo_manager.get_user_info(user_id).get("lang")
113
+ message = t("rate_limit.exceeded", lang, common=True)
114
+ await update.message.reply_text(message)
115
+ await get_subscription_keyboard(update, lang)
101
116
 
102
- def increment_action_count(self, user_id: int) -> None:
117
+ def increment_action_count(self, user_id: int, user_data: dict = None) -> None:
103
118
  """Increment the daily action count for the user."""
104
- user_data = self.mongo_manager.get_user_data(user_id)
119
+ if user_data is None:
120
+ user_data = self.mongo_manager.get_user_data(user_id)
105
121
  current_actions = user_data.get("actions_today", 0)
106
122
  self.mongo_manager.update_user_data(
107
123
  user_id,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: telegram_libs
3
- Version: 0.1.27
3
+ Version: 0.1.29
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,16 +2,16 @@ telegram_libs/__init__.py,sha256=xrsD5r6ZiJxPapHf1UhQ61z2gHtCCWrzW0CZHvlvXRc,82
2
2
  telegram_libs/constants.py,sha256=mATa2Pr4YzPGNrqw29kOHK7sSefWvENZ-byPPZCBTc8,958
3
3
  telegram_libs/error.py,sha256=uomabeEiSP4v4AEpKYbi_gR0l3G003sio6lKl72AinY,453
4
4
  telegram_libs/handlers.py,sha256=d5-4FvjyWcJeN1i3dHTYhF-2P4j7PbM1QElloFR_llg,2417
5
- telegram_libs/locales/en.json,sha256=4VVkME7lCKW6ZjkopaoA6Uq3DLbEVkWacGei3GNSXFM,843
6
- telegram_libs/locales/ru.json,sha256=LYI6rAlwdNLaTGgkrALn31Lt7jC3SZteei8P_i7ZpQI,1208
5
+ telegram_libs/locales/en.json,sha256=5GT1mk1xskau1oaku0E6tZ7-aGf3F1qwkJzD_bN0uV8,960
6
+ telegram_libs/locales/ru.json,sha256=KvlxUUpwNyiOtDBhUm3U13Gj7UtzLKX5SzHOdkPT6gY,1411
7
7
  telegram_libs/logger.py,sha256=oikvkZMk2kOLvQI9hVMUGMCoSnOPf_fhMvv2SbO-STo,891
8
8
  telegram_libs/mongo.py,sha256=j1UIGnldvaNZaFPltCvEUcUOOCoquBWvqPh7Q2HTrvY,4776
9
9
  telegram_libs/payment.py,sha256=cNb2vnBlYI_6KnTGz-PCM0uKahW2uGjHVn1mGYlYG-4,3564
10
10
  telegram_libs/subscription.py,sha256=NJlh1kcDfasX1XLpkVfe1UbZslpWSC1hXHaum0kfG8A,4693
11
11
  telegram_libs/support.py,sha256=a3BA7g3seVBUMasv65SzxebLayLigA069wvDcStYbCM,2748
12
12
  telegram_libs/translation.py,sha256=8Kb2cgqKKZH4X_i2Le0V_K1imZdoaCzYAca831DOBig,2049
13
- telegram_libs/utils.py,sha256=YnxeZFBLEb95swNlER6Vulv3-TXXVgUTGmzpYUROVY0,3796
14
- telegram_libs-0.1.27.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
15
- telegram_libs-0.1.27.dist-info/METADATA,sha256=DhniNOJ1-RW0DhJNn2QvH6lWL0tH56SvRNMrJPVUbOw,804
16
- telegram_libs-0.1.27.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
17
- telegram_libs-0.1.27.dist-info/RECORD,,
13
+ telegram_libs/utils.py,sha256=Y-WwncNugCbmzQjvDg95NEI-bHRIX9Ly6SVuySJjpjo,4698
14
+ telegram_libs-0.1.29.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
15
+ telegram_libs-0.1.29.dist-info/METADATA,sha256=Qi0yn7gicb9WnTwCqyF_EPSZWXCZqEt14vp5Caoi8hA,804
16
+ telegram_libs-0.1.29.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
17
+ telegram_libs-0.1.29.dist-info/RECORD,,