Fulfillment

The fulfillment stage of the model is where we add integration to our own application logic. Activities in this stage might include further validation of the intent and then performing the action to fulfill or carry out the intent. Using our airline example again, when booking a flight, the fulfillment stage might include some of the following tasks:

  • Check that the user is a valid traveler in the CRM system
  • Update the flight booking ERP system
  • Debit the user's credit card
  • Send a confirmation email

Of course, the real-life process is much more complicated than this, but you get the idea. 

We have a couple of options for the implementation of the fulfillment stage. These options are as follows:

  • Return the intent response data to the client application for fulfillment
  • Use a code hook to invoke a Lambda function

When Lex passes control to the Lambda fulfillment function, the function you specify is invoked with an event. This event is a JSON object that contains some contextual details about the conversation.

The following is a stripped-down example event that Lex uses to invoke the fulfillment function:

{
"currentIntent": {
"name": "BookFlight",
"slots": {
"originatingAirport": "Wellington",
"destinationAirport": "Auckland"
},
"confirmationStatus": "None"
},
"bot": {
"name": "AirlineChatbot",
...
},
"userId": "spatterson",
"inputTranscript": "Text extract from the conversation",
"invocationSource": "FulfillmentCodeHook"
}

You can see that it includes an object with the values of the slots. The confirmation status will be set if we have enabled that particular prompt, and additional valid values for that are Denied or Confirmed

Lex also expects a callback with a particular response format so that it can update the conversation to let the user know the outcome of the fulfillment. We can reasonably expect a couple of scenarios from the fulfillment:

  • Maybe all is well; the flight was booked successfully, so we should respond appropriately.
  • There may have been an error during the fulfillment and we need to prompt for more information.

In both cases, we need to assemble a JSON object to respond with. This will tell Lex how to handle the next part of the conversation.

The basic structure of the response object begins as follows:

{
"sessionAttributes": { ... },
"recentIntentSummaryView": [{ ... }],
"dialogAction": {
"type": "ElicitIntent" | "ElicitSlot" | "ConfirmIntent" | "Delegate" | "Close",
...
}
}

The first two keys are optional and help maintain the conversation's context if needed. The third key, dialogAction, is required. This is a specific instruction to respond back to Lex with, and each different type has a specific object structure.

You can check the documentation for the exact structure, but I wanted to at least supply an explanation of each dialog type because it is important. The following are possible options for types of dialogAction:

  • ElicitIntent: This prompts the user to say a phrase or use an utterance that includes an intent. I want to book a flight is not the same as I'm going on holiday, so we need to ask what the user would like to do.
  • ElicitSlot: This tells Lex that there is a slot that we need a value for. With the I want to book a flight to Auckland utterance, the booking system needs a time or a date to make a valid booking, so we have to go back to the user for more information.
  • ConfirmIntent: When you are looking for a yes or no confirmation from the user.
  • Delegate: This is when you delegate the responsibility for what happens next in the conversation back to Lex.
  • Close: This is the closing statement for the conversation and often confirm that the fulfillment was successful. It also tells Lex not to expect another response from the user. An example message would be: Thank you, your flight to Auckland has been booked.

This has given you a high-level introduction to the concepts involved. Next, we're going to build our own chatbot using Lex with Lambda as our fulfillment integration. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.65