alexa / alexa-skills-kit-sdk-for-java

The Alexa Skills Kit SDK for Java helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
http://developer.amazon.com/ask
Apache License 2.0
818 stars 748 forks source link

How to handle dialog COMPLETED, without throwing error #260

Closed harry-levick closed 4 years ago

harry-levick commented 4 years ago

I'm submitting a...

[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[ ] Other... Please describe:

I have a dialog that works correctly, I delegate this dialog to Alexa for the intent BeginDiagnosisIntent which prompts the user to fill the slot values for age and gender, which are slots of BeginDiagnosisIntent. However, when the last slot value is filled and dialogState == "COMPLETED", instead of being caught by the handler for intent BeginDiagnosisIntent and then I perform the behavior that I intended to perform, with the given slot values filled. The response is caught by the ErrorHandler and an error is thrown.

How can I control the dialog such that when dialogState is COMPLETED, I am able to perform the functionality that I wish to perform with the given slots filled?

{
  "interactionModel": {
    "languageModel": {
      "invocationName": "diagnose",
      "intents": [
        {
          "name": "BeginDiagnosisIntent",
          "slots": [
            {
              "name": "age",
              "type": "AMAZON.NUMBER"
            },
            {
              "name": "gender",
              "type": "GENDER"
            }
          ],
          "samples": [
            "{age}",
            "I am {age}",
            "I'm {age}",
            "I am {age} years",
            "I'm {age} years",
            "I am {age} years old",
            "I'm {age} years old",
            "I was born {age} years ago",
            "I am the old age of {age}",
            "I am the ripe old age of {age}",
            "I am {gender}"
          ]
        },
        {
          "name": "AMAZON.RepeatIntent"
        },
        {
          "name": "AMAZON.HelpIntent"
        },
        {
          "name": "AMAZON.StopIntent"
        },
        {
          "name": "AMAZON.CancelIntent"
        }
      ],
      "types": [
        {
          "name": "GENDER",
          "values": [
            {
              "name": {
                "value": "female",
                "synonyms": [
                  "girl",
                  "woman"
                ]
              }
            },
            {
              "name": {
                "value": "male",
                "synonyms": [
                  "boy",
                  "man"
                ]
              }
            }
          ]
        }
      ]
    },
    "dialog": {
      "delegationStrategy": "ALWAYS",
      "intents": [
        {
          "name": "BeginDiagnosisIntent",
          "prompts": {},
          "slots": [
            {
              "name": "age",
              "type": "AMAZON.NUMBER",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.1323207333291.854898796228"
              }
            },
            {
              "name": "gender",
              "type": "GENDER",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.1323207333291.563366041005"
              }
            }
          ]
        }
      ]
    },
    "prompts": [
      {
        "id": "Elicit.Slot.1323207333291.854898796228",
        "variations": [
          {
            "type": "PlainText",
            "value": "How old are you?"
          }
        ]
      },
      {
        "id": "Elicit.Slot.1323207333291.563366041005",
        "variations": [
          {
            "type": "PlainText",
            "value": "Are you male of female?"
          }
        ]
      }
    ]
  }
}

In the launchHandler, I delegate the dialog to BeginDiagnosisIntent:

public class LaunchHandler implements LaunchRequestHandler {

    @Override
    public boolean canHandle(HandlerInput handlerInput, LaunchRequest launchRequest) {
        return true;
    }

    @Override
    public Optional<Response> handle(HandlerInput handlerInput, LaunchRequest launchRequest) {
        Intent chainedIntent = Intent.builder().withName("BeginDiagnosisIntent").build();
        return handlerInput.getResponseBuilder()
                .addDelegateDirective(chainedIntent)
                .build();
    }
}

I then wish for alexa to handle this dialog, and then when the slots are filled, I can continue from BeginDiagnosisIntentHandler:

public class BeginDiagnosisIntentHandler implements IntentRequestHandler {

    @Override
    public boolean canHandle(HandlerInput input, IntentRequest intentRequest) {
        return intentRequest.getIntent().getName().equals("BeginDiagnosisIntent");
    }

    @Override
    public Optional<Response> handle(HandlerInput handlerInput, IntentRequest intentRequest) {
        Intent currentIntent = intentRequest.getIntent();

        if (intentRequest.getDialogState().equals("COMPLETED")) {
            return handlerInput.getResponseBuilder()
                    .withSpeech("I have all the information I need.")
                    .build();
        } else {
                return handlerInput.getResponseBuilder()
                        .addDelegateDirective(currentIntent)
                        .build();
        }

    }

}

However, when the dialogState is COMPLETED. My error handler catches the response. Why does this happen? How can I regain the context after the dialog is completed?

Your Environment

Java Info