P
P
pozner882021-10-14 16:35:31
Bots
pozner88, 2021-10-14 16:35:31

How to accept an album of photos by a telegram bot?

It is necessary that the bot accept and display the file_id of several photos at once. I can accept and process one photo, but if this is an album, then it doesn’t work. If anyone knows how to implement this I would be grateful. I work on aiogram, if there are examples for other libraries, then I will also be happy, I think I can redo

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
InternetMaster, 2021-10-14
@pozner88

There is such a thing when sending a photo - compression. So, if the user chose compression before sending, then it will be a photo (photo), and if compression is not selected, then this is already a document (document)

@bot.message_handler(content_types=['photo'])
def handle_docs_photo(message):
    try:
        chat_id = message.chat.id
        file_info_1 = bot.get_file(message.photo[-1].file_id)
        bot.send_message(message.chat.id, file_info_1)
        downloaded_file = bot.download_file(file_info_1.file_path)
        
        src = dir + '\\' + file_info_1.file_path.split('/')[-1]
        bot.send_message(message.chat.id, src)
        with open(src, 'wb') as new_file:
            new_file.write(downloaded_file)

    except Exception as e:
        bot.reply_to(message, e)

At the beginning of the code, in the handler, replace photo with document or just add it separated by commas.
This is an implementation of the PyTelegramBotAPI code, it is not difficult to convert it to aiogram, since all methods are described in the official Telegram API section (I attach the link below).
Documentation: https://core.telegram.org/bots/api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question