From 2ff6ae92ae61b029e7ee433892adc49d82b5531b Mon Sep 17 00:00:00 2001 From: kasperarts Date: Sun, 9 Jun 2024 15:35:14 +0300 Subject: [PATCH] small refactor --- TODO.md | 3 +- modules/keyboards.py | 14 +++++++++ modules/telegram.py | 69 +++++------------------------------------ test.html => test.html | 0 4 files changed, 23 insertions(+), 63 deletions(-) create mode 100644 modules/keyboards.py rename test.html => test.html (100%) diff --git a/TODO.md b/TODO.md index 0fd91ae..710479d 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,5 @@ - Организовать корректную проверку введенных данных. - Написать функцию по составлению SQL-запроса для БД - Сформировать БД из данной exel-таблицы -- Написать функцию для рассылки WatsApp и Email -- Вынести кнопки в отдельные класс. +- Написать функцию для рассылки WatsApp - Переписать обработчики сообщений, вынести отдельными функциями из __init__ \ No newline at end of file diff --git a/modules/keyboards.py b/modules/keyboards.py new file mode 100644 index 0000000..0fcfa3a --- /dev/null +++ b/modules/keyboards.py @@ -0,0 +1,14 @@ +from telebot.types import KeyboardButton, ReplyKeyboardMarkup + +class Keyboards: + def __init__(self) -> None: + self.markup = ReplyKeyboardMarkup() + + def create_buttons(self, data: tuple, row_width: int = 3) -> ReplyKeyboardMarkup: + self.markup = ReplyKeyboardMarkup() + self.markup.row_width = row_width + + for item in data: + self.markup.add(KeyboardButton(item)) + + return self.markup diff --git a/modules/telegram.py b/modules/telegram.py index bc2c49a..a5210f7 100644 --- a/modules/telegram.py +++ b/modules/telegram.py @@ -7,6 +7,7 @@ import telebot from telebot.types import BotCommand, KeyboardButton, ReplyKeyboardMarkup from modules.configuration import Configuration +from modules.keyboards import Keyboards class TGSenderBot: @@ -18,6 +19,7 @@ class TGSenderBot: self.bot = telebot.TeleBot(self.config.token) self.start_cmd = BotCommand(command="start", description="Start bot") self.start_mailing = BotCommand(command="mailing", description="Prepare to mailing") + self.keyboards = Keyboards() self.company = None self.template_msg_path = Path() self.mailing_method = None @@ -37,7 +39,7 @@ class TGSenderBot: message.chat.id, "Привет! Давай начнем подготовку к рассылке.\n\n" "Для начала - нужно выбрать компанию, от имени которой ты будешь делать рассылку.", - reply_markup=self.__company_buttons(), + reply_markup=self.keyboards.create_buttons(("COZY COTTON", "MixFix", "chic chick", "Ky Ky", "WOLF&OWL", "NOW")), ) # Messages @@ -55,7 +57,7 @@ class TGSenderBot: self.bot.send_message( message.chat.id, "Отлично! Шаблон сохранен. Осталось лишь проверить все данные, для рассылки.", - reply_markup= self.__enter_check(), + reply_markup=self.keyboards.create_buttons(("Проверить данные",)), ) else: self.bot.send_message( @@ -72,7 +74,7 @@ class TGSenderBot: f'Выбрана компания "{self.company}"\n\nТеперь необходимо выбрать метод,' "которым будут рассылаться сообщения.\n" "Пожалуйста, выбери формат шаблона.", - reply_markup=self.__mailing_methods(), + reply_markup=self.keyboards.create_buttons(("E-Mail", "WatsApp")) ) case "E-Mail" | "WatsApp": self.mailing_method = "mail" if message.text == "E-Mail" else "whatsapp" @@ -82,7 +84,7 @@ class TGSenderBot: message.chat.id, f"Рассылка будет произведена методом {msg}.\n\n" "Далее мы с тобой напишем шаблон сообщения для рассылки.", - reply_markup=self.__types_template_message(), + reply_markup=self.keyboards.create_buttons(("Текст", "HTML")) ) case "Текст" | "HTML": self.template_type = "plain" if message.text == "Текст" else "html" @@ -101,12 +103,12 @@ class TGSenderBot: f"Метод: {self.mailing_method}\n" f"Текст:\n\n{txt}", ) - self.bot.send_message(message.chat.id, msg,reply_markup=self.__finish_menu()) + self.bot.send_message(message.chat.id, msg,reply_markup=self.keyboards.create_buttons(("Изменить компанию", "Изменить метод", "Изменить шаблон", "Начать"))) case "Изменить компанию": self.bot.send_message( message.chat.id, "Выбери компанию, от имени которой ты будешь делать рассылку.", - reply_markup=self.__company_buttons() + reply_markup=self.keyboards.create_buttons(("COZY COTTON", "MixFix", "chic chick", "Ky Ky", "WOLF&OWL", "NOW")) ) case "Начать": self.bot.send_message( @@ -120,58 +122,3 @@ class TGSenderBot: self.mailing_method = None self.waiting_template = bool self.template_type = None - - def __enter_check(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 3 - markup.add(KeyboardButton("Проверить данные")) - return markup - - def __enter_company(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 3 - markup.add(KeyboardButton("Выбрать компанию")) - return markup - - def __types_template_message(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 2 - - for method in ("Текст", "HTML"): - markup.add(KeyboardButton(method)) - - return markup - - def __genetare_message_button(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 3 - markup.add( - KeyboardButton("Send Number", request_contact=True), - ) - return markup - - def __company_buttons(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 3 - - for company in ("COZY COTTON", "MixFix", "chic chick", "Ky Ky", "WOLF&OWL", "NOW"): - markup.add(KeyboardButton(company)) - - return markup - - def __mailing_methods(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 2 - - for method in ("E-Mail", "WatsApp"): - markup.add(KeyboardButton(method)) - - return markup - - def __finish_menu(self) -> ReplyKeyboardMarkup: - markup = ReplyKeyboardMarkup() - markup.row_width = 3 - - for state in ("Изменить компанию", "Изменить метод", "Изменить шаблон", "Начать"): - markup.add(KeyboardButton(state)) - return markup diff --git a/ test.html b/test.html similarity index 100% rename from test.html rename to test.html