[docs]classWaitingCategorizationState(BaseState):"""State where the user is expected to categorize their response by selecting an option from a list."""ERROR_NUMBER_RESPONSE="Sorry, I need the number corresponding to the option. Can you please provide the number?"
[docs]defgenerate_response(self,user_input,conversation_id,assessment_id):# Get the current question that was askedquestion_json=self.context._get_question_json(assessment_id)try:selected_index=int(user_input.strip())-1if0<=selected_index<len(question_json['options']):# Get the current assessmentassessment=self.context.assess_repo.get_assessment(assessment_id)# Save question resultself.context.assess_question_repo.save_assessment_question(assessment_id=assessment_id,question_number=assessment.current_question,question_text=question_json['question'],user_response=assessment.last_free_text,categorized_value=selected_index)# Check if assessment is completetotal_questions=len(self.context.questionnaires[assessment.assessment_type]['questions'])ifassessment.current_question+1>total_questions:# All questions answered# Changing state and triggering finalizationfromkusibot.chatbot.assesment_states.finalizing_stateimportFinalizingStateself.context._transition_to_next_state(FinalizingState())returnself.context.state.generate_response(user_input,conversation_id,assessment_id)else:# More questions to answer# Changing statefromkusibot.chatbot.assesment_states.asking_question_stateimportAskingQuestionStateself.context._transition_to_next_state(AskingQuestionState())# Updating assesment in DBself.context.assess_repo.update_assessment(assessment_id,current_state=self.context.STATE_ASKING,current_question=assessment.current_question+1,last_free_text=None)# Return next questionreturnself.context.state.generate_response(user_input,conversation_id,assessment_id)else:# Selected index is out of rangereturnself.ERROR_NUMBER_RESPONSEexceptValueError:# Input not a numberreturnself.ERROR_NUMBER_RESPONSE