IBM Bluemix

IBM Bluemix: Watson Conversation Java Integration

This post is again a part of the series to create a chat bot using IBM Bluemix, Watson Conversation, Java and for chat client window, Facebook. Till now we have cloned the project successfully in eclipse, Now we will see how to integrate a Java application to Watson Conversation and also how to create dialogues in Conversation.

Training Watson Conversation:

Start by going to the dashboard of Conversation, similar to the image shown below.Trainig_ConvIn the Conversation dashboard, IBM has given a good tutorial for start to a beginner. I recommend going through the tutorial to understand the terms like intents, entity and dialogue. Click on the launch tool to start creating with workspace for Conversation.Training_Conv_1Click on create a new workspace, and give a name and description to the workspace.Training_Conv_2Clicking on Create button will launch the build screen of the Conversation. From here we can create intents, entities and dialogs. Start with creating intents, intents represents the user input. For example I have created an intent named ‘Greetings’. For Watson to understand we have to give at least 5 examples for the intent.Intents in Watson

Now after creating intent, let’s move to dialogue tab to create a reply for the intent. In the dialogue, you can use the value from the context that we can send through our middleware application (in our case Java application). In this response, I have used the username from context. You can multiple responses, and can set the response variation order as a sequence as well as random.
Dialog in WatsonNow our dialogue is ready, let’s create the connection for Conversation to the Java application. For that let’s go back to the Conversation dashboard and click on Connection tab on side nav bar. Then click on create connection button and select the Java Liberty application to create the connection. That’s it the conversation is deployed and connected, now to use it we have to write a java program.

Integrating Conversation service with Java application:

To integrate Conversation service lets go to eclipse and to the project which we have cloned. You can use any build tool to build the project I have used maven as a building tool. For that, I have converted the project into Maven project and created a skeleton Maven project. To do that go to the working directory and run the following program (before that make sure Maven is installed ).

mvn archetype:generate -DartifactId=mavenFromScratch -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Then in the pom.xml, lets add the conversation dependencies.

Now let’s create a simple Java program to chat with Conversation.

package wasdev.sample.servlet;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;


public class TestConversation {

public static void main(String[] args) {
BufferedReader br = null;
MessageResponse response = null;
Map context = new HashMap();
try {
br = new BufferedReader(new InputStreamReader(;
TimeZone timeZone = TimeZone.getDefault();
System.out.println("Enter your Name: ");
String userName = br.readLine();
// Add userName to context to be used by Conversation.
context.put("userName", userName);
context.put("currentTime", Calendar.getInstance(timeZone).getTime().toString());
context.put("timezone", timeZone.getID());

while (true) {
System.out.print(userName + " : ");
String input = br.readLine();
response = conversationAPI(input, context);
System.out.println("Watson Response: " + response.getText().get(0));
context = response.getContext();
} catch (IOException e) {
public static MessageResponse conversationAPI(String input, Map context) {
ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_07_11);
// Credentials of Workspace of Conversation
service.setUsernameAndPassword("******************", "****************");
MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();
// Workspace ID of Conversation current workspace
String workspaceId = "******************";
MessageResponse response = service.message(workspaceId, newMessage).execute();
return response;


In this program give the user name, password and workspace Id, of the conversation app. Which you can get it from the conversation deploy tab.Conversation WorkspaceNow lets run the application and see the result.Result_Chat

So, as you can see we have successfully integrated the chat with Watson Conversation. And also bot is replying with value from the context.
In the next tutorial we will see how to integrate our java application with Facebook chat to have an end to end chat bot.

3 Comments on “IBM Bluemix: Watson Conversation Java Integration

Leave a Reply

Your email address will not be published. Required fields are marked *