telegram_libs 0.1.12__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.12"
5
+ __version__ = "0.1.13"
telegram_libs/utils.py CHANGED
@@ -7,11 +7,13 @@ from telegram import (
7
7
  )
8
8
  from telegram import Update
9
9
  from telegram.ext import ContextTypes, Application, CommandHandler, MessageHandler, filters
10
+ from telegram.ext.filters import BaseFilter
10
11
  from telegram_libs.constants import BOTS_AMOUNT
11
12
  from telegram_libs.translation import t
12
13
  from telegram_libs.mongo import mongo_client
13
14
 
14
15
 
16
+
15
17
  basicConfig(
16
18
  format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=INFO
17
19
  )
@@ -63,7 +65,7 @@ async def more_bots_list_command(update: Update, context: ContextTypes.DEFAULT_T
63
65
  await update.message.reply_text(message, disable_web_page_preview=True, parse_mode='HTML')
64
66
 
65
67
 
66
- async def support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
68
+ async def handle_support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
67
69
  """Support command handler"""
68
70
  await update.message.reply_text(
69
71
  t("support.message", update.effective_user.language_code, common=True)
@@ -71,55 +73,59 @@ async def support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
71
73
  context.user_data[SUPPORT_WAITING] = True
72
74
 
73
75
 
74
- async def handle_support_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
75
- """Handle user's support message"""
76
- if context.user_data.get(SUPPORT_WAITING):
77
- support_db = mongo_client["support"]
78
- support_collection = support_db["support"]
79
- support_doc = {
80
- "user_id": update.effective_user.id,
81
- "username": update.effective_user.username,
82
- "message": update.message.text,
83
- "bot_name": bot_name,
84
- "timestamp": datetime.now().isoformat(),
85
- "resolved": False,
86
- }
87
- support_collection.insert_one(support_doc)
88
- await update.message.reply_text(t("support.response", update.effective_user.language_code, common=True))
89
- context.user_data[SUPPORT_WAITING] = False
90
-
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
91
95
 
92
- 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:
93
112
  """Feedback command handler"""
94
113
  await update.message.reply_text(
95
114
  t("feedback.message", update.effective_user.language_code, common=True)
96
115
  )
97
116
  context.user_data[FEEDBACK_WAITING] = True
98
-
99
-
100
- async def handle_feedback_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
101
- """Handle user's feedback message"""
102
- if context.user_data.get(FEEDBACK_WAITING):
103
- feedback_db = mongo_client["feedback"]
104
- feedback_collection = feedback_db["feedback"]
105
- feedback_doc = {
106
- "user_id": update.effective_user.id,
107
- "username": update.effective_user.username,
108
- "feedback": update.message.text,
109
- "bot_name": bot_name,
110
- "timestamp": datetime.now().isoformat(),
111
- }
112
- feedback_collection.insert_one(feedback_doc)
113
- await update.message.reply_text(t("feedback.response", update.effective_user.language_code, common=True))
114
- context.user_data[FEEDBACK_WAITING] = False
115
-
116
-
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
+
117
124
  def register_feedback_and_support_handlers(app: Application, bot_name: str) -> None:
118
125
  """Register feedback and support handlers for the bot"""
119
- app.add_handler(CommandHandler("feedback", feedback_command))
120
- app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, partial(handle_feedback_response, bot_name=bot_name)))
121
- app.add_handler(CommandHandler("support", support_command))
122
- 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)))
123
129
 
124
130
 
125
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.12
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=Goxb5nZfiXVHZrzSy70a7AtGf3AiUSDJhBWl57NR_g4,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=qHxOZ0KfrSKsFDhlpKLjGKDInTBXHmjtPxTHTFkCplE,5125
9
- telegram_libs-0.1.12.dist-info/LICENSE,sha256=ZXkWPZbCc61L29Gz6ZHPwn1c4Pm0TnfIqtx8jGWi9F4,1069
10
- telegram_libs-0.1.12.dist-info/METADATA,sha256=8PtxVYaFnAwBi2XFIXu8I2MjTg_6Gq_jmgZS-Q_tI2g,804
11
- telegram_libs-0.1.12.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
12
- telegram_libs-0.1.12.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,,