Search in sources :

Example 1 with PostContentRequest

use of com.amazonaws.services.lexrts.model.PostContentRequest in project aws-sdk-android by aws-amplify.

the class InteractionClient method carryOnWithText.

/**
 * Accepts user's response as {@link String}.
 */
private void carryOnWithText(final String text, final Map<String, String> sessionAttributes, final Map<String, String> requestAttributes, final ResponseType mode) {
    // Ensure the client is not pre-occupied with a request.
    checkBusyState();
    // Send user's response to Amazon Lex service as a text.
    final InteractionClient client = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Handler handler = new Handler(context.getMainLooper());
            Runnable returnCallback;
            try {
                final PostContentRequest request = CreateLexServiceRequest.generatePostContentRequest(sessionAttributes, requestAttributes, interactionConfig, credentialsProvider, mode, text);
                final PostContentResult result = amazonlex.postContent(request);
                processResponseAudioPlayback(handler, result, client, mode, ResponseType.TEXT);
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        interactionListener.onInteractionError(null, e);
                    }
                };
                handler.post(returnCallback);
            } finally {
                setBusyState(NOT_BUSY);
            }
        }
    }).start();
}
Also used : PostContentRequest(com.amazonaws.services.lexrts.model.PostContentRequest) Handler(android.os.Handler) PostContentResult(com.amazonaws.services.lexrts.model.PostContentResult) 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)

Example 2 with PostContentRequest

use of com.amazonaws.services.lexrts.model.PostContentRequest in project aws-sdk-android by aws-amplify.

the class InteractionClient method carryOnWithMic.

/**
 * Starts listening for the user to speak, through the microphones. The voice interaction client
 * detects the start and end of speech.
 */
private void carryOnWithMic(final Map<String, String> sessionAttributes, final Map<String, String> requestAttributes, final ResponseType mode) {
    // Ensure that the client is not pre-occupied with another dlalog
    checkBusyState();
    // Send user's response to Amazon Lex service as an audio-stream.
    final InteractionClient client = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Handler handler = new Handler(context.getMainLooper());
            Runnable returnCallBack;
            try {
                // Create a new voice interaction client.
                if (AudioEncoding.LPCM.equals(interactionConfig.getAudioEncoding())) {
                    audioEncoder = new BufferedAudioEncoder(new L16PcmEncoder());
                } else {
                    audioEncoder = new BufferedAudioEncoder(new OpusEncoder());
                }
                // Set time-out limits for mic audio.
                audioTimeouts = new AudioTimeouts(interactionConfig.getNoSpeechTimeoutInterval(), interactionConfig.getMaxSpeechTimeoutInterval());
                // Set VAD configuration.
                vadConfig = new DnnVADConfig(interactionConfig.getLrtThreshold(), interactionConfig.getStartPointingThreshold(), interactionConfig.getEndPointingThreshold());
                lexAudioRecorder = new LexAudioRecorderBuilder(context).audioEncoder(audioEncoder).audioTimeouts(audioTimeouts).dnnVADConfig(vadConfig).build();
                // Calculate the maximum buffer size for pipes.
                final int maxTotalAudioLengthInMills = audioTimeouts.getNoSpeechTimeout() + audioTimeouts.getMaxSpeechTimeout();
                final int pipeSize = AudioRecorder.DEFAULT_SAMPLE_RATE * (int) TimeUnit.MILLISECONDS.toSeconds(maxTotalAudioLengthInMills) * (SAMPLE_SIZE / Byte.SIZE);
                final InputStream audioInStream = new BufferedInputStream(lexAudioRecorder.getConsumerStream(), pipeSize);
                final PostContentRequest request = CreateLexServiceRequest.generatePostContentRequest(sessionAttributes, requestAttributes, interactionConfig, credentialsProvider, mode, audioInStream, audioEncoder.getMediaType().toString());
                // Start the speech listener, service api's will be called only when the speech frames are detected.
                startListening(handler, microphoneListener, lexAudioRecorder, request, client, mode);
            } catch (final Exception e) {
                returnCallBack = new Runnable() {

                    @Override
                    public void run() {
                        interactionListener.onInteractionError(null, e);
                    }
                };
                handler.post(returnCallBack);
            } finally {
                setBusyState(NOT_BUSY);
            }
        }
    }).start();
}
Also used : BufferedAudioEncoder(com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.BufferedAudioEncoder) AudioTimeouts(com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.AudioTimeouts) PostContentRequest(com.amazonaws.services.lexrts.model.PostContentRequest) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Handler(android.os.Handler) L16PcmEncoder(com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.L16PcmEncoder) DnnVADConfig(com.amazonaws.mobileconnectors.lex.interactionkit.internal.vad.config.DnnVADConfig) 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) OpusEncoder(com.amazon.blueshift.bluefront.android.audio.encoder.OpusEncoder) BufferedInputStream(java.io.BufferedInputStream) LexAudioRecorderBuilder(com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.LexAudioRecorderBuilder)

Example 3 with PostContentRequest

use of com.amazonaws.services.lexrts.model.PostContentRequest in project aws-sdk-android by aws-amplify.

the class CreateLexServiceRequest method generatePostContentRequest.

/**
 * Creates a request to post speech input to the Amazon Lex service.
 *
 * @param sessionAttributes Session attributes for this current transaction.
 * @param requestAttributes Attributes to add to the current request
 * @param audioStream audio as {@link InputStream}.
 * @return {@link PostContentRequest}.
 */
public static PostContentRequest generatePostContentRequest(Map<String, String> sessionAttributes, Map<String, String> requestAttributes, InteractionConfig interactionConfig, AWSCredentialsProvider credentialsProvider, ResponseType mode, InputStream audioStream, String contentType) {
    final PostContentRequest request = generateRequestInternal(sessionAttributes, requestAttributes, interactionConfig, credentialsProvider, mode);
    // Set input and content type.
    request.setInputStream(audioStream);
    request.setContentType(contentType);
    return request;
}
Also used : PostContentRequest(com.amazonaws.services.lexrts.model.PostContentRequest)

Example 4 with PostContentRequest

use of com.amazonaws.services.lexrts.model.PostContentRequest in project aws-sdk-android by aws-amplify.

the class CreateLexServiceRequest method generatePostContentRequest.

/**
 * Creates a request to post text input to the Amazon Lex service.
 *
 * @param sessionAttributes Session attributes for this current transaction.
 * @param requestAttributes Attributes to add to the current request
 * @param text Input text.
 * @return {@link PostContentRequest}.
 */
public static PostContentRequest generatePostContentRequest(Map<String, String> sessionAttributes, Map<String, String> requestAttributes, InteractionConfig interactionConfig, AWSCredentialsProvider credentialsProvider, ResponseType mode, String text) {
    final PostContentRequest request = generateRequestInternal(sessionAttributes, requestAttributes, interactionConfig, credentialsProvider, mode);
    // Set input and content type.
    final byte[] textContent = text.getBytes(StringUtils.UTF8);
    request.setInputStream(new ByteArrayInputStream(textContent));
    request.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString());
    return request;
}
Also used : PostContentRequest(com.amazonaws.services.lexrts.model.PostContentRequest) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with PostContentRequest

use of com.amazonaws.services.lexrts.model.PostContentRequest in project aws-sdk-android by aws-amplify.

the class CreateLexServiceRequest method generateRequestInternal.

/**
 * Utility method for generating a request. Populates the request with a user ID from either interactionConfig or
 * the credentialsProvider, which must be an instance of CognitoCredentialsProvider.
 *
 * @param sessionAttributes Session attributes for this current transaction.
 * @param requestAttributes Attributes to add to the current request
 * @param interactionConfig The configuration for the interaction, which must contain the user ID if
 *                          credentialsProvider is not an instance of CognitoCredentialsProvider.
 * @param credentialsProvider The credentialsProvider to obtain the Cognito IdentityId. If the user ID is not set in
 *                            interactionConfig, credentialsProvider must be an instance of CognitoCredentialsProvider
 * @param mode The desired response type
 * @return a PostContentRequest with attributes and user ID, but no content
 */
private static PostContentRequest generateRequestInternal(Map<String, String> sessionAttributes, Map<String, String> requestAttributes, InteractionConfig interactionConfig, AWSCredentialsProvider credentialsProvider, ResponseType mode) {
    final PostContentRequest request = new PostContentRequest();
    request.setBotName(interactionConfig.getBotName());
    request.setBotAlias(interactionConfig.getBotAlias());
    request.setAccept(mode.toString());
    final Map<String, String> newSessionAttributes = new HashMap<String, String>();
    newSessionAttributes.putAll(interactionConfig.getGlobalSessionAttributes());
    if (sessionAttributes != null) {
        newSessionAttributes.putAll(sessionAttributes);
    }
    request.setSessionAttributes(mapToBase64(newSessionAttributes));
    // Set the request attributes
    if (requestAttributes != null) {
        request.setRequestAttributes(mapToBase64(requestAttributes));
    }
    if (interactionConfig.getUserId() == null || interactionConfig.getUserId().isEmpty()) {
        final CognitoCredentialsProvider cognitoCredentialsProvider = (CognitoCredentialsProvider) credentialsProvider;
        request.setUserId(cognitoCredentialsProvider.getIdentityId());
    } else {
        request.setUserId(interactionConfig.getUserId());
    }
    return request;
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) PostContentRequest(com.amazonaws.services.lexrts.model.PostContentRequest) HashMap(java.util.HashMap)

Aggregations

PostContentRequest (com.amazonaws.services.lexrts.model.PostContentRequest)5 Handler (android.os.Handler)2 AmazonClientException (com.amazonaws.AmazonClientException)2 AudioPlaybackException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.AudioPlaybackException)2 DialogFailedException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.DialogFailedException)2 InvalidParameterException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.InvalidParameterException)2 LexClientException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.LexClientException)2 MaxSpeechTimeOutException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.MaxSpeechTimeOutException)2 NoSpeechTimeOutException (com.amazonaws.mobileconnectors.lex.interactionkit.exceptions.NoSpeechTimeOutException)2 OpusEncoder (com.amazon.blueshift.bluefront.android.audio.encoder.OpusEncoder)1 CognitoCredentialsProvider (com.amazonaws.auth.CognitoCredentialsProvider)1 AudioTimeouts (com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.AudioTimeouts)1 LexAudioRecorderBuilder (com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.LexAudioRecorderBuilder)1 BufferedAudioEncoder (com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.BufferedAudioEncoder)1 L16PcmEncoder (com.amazonaws.mobileconnectors.lex.interactionkit.internal.audio.encoder.L16PcmEncoder)1 DnnVADConfig (com.amazonaws.mobileconnectors.lex.interactionkit.internal.vad.config.DnnVADConfig)1 PostContentResult (com.amazonaws.services.lexrts.model.PostContentResult)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1