import logging from html import escape from aiogram import Bot from config import settings logger = logging.getLogger(__name__) async def _send_admin_log(bot: Bot, topic_id: int | None, text: str) -> None: if settings.admin_log_chat_id is None or topic_id is None: return try: await bot.send_message( settings.admin_log_chat_id, text, message_thread_id=topic_id, disable_web_page_preview=True, ) except Exception: logger.warning("Failed to send admin log notification", exc_info=True) def _format_username(username: str | None) -> str: if not username: return "-" return "@" + escape(username) async def notify_deposit( bot: Bot, *, user_id: int, amount: int, balance_before: int, balance_after: int, bill_id: int, payment_id: str, ) -> None: await _send_admin_log( bot, settings.admin_log_deposit_topic_id, "\n".join( [ "Пополнение баланса", f"Пользователь: {user_id}", f"Сумма: {amount}₽", f"Баланс: {balance_before}₽ -> {balance_after}₽", f"Bill ID: {bill_id}", f"Payment ID: {escape(payment_id)}", ] ), ) async def notify_subscription_purchase( bot: Bot, *, user_id: int, username: str | None, amount: int, balance_before: int, balance_after: int, devices: int, duration_days: int, whitelists: bool, credit: int, ) -> None: lines = [ "Покупка подписки", f"Пользователь: {user_id} ({_format_username(username)})", f"Стоимость: {amount}₽", f"Баланс: {balance_before}₽ -> {balance_after}₽", f"Устройства: {devices}", f"Срок: {duration_days} дн.", f"Whitelist: {'да' if whitelists else 'нет'}", ] if credit > 0: lines.append(f"Зачет неиспользованных дней: {credit}₽") await _send_admin_log(bot, settings.admin_log_subscriptions_topic_id, "\n".join(lines)) async def notify_subscription_renewal( bot: Bot, *, user_id: int, amount: int, balance_before: int, balance_after: int, devices: int, duration_days: int, whitelists: bool, end_date: str, ) -> None: await _send_admin_log( bot, settings.admin_log_subscriptions_topic_id, ( "Продление подписки", f"Пользователь: {user_id}", f"Стоимость: {amount}₽", f"Баланс: {balance_before}₽ -> {balance_after}₽", f"Устройства: {devices}", f"Срок: {duration_days} дн.", f"Whitelist: {'да' if whitelists else 'нет'}", f"Новая дата окончания: {escape(end_date)}", ), )