A
A
Anton Miroshnikov2022-04-06 18:58:02
Python
Anton Miroshnikov, 2022-04-06 18:58:02

How to save a photo from a user in a Telegram bot?

I'm writing a telegram bot in python aiogram, I can't save photos sent by the user, what's the problem?

# 1 вариант выдает предупреждение
@dp.message_handler(content_types=['photo'])
 async def get_photo(message: types.Message):
     await message.photo[-1].download('C:/Antony/py_projects/qr_code_bot/photos/ggg.jpg')

#Код работает, но ругается и пишет: DeprecationWarning: destination parameter is deprecated, please use destination_dir or destination_file.
  warn_deprecated(

# 2 вариант выдает ошибку 
@dp.message_handler(content_types=['photo'])
async def get_photo(message):
    file_info = await bot.get_file(message.photo[-1].file_id)
    downloaded_file = await bot.download_file(file_info.file_path)
    print(downloaded_file)

    with open("C:/Antony/py_projects/qr_code_bot/photos/file.jpg", 'wb') as file_object:
        file_object.write(downloaded_file)
        await bot.send_message(message, f'Ваш  файл сохранен"')

#пишет TypeError: a bytes-like object is required, not '_io.BytesIO'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2022-04-06
@phaggi

So read the deprecation warning. This is not an error, but a warning that this parameter will be removed in future versions, and other parameters will be introduced instead, and it is said which ones.
It works and it's great.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question