Search in sources :

Example 1 with MessageResponse

use of com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class ConversationTest method testSendMessage.

/**
 * Test send message.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessage() throws IOException, InterruptedException {
    String text = "I'd like to get insurance to for my home";
    MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
    server.enqueue(jsonResponse(mockResponse));
    InputData input = new InputData.Builder(text).build();
    RuntimeIntent intent = new RuntimeIntent();
    intent.setIntent("turn_off");
    intent.setConfidence(0.0);
    RuntimeEntity entity = new RuntimeEntity();
    entity.setEntity("car");
    entity.setValue("ford");
    MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(input).addIntent(intent).addEntity(entity).alternateIntents(true).build();
    // execute first request
    MessageResponse serviceResponse = service.message(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = StringUtils.join(PATH_MESSAGE, "?", VERSION, "=2018-02-16");
    assertEquals(path, request.getPath());
    assertArrayEquals(new String[] { "Do you want to get a quote?" }, serviceResponse.getOutput().getText().toArray(new String[0]));
    assertEquals(request.getMethod(), "POST");
    assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
    String expected = "{" + "\"input\":{\"text\":\"I'd like to get insurance to for my home\"}," + "\"intents\":[{\"confidence\":0.0,\"intent\":\"turn_off\"}]," + "\"entities\":[{\"value\":\"ford\",\"entity\":\"car\"}]," + "\"alternate_intents\":true" + "}";
    JsonParser parser = new JsonParser();
    JsonObject expectedObj = parser.parse(expected).getAsJsonObject();
    JsonObject actualObj = parser.parse(request.getBody().readUtf8()).getAsJsonObject();
    assertTrue(expectedObj.equals(actualObj));
    assertEquals(serviceResponse, mockResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) JsonObject(com.google.gson.JsonObject) InputData(com.ibm.watson.developer_cloud.conversation.v1.model.InputData) JsonParser(com.google.gson.JsonParser) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with MessageResponse

use of com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class ConversationTest method testSendMessageWithAlternateIntents.

/**
 * Test send message. use some different MessageOptions options like context and other public methods
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessageWithAlternateIntents() throws IOException, InterruptedException {
    MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
    server.enqueue(jsonResponse(mockResponse));
    Context contextTemp = new Context();
    contextTemp.put("name", "Myname");
    InputData inputTemp = new InputData.Builder("My text").build();
    MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(inputTemp).alternateIntents(false).context(contextTemp).entities(null).intents(null).build();
    // execute first request
    MessageResponse serviceResponse = service.message(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = StringUtils.join(PATH_MESSAGE, "?", VERSION, "=2018-02-16");
    assertEquals(path, request.getPath());
    assertArrayEquals(new String[] { "Do you want to get a quote?" }, serviceResponse.getOutput().getText().toArray(new String[0]));
    assertEquals(request.getMethod(), "POST");
    assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
    assertEquals("{\"input\":{\"text\":\"My text\"},\"alternate_intents\":false," + "\"context\":{\"name\":\"Myname\"}}", request.getBody().readUtf8());
    assertEquals(serviceResponse, mockResponse);
}
Also used : Context(com.ibm.watson.developer_cloud.conversation.v1.model.Context) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) InputData(com.ibm.watson.developer_cloud.conversation.v1.model.InputData) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with MessageResponse

use of com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class Conversation method message.

/**
 * Get a response to a user's input.
 *
 * @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));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) JsonObject(com.google.gson.JsonObject)

Example 4 with MessageResponse

use of com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class AssistantTest method testSendMessage.

/**
 * Test send message.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessage() throws IOException, InterruptedException {
    String text = "I'd like to get insurance to for my home";
    MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
    server.enqueue(jsonResponse(mockResponse));
    InputData input = new InputData.Builder(text).build();
    RuntimeIntent intent = new RuntimeIntent();
    intent.setIntent("turn_off");
    intent.setConfidence(0.0);
    RuntimeEntity entity = new RuntimeEntity();
    entity.setEntity("car");
    entity.setValue("ford");
    MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(input).addIntent(intent).addEntity(entity).alternateIntents(true).build();
    // execute first request
    MessageResponse serviceResponse = service.message(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = StringUtils.join(PATH_MESSAGE, "?", VERSION, "=2018-02-16");
    assertEquals(path, request.getPath());
    assertArrayEquals(new String[] { "Do you want to get a quote?" }, serviceResponse.getOutput().getText().toArray(new String[0]));
    assertEquals(request.getMethod(), "POST");
    assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
    String expected = "{" + "\"input\":{\"text\":\"I'd like to get insurance to for my home\"}," + "\"intents\":[{\"confidence\":0.0,\"intent\":\"turn_off\"}]," + "\"entities\":[{\"value\":\"ford\",\"entity\":\"car\"}]," + "\"alternate_intents\":true" + "}";
    JsonParser parser = new JsonParser();
    JsonObject expectedObj = parser.parse(expected).getAsJsonObject();
    JsonObject actualObj = parser.parse(request.getBody().readUtf8()).getAsJsonObject();
    assertTrue(expectedObj.equals(actualObj));
    assertEquals(serviceResponse, mockResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) JsonObject(com.google.gson.JsonObject) InputData(com.ibm.watson.developer_cloud.assistant.v1.model.InputData) JsonParser(com.google.gson.JsonParser) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 5 with MessageResponse

use of com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse in project java-sdk by watson-developer-cloud.

the class AssistantExample method main.

public static void main(String[] args) throws Exception {
    Assistant service = new Assistant("2018-02-16");
    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
    }
}
Also used : MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) CompletableFuture(jersey.repackaged.jsr166e.CompletableFuture) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.developer_cloud.assistant.v1.Assistant) OutputData(com.ibm.watson.developer_cloud.assistant.v1.model.OutputData)

Aggregations

Test (org.junit.Test)10 MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)8 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)7 MessageResponse (com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse)6 MessageOptions (com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions)5 JsonObject (com.google.gson.JsonObject)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)4 InputData (com.ibm.watson.developer_cloud.assistant.v1.model.InputData)4 InputData (com.ibm.watson.developer_cloud.conversation.v1.model.InputData)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 CompletableFuture (jersey.repackaged.jsr166e.CompletableFuture)3 JsonParser (com.google.gson.JsonParser)2 Assistant (com.ibm.watson.developer_cloud.assistant.v1.Assistant)2 Context (com.ibm.watson.developer_cloud.assistant.v1.model.Context)2 OutputData (com.ibm.watson.developer_cloud.assistant.v1.model.OutputData)2 RuntimeEntity (com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity)2 RuntimeIntent (com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent)2 Context (com.ibm.watson.developer_cloud.conversation.v1.model.Context)2 RuntimeEntity (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity)2 RuntimeIntent (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent)2