diff --git a/src/bot/bot_interface.py b/src/bot/bot_interface.py index a5fd9c1..aab0244 100644 --- a/src/bot/bot_interface.py +++ b/src/bot/bot_interface.py @@ -3,9 +3,17 @@ from typing import Self from django.conf import settings -from telegram.ext import Application, ApplicationBuilder, PicklePersistence +from telegram.ext import ( + Application, + ApplicationBuilder, + CallbackQueryHandler, + ConversationHandler, + PicklePersistence, +) -from bot.handlers.command_handlers import start_handler +from bot.constants.states import States +from bot.handlers.command_handlers import start, start_handler +from bot.handlers.conversation_handlers import go, next_time logger = logging.getLogger(__name__) @@ -55,7 +63,8 @@ async def _build_app(self) -> Application: .persistence(PicklePersistence(filepath=settings.PERSISTANCE_PATH)) .build() ) - app.add_handlers([start_handler]) + main_handler = await build_main_handler() + app.add_handlers([start_handler, main_handler]) return app async def _manage_webhook(self) -> None: @@ -79,3 +88,20 @@ async def _start_bot(self) -> None: async def _stop_bot(self) -> None: """Останавливает основное ASGI-приложение.""" await Application.stop(self._app) + + +async def build_main_handler(): + """123213.""" + return ConversationHandler( + entry_points=[start_handler], + persistent=True, + name="main_handler", + states={ + States.START: [ + CallbackQueryHandler(go, pattern="^go$"), + CallbackQueryHandler(next_time, pattern="^next_time$"), + ], + States.NEXT_TIME: [CallbackQueryHandler(start, pattern="^start$")], + }, + fallbacks=[], + ) diff --git a/src/bot/constants/buttons.py b/src/bot/constants/buttons.py index 4bbffb7..a9e5f75 100644 --- a/src/bot/constants/buttons.py +++ b/src/bot/constants/buttons.py @@ -1,2 +1,3 @@ GO_BUTTON = "GO" NEXT_TIME_BUTTON = "В следующий раз" +START_BUTTON = "Start" diff --git a/src/bot/constants/messages.py b/src/bot/constants/messages.py index e72b0ec..0b4c0f1 100644 --- a/src/bot/constants/messages.py +++ b/src/bot/constants/messages.py @@ -3,3 +3,4 @@ 'Для этого необходимо нажать на кнопку "Go". Если вы не готовы на этой неделе участвовать в ' 'Random Coffee просто нажмите "В следующий раз".' ) +NEXT_TIME_MESSAGE = "Будем ждать вас снова. Когда будете готовы, запустите бота командой /start" diff --git a/src/bot/constants/states.py b/src/bot/constants/states.py new file mode 100644 index 0000000..6952663 --- /dev/null +++ b/src/bot/constants/states.py @@ -0,0 +1,9 @@ +import enum + + +class States(str, enum.Enum): + """111.""" + + START = "start" + GO = "go" + NEXT_TIME = "next_time" diff --git a/src/bot/handlers/command_handlers.py b/src/bot/handlers/command_handlers.py index a02e10f..11fc4b1 100644 --- a/src/bot/handlers/command_handlers.py +++ b/src/bot/handlers/command_handlers.py @@ -2,14 +2,24 @@ from telegram.ext import CallbackContext, CommandHandler from bot.constants.messages import START_MESSAGE +from bot.constants.states import States from bot.keyboards.command_keyboards import start_keyboard_markup -async def start(update: Update, context: CallbackContext) -> None: +async def start(update: Update, context: CallbackContext): """Функция-обработчик команды start.""" - await update.message.reply_text( - text=START_MESSAGE, reply_markup=start_keyboard_markup - ) + if update.message is not None: + await update.message.reply_text( + START_MESSAGE, reply_markup=start_keyboard_markup + ) + else: + query = update.callback_query + await query.answer() + await query.edit_message_reply_markup(reply_markup=None) + await query.message.reply_text( + START_MESSAGE, reply_markup=start_keyboard_markup + ) + return States.START start_handler = CommandHandler("start", start) diff --git a/src/bot/handlers/conversation_handlers.py b/src/bot/handlers/conversation_handlers.py new file mode 100644 index 0000000..564352b --- /dev/null +++ b/src/bot/handlers/conversation_handlers.py @@ -0,0 +1,21 @@ +from telegram import Update +from telegram.ext import CallbackContext + +from bot.constants.messages import NEXT_TIME_MESSAGE +from bot.constants.states import States + + +async def go(update: Update, context: CallbackContext): + """123.""" + query = update.callback_query + await query.answer() + await query.edit_message_text("Тут будет логика для GO") + + +async def next_time(update: Update, context: CallbackContext): + """1234.""" + query = update.callback_query + await query.answer() + await query.edit_message_reply_markup(reply_markup=None) + await query.message.reply_text(NEXT_TIME_MESSAGE) + return States.NEXT_TIME diff --git a/src/bot/keyboards/conversation_keyboards.py b/src/bot/keyboards/conversation_keyboards.py new file mode 100644 index 0000000..e624637 --- /dev/null +++ b/src/bot/keyboards/conversation_keyboards.py @@ -0,0 +1,7 @@ +from telegram import InlineKeyboardButton, InlineKeyboardMarkup + +from bot.constants.buttons import START_BUTTON + +next_time_keyboard = InlineKeyboardMarkup( + [[InlineKeyboardButton(text=START_BUTTON, callback_data="start")]] +)