如何将来自 REST 请求的响应发送到对话流代理?我正在尝试将响应变量放在agent.add()下,但它不起作用



这是我写的功能,控制台显示输出,但最终答案(在对话流代理中(只打印第一条语句 - "你很快就会在这里得到你的股票价格......"。我试图了解如何将此值发送回对话流代理以打印为对用户查询的响应。

函数 getStockprice(agent( { agent.add('嗨,你很快就会在这里看到股票价格!只有上帝知道怎么做!

const companyName = agent.parameters['company_name'];
const priceType = agent.parameters['price_type'];
const date = agent.parameters['date'];
console.log("Company Name is: "+ companyName);
console.log("Price Type is: "+ priceType);
console.log("Date is: " + date);
var tickerMap = {
"Apple" : "AAPL",
"Microsoft" : "MSFT",
"IBM" : "IBM",
"Google" : "GOOG",
"Facebook" : "FB",
"Amazon" : "AMZN" 
};
var priceMap = {
"opening" : "open_price",
"closing" : "close_price",
"maximum" : "high_price",
"minimum" : "low_price"
};
var stockPriceTicker = tickerMap[companyName];
var priceTypeCode = priceMap[priceType];
var pathString = "/historical_data?ticker="+stockPriceTicker+"&item="+priceTypeCode;
console.log("Path String" + pathString);
var username = "#";
var password = "#";
var auth = "Basic " + new Buffer(username + ":" + password).toString('base64');
console.log('Authorizarion: ' + auth);
var request = https.get({
host : "api.intrinio.com",
path : pathString,
headers : {
"Authorization" : auth
}
}, function(response) {
var json = "";
response.on('data', function(chunk){
console.log("received response: " + chunk);
json += chunk;
});
response.on('end', function () {
var jsonData = JSON.parse(json);
var stockPrice = jsonData.data[0].value;
console.log("The stock price received is: "+ stockPrice);
var chat = "The" + priceType + "price for" + companyName + "on"
+ date + "was" + stockPrice;
agent.add('Here is your information. ${chat}');
});
});   
agent.add('Here is your information.');
}

intentMap.set('GetStockPrice', getStockprice(;

在这里查看帖子的答案。您需要从网络钩子传递一个承诺,例如

function dialogflowHanlderWithRequest(agent) {
return new Promise((resolve, reject) => {
request.get(options, (error, response, body) => {
JSON.parse(body)
// processing code
agent.add(...)
resolve();
});
});
};

相关内容

最新更新