in src/main/java/com/amazonaws/lex/twilio/sample/streaming/LexBidirectionalStreamingClient.java [105:157]
public BotConversation startConversation(TwilioCallOperator twilioCallOperator) throws URISyntaxException {
// create a new SDK client. you will need to use an async client.
LOG.info("step 1: creating a new Lex SDK client");
// this is created once at class creation time in static block.
// configure bot, alias and locale with which to have a conversation.
LOG.info("step 2: configuring bot details");
StartConversationRequest.Builder startConversationRequestBuilder = StartConversationRequest.builder()
.botId(botId)
.botAliasId(botAliasId)
.localeId(localeId);
// configure the conversation mode with bot (defaults to audio)
LOG.info("step 3: choosing conversation mode");
startConversationRequestBuilder = startConversationRequestBuilder.conversationMode(ConversationMode.AUDIO);
// assign a unique identifier for the conversation
LOG.info("step 4: choosing a unique conversation identifier");
startConversationRequestBuilder = startConversationRequestBuilder.sessionId(sessionId);
// build the initial request
StartConversationRequest startConversationRequest = startConversationRequestBuilder.build();
// create a stream of audio data to server. stream will start after connection is established with server.
EventsPublisher eventsPublisher = new EventsPublisher();
BotConversation botConversation = new BotConversation(eventsPublisher);
// create a class to handle responses from bot. after server processes streamed user data, it will respond back
// on another stream.
BotResponseHandler botResponseHandler = new BotResponseHandler(botConversation, twilioCallOperator);
// start a connection and pass in the a publisher that will stream audio and process bot responses.
LOG.info("step 5: starting the conversation ...");
CompletableFuture<Void> conversation = lexRuntimeServiceClient.startConversation(
startConversationRequest,
eventsPublisher,
botResponseHandler);
// wait till conversation finishes. conversation will finish if dialog state reaches "Closed" state - at which point
// client should gracefully stop the connection,or some exception occurs during the conversation - at which point
// client should send a disconnection event.
conversation.whenComplete((result, exception) -> {
if (exception != null) {
eventsPublisher.stop();
twilioCallOperator.hangUp(true);
}
});
return botConversation;
}