Skip to content

Commit

Permalink
Попытка разбить работу на состояния
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezereul committed Sep 18, 2023
1 parent 174a64d commit 29046b5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 7 deletions.
32 changes: 29 additions & 3 deletions src/bot/bot_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand All @@ -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=[],
)
1 change: 1 addition & 0 deletions src/bot/constants/buttons.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
GO_BUTTON = "GO"
NEXT_TIME_BUTTON = "В следующий раз"
START_BUTTON = "Start"
1 change: 1 addition & 0 deletions src/bot/constants/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
'Для этого необходимо нажать на кнопку "Go". Если вы не готовы на этой неделе участвовать в '
'Random Coffee просто нажмите "В следующий раз".'
)
NEXT_TIME_MESSAGE = "Будем ждать вас снова. Когда будете готовы, запустите бота командой /start"
9 changes: 9 additions & 0 deletions src/bot/constants/states.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import enum


class States(str, enum.Enum):
"""111."""

START = "start"
GO = "go"
NEXT_TIME = "next_time"
18 changes: 14 additions & 4 deletions src/bot/handlers/command_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 21 additions & 0 deletions src/bot/handlers/conversation_handlers.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions src/bot/keyboards/conversation_keyboards.py
Original file line number Diff line number Diff line change
@@ -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")]]
)

0 comments on commit 29046b5

Please sign in to comment.