Q
Q
qwwwwwty2021-11-10 21:36:24
Python
qwwwwwty, 2021-11-10 21:36:24

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:

618c10abcf8a0388366121.png

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

3 answer(s)
Y
yaflay, 2022-01-08
@yaflay

Do you have a space in your folder name?

src = 'C:/Users/andis/Desktop/Python/ EasyBot 2 /handlers/photo' + message.photo[1].file_id
change the name of the folder and that's it

P
Python_Prog, 2022-03-16
@Python_Prog

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

G
ginnsun, 2022-04-06
@ginnsun

The question is where did you get the file_path?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question