Adding custom responses (Advanced)

This guide explains how to configure webhook callback to have the Digital Person respond appropriately based on the user's input. A simple google survey form is attached below that will ask the user about their current mood and how much sleep they received. The final survey response will be tailored based on how much sleep they received.

https://docs.google.com/forms/d/14eipI8qStegaR-KiSC_EkPLma4fTRuXMEqF89zuIFDE/viewform?edit_requested=true

Sample Webhook

def lambda_handler(event, context): body: dict = json.loads(event.get("body", "{}")) survey_event = SurveyEvent(**body) if survey_event.type == "answer": response = onAnswer(survey_event) elif survey_event.type == "submit": response = onSubmit(survey_event) else: response = SubmitResponse(action="default") return { 'statusCode': 200, 'body': json.dumps(response.__dict__) } def onAnswer(answer_event: SurveyEvent) -> AnswerResponse: if answer_event.last_question_index == 1: how_doing_int = int(answer_event.answers[1].answer) if how_doing_int < 3: return AnswerResponse(say="I'm sorry to hear that", next_question_index=2, action="goto") elif how_doing_int == 3: return AnswerResponse(say="That is too neutral. Please choose something else", next_question_index=1, action="validation") else: return AnswerResponse(say="I'm very happy to hear you're doing well", next_question_index=3, action="goto") else: return SubmitResponse(action='default') def onSubmit(answer_event: SurveyEvent) -> SubmitResponse: name = answer_event.answers[0].answer how_doing_int = int(answer_event.answers[1].answer) num_sleep_hours = int(answer_event.answers[3].answer) if num_sleep_hours == 1: return SubmitResponse(say="One hour of sleep is not sustainable", action="validation", question_indexes=[3]) say = "{0}, thank you for answering our survey. {1}".format( name, "Since you're not feeling well, {}".format("may I suggest at least 7 hours of sleep" if num_sleep_hours < 7 else "and you're getting enough sleep, please see a doctor") if how_doing_int < 4 else "To keep feeling good, {} at least 7 hours of sleep a night".format("strive to get" if num_sleep_hours < 7 else "keep getting")) return SubmitResponse(say=say, action="default")

Configuring responses after each question

AnswerResponse has three fields:

  • say - determines what the Digital Person should say

  • next_question_index - indicates the next question the Digital Person should jump to (zero-based)

  • action - determines which action we want the Digital Person to take. There are currently three supported actions:

    • “goto” - skipping to whichever question indexed

    • “validation” - confirms answer is what we want (i.e.  that’s too neutral, please have a more opinionated response)

    • “default” - follow the normal flow of the survey

 

In the above example, the onAnswer method determines how the user responded to the question “How are you feeling today?”.

  • If the user scores < 3, the survey should jump to “What can we do to make your day better?”.

    • This demonstrates the “goto” action

  • If the user scores 3, the question will repeat because the response was “too neutral”.

    • This demonstrates the “validation” action

  • Any other score, the survey should jump to “How many hours of sleep did you get last night?”

    • This demonstrates the “goto” action

  • Anything else will result in the default flow of the survey

Configuring responses after completion of the survey

SubmitResponse has three fields:

  • say - determines what the Digital Person should say

  • action - determines which action we want the Digital Person to take. There are currently three supported actions:

    • “validation” - confirms all answers in the survey are valid

    • “default” - follow the normal flow of the survey

  • question_indexes - indicates which question is being validated

 

  • In the above example, the onSubmit method returns different responses based on the end results of the survey.

  • The code collects the name provided in Question 1 and uses that to personalize the response

  • If number hours of sleep < 1, the Digital Person requires the user to redo that question

    • This demonstrates the “validation” action

  • Otherwise, we are personalizing what to say when the survey is submitted based on the results