使用 Node js 在 Watson Assistant 中为特定对话节点编辑上下文变量
Editing the context variables in Watson Assistant for a specific dialogue node using Node js
我有一个将 Watson 对话链接到 Facebook Messenger 的代码。
现在,我想实现下面的对话-
用户:我需要一辆车
Bot:告诉需要车辆的位置
用户:圣莫尼卡码头
Bot:****将位置标识为来自 Watson Assistant 的上下文变量 ($location)*
*从节点应用程序中找出 ETA,并将其作为上下文变量($ETA)发送给 Watson Assistant ****
机器人:您的车辆将在 $ETA 分钟内到达。
我的代码是-
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var watson = require('watson-developer-cloud');
var app = express();
var contexid = "";
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
var conversation_id = "";
var w_conversation = watson.conversation({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME || 'username',
password: process.env.CONVERSATION_PASSWORD || 'pass',
version: 'v1',
version_date: '2016-07-11'
});
var workspace = process.env.WORKSPACE_ID || 'id';
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'fb_token') {
res.send(req.query['hub.challenge']);
}
res.send('Error : no token.');
});
app.post('/webhook/', function (req, res) {
var text = null;
messaging_events = req.body.entry[0].messaging;
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i];
sender = event.sender.id;
if (event.message && event.message.text) {
text = event.message.text;
}else if (event.postback && !text) {
text = event.postback.payload;
}else{
break;
}
var params = {
input: text,
// context: {"conversation_id": conversation_id}
context:contexid
}
var payload = {
workspace_id: "id"
};
if (params) {
if (params.input) {
params.input = params.input.replace("\n","");
payload.input = { "text": params.input };
}
if (params.context) {
payload.context = params.context;
}
}
callWatson(payload, sender);
}
res.sendStatus(200);
});
function callWatson(payload, sender) {
w_conversation.message(payload, function (err, convResults) {
console.log(convResults);
contexid = convResults.context;
if (err) {
return responseToRequest.send("Erro.");
}
if(convResults.context != null)
conversation_id = convResults.context.conversation_id;
if(convResults != null && convResults.output != null){
var i = 0;
while(i < convResults.output.text.length){
sendMessage(sender, convResults.output.text[i++]);
}
}
});
}
function sendMessage(sender, text_) {
text_ = text_.substring(0, 319);
messageData = { text: text_ };
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: {
recipient: { id: sender },
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
};
var token = "token";
var host = (process.env.VCAP_APP_HOST || 'localhost');
var port = (process.env.VCAP_APP_PORT || 3000);
app.listen(port, host);
如何从 Watson Assistant 将位置标识为上下文变量 ($location) 并将 ETA 从节点应用程序发送到 Watson Assistant 作为上下文变量?
我正在考虑使用操作在我的 Watson 对话节点中进行编程调用来执行此操作但无法实现。
convResults
将包含完整的上下文对象,包括您的对话框设置的上下文变量。
我有一个将 Watson 对话链接到 Facebook Messenger 的代码。
现在,我想实现下面的对话-
用户:我需要一辆车
Bot:告诉需要车辆的位置
用户:圣莫尼卡码头
Bot:****将位置标识为来自 Watson Assistant 的上下文变量 ($location)*
*从节点应用程序中找出 ETA,并将其作为上下文变量($ETA)发送给 Watson Assistant ****
机器人:您的车辆将在 $ETA 分钟内到达。
我的代码是-
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var watson = require('watson-developer-cloud');
var app = express();
var contexid = "";
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
var conversation_id = "";
var w_conversation = watson.conversation({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME || 'username',
password: process.env.CONVERSATION_PASSWORD || 'pass',
version: 'v1',
version_date: '2016-07-11'
});
var workspace = process.env.WORKSPACE_ID || 'id';
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'fb_token') {
res.send(req.query['hub.challenge']);
}
res.send('Error : no token.');
});
app.post('/webhook/', function (req, res) {
var text = null;
messaging_events = req.body.entry[0].messaging;
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i];
sender = event.sender.id;
if (event.message && event.message.text) {
text = event.message.text;
}else if (event.postback && !text) {
text = event.postback.payload;
}else{
break;
}
var params = {
input: text,
// context: {"conversation_id": conversation_id}
context:contexid
}
var payload = {
workspace_id: "id"
};
if (params) {
if (params.input) {
params.input = params.input.replace("\n","");
payload.input = { "text": params.input };
}
if (params.context) {
payload.context = params.context;
}
}
callWatson(payload, sender);
}
res.sendStatus(200);
});
function callWatson(payload, sender) {
w_conversation.message(payload, function (err, convResults) {
console.log(convResults);
contexid = convResults.context;
if (err) {
return responseToRequest.send("Erro.");
}
if(convResults.context != null)
conversation_id = convResults.context.conversation_id;
if(convResults != null && convResults.output != null){
var i = 0;
while(i < convResults.output.text.length){
sendMessage(sender, convResults.output.text[i++]);
}
}
});
}
function sendMessage(sender, text_) {
text_ = text_.substring(0, 319);
messageData = { text: text_ };
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: {
recipient: { id: sender },
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
};
var token = "token";
var host = (process.env.VCAP_APP_HOST || 'localhost');
var port = (process.env.VCAP_APP_PORT || 3000);
app.listen(port, host);
如何从 Watson Assistant 将位置标识为上下文变量 ($location) 并将 ETA 从节点应用程序发送到 Watson Assistant 作为上下文变量? 我正在考虑使用操作在我的 Watson 对话节点中进行编程调用来执行此操作但无法实现。
convResults
将包含完整的上下文对象,包括您的对话框设置的上下文变量。