Search code examples
javaamazon-web-servicesalexa-skills-kitamazon-echo

How to return Dialog.Delegate in Amazon Alexa SDK?


A few of my Intents for my Alexa app require certain slots. The Alexa skills builder makes this easy. I can mark a slot as required and set what Alexa should ask in order for the user to provide the information for the slot. The thing is that as a developer, you have to tell Alexa with your lambda that you want Alexa to handle the slot filling.

Reading the documentation, I get to this part:

https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/dialog-interface-reference#details

It states

If the dialog is IN_PROGRESS, return Dialog.Delegate with no updatedIntent.

How do I do this? In my lambda I have

  @Override
  public SpeechletResponse onIntent(final IntentRequest request, final Session session)
      throws SpeechletException {

    Intent intent = request.getIntent();
    String intentName = (intent != null) ? intent.getName() : null;

    if ("AddTwoNumbers".equals(intentName)) {
      if (!request.getDialogState().equals("COMPLETED")) {
          return new DelegateDirective();
      } else {
       handleAdditionIntent();
      }
    } else { // handle other intents}
    }

Their code sample doesn't seem too helpful either.

} else if (intentRequest.dialogState != "COMPLETED"){
    // return a Dialog.Delegate directive with no updatedIntent property.
} else {

Solution

  • I ran into this issue the other day and I got the solution based on another post. Here's the slightly modified version that works for me in version 1.5.0 of Alexa Skill Kit. Hope this helps. You might want to handle the IN_PROGRESS state differently if you have more than 1 slot to fill. This code here is only designed for 1 slot.

         if (speechletRequestEnvelope.getRequest().getDialogState() != IntentRequest.DialogState.COMPLETED)
            // 1. Create DialogIntent based on your original intent
            DialogIntent dialogIntent = new DialogIntent(speechletRequestEnvelope.getRequest().getIntent());
    
            // 2. Create Directive
            DelegateDirective dd = new DelegateDirective();
            dd.setUpdatedIntent(dialogIntent);
    
            List<Directive> directiveList = new ArrayList<>();
            directiveList.add(dd);
    
            SpeechletResponse speechletResp = new SpeechletResponse();
            speechletResp.setDirectives(directiveList);
            // 3. return the response.
            speechletResp.setNullableShouldEndSession(false);
            return speechletResp;
        }