Adding Dialog to an Alexa Skill

1 hour
  • 5 Learning Objectives

About this Hands-on Lab

In this lab, you will change the skill to use dialog directives and entity resolution to make the skill a more natural-flowing conversation with the user. Objectives for this lab include creating a dialog directive that asks the user for the information needed for slots, using entity resolution to resolve the breed of the pet to pet type, and testing the slots using `ask simulate`. **Note:** You must have your own Amazon Developer account, which you can [sign up](https://developer.amazon.com/) for if you do not already have one.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Configure Amazon Skills Kit to Use Your AWS Developer Account
  1. Initialize the ASK CLI:

    ask init --no-browser
  2. Copy/paste the URL that appears from the terminal to a browser window.

  3. Log in to the developer console when prompted.

  4. Copy/paste the authorization code.

  5. Enter y for Yes to connect to the AWS account already set up on the VM.

  6. Choose the default AWS account.

Clone and Check Out Branch
  1. Use the following to clone the template skill:

    ask new --url https://github.com/linuxacademy/content-aws-skill-builder.git
  2. Navigate into the directory with:

    cd content-aws-skill-builder
  3. To start with a template and perform the tasks for this lab yourself:

    git checkout lab_dialog
  4. To start with the solution to the lab:

    git checkout lab_dialog_solution
Develop the Skill

In ../models/en-US.json:

  1. Add samples to the RegisterPetIntent slots, and update the intent samples to include those same samples:

       ...},
       {
        "name": "RegisterPetIntent",
        "slots": [
          {
            "name": "pet_type",
            "type": "petType",
            "samples": [
              "i have a {pet_type}"
            ]
          },
          {
            "name": "pet_name",
            "type": "petName",
            "samples": [
              "his name is {pet_name}",
              "her name is {pet_name}"
            ]
          }
        ],
        "samples": [
          "i want to register my pet",
          "i have a {pet_type}",
          "his name is {pet_name}",
          "her name is {pet_name}"
        ]
      },
    ...
  2. Update the "types" to include "synonyms":

      ...,
        "types": [
          {
            "name": "petType",
            "values": [
              {
                "name": {
                  "value": "dog",
                  "synonyms": [
                    "beagle",
                    "greyhound"
                  ]
                }
              },
              {
                "name": {
                  "value": "cat",
                  "synonyms": [
                    "ragdoll",
                    "siamese"
                  ]
                }
              }
            ]
          },
          {
            "name": "petName",
            "values": [
                {
                  "name": {
                    "value": "pet_name"
                   }
                }
            ]
          }
        ]
      ...,
  3. Add the dialog directive at the same level as the languageModel, and create a directive for the RegisterPetIntent and create a elicitation prompt for each slot:

      ...},
        "dialog": {
          "intents": [
            {
            "name": "RegisterPetIntent",
            "confirmationRequired": false,
            "prompts": {},
            "slots": [
                {
                  "name": "pet_type",
                  "type": "petType",
                  "elicitationRequired": true,
                  "confirmationRequired": false,
                  "prompts": {
                      "elicitation": "Elicit.Intent-RegisterPetIntent.IntentSlot-pet_type"
                  }
                },
                {
                  "name": "pet_name",
                  "type": "petName",
                  "elicitationRequired": true,
                  "confirmationRequired": false,
                  "prompts": {
                      "elicitation": "Elicit.Intent-RegisterPetIntent.IntentSlot-pet_name"
                  }
                }
              ]
            }
          ]
        },
      ...
  4. Add prompts at the same level as dialog, and create the actual prompt for Alexa to speak:

      ...},
        "prompts": [
          {
            "id": "Elicit.Intent-RegisterPetIntent.IntentSlot-pet_type",
            "variations": [
              {
                  "type": "PlainText",
                  "value": "We are limited to beagles, greyhounds, ragdolls, and siamese breeds. Which of these breeds do you want to register?"
              }
            ]
          },
          {
            "id": "Elicit.Intent-RegisterPetIntent.IntentSlot-pet_name",
            "variations": [
              {
                  "type": "PlainText",
                  "value": "We love those. One of our favorite breeds. What is your pet's name?"
              }
            ]
          }
        ]
      ...

