-->

Watson Responds with one API code

2019-03-02 08:36发布

问题:

i know if I want send any to Watson in conversation I use the:

var latestResponse = Api.getResponsePayload();
var context = latestResponse.context;
Api.sendRequest("Hi Watson!", context);

This result of my code:

I want to know how do I get Watson to send something in the conversation. I saw some examples and tried and it did not work. Can someone help?

I dont now If I'm doing right, but My example is:

// var responseText = null;
   //responseText = {};
 var latestResponse = Api.setResponsePayload(); // I dont know if this is true
  var context = latestResponse.context;
  Api.sendRequest('Hi ' + context); // I try this
  responseText = 'Hi ' + context; // I try this too

This is what i want:

回答1:

Did you check the demo app at https://github.com/watson-developer-cloud/conversation-simple ?

You can add objects to the context in a JSON way.

context.myproperty = "Hello World";

And send this with the input to the service

The other way around, inside the conversation service, you can assign a variable (in this case username) to the text provided in the previous step (in this case input.text). By using $variablename (in this case $username) you can generate a dynamic response. Don't let the order in the advance response screen disturb you, context is processed before output...

In the client (in my case Java)

MessageRequest.Builder messageRequestBuilder = new MessageRequest.Builder();
        messageRequestBuilder.inputText("Joe");
        messageRequestBuilder.context(question.context); //this context comes from a previous step 


ServiceCall<MessageResponse> response = conversationService.message(workspaceId, messageRequestBuilder.build());
        MessageResponse mAnswer =  response.execute();

        Object textObject = mAnswer.getOutput().get("text");

This textObject will contain: Hi Joe, nice to meet you. I am here to answer questions about....

(Node.) JS code copied (and removed some lines ) from the sample app

// Create the service wrapper
var conversation = watson.conversation ( {
  username: process.env.CONVERSATION_USERNAME || '<username>',
  password: process.env.CONVERSATION_PASSWORD || '<password>',
  version_date: '2016-07-11',
  version: 'v1'
} );


// Endpoint to be called from the client side
app.post ( '/api/message', function (req, res) {

  var payload = {
    workspace_id: workspace_id,
    context: {}
  };
  if ( req.body ) {
    if ( req.body.input ) {
      payload.input = req.body.input;
    }
    if ( req.body.context ) {
      // The client must maintain context/state
      payload.context = req.body.context;
    }
  }
  // Send the input to the conversation service
  conversation.message ( payload, function (data) {
    return res.json ( data );
  } );