Configuring the chatbot to ask the questions and use response bots - QnABot on AWS

Configuring the chatbot to ask the questions and use response bots

You can configure your chatbot to ask questions and process your end user’s answers. Use this feature for data collection and validation; to implement surveys, quizzes, personalized recommendations; or triage chatbot applications.

Use the following procedure to configure the chatbot to ask questions.

  1. Log in to the content designer and choose Add.

  2. Enter ID: ElicitResponse.001

  3. Enter question: Ask my name

  4. Enter answer: Hello. Can you give me your First Name and Last Name please?

  5. Choose Advanced.

    • Enter Elicit Response: ResponseBot Hook:QNAName

    • Enter Elicit Response: Response Session Attribute Namespace:name_of_user

  6. Choose CREATE to save the new item.

  7. Use the web UI to say: “Ask my name

  8. Respond by entering your name. Try responding naturally and see if chatbot confirms your name correctly. If not, you can choose NO and try again.

    The ResponseBot Hook field specifies the name of an Amazon Lex chatbot. In this case we specified the name of a chatbot, QNAName, that was automatically created for us when the solution was installed. QNAName is a built-in response chatbot designed to process names (first and last name). It handles a variety of ways the user might state their name, and it will prompt the user to confirm or to try again. If the user confirms by choosing YES, the response chatbot returna the values for the FirstName and LastName elements back to the solution as slot values in a fulfilled response.

    The solution stores the returned values for FirstName and LastName in a session attribute. The name of the session attribute is determined by the value you provided for Response Session Attribute Namespace (in this case name_of_user) and the slot name(s) returned by the response chatbot (in this case FirstName and LastName).

    The session attribute set by Elicit Response can be used in other items to provide conditional or personalized responses.

  9. Log in to the content designer, and choose Add.

  10. Enter ID: ElicitResponse.002

  11. Enter question: Ask my age

  12. Enter answer: Hello {{SessionAttributes.name_of_user.FirstName}} – What is your age in years?

  13. Choose Advanced.

    • Enter Elicit Response: ResponseBot Hook: QNAAge

    • Enter Elicit Response: Response Session Attribute Namespace: age_of_user

  14. Choose CREATE to save the new item.

  15. Use the Web UI to say: “Ask my age”

Response bots

The solution provides a set of built-in response bots that you can use out of the box:

  • QNAYesNo: returns slot {Yes_No} with value either “Yes” or “No”

  • QNAYesNoExit: returns slot {Yes_No_Exit} with value either “Yes”, “No”, or “Exit”

  • QNADate: returns slot {Date} with value of "<date (YYYY-MM-DD)>"

  • QNADayOfWeek: returns slot {DayOfWeek}

  • QNAMonth: returns slot {Month}

  • QNANumber: returns slot {Number}

  • QNAAge: returns slot {Age}

  • QNAPhoneNumber: returns slot {PhoneNumber}

  • QNATime: returns slot {Time} with value of time (hh:mm)

  • QNAEmailAddress: returns slot {EmailAddress}

  • QNAName: returns slots {FirstName} and {LastName}

  • QNAFreeText: returns slots {FreeText} and {Sentiment}.

You can also add your own Amazon Lex bots and use them as response bots. Response chatbot names must start with the letters “QNA”. The solution calls your chatbot with the user’s response, and captures all the slot names and values returned when your chatbot sends back a fulfilled message.

Advancing and branching through a series of questions

The following example configures the solution to automatically ask your age after you provide your name.

  1. Log in to the content designer and edit item ElicitResponse.00.1

  2. Choose Advanced.

  3. Enter Document Chaining: Chaining Rule: 'ask my age'

  4. Choose UPDATE to save the modified item.

  5. Use the web UI to ask: “Ask my name” Enter and confirm your name.

    • Enter and confirm your name.

    • Enter and confirm your age.

The solution automatically asks you for your age after you confirm your name. Because you specified the next question, ‘ask my age’, as the chaining rule, the solution automatically found and advanced to the matching item.

Next, create a conditional chaining rule that will branch to different items depending on previous answers.

  1. Log in to the content designer and add two new items:

    • ID: ElicitResponse.003, question: “Under 18″, answer: “Under 18 answer“.

    • ID: ElicitResponse.004, question: “Over 18“, answer: “Over 18 answer“.

  2. Edit item ElicitResponse.002

    • Add Chaining Rule: (SessionAttributes.age_of_user.Age< 18) ? "Under 18" : "Over 18" 

    • Choose UPDATE to save the modified item.

  3. Use the web UI to ask: “Ask my name”.

    • Enter and confirm your name.

    • Enter and confirm your age.

When you confirm your age, the solution automatically branches to one of the two new items you added, depending on your age. The chaining rule is a JavaScript programming expression used to test the value of the session attribute set by elicit response; if it is less than 18 then advance to the item matching the question ‘Under 18′, otherwise advance to the item matching the question ‘Over 18′.

