A
A
Andrew2021-12-09 01:08:43
Python
Andrew, 2021-12-09 01:08:43

The bot sends a photo from the directory to the chat, how can I get the file_id so that it does not load every time from the folder?

There is a code that catches the photos sent to the bot, and I can get the file_id this way, but it does not work when the bot itself sends the photo

@dp.message_handler(content_types=['photo'], state='*')
async def scan_photo(message: types.Message):
    doc = message.photo[0].file_id
    info = await bot.get_file(doc)


Let's just say I have about 700 small pictures lying on my computer. Now this bot sends them according to my algorithm, but always downloads them. I would like to turn these pictures into file_id, but I would not want to do it manually and upload everything myself. It seems to me there is a possibility to automate all this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bl4ckm45k, 2021-12-10
@remberq

photo_data = await db.get_file_info(f'{files_dir}file_name.png')
if photo_data is None:
    media = types.InputFile(f'{files_dir}file_name.png')
    msg = await dp.bot.send_photo(chat_id=message.chat.id, photo=media)
    full_size_photo_id = msg['photo'][-1]['file_id']
    await db.add_file_info(full_size_photo_id)
else:
    await dp.bot.send_photo(chat_id=message.chat.id, photo=photo_data)

Depending on which database you are using fetchval or fetch, the condition may need to be changed to:
if photo_data is None:
if len(photo_data) == 0:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question