如何将REST请求的响应发送给dialogflow代理?我正在尝试将响应变量放在agent.add()下,但是它不起作用

时间:2018-09-16 18:13:02

标签: rest dialogflow

这是我编写的函数,控制台显示输出,但最终答案(在dialogflow代理中)仅打印第一条语句-“您很快就会在这里得到股票价格...”。我试图了解如何将该值发送回dialogflow代理,以打印出来作为对用户查询的响应。

函数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);

1 个答案:

答案 0 :(得分:1)

检查帖子here的答案。您需要像这样通过网络挂钩传递一个承诺

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