feat(bot): forward download errors to admin bot
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 51s

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 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-07-21 18:16:42 +00:00
parent 72ba00bdcd
commit be1acaaf2b

42
bot.py
View file

@ -558,6 +558,45 @@ async def send_video_to_admin_bot(video_path: str, url: str, from_user=None):
logger.error(f"Ошибка при отправке видео админ боту: {e}") 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}")
# ============================================================================ # ============================================================================
# СИСТЕМА ОЧЕРЕДЕЙ # СИСТЕМА ОЧЕРЕДЕЙ
# ============================================================================ # ============================================================================
@ -615,6 +654,7 @@ async def process_queue_item(item: QueueItem):
size_mb = file_size / (1024 * 1024) size_mb = file_size / (1024 * 1024)
error_msg = get_text(item.locale, 'error_file_too_large', size_mb=size_mb) 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) await item.status_message.edit_text(error_msg)
# Удаляем слишком большой файл # Удаляем слишком большой файл
try: try:
@ -689,6 +729,8 @@ async def process_queue_item(item: QueueItem):
source = detect_video_source(item.url) source = detect_video_source(item.url)
increment_error_count(source) 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)) error_msg = get_text(item.locale, 'error', error=str(e))
try: try:
await item.status_message.edit_text(error_msg) await item.status_message.edit_text(error_msg)