Search code examples
javaibm-watsonwatson-conversation

Get all the output from Watson conversation using java


If I have a IBM bluemix Watson conversation dialog output JSON like:

"output": {
    "text": {
        "values": [
            "What is your name?",
            "Name of the person?",
            "Please specify the name of the person."
        ],
        "selection_policy": "random",
        "append": true
    }
}

How can I get all the suggestions from the output response?


Solution

  • You can use context variables for saves what the user says using <? input.text ?>. Try to follow this simple example:

    Create one child node in this Node above, and add:

    {
      "context": {
        "userTypes": "<? input.text ?>"
      },
      "output": {
        "text": {
          "values": [
            "All you said here: $userTypes."
          ],
          "selection_policy": "sequential"
        }
      }
    }
    

    So, in your Java example, you can get the value of this context variable with this follow method:

    private Map<String, Object> context = new HashMap<>();
    
     ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
     service.setUsernameAndPassword('Your Watson service UserName', 'Your watson service PassWord');
     MessageRequest newMessage = new MessageRequest.Builder().inputText(inputmessage).context(context).build();
     MessageResponse response = service.message('Your Workspace Id', newMessage).execute();
    
       //Passing Context of last conversation
        if(response.getContext() !=null)
          {
            context.clear();
    
            context = response.getContext();
    
         }