def _handle_valid_input(update, context): current_state_id, _ = _get_current_state_from_context(context) if current_state_id is None or current_state_id == '': return _handle_global_reset(update, context) # Get the correct state machine sm = _get_sm_from_context(context) intent = get_intent(update.message.text) try: msgs, imgs, state_id, state_name, terminal = sm.get_msg_and_next_state( current_state_id, context.user_data['child_gender'], intent) except ValueError: # Ok, something went wrong with the input and a transition. # We already know it is a numeric input as a precodition, so we can # ask for the input again return _handle_wrong_input(update, context) # Ok, valid input and valid transition! msgs, imgs = replace_custom_message(msgs, imgs, context) _save_state_and_process(update, context, msgs, imgs, state_id, state_name) # Last bit, if it's a leaf node, send them back to the main menu # UNLESS we have a nurse state if terminal and not sm.is_nurse_state(state_name): return _handle_global_reset(update, context)
def _handle_global_menu_input(update, context): intent = get_intent(update.message.text) max_module = settings.MAX_MODULE_6 if int( context.user_data['track']) == 6 else settings.MAX_MODULE_12 gm_module = settings.GM_MODULE_6 if int( context.user_data['track']) == 6 else settings.GM_MODULE_12 if intent < Intent.ONE or intent > Intent.TEN: # Input was way off, so escalate up to the nurse msgs, state_id, state_name = _escalate_to_nurse(update, context) return _save_state_and_process(update, context, msgs, [], state_id, state_name) elif intent >= context.user_data['next_module'] and ( context.user_data['cohort'] < 2 and context.user_data['cohort'] >= 0): # They gave a number input, but it was out of bounds, so just re-prompt them return _handle_wrong_input(update, context) elif context.user_data['cohort'] >= 2 and ( intent > max_module or (gm_module >= context.user_data['next_module'] and intent == gm_module)): # They gave a number input, but it was out of bounds, so just re-prompt them return _handle_wrong_input(update, context) # Ok, we have valid input from our current state, so we can process it return _handle_valid_input(update, context)
def process_user_input(update, context): """Handle a user message.""" # Log and fetch user data _, current_state_name = _log_and_fetch_user_data(update, context) # Special case: handle the echo state if current_state_name == 'echo': logger.info(f'[{get_chat_id(update, context)}] - Calling echo') return _handle_echo(update, context) # Reset timers remove_old_timer_add_new(context) # Handle global reset intent = get_intent(update.message.text) # if intent == Intent.GREET or intent == Intent.RESTART: if intent == Intent.GREET: logger.info(f'[{get_chat_id(update, context)}] - Calling global reset') return _handle_global_reset(update, context) # handle global main menu inputs if current_state_name == GLOBAL_MAIN_MENU_STATE: logger.info( f'[{get_chat_id(update, context)}] - Calling global menu input') return _handle_global_menu_input(update, context) # Before we move on, check if we are currently in a state to speak to the nurse sm = _get_sm_from_context(context) if sm.is_nurse_state(current_state_name): # The message should go direct to the nurse msgs, state_id, state_name = _escalate_to_nurse(update, context) return _save_state_and_process(update, context, msgs, [], state_id, state_name) # Handle unknown # TODO: In the future we will change this section to allow non-numeric # input (e.g., yes and no as appropriate ) if intent < Intent.ONE or intent > Intent.TEN: # Non-numeric input at this stage, so escalate up to the nurse. msgs, state_id, state_name = _escalate_to_nurse(update, context) return _save_state_and_process(update, context, msgs, [], state_id, state_name) # Handle valid input (standard case) return _handle_valid_input(update, context)
def _handle_echo(update, context): intent = get_intent(update.message.text) if intent == Intent.UNKNOWN or (int(intent) < 1 or int(intent) > 10): if settings.HINDI: send_text_reply( "मुझे खेद है कि मुझे यह समझ में नहीं आया। यह 1 से 10 के बीच एक भी अंक नहीं है। कृपया 1 से 10 के बीच का कोई एक अंक लिखें।", update) else: send_text_reply( "I am sorry I did not understand that. It is not a single digit number between 1 to 10. Please enter one digit between 1 to 10.", update) else: if settings.HINDI: send_text_reply(f'आपने {str(int(intent))} नंबर दर्ज किया है।', update) else: send_text_reply(f'You have said: {str(int(intent))}', update)
def confirm_name(update, context): _log_msg(update.message.text, 'user', update, state='registration') intent = get_intent(update.message.text) if intent == Intent.YES: return _save_name(update, context, update.message.from_user.first_name) elif intent == Intent.NO: if settings.HINDI: send_text_reply( 'ठीक है, कोई बात नहीं। आप मुझसे खुद को क्या बुलवाना पसंद करेंगी (बस एक पहला नाम ठीक है)?', update, state='registration_1b') else: send_text_reply( 'Ok, no problem. What would you like me to call you (just a first name is fine)?', update, state='registration_1b') return ASK_NAME if settings.HINDI: send_text_reply( f"मुझे वह सही से समझ नहीं आया। कृपया 'हां' या 'नहीं' के साथ जवाब दें। क्या मैं आपको {update.message.from_user.first_name} कह कर आपसे बात कर सकती हूँ?", update, state='registration_1x') else: send_text_reply( f"I didn't quite get that. Please respond with 'yes' or 'no'. Can I call you {update.message.from_user.first_name}?", update, state='registration_1x') return CONFIRM_NAME
def save_child_gender(update, context): _log_msg(update.message.text, 'user', update, state='registration_3.5') intent = get_intent(update.message.text) if intent == Intent.ONE: context.user_data['child_gender'] = 'F' elif intent == Intent.TWO: context.user_data['child_gender'] = 'M' else: if settings.HINDI: send_text_reply( f"मुझे वह सही से समझ नहीं आया। कृपया '1' या '2' के साथ जवाब दें। आपके बच्चे का लिंग क्या है?\n1) लड़की\n2) लड़का", update, state='registration_3.5x') else: send_text_reply( f"I didn't quite get that. Please respond with '1' or '2'. What is the gender of your child?\n1) Girl\n2) Boy?", update, state='registration_3.5x') return ASK_CHILD_GENDER if settings.HINDI: send_text_reply( f'{context.user_data["child_name"]} की जन्म की तारिख कब की है? कृपया उस संख्या को इस प्रारूप में लिखें: YYYY-MM-DD (साल-महीना-महीने का दिन)', update, state='registration_3.5') else: send_text_reply( f'What is the birthday for {context.user_data["child_name"]}? Please enter in this format: YYYY-MM-DD', update, state='registration_3.5') return ASK_CHILD_BIRTHDAY