N
N
Nickie0002021-10-06 19:12:51
Python
Nickie000, 2021-10-06 19:12:51

How to implement user data entry in telegram bot in Python?

In general, I just started learning Python. I have long wanted to implement a telegram bot for placing orders. As a result, I ran into a problem: by pressing the "Place an order" button, the bot sends a message and requests personal data starting with full name (let's dwell on this for now). It is necessary that the bot understands that the name is written in the next message.
I'm trying to make the user write down their data starting with the full name and if the bot sees the full name in the message, then it saves the data in the FSM storage
I use aiogram and Telethon
Here is the source (do not judge strictly)

class CheckOut(StatesGroup):
    waiting_buyer_name = State()

@dp.message_handler(Text(equals=["Оформить заказ", "/checkout"]))
async def checkout(message: types.Message):
    
    await message.answer("Я начинаю собирать ваш заказ\nДля этого мне нужны Ваши данные\nЗапишите ваши ФИО <b>(начинайте сообщение с ФИО)</b>", reply_markup=types.ReplyKeyboardRemove())
    await CheckOut.waiting_buyer_name.set()

async def checkout_choosen_name(message: types.Message, state: FSMContext):
    if "фио" not in message.text.lower():
        await message.answer("Скорее всего вы ввели Ваши данные в неправильном формате.\n Пожалуйста, Запишите ваши ФИО <b>(начинайте сообщение с ФИО)</b>")
        return
    await state.update_data(buyer_name_writen=message.text.lower())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nickie000, 2021-10-08
@nickie000

I found the solution in the official documentation of aiogram. Everything seems to be clear there =)

V
Vindicar, 2021-10-06
@Vindicar

If you need to implement a scenario in which the user sends multiple messages, deal with register_next_step_handler()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question