Issue
I am writing an Aiogram bot, that takes the link to audio('https://xxxxxxx.mp3'), and sends to user this audio in format of audio.
import requests
from io import BytesIO
from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command, CommandStart
from aiogram.types import Message, BotCommand
from aiogram.types.input_file import InputFile
from config_weather import TOKEN_BOT
bot = Bot(token=TOKEN_BOT)
dp = Dispatcher()
@dp.message(lambda link: '.mp3' in link.text)
async def process_mp3_link(message: Message):
try:
mp3_file = BytesIO(requests.get(message.text).content)
await bot.send_audio(chat_id=message.chat.id, audio=InputFile(mp3_file))
except Exception as ex:
await message.answer('Error!')
print(ex)
if __name__ == '__main__':
dp.run_polling(bot)
Whenever i send a valid link, it replies me with error and terminal throws exception
Can't instantiate abstract class InputFile with abstract method read
Saving file to my PC isn't option, I want to implement it without it.
I tries to pass just response of link. response.get().content, BytesIO(response.get().content). ALso tries to implement in through URLInputFile, InputMediaAudio. Didn't work.
Solution
The error happens of course because according to the documents, InputFile.read
is an abstract function. Source: Inputfile.read.
What you should be able to do is to skip BytesIO altogether, and even requests. What you can do instead is to use InputMediaAudio.
You don't specify what error you get, but with InputMediaAudio
, you can use the url directly by passing it as the media parameter. Did you try that?
await bot.send_audio(chat_id=message.chat.id, audio=InputMediaAudio(media=message.text))
Also, you should be able to even skip the whole InputMediaAudio, as bot.send_audio
also accepts urls see documentation
await bot.send_audio(chat_id=message.chat.id, audio=message.text)
Answered By - Teemu Risikko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.