AIOGram获取所有消息照片



所以,我想通过消息获取所有照片,但我的代码只返回一张照片,而不是所有照片。

现在我有:

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

这会返回消息对象,其中只包含一张照片的信息,而不是所有上传的

其中一种方法是使用Aiogram FSM,并将所有文件id的保存在那里

例如,它可能是这样的:

@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()

相关内容

  • 没有找到相关文章

最新更新