telegram_libs 0.1.21__py3-none-any.whl → 0.1.22__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.
@@ -5,6 +5,7 @@ required_constants = []
5
5
  BOTS_AMOUNT = os.getenv("BOTS_AMOUNT")
6
6
  MONGO_URI = os.getenv("MONGO_URI")
7
7
  SUBSCRIPTION_DB_NAME = os.getenv("SUBSCRIPTION_DB_NAME")
8
+ LOGS_DB_NAME = os.getenv("LOGS_DB_NAME", "logs")
8
9
  DEBUG = os.getenv("DEBUG", "False").lower() in ("true", "1", "yes")
9
10
 
10
11
  required_constants.append(("BOTS_AMOUNT", BOTS_AMOUNT))
telegram_libs/error.py CHANGED
@@ -1,9 +1,11 @@
1
1
  from logging import getLogger
2
2
  from telegram import Update
3
3
  from telegram.ext import ContextTypes
4
+ from telegram_libs.logger import BotLogger
4
5
 
5
6
  logger = getLogger(__name__)
6
7
 
7
8
 
8
- async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
9
- logger.error(f"Update {update} caused error {context.error}")
9
+ async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_logger: BotLogger, bot_name: str) -> None:
10
+ logger.error(f"Update {update} caused error {context.error}")
11
+ bot_logger.log_action(update.effective_user.id, "error_handler", bot_name, context.error)
telegram_libs/handlers.py CHANGED
@@ -17,28 +17,29 @@ from telegram_libs.support import (
17
17
  )
18
18
  from telegram_libs.utils import more_bots_list_command
19
19
  from telegram_libs.error import error_handler
20
+ from telegram_libs.logger import BotLogger
20
21
 
21
22
 
22
23
  def register_subscription_handlers(
23
- app: Application, mongo_manager: MongoManager
24
+ app: Application, mongo_manager: MongoManager, bot_logger: BotLogger
24
25
  ) -> None:
25
26
  """Register subscription-related handlers."""
26
- app.add_handler(CallbackQueryHandler(subscription_callback, pattern="^sub_"))
27
- app.add_handler(CommandHandler("subscribe", partial(subscribe_command, mongo_manager=mongo_manager)))
27
+ app.add_handler(CallbackQueryHandler(partial(subscription_callback, bot_logger=bot_logger), pattern="^sub_"))
28
+ app.add_handler(CommandHandler("subscribe", partial(subscribe_command, mongo_manager=mongo_manager, bot_logger=bot_logger)))
28
29
  app.add_handler(CommandHandler("status", partial(check_subscription_command, mongo_manager=mongo_manager)))
29
30
 
30
31
  # Payment handlers
31
32
  app.add_handler(PreCheckoutQueryHandler(precheckout_handler))
32
- app.add_handler(MessageHandler(filters.SUCCESSFUL_PAYMENT, partial(successful_payment, mongo_manager=mongo_manager)))
33
+ app.add_handler(MessageHandler(filters.SUCCESSFUL_PAYMENT, partial(successful_payment, mongo_manager=mongo_manager, bot_logger=bot_logger)))
33
34
 
34
35
 
35
- def register_support_handlers(app: Application, bot_name: str) -> None:
36
+ def register_support_handlers(app: Application, bot_name: str, bot_logger: BotLogger) -> None:
36
37
  """Register support handlers for the bot"""
37
- app.add_handler(CommandHandler("support", handle_support_command))
38
+ app.add_handler(CommandHandler("support", partial(handle_support_command, bot_logger=bot_logger)))
38
39
  app.add_handler(
39
40
  MessageHandler(
40
41
  filters.TEXT & ~filters.COMMAND & SupportFilter(),
41
- partial(_handle_user_response, bot_name=bot_name),
42
+ partial(_handle_user_response, bot_name=bot_name, bot_logger=bot_logger),
42
43
  )
43
44
  )
44
45
 
@@ -47,10 +48,11 @@ def register_common_handlers(
47
48
  app: Application, bot_name: str, mongo_manager: MongoManager
48
49
  ) -> None:
49
50
  """Register common handlers for the bot"""
51
+ bot_logger = BotLogger()
50
52
  app.add_handler(CommandHandler("more", more_bots_list_command))
51
53
 
52
- register_support_handlers(app, bot_name)
53
- register_subscription_handlers(app, mongo_manager)
54
+ register_support_handlers(app, bot_name, bot_logger)
55
+ register_subscription_handlers(app, mongo_manager, bot_logger)
54
56
 
