0

Пишу бота через Aiogram который будет отвечать текстом на голосовые. Как можно скачать голосовое?

2 ответа 2

3

Забираете из объекта Message объект Voice и сохраняете файл, aiogram предоставляет для этого всё:

from pathlib import Path
from aiogram.types import ContentType, File, Message

...

async def handle_file(file: File, file_name: str, path: str):
    Path(f"{path}").mkdir(parents=True, exist_ok=True)

    await bot.download_file(file_path=file.file_path, destination=f"{path}/{file_name}")

@dp.message_handler(content_types=[ContentType.VOICE])
async def voice_message_handler(message: Message):
    voice = await message.voice.get_file()
    path = "/files/voices"

    await handle_file(file=voice, file_name=f"{voice.file_id}.ogg", path=path)
1
file_id = message.voice.file_id
file = await bot.get_file(file_id)
file_path = file.file_path
await bot.download_file(file_path, "123.mp3")

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.