Files
remnawave-bedolaga-telegram…/app/handlers/common.py
c0mrade 9a2aea038a chore: add uv package manager and ruff linter configuration
- Add pyproject.toml with uv and ruff configuration
- Pin Python version to 3.13 via .python-version
- Add Makefile commands: lint, format, fix
- Apply ruff formatting to entire codebase
- Remove unused imports (base64 in yookassa/simple_subscription)
- Update .gitignore for new config files
2026-01-24 17:45:27 +03:00

110 lines
3.8 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.

import logging
from aiogram import Dispatcher, F, types
from aiogram.filters import StateFilter
from aiogram.fsm.context import FSMContext
from sqlalchemy.ext.asyncio import AsyncSession
from app.database.models import User
from app.keyboards.inline import get_back_keyboard
from app.localization.texts import get_rules, get_texts
logger = logging.getLogger(__name__)
async def handle_delete_ban_notification(
callback: types.CallbackQuery,
):
"""Удаляет уведомление о бане при нажатии на кнопку"""
try:
await callback.message.delete()
await callback.answer('Уведомление удалено')
except Exception as e:
logger.warning(f'Не удалось удалить уведомление: {e}')
await callback.answer('Не удалось удалить', show_alert=False)
async def handle_unknown_callback(callback: types.CallbackQuery, db_user: User):
texts = get_texts(db_user.language if db_user else 'ru')
await callback.answer(
texts.t(
'UNKNOWN_CALLBACK_ALERT',
'❓ Неизвестная команда. Попробуйте ещё раз.',
),
show_alert=True,
)
logger.warning(f'Неизвестный callback: {callback.data} от пользователя {callback.from_user.id}')
async def handle_noop(callback: types.CallbackQuery, db_user: User):
try:
await callback.answer()
except Exception:
pass
async def handle_current_page(callback: types.CallbackQuery, db_user: User):
try:
await callback.answer()
except Exception:
pass
async def handle_cancel(callback: types.CallbackQuery, state: FSMContext, db_user: User):
texts = get_texts(db_user.language)
await state.clear()
await callback.message.edit_text(texts.OPERATION_CANCELLED, reply_markup=get_back_keyboard(db_user.language))
await callback.answer()
async def handle_unknown_message(
message: types.Message,
db_user: User | None = None,
):
texts = get_texts(db_user.language if db_user else 'ru')
await message.answer(
texts.t(
'UNKNOWN_COMMAND_MESSAGE',
'Не понимаю эту команду. Используйте кнопки меню.',
),
reply_markup=get_back_keyboard(db_user.language if db_user else 'ru'),
)
async def show_rules(callback: types.CallbackQuery, db_user: User, db: AsyncSession):
get_texts(db_user.language)
rules_text = await get_rules(db_user.language)
await callback.message.edit_text(rules_text, reply_markup=get_back_keyboard(db_user.language))
await callback.answer()
def register_handlers(dp: Dispatcher):
# Удаление уведомлений о банах
dp.callback_query.register(handle_delete_ban_notification, F.data == 'ban_notify:delete')
dp.callback_query.register(show_rules, F.data == 'menu_rules')
# No-op utility handlers used in many keyboards
dp.callback_query.register(handle_noop, F.data == 'noop')
dp.callback_query.register(handle_current_page, F.data == 'current_page')
dp.callback_query.register(handle_cancel, F.data.in_(['cancel', 'subscription_cancel']))
# Самый последний: ловим любые неизвестные текстовые сообщения
# Исключаем специальные сервисные события (например, успешные платежи),
# чтобы их обработка не прерывалась общим хендлером неизвестных сообщений
dp.message.register(
handle_unknown_message,
StateFilter(None),
F.successful_payment.is_(None),
F.text.is_not(None),
~F.text.startswith('/'),
)