intra_max_chatbot/bot/handlers/menu.py
2026-03-26 15:39:02 +03:00

64 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Обработчик главного меню."""
from maxapi.dispatcher import Router
from maxapi.filters import F
from maxapi.types.updates.message_callback import MessageCallback
from maxapi.context import BaseContext
from bot.keyboards import main_menu_kb
from bot.send_helper import bot_reply
from database.connection import get_db
from database.queries.intradesk import (
count_active_tasks_for_local_org,
count_tasks_for_local_org,
)
from database.queries.organizations import get_organization
router = Router(router_id="menu")
async def build_main_menu_text(bot_user: dict) -> str:
"""Формирует текст главного меню с именем объекта и статистикой заявок."""
org_id = bot_user.get("org_id")
org_name: str | None = None
if org_id:
org = await get_organization(org_id)
org_name = org["name"] if org else None
lines: list[str] = []
if org_name:
lines.append(f"🏢 {org_name}")
else:
lines.append("🏠 Главное меню")
if org_id:
db = get_db()
total = await count_tasks_for_local_org(db, org_id)
active = await count_active_tasks_for_local_org(db, org_id)
if total == 0:
lines.append("Заявок пока нет.")
else:
lines.append(f"Заявок: {total} (активных: {active})")
lines.append("\nВыберите действие:")
return "\n".join(lines)
@router.message_callback(F.callback.payload == "main_menu")
async def cb_main_menu(
event: MessageCallback, context: BaseContext, bot_user: dict
) -> None:
from bot.handlers.create_ticket import cleanup_ticket_flow
await cleanup_ticket_flow(event.bot, context)
await context.set_state(None)
await event.answer(notification="")
text = await build_main_menu_text(bot_user)
await bot_reply(
event,
text=text,
attachments=[main_menu_kb()],
user_id=bot_user["max_user_id"],
chat_id=event.message.recipient.chat_id if event.message else None,
)