telegram_libs 0.1.12__tar.gz → 0.1.14__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: telegram_libs
3
- Version: 0.1.12
3
+ Version: 0.1.14
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,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "telegram_libs"
3
- version = "0.1.12"
3
+ version = "0.1.14"
4
4
  description = "Common libraries for Telegram bots"
5
5
  authors = ["Andrey Gritsaenko gricaenko.95a@gmail.com"]
6
6
  readme = "README.md"
@@ -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.14"
@@ -9,11 +9,7 @@
9
9
  "info": "Buying a subscription you will get unlimited access to other {0} bots, to see all bots click /more"
10
10
  },
11
11
  "support": {
12
- "message": "If you have any questions or need help, please write it now and we will solve your issue as soon as possible.",
12
+ "message": "Write down any questions, issues or suggestions you have, and we will resolve them as soon as possible 👇 ",
13
13
  "response": "Thank you! Our support team will contact you soon."
14
- },
15
- "feedback": {
16
- "message": "We appreciate your feedback! Please send your suggestions or issues and we will review them as soon as possible.",
17
- "response": "Thank you for your feedback!"
18
14
  }
19
15
  }
@@ -9,11 +9,7 @@
9
9
  "info": "Купив подписку, вы получите неограниченный доступ к другим {0} ботам, чтобы увидеть всех ботов, нажмите /more"
10
10
  },
11
11
  "support": {
12
- "message": "Если у вас есть вопросы или нужна помощь, пожалуйста, напишите сейчас, и мы решим вашу проблему как можно скорее.",
12
+ "message": "Напишите любые вопросы, проблемы, или предложения и мы решим их как можно скорее 👇 ",
13
13
  "response": "Спасибо! Наша служба поддержки свяжется с вами в ближайшее время."
14
- },
15
- "feedback": {
16
- "message": "Мы ценим ваш отзыв! Пожалуйста, присылайте ваши предложения или проблемы, и мы рассмотрим их как можно скорее.",
17
- "response": "Спасибо за ваш отзыв!"
18
14
  }
19
15
  }
