gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:54:27 24 4
gpt4 key购买 nike

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

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

最佳答案

检查帖子的答案here .您需要像这样从 webhook 传递一个 Promise

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

关于rest - 如何将 REST 请求的响应发送到 dialogflow 代理?我试图将响应变量放在 agent.add() 下,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52357076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com