Answer the question
In order to leave comments, you need to log in
How to download a photo through a bot when given a specific Python Aiogram command?
How to download a photo through a bot when given a specific Python Aiogram command? An example of the situation in the screenshot:
that very specific !dem command. I already have some code, but for some reason it doesn't work, the bot just doesn't download anything. the console does not give any errors.
here is the code:
@dp.message_handler(commands=['dem'], commands_prefix="!/")
async def cmd_create_dem(msg: types.Message):
file_info = bot.get_file(message.photo[len(message.photo) - 1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = 'C:/Users/andis/Desktop/Python/EasyBot 2/handlers/photo' + message.photo[1].file_id
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
bot.message.answer(message, "Фото успешно скачано!")
Answer the question
In order to leave comments, you need to log in
Do you have a space in your folder name?
src = 'C:/Users/andis/Desktop/Python/ EasyBot 2 /handlers/photo' + message.photo[1].file_idchange the name of the folder and that's it
You forgot to put await before bot.get_file(message.photo[len(message.photo) - 1].file_id)
And if you want to get the last value, you can do it like this: bot.get_file(message.photo[-1] .file_id) and also await
before bot.download_file(file_info.file_path) , aiogram is an asynchronous module
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question