telegram_libs 0.1.11__py3-none-any.whl → 0.1.13__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/__init__.py CHANGED
@@ -2,4 +2,4 @@
2
2
  Telegram Libs - Common libraries for Telegram bots
3
3
  """
4
4
 
5
- __version__ = "0.1.11"
5
+ __version__ = "0.1.13"
telegram_libs/utils.py CHANGED
@@ -1,13 +1,24 @@
1
+ from functools import partial
2
+ from logging import basicConfig, getLogger, INFO
3
+ from datetime import datetime
1
4
  from telegram import (
2
5
  InlineKeyboardButton,
3
6
  InlineKeyboardMarkup,
4
7
  )
5
8
  from telegram import Update
6
9
  from telegram.ext import ContextTypes, Application, CommandHandler, MessageHandler, filters
10
+ from telegram.ext.filters import BaseFilter
7
11
  from telegram_libs.constants import BOTS_AMOUNT
8
12
  from telegram_libs.translation import t
9
13
  from telegram_libs.mongo import mongo_client
10
- from functools import partial
14
+
15
+
16
+
17
+ basicConfig(
18
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=INFO
19
+ )
20
+ logger = getLogger(__name__)
21
+
11
22
 
12
23
  FEEDBACK_WAITING = "feedback_waiting"
13
24
  SUPPORT_WAITING = "support_waiting"
@@ -54,7 +65,7 @@ async def more_bots_list_command(update: Update, context: ContextTypes.DEFAULT_T
54
65
  await update.message.reply_text(message, disable_web_page_preview=True, parse_mode='HTML')
55
66
 
56
67
 
57
- async def support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
68
+ async def handle_support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
58
69
  """Support command handler"""
59
70
  await update.message.reply_text(
60
71
  t("support.message", update.effective_user.language_code, common=True)
@@ -62,55 +73,59 @@ async def support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
62
73
  context.user_data[SUPPORT_WAITING] = True
63
74
 
64
75
 
65
- async def handle_support_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
66
- """Handle user's support message"""
67
- if context.user_data.get(SUPPORT_WAITING):
68
- support_db = mongo_client["support"]
69
- support_collection = support_db["support"]
70
- support_doc = {
71
- "user_id": update.effective_user.id,
72
- "username": update.effective_user.username,
73
- "message": update.message.text,
74
- "bot_name": bot_name,
75
- "timestamp": update.message.date.isoformat(),
76
- "resolved": False,
77
- }
78
- support_collection.insert_one(support_doc)
79
- await update.message.reply_text(t("support.response", update.effective_user.language_code, common=True))
80
- context.user_data[SUPPORT_WAITING] = False
81
-
76
+ async def _handle_user_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
77
+ """Handle user's support or feedback message"""
78
+ if context.user_data.get(FEEDBACK_WAITING):
79
+ db_name = "feedback"
80
+ collection_name = "feedback"
81
+ message_key = "feedback.response"
82
+ doc_field_name = "feedback"
83
+ context_key = FEEDBACK_WAITING
84
+ extra_fields = {}
85
+ elif context.user_data.get(SUPPORT_WAITING):
86
+ db_name = "support"
87
+ collection_name = "support"
88
+ message_key = "support.response"
89
+ doc_field_name = "message"
90
+ context_key = SUPPORT_WAITING
91
+ extra_fields = {"resolved": False}
92
+ else:
93
+ # Should not happen if filter is correct
94
+ return
82
95
 
83
- async def feedback_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
96
+ db = mongo_client[db_name]
97
+ collection = db[collection_name]
98
+ doc = {
99
+ "user_id": update.effective_user.id,
100
+ "username": update.effective_user.username,
101
+ doc_field_name: update.message.text,
102
+ "bot_name": bot_name,
103
+ "timestamp": datetime.now().isoformat(),
104
+ }
105
+ doc.update(extra_fields)
106
+ collection.insert_one(doc)
107
+ await update.message.reply_text(t(message_key, update.effective_user.language_code, common=True))
108
+ context.user_data[context_key] = False
109
+
110
+
111
+ async def handle_feedback_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
84
112
  """Feedback command handler"""
85
113
  await update.message.reply_text(
86
114
  t("feedback.message", update.effective_user.language_code, common=True)
87
115
  )
88
116
  context.user_data[FEEDBACK_WAITING] = True
