C
C
Cesoneemzzz2020-07-28 23:50:18
Python
Cesoneemzzz, 2020-07-28 23:50:18

How can I force the next handler to execute immediately after the other AIOgram has executed?

I want to loop the execution of a certain handler until the user himself exits this loop by pressing a button.

Here is the code for the first handler:

@dp.message_handler(state=Testing.easy_difficult_selected)
@dp.message_handler(lambda msg: msg == 'легкий')
async def testing_easy_difficult(message: types.Message, state: FSMContext):
    import random
    data = await state.get_data()
    i = 0
    answers = {}

    kbAnswersChoose = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True, row_width=2)
    category = data.get("category")
    words = await db.async_testing_easy_difficult()
    for word in words:
        answers[i] = word[0][1]
        i += 1
    for button_name, word in answers.items():
        button_name = KeyboardButton(word)
        kbAnswersChoose.insert(button_name)
    btnGoBack = KeyboardButton("Вернуться в главное меню")
    kbAnswersChoose.insert(btnGoBack)

    word_and_translate = random.choice(words[random.randint(0, 4)])
    word_originally = word_and_translate[0]
    word_translate = word_and_translate[1]

    await state.update_data(word_translate=word_translate)

    await message.answer(f"Дано слово: {word_originally}\n\n Каков его перевод?",
                         reply_markup=kbAnswersChoose)

    await Testing.waiting_for_choose_right_answer.set()


Here is the code of the handler that control passes to after the user has sent the word:
@dp.message_handler(state=Testing.waiting_for_choose_right_answer)
async def testing_answer(message: types.Message, state: FSMContext):
    await state.update_data(answer=message.text)

    data = await state.get_data()

    if data.get('answer') == data.get('word_translate'):
        await message.answer(f"Верно! Это слово переводится, как {data.get('answer')}!")
    else:
        await message.answer(f"К сожалению, ты ошибься. Это слово переводится, как {data.get('word_translate')}. Будь "
                             f"внимательнее в следующий раз!")

    await Testing.easy_difficult_selected.set()


Here I need the execution of the last handler to immediately start executing the previous one; without sending a message by the user. Now it starts working only if the user has entered any message.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
9
96Semka, 2021-03-10
@96Semka

just call that handler like a function

S
Sergey, 2021-08-05
@serdjo2011

async def test(message: types.Message, state: FSMContext):  # Это хендлер
    await test_fun()  # Обычная функция
    await test_fun1(message, state)  # Функция с хендлера (с аргументами message, state)


async def test_fun():
    print("hello, world")


async def test_fun1(message: types.Message, state: FSMContext):
    print("word")
    await message.answer("44")
    date = await state.get_data()  # извлекаем данные с машины состояний
    print(str(date))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question