-1

I have this func:


@dp.message_handler(state=StateInfo.user_answer)
async def reset_bot(message: types.Message, state: FSMContext):
    await state.reset_state(True)
    if message.text == 'Send new task':
        await start_uma(message)
    else:
        await bot.send_message(text="Error!", chat_id=message.chat.id)
        await reset_bot(message, state)

But after running this year I get endless error messages I've tried using the get_updetes and loops but nothing seems to work. Please tell me what I did wrong

1 Answers1

0

Your recursive function is wrong, as you are calling the reset_bot with the same wrong message in the else block and it goes into an infinite loop.

Code:

def validate_text(text):
    if text == 'Send new task':
        return True
    else:
        return False


# Check for valid text
@dp.message_handler(lambda message: not validate_text(message.text), state=state=StateInfo.user_answer)
async def process_text_invalid(message: types.Message):
    """
    If text is invalid
    """
    return await message.reply("Text gotta be a wrong.\nRetry again with valid text.")    


@dp.message_handler(lambda message: validate_text(message.text), state=state=StateInfo.user_answer)
async def reset_bot(message: types.Message, state: FSMContext):
"""
If text is valid
"""
    await state.reset_state(True)
    await start_uma(message)

    # or finish conversation
    # await state.finish()

I took reference from an official example from doc.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54