V
V
violpeople2021-07-24 22:04:20
Python
violpeople, 2021-07-24 22:04:20

How to use the data in the state that I received?

Hello everyone, the bot is on aiogram and here is my question that I asked above. Why do I need this - I created a small client registration - First name, last name, phone number, etc., which are added to the database:

@dp.message_handler(state=reg.login, content_types=types.ContentTypes.TEXT)
async def reg_password(message:types.Message,state:FSMContext):
    if any(map(str.isdigit, message.text)):
        await message.reply("Повторите попытку")
        return
    await state.update_data(name_user = message.text)
    await message.answer("Отлично, будем знакомы!\n\nСледующий шаг - введите место, откуда вы(куда везти товар):")
    await reg.place.set()


@dp.callback_query_handler(text = 'yes',state = reg.last)
async def yes_message(call:types.CallbackQuery, state:FSMContext):
    await call.message.reply('Добро пожаловать в магазин "Мир Цветов".\nНажмите /help для дальнейших действий')
    
    user_data = await state.get_data()
    name = user_data["name_user"]
    place = user_data["place_user"]
    phone = user_data["phone_user"]
    print(name, place, phone)
    
    user = [name, place, phone]
    cur = con.cursor()
    cur.executemany('INSERT INTO users (user_name, user_place, user_phone) VALUES (?, ?, ?)',(user,))

    con.commit()
    await state.finish()

Next, I have a function to add to the same database the product that the client ordered:
@dp.callback_query_handler(lambda c: c.data.startswith('add_'),state = '*")
async def add_to_cart(call: types.CallbackQuery, state:FSMContext):
    operation = call.data.split('_')[0]
    current_item = int(call.data.split('_')[-1])
    item = items[current_item]
    prices = price[current_item]

    user_data = await state.get_data()
    name = user_data["name_user"]
    print(f"Имя - {user_data['name_user']}")
    
    cur = con.cursor()
    cur.execute(' INSERT INTO users(user_item) VALUES (item) WHERE user_name = name ')

And then I don’t understand - I need to link this product to a specific person who has just registered, and when I try to pull his name from the State's in this way, so that later I can sew these products to this person in the database - an error

print(f"Name - {user_data['name_user']}")
KeyError: 'name_user'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question