Overview

Skill Level: Beginner

Beginner

Create your Bluemix account https://console.ng.bluemix.net/ . Then create Watson conversation service and configure workspace . Develop java code which uses Watson APIs to send/receive requests/responses to conversation service

Ingredients

Bluemix Account

Eclipse IDE

 

Step-by-step

  1. Create Bluemix account

     Click the below link and follow the steps.

    https://console.ng.bluemix.net/

     

     Bluemixlogin

     

    Click on Create a free account and fill the below form to complete the registration.          

     

     
     bluemixform

  2. Create Conversation service

     Click on category and select Conversation service under Watson category as show below

    watson_con1

     

    Give unique name for service and select the and select free pricing plan.

    watson_con2

  3. Creating the workspace

    On successful creation of service, you will be redirected to your conversation service page. Click on Launch tool.

    wrok1

     

    Create workspace by defining intents, entities and dialogs. For more detailed information on configuring workspace, please go through below link.

    https://www.ibm.com/watson/developercloud/doc/conversation/configure-workspace.html

    Click on import option and load the attached json file for testing purpose at https://github.com/watson-developer-cloud/car-dashboard/tree/master/training

    work2

  4. Develop java application

    Create new java class and copy the below the code.

    Dowload the java-sdk-3.3.1-jar-with-dependencies.jar file at link https://github.com/watson-developer-cloud/java-sdk

    and import it into your project

     

     

    import com.ibm.watson.developer_cloud.conversation.v1.ConversationService;

    import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;

    import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class BVTService {

    public static void main( String[] args )
    {

    BufferedReader br = null;
    MessageResponse response = null;
    Map context = new HashMap();
    try {
    br = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
    System.out.print(“Enter something : “);
    String input = br.readLine();
    if (“q”.equals(input)) {
    System.out.println(“Exit!”);
    System.exit(0);
    }
    response = conversationAPI(input,context);
    System.out.println(“Watson Response:”+ response.getText().get(0));
    context = response.getContext();
    System.out.println(“———–
    “);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static MessageResponse conversationAPI(String input,Map context){
    ConversationService service = new ConversationService(“2017-02-03”);
    service.setUsernameAndPassword(“<<Username>>”, “<<Password>>”);
    MessageRequest newMessage = new MessageRequest.Builder()
    .inputText(input).context(context).build();
    String workspaceId = “<<Workspace ID>”;
    MessageResponse response = service.message(workspaceId, newMessage).execute();
    return response;
    }
    }

     

     

  5. Configuration and Testing

    Service Credentials:
    Replace <<Username>> and <<Password>> with your service credentials in java code. Go to your service page and click on Service Credentials. Then click on View Credentials to get username and password.

    serv1

     

    Workspace ID:
    Replace the <<Workspace ID>> with your workspace Id. Go to your workspace page and click on View details .

     

    serv2

    Now run the java application in eclipse and try following inputs.

    Hello, turn on the lights, where is the nearest restaurant, Play some music

2 comments on"Integration of IBM Watson Conversation service to your java application"

  1. Vishal-Ramani August 08, 2017

    I am getting this error at time of run this code
    “This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server (gateway.watsonplatform.net)”

    So can you please provide solution.

  2. How to call service in front end.
    means UI mode can please provide sample things.

Join The Discussion