pythonstate-machinefsmaiogram

FSM - aiogram | How to get file?


@dp.message_handler(text_contains='Send file')
async def start_bots(msg):
    await bot.send_message(msg.from_user.id, 'Ok, send me a file')
    await HZ.getFile.set()



@dp.message_handler(state=HZ.getFile)
async def getID(msg: types.Message, state: FSMContext):
    file_id = msg.document.file_id
    file = await bot.get_file(file_id)
    file_path = file.file_path
    print(file_path)
    await state.finish()

It doesn't cares my file, how to download file when user sent message like Send file?


Solution

  • @dp.message_handler(state=HZ.getFile, content_types=['document'])
    async def getID(msg: types.Message, state: FSMContext):
        file_id = msg.document.file_id
        file = await bot.get_file(file_id)
        file_path = file.file_path
        print(file_path)
        await state.finish()
    

    Таким образом aiogram будет понимать, что при вызове состояния нужно ожидать документ.

    Все при помощи: content_types=['document']