Watson响应一个API代码 [英] Watson Responds with one API code
问题描述
我知道是否要在对话中发送给Watson,我使用:
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);
我的代码的结果:
我想知道如何让Watson在对话中发送一些信息.我看到了一些示例,并尝试了一下,但没有成功.有人可以帮忙吗?
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?
如果我做对了,我现在不这样做,但是我的例子是:
// 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
这就是我想要的:
推荐答案
您是否在 https://github.com/watson-developer-cloud/conversation-simple 吗?
您可以以JSON方式将对象添加到上下文中.
You can add objects to the context in a JSON way.
context.myproperty = "Hello World";
并将其与输入内容一起发送给服务
And send this with the input to the service
相反,在对话服务内部,可以为上一步提供的文本(在此情况下为input.text)分配变量(在此情况下为用户名).
通过使用$variablename
(在本例中为$username
),您可以生成动态响应.
不要让预先响应屏幕中的命令打扰您,在输出之前先处理上下文...
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...
在客户端(在我的情况下为Java)
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");
此textObject将包含:乔,很高兴认识您.我在这里回答有关...的问题.
This textObject will contain: Hi Joe, nice to meet you. I am here to answer questions about....
(节点.)JS代码从示例应用程序复制(并删除了一些行)
(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 );
} );
这篇关于Watson响应一个API代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!