Files
remnawave-bedolaga-telegram…/app/handlers/common.py
Fringg d9de15a5a0 feat: add close button to all webhook notifications
Add dismissible close button (✖️) to every webhook notification message.
Users can now close any webhook notification by tapping the button,
which deletes the message via webhook:close callback handler.
2026-02-10 06:22:17 +03:00

122 lines
4.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.

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_webhook_notification_close(
callback: types.CallbackQuery,
):
"""Удаляет webhook-уведомление при нажатии кнопки Закрыть."""
try:
await callback.message.delete()
except Exception:
pass
await callback.answer()
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(handle_webhook_notification_close, F.data == 'webhook:close')
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('/'),
)