89
-
90
-
91
- async def handle_feedback_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
92
- """Handle user's feedback message"""
93
- if context.user_data.get(FEEDBACK_WAITING):
94
- feedback_db = mongo_client["feedback"]
95
- feedback_collection = feedback_db["feedback"]
96
- feedback_doc = {
97
- "user_id": update.effective_user.id,
98
- "username": update.effective_user.username,
99
- "feedback": update.message.text,
100
- "bot_name": bot_name,
101
- "timestamp": update.message.date.isoformat(),
102
- }
103
- feedback_collection.insert_one(feedback_doc)
104
- await update.message.reply_text(t("feedback.response", update.effective_user.language_code, common=True))
105
- context.user_data[FEEDBACK_WAITING] = False
106
-
107
-
117
+
118
+
119
+ class CombinedFeedbackSupportFilter(BaseFilter):
120
+ def __call__(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> bool:
121
+ return context.user_data.get(FEEDBACK_WAITING, False) or context.user_data.get(SUPPORT_WAITING, False)
122
+
123
+
108
124
  def register_feedback_and_support_handlers(app: Application, bot_name: str) -> None:
109
125
  """Register feedback and support handlers for the bot"""
110
- app.add_handler(CommandHandler("feedback", feedback_command))
111
- app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, partial(handle_feedback_response, bot_name=bot_name)))
112
- app.add_handler(CommandHandler("support", support_command))
113
- app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, partial(handle_support_response, bot_name=bot_name)))
126
+ app.add_handler(CommandHandler("feedback", handle_feedback_command))
127
+ app.add_handler(CommandHandler("support", handle_support_command))
128
+ app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND & CombinedFeedbackSupportFilter(), partial(_handle_user_response, bot_name=bot_name)))
114
129
 
115
130
 
116
131
  def register_common_handlers(app: Application, bot_name: str) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: telegram_libs
3
- Version: 0.1.11
3
+ Version: 0.1.13
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
@@ -1,12 +1,12 @@
1
- telegram_libs/__init__.py,sha256=4EFE0ca4i7eVCTGZh7B867iJX5Ftqvks0IGv3XeT9gg,82
1
+ telegram_libs/__init__.py,sha256=dNLCvwrJMaT89UiB1LKABg-r7VCBVbkhcHZvZlnHpSQ,82
2
2
  telegram_libs/constants.py,sha256=F6pgwAuv2FgGZxxFwYYJomM4_hosexLoYsvHXQiuVNg,538
3
3
  telegram_libs/locales/en.json,sha256=kMGNE9ThGbaKH3PwappBGkWRgTupPETTG46zCMU5myk,749
4
4
  telegram_libs/locales/ru.json,sha256=lShsh98seaHRtanmq1IRv8_bgRLieTaq-jHjaoOwS0M,1134
5
5
  telegram_libs/mongo.py,sha256=7UOy_cE0ofIbH7QiiirAjOgo_FM9JImtgxQ8ouEsFeo,245
6
6
  telegram_libs/subscription.py,sha256=d7xmzplUrm1nNlWlkqW6dddOYa3t_7PAM3iPme0K5F0,1690
7
7
  telegram_libs/translation.py,sha256=8Kb2cgqKKZH4X_i2Le0V_K1imZdoaCzYAca831DOBig,2049
8
- telegram_libs/utils.py,sha256=5_LKP34GCK9SSJPrPHlc2eRY-_UY1quPlgOH_FgHnaE,4931
9
- telegram_libs-0.1.11.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
10
- telegram_libs-0.1.11.dist-info/METADATA,sha256=USXVZmpW5T-iGgEjI5H3foJ8mkxiLFIY0qNrS_9Vf0Y,804
11
- telegram_libs-0.1.11.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
12
- telegram_libs-0.1.11.dist-info/RECORD,,
8
+ telegram_libs/utils.py,sha256=KmywKbWBlkP2fYGywiEsMhNYPQsmSD5CNTO4qKtvmDQ,4999
9
+ telegram_libs-0.1.13.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
10
+ telegram_libs-0.1.13.dist-info/METADATA,sha256=_HA97HXjehOltJk52MqoAwrfWo3FNydulQYxA9hRcDo,804
11
+ telegram_libs-0.1.13.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
12
+ telegram_libs-0.1.13.dist-info/RECORD,,