41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import os
|
|
import uuid
|
|
from functools import partial
|
|
from aiogram import types, Dispatcher, F
|
|
|
|
async def handle_voice_and_video(message: types.Message, redis_service, storage_path: str):
|
|
file_id = None
|
|
if message.content_type == types.ContentType.VOICE:
|
|
file_id = message.voice.file_id
|
|
elif message.content_type == types.ContentType.VIDEO_NOTE:
|
|
file_id = message.video_note.file_id
|
|
|
|
if not file_id:
|
|
return
|
|
|
|
file = await message.bot.get_file(file_id)
|
|
file_path = file.file_path
|
|
|
|
file_uuid = str(uuid.uuid4())
|
|
filename = f"{file_uuid}_{os.path.basename(file_path)}"
|
|
os.makedirs(storage_path, exist_ok=True)
|
|
destination = os.path.join(storage_path, filename)
|
|
|
|
await message.bot.download_file(file_path, destination)
|
|
|
|
task_data = {
|
|
"uuid": file_uuid,
|
|
"file_local_path": destination,
|
|
"user_id": message.from_user.id,
|
|
"chat_id": message.chat.id
|
|
}
|
|
redis_service.publish_task(task_data)
|
|
await message.reply("Your message has been received and queued for processing.")
|
|
|
|
def register_audio_handlers(dp: Dispatcher, redis_service, storage_path: str):
|
|
handler_callback = partial(handle_voice_and_video, redis_service=redis_service, storage_path=storage_path)
|
|
dp.message.register(
|
|
handler_callback,
|
|
F.content_type.in_({types.ContentType.VOICE, types.ContentType.VIDEO_NOTE})
|
|
)
|