In ../lambda/custom/index.js:

  1. Create an InProgessRegisterPetIntentHandler:

    const InProgressRegisterPetIntentHandler = {
        canHandle(handlerInput) {
          const request = handlerInput.requestEnvelope.request;
    
          return request.type === 'IntentRequest'
            && request.intent.name === 'RegisterPetIntent'
            && request.dialogState !== 'COMPLETED';
        },
        handle(handlerInput) {
          const currentIntent = handlerInput.requestEnvelope.request.intent;
    
          return handlerInput.responseBuilder
            .addDelegateDirective(currentIntent)
            .getResponse();
        }
    };
  2. Create a CompletedRegisterPetIntentHandler:

    const CompletedRegisterPetIntentHandler = {
        canHandle(handlerInput) {
            const request = handlerInput.requestEnvelope.request;
    
          return request.type === 'IntentRequest'
            && request.intent.name === 'RegisterPetIntent'
            && request.dialogState === 'COMPLETED';
        },
        handle(handlerInput) {
            pet_type = handlerInput.requestEnvelope.request.intent.slots.pet_type.resolutions.resolutionsPerAuthority[0].values[0].value.name
            pet_name = handlerInput.requestEnvelope.request.intent.slots.pet_name.value
            pet_breed = handlerInput.requestEnvelope.request.intent.slots.pet_type.value
            const speakOutput = "We are happy to welcome your " + pet_breed + '. Your ' + pet_type + ' named ' + pet_name + " is registered!";
    
            return handlerInput.responseBuilder
                .speak(speakOutput)
                //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
                .getResponse();
        }
    };
  3. Update exports.handler:

    exports.handler = Alexa.SkillBuilders.custom()
        .addRequestHandlers(
            LaunchRequestHandler,
            ExclusiveVetIntentHandler,
            // RegisterPetIntentHandler,
            InProgessRegisterPetIntentHandler,
            CompletedRegisterPetIntentHandler,
            HelpIntentHandler,
            CancelAndStopIntentHandler,
            FallbackIntentHandler,
            SessionEndedRequestHandler)
        .addErrorHandlers(
            ErrorHandler)
        .lambda();
Deploy and Test
  1. Deploy the skill:

    ask deploy 
  2. Begin performing a test:

    ask simulate --locale en-US --text 'open exclusive vet'
  3. Look for the response in the JSON and verify it is Exclusive Veterinary Services welcomes you. You can say I want to register my pet.".

  4. Enter:

    ask simulate --locale en-US --text 'I want to register my pet'
  5. Look for "Dialog.Delegate" as the value for "type".

  6. Enter:

    ask simulate --locale en-US --text 'I have a ragdoll'
  7. Look at the slots to see that "ragdoll" is the value for "pet_type", and then check that entity resolution worked by scrolling up to the requests and reviewing the "resolutions" under "pet_type" in the "request" section of the JSON object.

  8. Continue in this fashion with the animal name and checking the slots and the final response.

Clean Up Amazon Developer Account Alexa Console

At this point, the skill you just created should be deleted from your Alexa Developer Console.

Please do so at this link: Alexa Developer Console.

Additional Resources

After your last demonstration, the Exclusive Veterinary Services staff thinks you can do anything with Alexa.

They currently only work with two dog breeds — beagle and greyhound — and with two cat breeds — ragdoll and siamese. So they would like pet owners to be able to register their pet using the breed they work with and then the name of the pet. Remember: They are Exclusive Veterinary Services.

The registration intent should ask for pet breed and then the pet's name. Entity resolution should be used to return the pet type.

If you have not created an Amazon Developer account yet, please do so before proceeding: Amazon Developer account creation.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!