Yura2108
Yura2108

Reputation: 1

AIOGram get all message photos

So, I want to get all photos by the message, but my code returns only one photo, instead of all.

Now I have:

async def __photos(self, message: Message) -> None:
    forwarded_message = message.reply_to_message                   
    print(forwarded_message)

This returns the message object, which contains information about only one photo, instead of all uploaded

Upvotes: 0

Views: 6586

Answers (1)

Maksim K.
Maksim K.

Reputation: 326

One of the ways is to use Aiogram FSM, and save there all files id's

for example it could be like this:

@dp.message_handler(content_types=['photo'])
async def photo_handler(message: types.Message, state:FSMContext):
    # we are here if the first message.content_type == 'photo'
    
    # save the largest photo (message.photo[-1]) in FSM, and start photo_counter
    await state.update_data(photo_0=message.photo[-1], photo_counter=0)

    await state.set_state('next_photo') 


@dp.message_handler(content_types=['photo'], state='next_photo')
async def next_photo_handler(message: types.Message, state:FSMContext):
    # we are here if the second and next messages are photos

    async with state.proxy() as data:
        data['photo_counter'] += 1
        photo_counter = data['photo_counter']
        data[f'photo_{photo_counter}']=message.photo[-1]
    await state.set_state('next_photo')


@dp.message_handler(state='next_photo')
async def not_foto_handler(message: types.Message, state:FSMContext):
    # we are here if the second and next messages are not photos 

    async with state.proxy() as data:

        # here you can do something with data dictionary with all photos            
        print(data)

    await state.finish()

Upvotes: 3

Related Questions