gpt4 book ai didi

node.js - Dialogflow NodeJs Fulfillment V2 - Webhook 方法调用在完成回调之前结束

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:47 25 4
gpt4 key购买 nike

我正在使用 dialogflow-fulfillment-nodejs 开发 Dialogflow webhook 客户端查找城市的温度。在使用服务获取城市温度时,一切正常,并且也会生成正确的响应,但即使调用正确的方法,响应也不会发送给用户。

Here is the issue in the Dialogflow GitHub repo

代码

function getTemp(agent) {
const timeStart = new Date().getTime();
console.info(`temp function called`);
// agent.add(`Temperature in New Delhi is 50 degree Celsius!`); // This works
serviceRequest.getTemp("New Delhi", function(resp){
if (resp['status'] === 'Y') {
// success
const msg = `Temperature in New Delhi is ${resp['temp']} Celsius!`;
console.log(`Speech Response -- ${msg}`);
console.log(`Type of msg -> ${typeof msg}`);
agent.add(msg);
} else {
// failure
agent.add(`There was some error with the backend. Please try again later.`);
}
const timeEnds = new Date().getTime();
console.log("\nComplete 'getTemp' process took " + (timeEnds - timeStart) + " milliseconds.")
});
console.log("------ temperature function ends here ------");
}



'getTemp': function (city, callback) {
let respBack = {};
doTimeOutTest(function(resp){
respBack['temp'] = resp;
respBack['status'] = 'Y';
callback(respBack);
});

}


function doTimeOutTest(callback){
// below commented code takes < 10 ms to execute, but does not send any response back to dialogflow as call ends before it is executed
// setTimeout(function() {
// callback("30 degree");
// }, 1);

// Below code works even when it takes more time to execute
for(let i=0; i<10000; i++){
for(let j=0; j<10000; j++){
//
}
}
callback("30 degree");
}

控制台日志

注释代码运行时

>>>>>>> S E R V E R   H I T <<<<<<<

temp function called
------ temperature function ends here ------
Speech Response -- Temperature in New Delhi is 30 degree Celsius!
Type of msg -> string

Complete 'getTemp' process took 10 milliseconds.


当未注释的代码运行时

>>>>>>> S E R V E R   H I T <<<<<<<

temp function called
Speech Response -- Temperature in New Delhi is 30 degree Celsius!
Type of msg -> string

Complete 'getTemp' process took 77 milliseconds.
------ temperature function ends here ------

NodeJS Dialogflow src 代码链接 - https://github.com/dialogflow/dialogflow-fulfillment-nodejs/blob/master/src/dialogflow-fulfillment.js

最佳答案

您需要在处理函数中返回一个 promise 。函数处理程序现在支持 Promise,因此您可以返回 Promise 并处理 Promise 中的 http 请求等内容。以下是使用请求库的示例:

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

您还可以将 HTTP 调用移至另一个返回 Promise 的函数。这是 axios 库的示例:

function dialogflowHandlerWithAxios(agent) {
return callApi('www.google.com').then(response => {
agent.add('My response');
}).catch (error => {
// do something
})
};

function callApi(url) {
return axios.get(url);
}

关于node.js - Dialogflow NodeJs Fulfillment V2 - Webhook 方法调用在完成回调之前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51209035/

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