commit 0bb0cece15dd61d87b92c249ea5d926a05662b26 Author: kasperarts Date: Sat Jun 8 17:56:40 2024 +0300 initial commit diff --git a/ test.html b/ test.html new file mode 100644 index 0000000..65c5087 --- /dev/null +++ b/ test.html @@ -0,0 +1 @@ +

Hello World

\ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e0701d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +config.yaml +.vscode +.venv +__pycache__ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..0fd91ae --- /dev/null +++ b/TODO.md @@ -0,0 +1,7 @@ +- Делать разделение по юзерам во время использования. Т.е. под каждую сессию для раасыки - свой класс. +- Организовать корректную проверку введенных данных. +- Написать функцию по составлению SQL-запроса для БД +- Сформировать БД из данной exel-таблицы +- Написать функцию для рассылки WatsApp и Email +- Вынести кнопки в отдельные класс. +- Переписать обработчики сообщений, вынести отдельными функциями из __init__ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..a4c2397 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +"""SenderBot - Telegram bot for mailing to email and Watsapp-messanger.""" + +from modules.telegram import TGSenderBot + +if __name__ == "__main__": + bot = TGSenderBot() + bot.bot.infinity_polling() diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/configuration.py b/modules/configuration.py new file mode 100644 index 0000000..5dd174b --- /dev/null +++ b/modules/configuration.py @@ -0,0 +1,20 @@ +"""Module for work with config-file.""" + +from pathlib import Path + +import yaml + + +class Configuration: + """Class for parsing config-file.""" + + def __init__(self, path: str) -> None: + """Init class.""" + self.path = path + self.config = self.get() + self.token = self.config["telegram_token"] + + def get(self) -> dict: + """Open file and get all value.""" + with Path(self.path).open() as file: + return yaml.safe_load(file) diff --git a/modules/telegram.py b/modules/telegram.py new file mode 100644 index 0000000..bc2c49a --- /dev/null +++ b/modules/telegram.py @@ -0,0 +1,177 @@ +"""...""" +# -*- coding: utf-8 -*- + +from pathlib import Path + +import telebot +from telebot.types import BotCommand, KeyboardButton, ReplyKeyboardMarkup + +from modules.configuration import Configuration + + +class TGSenderBot: + """...""" + + def __init__(self) -> None: + """...""" + self.config = Configuration("config.yaml") + 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.company = None + self.template_msg_path = Path() + self.mailing_method = None + self.waiting_template = bool + self.template_type = None + + self.bot.set_my_commands([self.start_cmd, self.start_mailing]) + + # Commands + @self.bot.message_handler(commands=["start"]) + def __start_bot(message) -> None: ... + + @self.bot.message_handler(commands=["mailing"]) + def __start_mailing(message) -> None: + self.__clear_old_data() + self.bot.send_message( + message.chat.id, + "Привет! Давай начнем подготовку к рассылке.\n\n" + "Для начала - нужно выбрать компанию, от имени которой ты будешь делать рассылку.", + reply_markup=self.__company_buttons(), + ) + + # Messages + @self.bot.message_handler(func=lambda message: True, content_types=["document","text","contact"]) # noqa: ARG005 + def __send_message(message) -> None: + if self.waiting_template and message.document: + if message.document.mime_type.split("/")[1] == self.template_type: + template_info = self.bot.get_file(message.document.file_id) + download_file = self.bot.download_file(template_info.file_path) + + self.template_msg_path = Path(f"./templates/{message.document.file_name}") + + with self.template_msg_path.open("w+b") as file: + file.write(download_file) + self.bot.send_message( + message.chat.id, + "Отлично! Шаблон сохранен. Осталось лишь проверить все данные, для рассылки.", + reply_markup= self.__enter_check(), + ) + else: + self.bot.send_message( + message.chat.id, + "Неверный формат файла. Пожалуйста, отправь файл в формате " + f"{'.txt' if self.template_type == 'plain' else '.html'}", + ) + + match message.text: + case "COZY COTTON" | "MixFix" | "chic chick" | "Ky Ky" | "WOLF&OWL" | "NOW": + self.company = message.text + self.bot.send_message( + message.chat.id, + f'Выбрана компания "{self.company}"\n\nТеперь необходимо выбрать метод,' + "которым будут рассылаться сообщения.\n" + "Пожалуйста, выбери формат шаблона.", + reply_markup=self.__mailing_methods(), + ) + case "E-Mail" | "WatsApp": + self.mailing_method = "mail" if message.text == "E-Mail" else "whatsapp" + msg = "по E-Mail" if self.mailing_method == "mail" else "сообщениями в WatsApp" + + self.bot.send_message( + message.chat.id, + f"Рассылка будет произведена методом {msg}.\n\n" + "Далее мы с тобой напишем шаблон сообщения для рассылки.", + reply_markup=self.__types_template_message(), + ) + case "Текст" | "HTML": + self.template_type = "plain" if message.text == "Текст" else "html" + self.bot.send_message( + message.chat.id, + "Отлично. Теперь отправь мне шаблон сообщения файлом.", + ) + self.waiting_template = True + case "Проверить данные": + with self.template_msg_path.open() as file: # type: ignore # noqa: PGH003 + txt = file.read() + msg = str(f'Отлично! Мы на финишной прямой. Проверь все данные. ' + f'если все верно - нажми на кнопку "Начать", ' + f"если где-то ошибка, пожалуйста, нажми на кнопку, что ты хочешь изменить.\n\n" + f"Компания: {self.company}\n" + f"Метод: {self.mailing_method}\n" + f"Текст:\n\n{txt}", + ) + self.bot.send_message(message.chat.id, msg,reply_markup=self.__finish_menu()) + case "Изменить компанию": + self.bot.send_message( + message.chat.id, + "Выбери компанию, от имени которой ты будешь делать рассылку.", + reply_markup=self.__company_buttons() + ) + case "Начать": + self.bot.send_message( + message.chat.id, + "Рассылка начата. По ее окончанию - я сообщу Вам.", + ) + + def __clear_old_data(self) -> None: + self.company = None + self.template_msg_path = None + 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/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e353f8f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[tool.ruff] +line-length = 120 + +[tool.ruff.lint] +select = ["ALL"] +ignore = ["ANN001", "ANN101", "RUF001"] \ No newline at end of file diff --git a/templates/test.html b/templates/test.html new file mode 100644 index 0000000..65c5087 --- /dev/null +++ b/templates/test.html @@ -0,0 +1 @@ +

Hello World

\ No newline at end of file diff --git a/templates/test.txt b/templates/test.txt new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/templates/test.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file