J
J
James_Obry2021-01-01 00:22:57
Python
James_Obry, 2021-01-01 00:22:57

How to understand what state or group of states the telegram bot is in?

I am writing a bot in telegrams and there was a need to somehow find out what state the bot is in when the user clicks on the inline button, please tell me how this can be done?

class registration(StatesGroup):
    waiting_for_name = State()
    waiting_for_age = State()
    waiting_for_sex = State()
    waiting_for_city = State()
    waiting_for_interest = State()
    waiting_for_voice = State()

If the user is in one of these states, the bot should throw an error.

I write the bot in the latest version of python and the aiogram library.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
James_Obry, 2021-01-02
@James_Obry

class registration(StatesGroup):
    waiting_for_name = State()
    waiting_for_age = State()
    waiting_for_sex = State()
    waiting_for_city = State()
    waiting_for_interest = State()
    waiting_for_voice = State()

async def check_state(message: types.Message, state: FSMContext):
    current_state = await state.get_state()  # текущее машинное состояние пользователя
    if current_state in registration: # registration - название класса состояний
        print('Пользователь в одном из состояний регистрации')
    if current_state == 'registration:waiting_for_name':
        print('Пользователь находиться в конкретном состоянии - waiting_for_name из класса registration')

A
ambal245, 2021-01-01
@ambal245

As an output to write down in a DB value. After clicking on the button, the user checks the value and sends the desired message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question