首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AIOGram获取所有信息照片

AIOGram获取所有信息照片
EN

Stack Overflow用户
提问于 2022-03-27 18:43:03
回答 1查看 1.5K关注 0票数 0

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

现在我有:

代码语言:javascript
复制
async def __photos(self, message: Message) -> None:
    forwarded_message = message.reply_to_message                   
    print(forwarded_message)

这将返回message对象,该对象只包含一张照片的信息,而不是所有上传的照片。

EN

回答 1

Stack Overflow用户

发布于 2022-03-31 16:34:36

其中一种方法是使用,并保存所有文件id的

例如,可以是这样的:

代码语言:javascript
复制
@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()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71639290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档