Create A Watson Conversation Service And Consume From .NET Code

Creating a Watson Conversation Service

While searching on the internet, you may find out about more blogs or topics discussing Watson Conversation Service. The best guide document is the official document page. The document page link is given here.

I will demonstrate the process to create a really simple Conversation Service (chatbot) hosted in the Bluemix cloud.

Step 1

Create an account on Bluemix or login with your existing account.

Step 2

If you are signing up, then you have to confirm your email address. After your email confirmation, when you login for the first time , you will get the screen, as shown below.

 
  • Accept the terms and click on continue. Now, you have to setup the organization and the Workspace.



  • Select the Deployment Server location and name your organization.



  • Click on the create.



  • Name your space. I selected the suggestion Dev.

     

  • Click Create.

     

  • Click I am Ready. This will bring you to the Dashboard. This screen is the same, when you are returning logging after this initial setup of your account.

     

Step 3

Click Create app.

 

Step 4

Search for Conversation.

 

Step 5

Click on the Conversation Service (First Result).

 

Step 6

Click on the Create button.

 

Step 7

Go-to Service Credential tab.

 

Step 8

Click on the View Credential. It will display the endpoint information, which will be required in future to connect to this Service from our .NET code. Please note down the details.

 

Step 9

Go back to Manage Tab and click on the Launch Tool button

 

It will open a new Window (or tab). This is called the workspace. Here, we have to define the intent, entities and the dialog. For more information, please refer to the official documentation link mentioned above.

 

Step 10

Click on create. It will open a popup. Provide the Workspace name and description(optional).

 

Step 11 

Click Create.

 

Step 12

Click Create new button to create a new Intent.

 

Step 13

Provide an intent name and the sample example.

 

Step 14

Click Create.

 

Create another Intent.

 

Step 15

Now, go to Entity tab.

 

Step 16

Click Dialog tab.

 

Step 17

Click Create. Edit the selected node, as per the screen.

 

Step 18

Create a conversation start node, as shown below.

 

Create other dialog nodes.

 

Create fallback node (AnythingElse).

 

Step 19

Now, test the Service. Click on the top right corner small chat icon. Now, chat with BOT in the right hand panel.

 
 

Step 20

Click on the Hamburger menu and click on the Back to Workspace.

 

Step 21

Click on the more button.

 
 

Step 22

Click on the view details.

 

Step 23

Note down the Workspace Id. This Workspace Id is required to connect to the Service.

Consume the Service from the .NET code

Step 1

Let’s create a class called ConversationHelper. Sample code, which is given below.

  1. public class ConversationHelper {  
  2.     private readonly string _Server;  
  3.     private readonly NetworkCredential _NetCredential;  
  4.     public ConversationHelper(string workSpaceId, string userId, string password) {  
  5.         _Server = string.Format("https://gateway.watsonplatform.net/conversation/api/v1/workspaces/{0}/message?version={1}", workSpaceId, DateTime.Today.ToString("yyyy-MM-dd"));  
  6.         _NetCredential = new NetworkCredential(userId, password);  
  7.     }  
  8.     public async Task < string > GetResponse(string input, string context = null) {  
  9.         string req = null;  
  10.         if (string.IsNullOrEmpty(context)) req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}";  
  11.         else req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}, \"context\": \"" + context + "\"";  
  12.         using(var handler = new HttpClientHandler {  
  13.             Credentials = _NetCredential  
  14.         })  
  15.         using(var client = new HttpClient(handler)) {  
  16.             var cont = new HttpRequestMessage();  
  17.             cont.Content = new StringContent(req.ToString(), Encoding.UTF8, "application/json");  
  18.             var result = await client.PostAsync(_Server, cont.Content);  
  19.             return await result.Content.ReadAsStringAsync();  
  20.         }  
  21.     }  
  22. }  

Step 2

Now, from the caller class, create an instance of this class by providing the workspace Id, user Id and password. Now, call the method GetResponse. Now, you have successfully created a chatbot Service and consumed from .NET code.

The sample consume code is given below.

  1. class Program  
  2. {  
  3.     static void Main(string[] args) {  
  4.         ConversationHelper helper = new ConversationHelper("62a2a278-894d-42eb-999d-5aedaad8218d""194757ef-a260-4da3-a343-fe48bf5b87ab""grUFElBwPT2O");  
  5.         var res = helper.GetResponse("Hi").GetAwaiter().GetResult();  
  6.     }  
  7. }  

Please refer to the documentation for the model details of the input to the conversation Service and the response from the Service. The API documentation is here - https://watson-api-explorer.mybluemix.net/apis/conversation-v1#/

Conclusion

IBM Watson Conversation Service is very easy to learn and create on Bluemix. As the communication to the Service is via JSON, this Service can be consumed from almost all the languages and platforms.

Up Next
    Ebook Download
    View all
    Learn
    View all