question

Jerome E. Garcia avatar image
Jerome E. Garcia asked

DialogState missing in 2.0.x and 2.x_public-beta

I cannot find the DialogState class in either of the 2.x GIT SDK downloads at https://github.com/alexa/alexa-skills-kit-sdk-for-java. Where is it? I cannot build with statements like:

DialogState dialogueState = request.getDialogState();

I tried alexa-skills-kit-sdk-for-java-2.x_public-beta

And, I tried alexa-skills-kit-sdk-for-java-2.0.x

alexa skills kitlambdanodejssdk v2
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Amazon_Bernardo Bezerra avatar image
Amazon_Bernardo Bezerra answered

Hi @Jerome E. Garcia.

Thank you for your patience on this issue.

DialogState is a property on an IntentRequest object. You access DialogState via the getter method getDialogState(). IntentRequest is a subclass of the abstract Request object. So, to get the IntentRequest from a request, you get the RequestEnvelope, then call getRequest() and cast the result as IntentRequest. Then you can call getDialogState(). If you don't do the cast, you can't call getDialogState(), since the method does not exist on Request.

Here's an example of a canHandle method for a handler that just sends the Delegate directive for all requests where DialogState is not complete. This checks 2 conditions:

  • The requestType is an IntentRequest
  • The DialogState is not equal to DialogState.COMPLETED

Note the imports that are required for this code to work.

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.DialogState;
import com.amazon.ask.model.IntentRequest;
import com.amazon.ask.model.Response;

import java.util.Optional;

import static com.amazon.ask.request.Predicates.requestType;

public class IncompleteIntentRequest implements RequestHandler {
    @Override
    public boolean canHandle(HandlerInput handlerInput) {

        // handle all incomplete requests, since we will just
        // delegate on all. So not testing the intent name here.
        return handlerInput.matches(requestType(IntentRequest.class))
                && ((IntentRequest)handlerInput.getRequestEnvelope()
                .getRequest()).getDialogState() != DialogState.COMPLETED;

    }

    @Override
    public Optional<Response> handle(HandlerInput handlerInput) {

        IntentRequest intentRequest = (IntentRequest) handlerInput
                .getRequestEnvelope().getRequest();

        return handlerInput.getResponseBuilder()
                .addDelegateDirective(intentRequest.getIntent())
                .build();
    }
}

It may be a little hard to parse since the code has been condensed to retrieve the request, cast it as an IntentRequest and then retrieve the DialogState into one statement:

((IntentRequest)handlerInput.getRequestEnvelope().getRequest()).getDialogState()

That code gets the value returned by handlerInput.getRequestEnvelope().getRequest() and casts it as IntentRequest. Then it calls getDialogState().

The second method (handle) shows returning a dialog directive. You use the ResponseBuilder and call addDelegateDirective. In this case the intent that was part of the request is being passed along with no changes, because there is no need to change any slot values.

I hope this helps with your issue.

Regards,
Barry

2 comments
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you very much for clarifying how this works.

Regards,

Jerome

0 Likes 0 ·

My pleasure @Jerome E. Garcia.

Feel free to contact us again if you bump into any other issues.

Regards,
Barry

0 Likes 0 ·
Amazon_Bernardo Bezerra avatar image
Amazon_Bernardo Bezerra answered

Hello @Jerome E. Garcia and thanks for your message.

Have you checked the documentation here?

Regards,
Barry

3 comments
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Yes. It is in the documents so I started coding with it but I downloaded the both versions of the SDK available at

https://github.com/alexa/alexa-skills-kit-sdk-for-java

and DialogState is not included in either of the expanded zipped directories. The first clue was that my compilations failed as soon as I tried to reference the class.

It does not look like the contents have changed over the last few days.

0 Likes 0 ·

Thanks for the feedback @Jerome E. Garcia. I have created an internal ticket to check it and will inform you once I get a reply.

Regards,
Barry

0 Likes 0 ·
Jerome E. Garcia avatar image Jerome E. Garcia Amazon_Bernardo Bezerra ♦♦ ·

Hi Barry,

Do you have any update on this issue? I will have to decide soon whether I can wait for DialogState to appear in the SDK or whether I will need to use the technique I used originally with the 1.X version of the Alexa Skill SDK to implement some code. I would prefer to use DialogState if possible.

Thanks,

Jerome

0 Likes 0 ·