From e7e6323c6c2c3945e156e7851c459e5f709f2172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=ADITQ?= Date: Fri, 22 Mar 2024 11:02:43 +0300 Subject: [PATCH] feat: Added menu command --- app/bot.py | 17 ++++++++++----- app/callbacks/menu.py | 40 ++++++++++++++++++++++++++++++++++++ app/handlers/menu_command.py | 17 +++++++++++++++ app/keyboards/menu.py | 31 ++++++++++++++++++++++++++++ app/messages.py | 2 ++ 5 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 app/callbacks/menu.py create mode 100644 app/handlers/menu_command.py create mode 100644 app/keyboards/menu.py diff --git a/app/bot.py b/app/bot.py index 892b576..acc105d 100644 --- a/app/bot.py +++ b/app/bot.py @@ -6,9 +6,14 @@ from aiogram import Bot, Dispatcher from aiogram.enums import ParseMode from aiogram.fsm.storage.redis import RedisStorage -from app.callbacks import profile +from app.callbacks import menu, profile from app.config import Config -from app.handlers import help_command, profile_command, start_command +from app.handlers import ( + help_command, + menu_command, + profile_command, + start_command, +) from app.middlewares.throttling import ThrottlingMiddleware @@ -23,12 +28,14 @@ async def main() -> None: bot = Bot(bot_token, parse_mode=ParseMode.HTML) dp.message.middleware(ThrottlingMiddleware(0.5)) - # type: ignore + dp.include_routers( start_command.router, - profile_command.router, - profile.router, # type: ignore help_command.router, + menu_command.router, + profile_command.router, + menu.router, # type: ignore + profile.router, # type: ignore ) await bot.delete_webhook(drop_pending_updates=True) diff --git a/app/callbacks/menu.py b/app/callbacks/menu.py new file mode 100644 index 0000000..d485e58 --- /dev/null +++ b/app/callbacks/menu.py @@ -0,0 +1,40 @@ +# type: ignore +__all__ = () + +from aiogram import F, Router +from aiogram.exceptions import TelegramBadRequest +from aiogram.types import CallbackQuery + +from app import messages +from app.filters.user import RegisteredCallback +from app.keyboards.profile import get +from app.models.user import User + + +router = Router(name="menu_callback") + + +@router.callback_query(F.data == "menu_profile", RegisteredCallback()) +async def profile_callback(callback: CallbackQuery) -> None: + if callback.data is None or callback.message is None: + return + + user = User().get_user_by_telegram_id(callback.from_user.id) + + await callback.message.answer( + messages.PROFILE.format( + username=user.username, + age=user.age, + bio=user.bio if user.bio else messages.NOT_SET, + sex=user.sex.capitalize(), + country=user.country, + city=user.city, + ), + reply_markup=get(), + ) + await callback.answer() + + try: + await callback.message.delete() + except TelegramBadRequest: + pass diff --git a/app/handlers/menu_command.py b/app/handlers/menu_command.py new file mode 100644 index 0000000..9dea594 --- /dev/null +++ b/app/handlers/menu_command.py @@ -0,0 +1,17 @@ +__all__ = () + +from aiogram import Router +from aiogram.filters import Command +from aiogram.types import Message + +from app import messages +from app.filters.user import Registered +from app.keyboards.menu import get + + +router = Router(name="menu_command") + + +@router.message(Command("menu"), Registered()) +async def command_menu_handler(message: Message) -> None: + await message.answer(messages.MENU, reply_markup=get()) diff --git a/app/keyboards/menu.py b/app/keyboards/menu.py new file mode 100644 index 0000000..8cb6dac --- /dev/null +++ b/app/keyboards/menu.py @@ -0,0 +1,31 @@ +__all__ = ("get",) + +from aiogram import types +from aiogram.utils.keyboard import InlineKeyboardBuilder + + +def get(): + builder = InlineKeyboardBuilder() + + builder.row( + types.InlineKeyboardButton( + text="👤 Profile", + callback_data="menu_profile", + ), + types.InlineKeyboardButton( + text="➕ Create travel", + callback_data="menu_create_travel", + ), + ) + builder.row( + types.InlineKeyboardButton( + text="📃 Travels", + callback_data="menu_travels", + ), + types.InlineKeyboardButton( + text="🔵 Temp", + callback_data="menu_temp", + ), + ) + + return builder.as_markup() diff --git a/app/messages.py b/app/messages.py index 8e593dd..4adfdf2 100644 --- a/app/messages.py +++ b/app/messages.py @@ -1,5 +1,7 @@ # flake8: noqa +MENU = "Menu:" + WELCOME_MESSAGE = "Hello, {name}! Welcome to the ✈️ Travel Agent bot! Let's start our journey by filling out some information about you." WELCOME_AGAIN_MESSAGE = "Hello, {name}! Welcome back to the ✈️ Travel Agent bot! If you get lost, you can always call the /help command for assistance."