Combine expressions with logical operators to test multiple session attributes in a single rule, and use nested expressions to implement more than two branches in a chaining rule. Use the alternate syntax SessionAttributes('age_of_user.Age') to avoid a processing error if the referenced session attribute does not exist.

You can also apply chaining rule expressions to all the context variables supported by the handlebars feature including UserInfo fields, Settings fields, and more. For more information, see the README.md file in GitHub for a list of available variables.

Identify the next document using its QID value instead of a question using a string that starts with ‘QID::‘ followed by the QID value of the document, for example, a rule that evaluates to “QID::Admin001” will chain to item Admin.001.

You can optionally specify an AWS Lambda function instead of a JavaScript expression when you need to evaluate complex chaining rule logic. Your Lambda function is invoked with the full user request context and should evaluate and return the next question as a simple string. Alternatively, the Lambda function may return an event object where the event.req.question key was updated to specify the next question—by returning an event object, your chaining rule Lambda function can modify session attributes, similar to Lambda Hooks. Use Lambda functions to implement chaining rules that require complex logic such as a data lookup. A chaining rule Lambda function name must start with the letters “QNA”, and is specified in the Document Chaining:Chaining Rule field as >Lambda::FunctionNameOrARN.

Note

If the chaining rule has an error, the solution will return the message, “Unfortunately I encountered an error when searching for your answer. Please ask me again later.”

Configuration of bot routing

Configuration of Bot Routing is simple. Each question in QnAbot contains an optional section which allows configuration of a BotRouter.

Note

This is optional. Please leave empty and QnABot will not act as a BotRouter for the question being edited.

Bot routing

Bot routing

The example image shows an integration we've developed which communicates with the Nutritionix Bot.

  • Bot Name or Lambda function- You can configure and existing Lex Bot or configure a specialty BotRouter implemented via a Lambda function.

  • Simple name - A short string that we expect web User Interfaces to use as a breadcrumb to identify where in an enterprise the user is interacting.

  • Lex Alias - If a target bot is configured, the alias used to communicate with the target bot.

Note

When integrating with other Lex Bots or Lambdas, the permission to communicate with the target Lex bot or with a new BotRouter (Lambda) need to be added to the QnABot's fulfillment lambda role.

Bot routing

Bots come in many shapes and sizes, and exist to perform a variety of automation tasks. Usually they take input from a human and respond performing some task. Bots might ask for additional input, verify the input, and respond with completion. Bots might be implemented using Amazon Lex or might be implemented using other toolsets. A great example is the nutritionix botwhere you can tell the bot what you've had for breakfast and it will respond with nutrition information.

The solution adopted by QnABot is to coordinate (route) bot requests through a Supervisory Bot, to the appropriate Bot based on questions or tasks.

Content designers associate questions or tasks (qid's) that identify a BotRouter to target for the question. This is performed using the QnABot UI Designer. Once configured, if a user asks a question or directs the bot with some instruction, QnABot responds with an answer and sets up a channel to communicate with the specialty Bot. From that point, messages or responses from the user are delivered to the Specialty Bot. Specialty Bots respond to actions and QnABot delivers the answers.

This flow continues until one of these events occurs:

  1. The user user cancels the conversation with the specialty Bot by uttering "exit", "quit", "bye", or a configurable phrase defined in the settings configuration of QnABot.

  2. The specialty BotRouter (custom code) responds with a message indicating the conversation should be discontinued. (QNABOT_END_ROUTING) .

  3. The specialty Bot is a LexBot (non QnABot) that indicates fulfillment is complete.

  4. If the target Bot is another QnABot, session attributes can be set by the specialty QnABot set indicating the conversation should be discontinued. (QNABOT_END_ROUTING)

Specialty Bots can be developed for specific parts of an organization like IT, or Finance, or Project Management, or Documentation. A supervisory Bot at an enterprise level can direct users to answers from any of their Bots.

Message Protocol for a new Bot Router implemented in Lambda

The input json payload to the target Lambda will be the following:

req: { request: "message", inputText: <String>, sessionAttributes: <Object>), userId: <String> }

The expected response payload from the target lambda is the following:

{ response: "message", status: "success", "failed" message: <String>, messageFormat: "PlainText", "CustomPayload", "SSML", "Composite" sessionAttributes: Object, sessionAttributes.appContext.altMessages.ssml: <String>, sessionAttributes.appContext.altMessages.markdown: <String>, sessionAttributes.QNABOT_END_ROUTING: <AnyValue> responseCard: <standard Lex Response Card Object> }

Sample bot router

The Nutrionix nodejs based sample BotRouter is provided in the github repo as a zip file at sample bot router . To use this sample you'll need to provision an API account with Nutritionix and configure the source to use your own x-app-id and x-app-key from Nutritionix.

'x-app-id': process.env.xAppId, 'x-app-key': process.env.xAppKey

Next, you will need to build and deploy the code into Lambda using your favorite techniques and grant permission within the QnABot Fulfillment Lambda Role using IAM to invoke this Lambda function.

Hint: If you name the Lambda function starting with 'qna', QnABot is already configured with permissions to invoke this Lambda.