S
S
stavrq2021-09-04 14:00:14
Bots
stavrq, 2021-09-04 14:00:14

How to get file_id in telegram?

The task is this: I send a photo to the bot, and in response I want to see the file_id of this photo for further sending. I tried a lot, but the knowledge is not enough yet. I will be grateful for help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-09-04
@stavrq

When sending a photo, you can get its file_id and next time send only it, for example:

files = {}
if photo_name in files:
    file_id = files[photo_name]
    bot.send_photo(message.chat.id, file_id)
else:
    with open(photo_name, "rb") as f:
        bot.send_chat_action(message.chat.id, "upload_photo")
        r = bot.send_photo(message.chat.id, f)
        file_id = r.photo[0].file_id
        files[photo_name] = file_id

For incoming messages:
@bot.message_handler(content_types=['photo'])
def image_message(message):
  file_id = message.photo[0].file_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question