Search in sources :

Example 1 with LexServiceContinuation

use of com.amazonaws.mobileconnectors.lex.interactionkit.continuations.LexServiceContinuation in project aws-sdk-android by aws-amplify.

the class InteractionClient method processResponse.

/**
 * Analyzes response from Amazon Lex service. Returns a {@link Runnable}
 * with the next step, which is usually a callback method in the
 * {@link InteractionListener} object.
 *
 * @param handler      {@link Handler}, to interact with app components in the
 *                     main thread.
 * @param result       {@link PostContentResult}, response from the Amazon Lex
 *                     service.
 * @param client       {@link InteractionClient}, reference to this object.
 * @param responseMode {@link ResponseType}, current response type.
 */
private void processResponse(final Handler handler, final PostContentResult result, final InteractionClient client, final ResponseType responseMode, final ResponseType requestMode) {
    Runnable response;
    try {
        setBusyState(NOT_BUSY);
        final Response serviceResponse = new Response(result);
        if (DialogState.Failed.toString().equals(result.getDialogState())) {
            // Amazon Lex service reported an error.
            response = new Runnable() {

                @Override
                public void run() {
                    interactionListener.onInteractionError(serviceResponse, new DialogFailedException("Failed to fulfill current request."));
                }
            };
        } else if (DialogState.ReadyForFulfillment.toString().equals(result.getDialogState())) {
            // The current dialog is ready for fulfillment by the client, no
            // further action is required.
            response = new Runnable() {

                @Override
                public void run() {
                    interactionListener.onReadyForFulfillment(new Response(result));
                    interactionListener.promptUserToRespond(serviceResponse, null);
                }
            };
        } else if (DialogState.Fulfilled.toString().equals(result.getDialogState())) {
            // Request was successfully fulfilled, no further action required.
            response = new Runnable() {

                @Override
                public void run() {
                    interactionListener.promptUserToRespond(serviceResponse, null);
                }
            };
        } else {
            // User's response is required to continue.
            final LexServiceContinuation continuation = new LexServiceContinuation(client, responseMode, requestMode);
            // set the session attributes on the continuation
            continuation.setSessionAttributes(serviceResponse.getSessionAttributes());
            response = new Runnable() {

                @Override
                public void run() {
                    interactionListener.promptUserToRespond(serviceResponse, continuation);
                }
            };
        }
    } catch (final Exception e) {
        response = new Runnable() {

            @Override
            public void run() {
                interactionListener.onInteractionError(null, e);
            }
        };
    } finally {
        setBusyState(NOT_BUSY);
    }
    handler.post(response);
}
Also used : DialogFailedException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.DialogFailedException) LexServiceContinuation(com.amazonaws.mobileconnectors.lex.interactionkit.continuations.LexServiceContinuation) InvalidParameterException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.InvalidParameterException) LexClientException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.LexClientException) MaxSpeechTimeOutException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.MaxSpeechTimeOutException) DialogFailedException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.DialogFailedException) NoSpeechTimeOutException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.NoSpeechTimeOutException) AmazonClientException(com.amazonaws.AmazonClientException) AudioPlaybackException(com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.AudioPlaybackException)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)1 LexServiceContinuation (com.amazonaws.mobileconnectors.lex.interactionkit.continuations.LexServiceContinuation)1 AudioPlaybackException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.AudioPlaybackException)1 DialogFailedException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.DialogFailedException)1 InvalidParameterException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.InvalidParameterException)1 LexClientException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.LexClientException)1 MaxSpeechTimeOutException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.MaxSpeechTimeOutException)1 NoSpeechTimeOutException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.NoSpeechTimeOutException)1