Update message sender for video support
This commit is contained in:
@@ -550,89 +550,13 @@ class TaskExecutor:
|
|||||||
from bot.utils.helpers import format_duration
|
from bot.utils.helpers import format_duration
|
||||||
caption += f"\n⏱ Длительность: {format_duration(result['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)
|
message_id = get_task_message(task.id)
|
||||||
success = False
|
|
||||||
|
|
||||||
if message_id:
|
# Send file
|
||||||
try:
|
logger.info(f"Sending file for task {task.id}: {result['file_path']}")
|
||||||
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(
|
|
||||||
chat_id=task.user_id,
|
|
||||||
message_id=message_id,
|
|
||||||
media=media
|
|
||||||
)
|
|
||||||
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(
|
success = await send_file_to_user(
|
||||||
client=app_client,
|
client=app_client,
|
||||||
chat_id=task.user_id,
|
chat_id=task.user_id,
|
||||||
@@ -640,6 +564,7 @@ class TaskExecutor:
|
|||||||
caption=caption,
|
caption=caption,
|
||||||
thumbnail=result.get('thumbnail')
|
thumbnail=result.get('thumbnail')
|
||||||
)
|
)
|
||||||
|
|
||||||
# Delete old status message if file sent successfully
|
# Delete old status message if file sent successfully
|
||||||
if success and message_id:
|
if success and message_id:
|
||||||
try:
|
try:
|
||||||
@@ -647,20 +572,9 @@ class TaskExecutor:
|
|||||||
chat_id=task.user_id,
|
chat_id=task.user_id,
|
||||||
message_ids=message_id
|
message_ids=message_id
|
||||||
)
|
)
|
||||||
|
logger.info(f"Deleted old status message for task {task.id}")
|
||||||
except Exception as delete_error:
|
except Exception as delete_error:
|
||||||
logger.debug(f"Failed to delete old message: {delete_error}")
|
logger.debug(f"Failed to delete old status message for task {task.id}: {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"File sending result: success={success}")
|
logger.info(f"File sending result: success={success}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user