Y
Y
Yura Mailler2020-10-29 16:46:56
Python
Yura Mailler, 2020-10-29 16:46:56

Can't figure out how machine state works in aiogram?

Hello! Not long ago I started studying aiogram and ran into the problem that I needed to create a registration system. I already wrote the code, but it stops working at one place in the code, I’ll show you exactly where (I’m doing it for the sake of the test, otherwise I’ll use it to find out what the user wrote) I learned about the state machine, but I can’t understand why the code doesn’t work. Vopsche wrote according to the tutorial, but the author, as for me, told everything very bad
code:

class Register (StatesGroup):  #создаю класс где буду хранить данные 
    Q1 = State()
    Q2 = State()

@dp.message_handler(Command('reg'), state = None)
async def Stert_Reg(message: types.Message):
    await message.answer('Введите пароль:')
# код заканчивается здесь
    await Register.Q1.set() #даю понят програме на какой стадии она должна находиться  (1 стадия)

@dp.message_handler(state = Register.Q1) #первая стадия
async def answer_q1(message:types.Message , state: FSMContext):
    answer = message.text #добавляю текст от пользователя в переменную
    await state.update_data(answer1 = answer)#добавляю полученную инфу в класс выше
    await message.answer('Введи логин:') 
    await Register.next()

@dp.message_handler(state = Register.Q2)
async def answer_q2(message:types.Message , state: FSMContext):
    answer20 = message.text #добавляю текст в переменную
    await state.update_data(answer2 = answer20)#добавляю полученную инфу в класс выше
    await message.answer('Спасибо!')
    await state.finish() #конец

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zaharovmail, 2020-11-05
@zaharovmail

Perhaps it will help . How can I immediately add information to the State (aiogram) storage?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question