use of com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.
the class ConversationServiceIT method testSendMessages.
/**
* Test send messages.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void testSendMessages() throws InterruptedException {
final String[] messages = new String[] { "turn ac on", "turn right", "no", "yes" };
Context context = null;
for (final String message : messages) {
MessageOptions request = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(message).build()).alternateIntents(true).context(context).nodesVisitedDetails(true).build();
if (message.equals("yes")) {
RuntimeIntent offTopic = new RuntimeIntent();
offTopic.setIntent("off_topic");
offTopic.setConfidence(1.0);
request = request.newBuilder().addIntent(offTopic).build();
}
MessageResponse response = service.message(request).execute();
assertMessageFromService(response);
assertNotNull(response.getOutput().getNodesVisitedDetails());
for (RuntimeEntity entity : response.getEntities()) {
assertNotNull(entity.getGroups());
}
context = new Context();
context.putAll(response.getContext());
Thread.sleep(500);
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.
the class Assistant method message.
/**
* Get a response to a user's input. There is no rate limit for this operation.
*
* @param messageOptions the {@link MessageOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link MessageResponse}
*/
public ServiceCall<MessageResponse> message(MessageOptions messageOptions) {
Validator.notNull(messageOptions, "messageOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "message" };
String[] pathParameters = { messageOptions.workspaceId() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
if (messageOptions.nodesVisitedDetails() != null) {
builder.query("nodes_visited_details", String.valueOf(messageOptions.nodesVisitedDetails()));
}
final JsonObject contentJson = new JsonObject();
if (messageOptions.input() != null) {
contentJson.add("input", GsonSingleton.getGson().toJsonTree(messageOptions.input()));
}
if (messageOptions.alternateIntents() != null) {
contentJson.addProperty("alternate_intents", messageOptions.alternateIntents());
}
if (messageOptions.context() != null) {
contentJson.add("context", GsonSingleton.getGson().toJsonTree(messageOptions.context()));
}
if (messageOptions.entities() != null) {
contentJson.add("entities", GsonSingleton.getGson().toJsonTree(messageOptions.entities()));
}
if (messageOptions.intents() != null) {
contentJson.add("intents", GsonSingleton.getGson().toJsonTree(messageOptions.intents()));
}
if (messageOptions.output() != null) {
contentJson.add("output", GsonSingleton.getGson().toJsonTree(messageOptions.output()));
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(MessageResponse.class));
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.
the class ConversationServiceIT method testExample.
/**
* Test Example.
*/
@Test
public void testExample() {
// Conversation service = new Conversation(Conversation.VERSION_DATE_2017_05_26);
// service.setUsernameAndPassword("<username>", "<password>");
InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build();
// sync
MessageResponse response = service.message(options).execute();
System.out.println(response);
// async
service.message(options).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
System.out.println(response);
}
@Override
public void onFailure(Exception e) {
}
});
// rx callback
service.message(options).rx().thenApply(new CompletableFuture.Fun<MessageResponse, OutputData>() {
@Override
public OutputData apply(MessageResponse message) {
return message.getOutput();
}
}).thenAccept(new CompletableFuture.Action<OutputData>() {
@Override
public void accept(OutputData output) {
System.out.println(output);
}
});
// rx async callback
service.message(options).rx().thenApplyAsync(new CompletableFuture.Fun<MessageResponse, OutputData>() {
@Override
public OutputData apply(MessageResponse message) {
return message.getOutput();
}
}).thenAccept(new CompletableFuture.Action<OutputData>() {
@Override
public void accept(OutputData output) {
System.out.println(output);
}
});
// rx sync
try {
MessageResponse rxMessageResponse = service.message(options).rx().get();
System.out.println(rxMessageResponse);
} catch (Exception ex) {
// Handle exception
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.
the class ConversationServiceIT method testReadme.
/**
* Test README.
*/
@Test
public void testReadme() {
// Conversation service = new Conversation(Conversation.VERSION_DATE_2017_05_26);
// service.setUsernameAndPassword("<username>", "<password>");
InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build();
MessageResponse response = service.message(options).execute();
System.out.println(response);
}
Aggregations