@@ -0,0 +1,57 @@
1
+ from functools import partial
2
+ from datetime import datetime
3
+ from telegram import Update
4
+ from telegram.ext import ContextTypes, Application, CommandHandler, MessageHandler, filters
5
+ from telegram.ext.filters import BaseFilter
6
+ from telegram_libs.mongo import mongo_client
7
+ from telegram_libs.translation import t
8
+
9
+
10
+ SUPPORT_WAITING = "support_waiting"
11
+
12
+
13
+ async def handle_support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
14
+ """Support command handler"""
15
+ await update.message.reply_text(
16
+ t("support.message", update.effective_user.language_code, common=True)
17
+ )
18
+ context.user_data[SUPPORT_WAITING] = True
19
+
20
+
21
+ async def _handle_user_response(update: Update, context: ContextTypes.DEFAULT_TYPE, bot_name: str) -> None:
22
+ """Handle user's support message"""
23
+ if context.user_data.get(SUPPORT_WAITING):
24
+ db_name = "support"
25
+ collection_name = "support"
26
+ message_key = "support.response"
27
+ doc_field_name = "message"
28
+ context_key = SUPPORT_WAITING
29
+ extra_fields = {"resolved": False}
30
+ else:
31
+ # Should not happen if filter is correct
32
+ return
33
+
34
+ db = mongo_client[db_name]
35
+ collection = db[collection_name]
36
+ doc = {
37
+ "user_id": update.effective_user.id,
38
+ "username": update.effective_user.username,
39
+ doc_field_name: update.message.text,
40
+ "bot_name": bot_name,
41
+ "timestamp": datetime.now().isoformat(),
42
+ }
43
+ doc.update(extra_fields)
44
+ collection.insert_one(doc)
45
+ await update.message.reply_text(t(message_key, update.effective_user.language_code, common=True))
46
+ context.user_data[context_key] = False
47
+
48
+
49
+ class SupportFilter(BaseFilter):
50
+ def __call__(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> bool:
51
+ return context.user_data.get(SUPPORT_WAITING, False)
52
+
53
+
54
+ def register_support_handlers(app: Application, bot_name: str) -> None:
55
+ """Register support handlers for the bot"""
56
+ app.add_handler(CommandHandler("support", handle_support_command))
57
+ app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND & SupportFilter(), partial(_handle_user_response, bot_name=bot_name)))
@@ -0,0 +1,66 @@
1
+ from logging import basicConfig, getLogger, INFO
2
+ from telegram import (
3
+ InlineKeyboardButton,
4
+ InlineKeyboardMarkup,
5
+ )
6
+ from telegram import Update
7
+ from telegram.ext import ContextTypes, Application, CommandHandler
8
+ from telegram_libs.constants import BOTS_AMOUNT
9
+ from telegram_libs.translation import t
10
+ from telegram_libs.mongo import mongo_client
11
+ from telegram_libs.support_handlers import register_support_handlers
12
+
13
+
14
+ basicConfig(
15
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=INFO
16
+ )
17
+ logger = getLogger(__name__)
18
+
19
+
20
+ async def get_subscription_keyboard(update: Update, lang: str) -> InlineKeyboardMarkup:
21
+ """Get subscription keyboard
22
+
23
+ Args:
24
+ update (Update): Update object
25
+ lang (str): Language code
26
+
27
+ Returns:
28
+ InlineKeyboardMarkup: Inline keyboard markup
29
+ """
30
+ await update.message.reply_text(
31
+ t("subscription.info", lang, common=True).format(int(BOTS_AMOUNT) - 1)
32
+ )
33
+ return [
34
+ [
35
+ InlineKeyboardButton(
36
+ t("subscription.plans.1month", lang, common=True), callback_data="sub_1month"
37
+ ),
38
+ InlineKeyboardButton(
39
+ t("subscription.plans.3months", lang, common=True), callback_data="sub_3months"
40
+ ),
41
+ ],
42
+ [
43
+ InlineKeyboardButton(
44
+ t("subscription.plans.1year", lang, common=True), callback_data="sub_1year"
45
+ ),
46
+ ],
47
+ ]
48
+
49
+
50
+ async def more_bots_list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
51
+ message = """Here is the list of all bots:
52
+
53
+
54
+ - <a href="https://t.me/MagMediaBot">Remove Background</a>
55
+ - <a href="https://t.me/UpscaleImageGBot">Upscale Image</a>
56
+ - <a href="https://t.me/GenerateBackgroundGBot">Generate a Background</a>
57
+ - <a href="https://t.me/kudapoyti_go_bot">Recommend a place to visit</a>
58
+ - <a href="https://t.me/TryOnOutfitGBot">Try On Outfit</a>
59
+ """
60
+ await update.message.reply_text(message, disable_web_page_preview=True, parse_mode='HTML')
61
+
62
+
63
+ def register_common_handlers(app: Application, bot_name: str) -> None:
64
+ """Register common handlers for the bot"""
65
+ app.add_handler(CommandHandler("more", more_bots_list_command))
66
+ register_support_handlers(app, bot_name)
@@ -1,128 +0,0 @@
1
- from functools import partial
2
- from logging import basicConfig, getLogger, INFO
3
- from datetime import datetime
4
- from telegram import (
5
- InlineKeyboardButton,
6
- InlineKeyboardMarkup,
7
- )
8
- from telegram import Update
9
- from telegram.ext import ContextTypes, Application, CommandHandler, MessageHandler, filters
10
- from telegram_libs.constants import BOTS_AMOUNT
11
- from telegram_libs.translation import t
12
- from telegram_libs.mongo import mongo_client
13
-
14
-
15
- basicConfig(
16
- format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=INFO
17
- )
18
- logger = getLogger(__name__)
19
-
20
-
21
- FEEDBACK_WAITING = "feedback_waiting"
22
- SUPPORT_WAITING = "support_waiting"
23
-
24
-
25
- async def get_subscription_keyboard(update: Update, lang: str) -> InlineKeyboardMarkup:
26
- """Get subscription keyboard
27
-
28
- Args:
29
- update (Update): Update object
30
- lang (str): Language code
31
-
32
- Returns:
33
- InlineKeyboardMarkup: Inline keyboard markup
34
- """
35
- await update.message.reply_text(
36
- t("subscription.info", lang, common=True).format(int(BOTS_AMOUNT) - 1)
37
- )
38
- return [
39
- [
40
- InlineKeyboardButton(
41
- t("subscription.plans.1month", lang, common=True), callback_data="sub_1month"
42
- ),
43
- InlineKeyboardButton(
44
- t("subscription.plans.3months", lang, common=True), callback_data="sub_3months"
45
- ),
46
- ],
47
- [
48
- InlineKeyboardButton(
49
- t("subscription.plans.1year", lang, common=True), callback_data="sub_1year"
50
- ),
51
- ],
52
- ]
53
-
54
-
55
- async def more_bots_list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
56
- message = """Here is the list of all bots: \n\n
57
- - <a href="https://t.me/MagMediaBot">Remove Background</a>
58
- - <a href="https://t.me/UpscaleImageGBot">Upscale Image</a>
59
- - <a href="https://t.me/GenerateBackgroundGBot">Generate a Background</a>
60
- - <a href="https://t.me/kudapoyti_go_bot">Recommend a place to visit</a>
61
- - <a href="https://t.me/TryOnOutfitGBot">Try On Outfit</a>
62
- """
63
- await update.message.reply_text(message, disable_web_page_preview=True, parse_mode='HTML')
64
-
65
-
66
- async def support_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
67
- """Support command handler"""
68
- await update.message.reply_text(
69
- t("support.message", update.effective_user.language_code, common=True)
70
- )
71
- context.user_data[SUPPORT_WAITING] = True
72
-
73
-
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
-
91
-
92
- async def feedback_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
93
- """Feedback command handler"""
94
- await update.message.reply_text(
95
- t("feedback.message", update.effective_user.language_code, common=True)
96
- )
97
- 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
- def register_feedback_and_support_handlers(app: Application, bot_name: str) -> None:
118
- """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)))
123
-
124
-
125
- def register_common_handlers(app: Application, bot_name: str) -> None:
126
- """Register common handlers for the bot"""
127
- app.add_handler(CommandHandler("more", more_bots_list_command))
128
- register_feedback_and_support_handlers(app, bot_name)
File without changes
File without changes