"""/start, /help, /menu, main menu buttons and in-section navigation.""" from aiogram import F, Router from aiogram.filters import Command, CommandStart from aiogram.fsm.context import FSMContext from aiogram.types import CallbackQuery, Message from app.bot.menu import Nav, NavAction, Section, main_menu, section_by_button, section_nav from app.texts import Texts def build_router(texts: Texts, admin_ids: frozenset[int]) -> Router: router = Router(name="common") sections = section_by_button(texts) section_text = { Section.COMMIT: texts.section.commit, Section.STATUS: texts.section.status, Section.LOGS: texts.section.logs, Section.ALERTS: texts.section.alerts, Section.MEMBERS: texts.section.members, } def is_admin(message: Message) -> bool: return message.from_user is not None and message.from_user.id in admin_ids @router.message(CommandStart()) async def start(message: Message, state: FSMContext) -> None: await state.clear() await message.answer(texts.start.welcome, reply_markup=main_menu(texts, is_admin=is_admin(message))) @router.message(Command("menu")) async def menu(message: Message, state: FSMContext) -> None: await state.clear() await message.answer(texts.menu.placeholder, reply_markup=main_menu(texts, is_admin=is_admin(message))) @router.message(Command("help")) async def help_(message: Message) -> None: await message.answer(texts.help.text) @router.message(F.text.in_(sections)) async def open_section(message: Message, state: FSMContext) -> None: section = sections[message.text or ""] if section is Section.MEMBERS and not is_admin(message): return await state.clear() await message.answer(section_text[section], reply_markup=section_nav(texts)) @router.callback_query(Nav.filter()) async def navigate(query: CallbackQuery, callback_data: Nav, state: FSMContext) -> None: # At a section's root "Back" and "Cancel" both close it; nested screens will override "Back" per section. await state.clear() text = texts.section.cancelled if callback_data.action is NavAction.CANCEL else texts.menu.placeholder if isinstance(query.message, Message): await query.message.edit_text(text) await query.answer() return router