这是我写的函数,控制台显示输出,但是最后的答案(在对话框代理中)只打印第一条语句--‘你很快就会得到你的股票价格……’。我试图了解如何将此值发送回对话框流代理,以作为对用户查询的响应。
函数getStockprice(代理){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);
发布于 2018-09-16 19:09:53
检查post here的答案。你需要从网钩上传递一个承诺,就像
function dialogflowHanlderWithRequest(agent) {
return new Promise((resolve, reject) => {
request.get(options, (error, response, body) => {
JSON.parse(body)
// processing code
agent.add(...)
resolve();
});
});
};https://stackoverflow.com/questions/52357076
复制相似问题