0

As a result of my "misunderstandings", the show_summary function in the code below is not outputting values correctly. This is what is output in (show_summary):

The following is a summary of the information you provided:
Student Name: (Parent) # Here for some reason the first value (`Parent`) from the first `command_start` function in the code is output/accepted instead of `student_name`, hence the rest of the "sequence" is broken too.
Date of birth: (full name)
Parent's name: (YYYYY-MM-DD)
Parent's phone number: (full name of (parent))
Thank you for enrolling your child. You will receive further instructions.

What's in brackets I entered manually while testing in the Telegram bot (for demonstration purposes)

Message/show_summary should be like this:

Here is the summary of the information you provided:
Student Name: (full name) # It should be like this, i.e. it takes the Full name of the user, not the `Parent` from the first function.
Date of Birth: (YYYYY-MM-DD)
Parent Name: (full name of (parent))
Parent Phone: (number phone (parent))
Thank you for enrolling your child. You will receive further instructions.

Here's full code (with the exception of imports and so on):

class Form(StatesGroup):
    select = State()
    student_name = State()
    student_dob = State()
    parent_name = State()
    parent_phone = State()
    finish = State()

@router.message(CommandStart())
async def command_start(message: Message, state: FSMContext) -> None:
    await state.set_state(Form.select)
    await message.answer(
        "Welcome! We are happy to welcome you to our bot. "
        "By continuing, you automatically agree to the terms of use and privacy policy. "
        "Are you a parent or a student?",
    reply_markup=ReplyKeyboardMarkup(
        keyboard=[
            [
                KeyboardButton(text="Parent"), # Here is `Parent`, which for some reason is output, although it should be omitted in `show_summary`.
                KeyboardButton(text="Student")
            ]
        ],
        resize_keyboard=True,
        ),
    )

# @router.message(Form.select, F.text.casefold() == "student")
# async def student(message: Message, state: FSMContext) -> None:
#    await state.clear()
#    await message.answer(
#        "Not bad not terrible.\nSee you soon.",
#        reply_markup=ReplyKeyboardRemove(),
#    )

@router.message(Form.select, F.text.casefold() == "parent")
async def student_name(message: Message, state: FSMContext) -> None:
    await state.update_data(student_name=message.text) # From now on I want to accept `update_date`, i.e. student name etc. But not `Parent` from the first function.
    await state.set_state(Form.student_dob)
    await message.reply(
        "Great! To enroll your child, please enter the student's full name:",
        reply_markup=ReplyKeyboardRemove(),
    )

@router.message(Form.student_dob)
async def student_dob(message: Message, state: FSMContext) -> None:
    await state.update_data(student_dob=message.text)
    await state.set_state(Form.parent_name)
    await message.reply(
        "Thank you! Please enter the student's date of birth (YYYYY-MM-DD).",
        reply_markup=ReplyKeyboardRemove(),
    )

@router.message(Form.parent_name)
async def parent_name(message: Message, state: FSMContext) -> None:
    await state.update_data(parent_name=message.text)
    await state.set_state(Form.parent_phone)
    await message.reply(
        "Please enter your full name (parent):",
        reply_markup=ReplyKeyboardRemove(),
    )

@router.message(Form.parent_phone)
async def parent_phone(message: Message, state:FSMContext) -> None:
    await state.update_data(parent_phone=message.text)
    await state.set_state(Form.finish)
    await message.reply(
        "Last step - enter your phone number (parent).",
        reply_markup=ReplyKeyboardRemove(),
    )

@router.message(Form.finish)
async def process_register(message: Message, state: FSMContext) -> None:
    data = await state.get_data()
    await state.clear()
    await message.answer(
        "Congratulations! You have successfully enrolled your child.",
        reply_markup=ReplyKeyboardRemove(),
    )
    await show_summary(message=message, data=data)

async def show_summary(message: Message, data: Dict[str, Any], positive: bool = True) -> None:
    student_name = data.get("student_name", "Unknown")
    student_dob = data.get("student_dob", "Unknown")
    parent_name = data.get("parent_name", "Unknown")
    parent_phone = data.get("parent_phone", "Unknown")
    
    summary_text = "Here is the summary of the information you provided:\n"
    summary_text += f"Student Name: {student_name}\n"
    summary_text += f"Date of Birth: {student_dob}\n"
    summary_text += f"Parent Name: {parent_name}\n"
    summary_text += f"Parent Phone: {parent_phone}\n"
    
    if positive:
        summary_text += "Thank you for enrolling your child. You will receive further instructions."
    else:
        summary_text += "We're sorry to hear that you encountered issues during the registration process."
    
    await message.answer(text=summary_text, reply_markup=ReplyKeyboardRemove())
Colorado
  • 1
  • 1
  • I think the main problem I am facing is related to how the data is stored and retrieved from the FSMContext, in my case the retrieval starts from the first function where there is a key with `Parent` data (from the `KeyboardButton`). In the `show_summary` function, I try to retrieve values from the data dictionary using keys such as `"student_name"`, `"student_name"`, etc. However, these keys are not set correctly in `update_data` calls in message handlers, or rather they are set, but starting from the very first function where there is a `Parent` data button. – Colorado Aug 25 '23 at 20:33

0 Answers0