I was writing a Python script to create a telegram bot so one of the function of the bot is collecting images from user, the problem comes here when a user selects multiple image and send them at the same time the bot is not getting there proper file id here is the code :
# Function to handle picture collection
def collect_pictures(message):
user_id = message.chat.id
if message.photo:
# Iterate through each photo in the array
for photo_data in message.photo:
file_id = photo_data.file_id
user_data[user_id]['pictures'].append(file_id)
# Print information about the received picture
print(f"User {message.from_user.username} sent a picture with file_id: {file_id}")
# Total number of pictures received
total_pictures = len(user_data[user_id]['pictures'])
print(f"User {message.from_user.username} has sent {total_pictures} pictures in total.")
# You can proceed with further processing or confirmation here
markup = create_confirm_keyboard()
bot.send_message(message.chat.id, "Do you want to submit the collected information and pictures?", reply_markup=markup)
bot.register_next_step_handler(message, handle_additional_pictures)
else:
bot.send_message(message.chat.id, "Invalid input. Please send pictures.")
bot.register_next_step_handler(message, collect_pictures)
- I tried to get the file_id accordingly and i used for loop but it's not functioning as expected it get the number of uploaded pictures but after iteration and getting the id it stores the first image file_id in all other images file_id
So basically in telegram - combined media is a separate messages that visually grouped. What you can do is use middleware to capture them in one handler.
And then get
data["album"]by addingalbumargument to the handler func:To register middleware before starting bot you can use: