Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Created June 27, 2019 10:22
Show Gist options
  • Save germanviscuso/e4231ab43359cdbc50c8462314aba450 to your computer and use it in GitHub Desktop.
Save germanviscuso/e4231ab43359cdbc50c8462314aba450 to your computer and use it in GitHub Desktop.
Progressive Response in Java
sendProgressiveResponseMessage("Please wait!", handlerInput);
public static void sendProgressiveResponseMessage(final String message,
final HandlerInput handlerInput){
final SpeakDirective speakDirective = SpeakDirective.builder()
.withSpeech(message)
.build();
final String requestId = handlerInput.getRequestEnvelope().getRequest().getRequestId();
final SendDirectiveRequest sendDirectiveRequest = SendDirectiveRequest.builder()
.withDirective(speakDirective)
.withHeader(Header.builder().withRequestId(requestId).build())
.build();
handlerInput.getServiceClientFactory().getDirectiveService().enqueue(sendDirectiveRequest);
}
}
@kinisoftware
Copy link

This is my code, quite similar but in Kotlin:

private fun enqueueMessage(message: String) {
        val requestId = input.requestEnvelope.request.requestId
        val sendDirectiveRequest = SendDirectiveRequest.builder()
                .withDirective(SpeakDirective.builder()
                        .withSpeech("${getRandomOkSpeech()}, $message")
                        .build())
                .withHeader(Header.builder()
                        .withRequestId(requestId)
                        .build())
                .build()
        input.serviceClientFactory.directiveService.enqueue(sendDirectiveRequest)
    }

Now the challenge is to make the logic await till the progressive response is done before returning the final response :) Working on it!

@germanviscuso
Copy link
Author

Nice!! Yes, it's basically the same. Let me know if you manage to "await" :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment