Update message sender for video support

This commit is contained in:
2025-12-04 18:24:32 +03:00
parent fbbee50996
commit 4752183ed9

View File

@@ -550,117 +550,31 @@ class TaskExecutor:
from bot.utils.helpers import format_duration
caption += f"\n⏱ Длительность: {format_duration(result['duration'])}"
# Replace status message with video/file using edit_message_media
# Send file and delete old status message
# Note: edit_message_media cannot replace text message with media,
# so we send file and delete old message to create replacement effect
message_id = get_task_message(task.id)
success = False
if message_id:
# Send file
logger.info(f"Sending file for task {task.id}: {result['file_path']}")
success = await send_file_to_user(
client=app_client,
chat_id=task.user_id,
file_path=result['file_path'],
caption=caption,
thumbnail=result.get('thumbnail')
)
# Delete old status message if file sent successfully
if success and message_id:
try:
from pyrogram.types import InputMediaVideo, InputMediaPhoto, InputMediaAudio, InputMediaDocument
file = Path(result['file_path'])
file_ext = file.suffix.lower()
# Determine media type and create appropriate InputMedia
if file_ext in ['.mp4', '.avi', '.mov', '.mkv', '.webm']:
# Video - prepare thumbnail
thumbnail_path = result.get('thumbnail')
if thumbnail_path and Path(thumbnail_path).exists():
media = InputMediaVideo(
media=str(file),
caption=caption,
thumb=str(thumbnail_path)
)
else:
# Try to generate thumbnail if not exists
from bot.utils.file_processor import generate_thumbnail
thumbnail_path_temp = f"downloads/thumb_{file.stem}.jpg"
if await generate_thumbnail(str(file), thumbnail_path_temp):
media = InputMediaVideo(
media=str(file),
caption=caption,
thumb=thumbnail_path_temp
)
else:
media = InputMediaVideo(
media=str(file),
caption=caption
)
elif file_ext in ['.jpg', '.jpeg', '.png', '.gif', '.webp']:
# Image
media = InputMediaPhoto(
media=str(file),
caption=caption
)
elif file_ext in ['.mp3', '.wav', '.ogg', '.m4a', '.flac']:
# Audio
thumbnail_path = result.get('thumbnail')
if thumbnail_path and Path(thumbnail_path).exists():
media = InputMediaAudio(
media=str(file),
caption=caption,
thumb=str(thumbnail_path)
)
else:
media = InputMediaAudio(
media=str(file),
caption=caption
)
else:
# Document
thumbnail_path = result.get('thumbnail')
if thumbnail_path and Path(thumbnail_path).exists():
media = InputMediaDocument(
media=str(file),
caption=caption,
thumb=str(thumbnail_path)
)
else:
media = InputMediaDocument(
media=str(file),
caption=caption
)
# Replace message with media
await app_client.edit_message_media(
await app_client.delete_messages(
chat_id=task.user_id,
message_id=message_id,
media=media
message_ids=message_id
)
logger.info(f"Replaced status message with media for task {task.id}")
success = True
except Exception as e:
# If edit_message_media fails, fallback to sending file normally
logger.warning(f"Failed to replace message with media for task {task.id}: {e}, sending file normally")
try:
success = await send_file_to_user(
client=app_client,
chat_id=task.user_id,
file_path=result['file_path'],
caption=caption,
thumbnail=result.get('thumbnail')
)
# Delete old status message if file sent successfully
if success and message_id:
try:
await app_client.delete_messages(
chat_id=task.user_id,
message_ids=message_id
)
except Exception as delete_error:
logger.debug(f"Failed to delete old message: {delete_error}")
except Exception as send_error:
logger.error(f"Failed to send file as fallback: {send_error}")
else:
# No message_id, send file normally
logger.info(f"No message_id for task {task.id}, sending file normally")
success = await send_file_to_user(
client=app_client,
chat_id=task.user_id,
file_path=result['file_path'],
caption=caption,
thumbnail=result.get('thumbnail')
)
logger.info(f"Deleted old status message for task {task.id}")
except Exception as delete_error:
logger.debug(f"Failed to delete old status message for task {task.id}: {delete_error}")
logger.info(f"File sending result: success={success}")