55
57
  # Error handler
56
- app.add_error_handler(error_handler)
58
+ app.add_error_handler(partial(error_handler, bot_logger=bot_logger, bot_name=bot_name))
@@ -0,0 +1,26 @@
1
+ from datetime import datetime
2
+ from telegram_libs.mongo import MongoManager
3
+ from telegram_libs.constants import DEBUG, LOGS_DB_NAME
4
+
5
+
6
+ class BotLogger:
7
+ def __init__(self):
8
+ self.mongo_manager = MongoManager(mongo_database_name=LOGS_DB_NAME)
9
+ self.logs_collection = (
10
+ self.mongo_manager.client[LOGS_DB_NAME]["logs_test"]
11
+ if DEBUG
12
+ else self.mongo_manager.client[LOGS_DB_NAME]["logs"]
13
+ )
14
+
15
+ def log_action(
16
+ self, user_id: int, action_type: str, bot_name: str, details: dict = None
17
+ ) -> None:
18
+ """Log a user action to the database."""
19
+ log_entry = {
20
+ "user_id": user_id,
21
+ "action_type": action_type,
22
+ "bot_name": bot_name,
23
+ "timestamp": datetime.now().isoformat(),
24
+ "details": details or {},
25
+ }
26
+ self.logs_collection.insert_one(log_entry)
telegram_libs/payment.py CHANGED
@@ -6,6 +6,7 @@ from telegram_libs.translation import t
6
6
  from telegram_libs.subscription import add_subscription_payment
7
7
  from telegram_libs.utils import get_user_info
8
8
  from telegram_libs.mongo import MongoManager
9
+ from telegram_libs.logger import BotLogger
9
10
 
10
11
  logger = getLogger(__name__)
11
12
 
