A
A
Angelex2020-07-20 17:51:36
Python
Angelex, 2020-07-20 17:51:36

How to immediately add information to the State (aiogram) storage?

Hey, hey! The question is for those who have worked with the aiogram library , namely with the State (Finite state machine) class. Do you know how immediately, after the set( <>.set() ), to enter information into the storage? ( update_data() )

I know how to update the store and return the data from there after the message sent by the user, but I do not know how to do it before sending, right away, to use in the following answers. Thank you for your attention <3

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
i_ikigai, 2020-07-23
@angelex

class reg(StatesGroup):
    name = State()
    fname = State()
    age = State()


@dp.message_handler(commands="reg", state="*")
async def name_step(message: types.Message, state: FSMContext):
    await message.answer(text='Напиши имя ')
    await reg.name.set()


@dp.message_handler(state=reg.name, content_types=types.ContentTypes.TEXT)
async def fname_step(message: types.Message, state: FSMContext):
    if any(map(str.isdigit, message.text)):
        await message.reply("Пожалуйста напишите свое имя")
        return
    await state.update_data(name_user=message.text.title())
    await message.answer(text='Напиши фамилию ')
    await reg.fname.set()


@dp.message_handler(state=reg.fname, content_types=types.ContentTypes.TEXT)
async def age_step(message: types.Message, state: FSMContext):
    if any(map(str.isdigit, message.text)):
        await message.reply("Пожалуйста напишите свою фамилию")
        return
    await message.answer(text='Напиши возраст ')
    await state.update_data(fname_user=message.text.title())
    await reg.age.set()


@dp.message_handler(state=reg.age, content_types=types.ContentTypes.TEXT)
async def res_step(message: types.Message, state: FSMContext):
    if not any(map(str.isdigit, message.text)):
        await message.reply("Пожалуйста введите свой возраст")
        return
    await state.update_data(age_user=message.text.lower())
    user_data = await state.get_data()
    await state.finish()

in my code, user_data has 3 user posts written to a dictionary. From there, it's easy to get them through the call f'{user_data[name_user}} '
for example and it's easier to understand

S
Stefan, 2021-02-26
@MEDIOFF

Immediately after <>.set() you write:
state = Dispatcher.get_current().current_state()
And this will be your state, which you can then refer to as usual
state.update_data(key=value)

E
enabl3, 2021-09-03
@enabl3

And you can somehow cancel the steps, for example, if you add a cancel button

await message.answer(text='Напиши имя ', reply_markup=keyboards.cancelKeyboard)

where in the keyboards
cancel = InlineKeyboardButton(text="Отмена", callback_data="cancel")

cancelKeyboard = InlineKeyboardMarkup()
cancelKeyboard.add(cancel)

and in callbacks
if call.data == "cancel":
        await call.message.edit_text(text="Отмена")

now the message with the name displays a button to cancel, but when you click on it, nothing happens

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question