Answer the question
In order to leave comments, you need to log in
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
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()
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)
And you can somehow cancel the steps, for example, if you add a cancel button
await message.answer(text='Напиши имя ', reply_markup=keyboards.cancelKeyboard)
cancel = InlineKeyboardButton(text="Отмена", callback_data="cancel")
cancelKeyboard = InlineKeyboardMarkup()
cancelKeyboard.add(cancel)
if call.data == "cancel":
await call.message.edit_text(text="Отмена")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question