@@ -33,12 +34,14 @@ async def precheckout_handler(update: Update, context: ContextTypes.DEFAULT_TYPE
33
34
  logger.error(f"Error sending pre-checkout error: {e2}")
34
35
 
35
36
 
36
- async def successful_payment(update: Update, context: ContextTypes.DEFAULT_TYPE, mongo_manager: MongoManager) -> None:
37
+ async def successful_payment(update: Update, context: ContextTypes.DEFAULT_TYPE, mongo_manager: MongoManager, bot_logger: BotLogger) -> None:
37
38
  """Handle successful payments"""
38
39
  user_info = get_user_info(update, mongo_manager)
39
40
  user_id = user_info["user_id"]
40
41
  lang = user_info["lang"]
41
42
  payment_info = update.message.successful_payment
43
+ bot_name = context.bot.name
44
+ bot_logger.log_action(user_id, "successful_payment", bot_name, {"payload": payment_info.invoice_payload, "amount": payment_info.total_amount, "currency": payment_info.currency})
42
45
  logger.info(f"Payment info received: {payment_info}")
43
46
 
44
47
  # Determine which plan was purchased
@@ -5,6 +5,7 @@ from telegram_libs.constants import SUBSCRIPTION_DB_NAME, DEBUG, BOTS_AMOUNT
5
5
  from telegram_libs.mongo import MongoManager
6
6
  from telegram_libs.utils import get_user_info
7
7
  from telegram_libs.translation import t
8
+ from telegram_libs.logger import BotLogger
8
9
 
9
10
 
10
11
  # Define the subscription database and collection
@@ -89,10 +90,13 @@ async def get_subscription_keyboard(update: Update, lang: str) -> InlineKeyboard
89
90
 
90
91
 
91
92
  async def subscription_callback(
92
- update: Update, context: ContextTypes.DEFAULT_TYPE
93
+ update: Update, context: ContextTypes.DEFAULT_TYPE, bot_logger: BotLogger
93
94
  ) -> None:
94
95
  """Handle subscription button clicks"""
95
96
  query = update.callback_query
97
+ user_id = query.from_user.id
98
+ bot_name = context.bot.name
99
+ bot_logger.log_action(user_id, "subscription_button_click", bot_name, {"plan": query.data})
96
100
  await query.answer()
97
101
  plan = query.data
98
102
 
@@ -142,11 +146,14 @@ async def subscription_callback(
142
146
 
143
147
 
144
148
  async def subscribe_command(
145
- update: Update, context: ContextTypes.DEFAULT_TYPE, mongo_manager: MongoManager
149
+ update: Update, context: ContextTypes.DEFAULT_TYPE, mongo_manager: MongoManager, bot_logger: BotLogger
146
150
  ) -> None:
147
151
  """Show subscription options"""
148
152
  user_info = get_user_info(update, mongo_manager)
153
+ user_id = user_info["user_id"]
149
154
  lang = user_info["lang"]
155
+ bot_name = context.bot.name
156
+ bot_logger.log_action(user_id, "subscribe_command", bot_name)
150
157
 
151
158
  reply_markup = await get_subscription_keyboard(update, lang)
152
159
 
telegram_libs/support.py CHANGED
@@ -6,6 +6,7 @@ from telegram.ext.filters import BaseFilter
6
6
  from telegram_libs.mongo import MongoManager
7
7
  from telegram_libs.constants import DEBUG, SUBSCRIPTION_DB_NAME
8
8
  from telegram_libs.translation import t
9
+ from telegram_libs.logger import BotLogger
9
10
 
10
11
 
11
12
  SUPPORT_WAITING = "support_waiting"
@@ -13,17 +14,22 @@ SUPPORT_WAITING = "support_waiting"
13
14
  mongo_manager_instance = MongoManager(mongo_database_name=SUBSCRIPTION_DB_NAME) # Use an existing or create a new MongoManager instance
14
15
 
15
16
 
16
- async def handle_support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
17
+ async def handle_support_command(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_logger: BotLogger) -> None:
17
18
  """Support command handler"""
19
+ user_id = update.effective_user.id
20
+ bot_name = context.bot.name
21
+ bot_logger.log_action(user_id, "support_command", bot_name)
18
22
  await update.message.reply_text(
19
23
  t("support.message", update.effective_user.language_code, common=True)
20
24
  )
21
25
  context.user_data[SUPPORT_WAITING] = True
22
26
 
23
27
 
24
- async def _handle_user_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
28
+ async def _handle_user_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str, bot_logger: BotLogger) -> None:
25
29
  """Handle user's support message"""
30
+ user_id = update.effective_user.id
26
31
  if context.user_data.get(SUPPORT_WAITING):
32
+ bot_logger.log_action(user_id, "support_message_sent", bot_name, {"message": update.message.text})
27
33
  db_name = "support"
28
34
  collection_name = "support" if not DEBUG else "support_test"
29
35
  message_key = "support.response"
telegram_libs/utils.py CHANGED
@@ -4,11 +4,11 @@ from telegram import (
4
4
  InlineKeyboardMarkup,
5
5
  )
6
6
  from telegram import Update
7
- from telegram.ext import ContextTypes, Application, CommandHandler, CallbackQueryHandler
7
+ from telegram.ext import ContextTypes
8
8
  from telegram_libs.constants import BOTS_AMOUNT
9
9
  from telegram_libs.translation import t
10
- from telegram_libs.support import register_support_handlers
11
10
  from telegram_libs.mongo import MongoManager
11
+ from telegram_libs.logger import BotLogger
12
12
 
13
13
 
14
14
  basicConfig(
@@ -47,7 +47,10 @@ async def get_subscription_keyboard(update: Update, lang: str) -> InlineKeyboard
47
47
  ]
48
48
 
49
49
 
50
- async def more_bots_list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
50
+ async def more_bots_list_command(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_logger: BotLogger) -> None:
51
+ user_id = update.effective_user.id
52
+ bot_name = context.bot.name
53
+ bot_logger.log_action(user_id, "more_bots_list_command", bot_name)
51
54
  message = """Here is the list of all bots:
52
55
 
53
56
 
@@ -73,8 +76,3 @@ def get_user_info(update: Update, mongo_manager: MongoManager) -> dict:
73
76
  "lang": user_data.get("language", "en"),
74
77
  **user_data,
75
78
  }
76
-
77
- def register_common_handlers(application: Application, bot_name: str) -> None:
78
- """Register common handlers to the application."""
79
- application.add_handler(CommandHandler("more", more_bots_list_command))
80
- register_support_handlers(application, bot_name)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: telegram_libs
3
- Version: 0.1.21
3
+ Version: 0.1.22
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
@@ -0,0 +1,17 @@
1
+ telegram_libs/__init__.py,sha256=xrsD5r6ZiJxPapHf1UhQ61z2gHtCCWrzW0CZHvlvXRc,82
2
+ telegram_libs/constants.py,sha256=cGQBM1z3jmh00pHQlBP-SbbPqKGOxlCRVUsrsThkWCU,655
3
+ telegram_libs/error.py,sha256=uomabeEiSP4v4AEpKYbi_gR0l3G003sio6lKl72AinY,453
4
+ telegram_libs/handlers.py,sha256=uiXJGkwF8BZhaiWVYq1IYOc3OKAIowRR2Q0iv3REsZY,2385
5
+ telegram_libs/locales/en.json,sha256=4VVkME7lCKW6ZjkopaoA6Uq3DLbEVkWacGei3GNSXFM,843
6
+ telegram_libs/locales/ru.json,sha256=LYI6rAlwdNLaTGgkrALn31Lt7jC3SZteei8P_i7ZpQI,1208
7
+ telegram_libs/logger.py,sha256=oikvkZMk2kOLvQI9hVMUGMCoSnOPf_fhMvv2SbO-STo,891
8
+ telegram_libs/mongo.py,sha256=urUvtIamU_WAqf1S6fDiX4ZOzOaTuRph-ovACKIAjlM,2519
9
+ telegram_libs/payment.py,sha256=PSoagCTUt4xad-39kVYc2tcFgaPMQsP9eplCVqI9hnc,3661
10
+ telegram_libs/subscription.py,sha256=PFgoWKgjRrjzBjK-AMrESt_7aFk0pa8Qr_Wl1aQIoW8,6430
11
+ telegram_libs/support.py,sha256=a3BA7g3seVBUMasv65SzxebLayLigA069wvDcStYbCM,2748
12
+ telegram_libs/translation.py,sha256=8Kb2cgqKKZH4X_i2Le0V_K1imZdoaCzYAca831DOBig,2049
13
+ telegram_libs/utils.py,sha256=wgkFCziQq4F3z-2AFMirgyY-EDu9D863vQy2ZkjPZSg,2605
14
+ telegram_libs-0.1.22.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
15
+ telegram_libs-0.1.22.dist-info/METADATA,sha256=YCO340LDQxpqHfo9FMmNLwulB4zNOmR4YyxhgpqKo_g,804
16
+ telegram_libs-0.1.22.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
17
+ telegram_libs-0.1.22.dist-info/RECORD,,
@@ -1,16 +0,0 @@
1
- telegram_libs/__init__.py,sha256=xrsD5r6ZiJxPapHf1UhQ61z2gHtCCWrzW0CZHvlvXRc,82
2
- telegram_libs/constants.py,sha256=7-L1uK9UKCVAZNKYnqmM4B18JDxfxHMpZgTIjh0rATg,606
3
- telegram_libs/error.py,sha256=YtI2qCweDmANzPp3jae6yNzMQMtC9Z5TIeZ2-utGi_4,270
4
- telegram_libs/handlers.py,sha256=5c8jRL4eEC4UyCugi5SaoUM7zEbgPHZum9i6Jh8WswM,2059
5
- telegram_libs/locales/en.json,sha256=4VVkME7lCKW6ZjkopaoA6Uq3DLbEVkWacGei3GNSXFM,843
6
- telegram_libs/locales/ru.json,sha256=LYI6rAlwdNLaTGgkrALn31Lt7jC3SZteei8P_i7ZpQI,1208
7
- telegram_libs/mongo.py,sha256=urUvtIamU_WAqf1S6fDiX4ZOzOaTuRph-ovACKIAjlM,2519
8
- telegram_libs/payment.py,sha256=CCB7xtODlR_0o3layVjtE9hhHxJTZ3mhptDPDqfqYSg,3381
9
- telegram_libs/subscription.py,sha256=0ubzbDeyENMAdB2shH9qgH_eNzxr_zRsYgkhjolZqHo,6047
10
- telegram_libs/support.py,sha256=UCgJPLJdQUfsbKTIIsNfOpP9CCdw5GjlcSl6aBwNw1U,2378
11
- telegram_libs/translation.py,sha256=8Kb2cgqKKZH4X_i2Le0V_K1imZdoaCzYAca831DOBig,2049
12
- telegram_libs/utils.py,sha256=OuFcSZ8LEqfDlW5F5t-R7qpjfBwmrRGfsbgIWdh23OI,2772
13
- telegram_libs-0.1.21.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
14
- telegram_libs-0.1.21.dist-info/METADATA,sha256=RiSQv1QF5-3sXUxKgK4ciHtyQNVN9STRVMKbkkOmlCE,804
15
- telegram_libs-0.1.21.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
16
- telegram_libs-0.1.21.dist-info/RECORD,,