From be1acaaf2b77c9df04d09edbad1bb99219653639 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Tue, 21 Jul 2026 18:16:42 +0000 Subject: [PATCH] feat(bot): forward download errors to admin bot Notify admin (URL + error text + user info) on any download failure across all sources, and on the file-too-large case, mirroring the existing successful-download notification. Co-Authored-By: Claude Sonnet 5 --- bot.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index e618c87..f322903 100644 --- a/bot.py +++ b/bot.py @@ -558,6 +558,45 @@ async def send_video_to_admin_bot(video_path: str, url: str, from_user=None): logger.error(f"Ошибка при отправке видео админ боту: {e}") +async def notify_admin_error(url: str, error_text: str, from_user=None): + """Отправляет админ боту уведомление об ошибке скачивания""" + if not ADMIN_BOT_TOKEN: + return # Админ бот не настроен, пропускаем + + admin_chat_id = get_admin_chat_id() + if not admin_chat_id: + logger.debug("Админ chat_id не найден, пропускаю отправку ошибки") + return + + try: + request = HTTPXRequest( + read_timeout=30, + write_timeout=30, + connect_timeout=15, + pool_timeout=15 + ) + admin_bot = Bot(token=ADMIN_BOT_TOKEN, request=request) + + user_info = "" + if from_user: + username = f"@{from_user.username}" if from_user.username else "без username" + user_info = f"Пользователь: {username} (ID: {from_user.id})" + if from_user.first_name: + user_info += f", {from_user.first_name}" + + text = f"⚠️ Ошибка скачивания\n\n🔗 URL: {url}\n\n❗ {error_text}" + if user_info: + text += f"\n👤 {user_info}" + if len(text) > 4000: + text = text[:4000] + "…" + + await admin_bot.send_message(chat_id=admin_chat_id, text=text) + logger.info(f"Уведомление об ошибке отправлено админ боту: {url}") + + except Exception as e: + logger.error(f"Ошибка при отправке уведомления об ошибке админ боту: {e}") + + # ============================================================================ # СИСТЕМА ОЧЕРЕДЕЙ # ============================================================================ @@ -612,9 +651,10 @@ async def process_queue_item(item: QueueItem): # Определяем источник и увеличиваем счетчик ошибок source = detect_video_source(item.url) increment_error_count(source) - + size_mb = file_size / (1024 * 1024) error_msg = get_text(item.locale, 'error_file_too_large', size_mb=size_mb) + await notify_admin_error(item.url, f"Файл слишком большой: {size_mb:.1f} MB", item.original_message.from_user) await item.status_message.edit_text(error_msg) # Удаляем слишком большой файл try: @@ -688,7 +728,9 @@ async def process_queue_item(item: QueueItem): # Определяем источник и увеличиваем счетчик ошибок source = detect_video_source(item.url) increment_error_count(source) - + + await notify_admin_error(item.url, str(e), item.original_message.from_user) + error_msg = get_text(item.locale, 'error', error=str(e)) try: await item.status_message.edit_text(error_msg)