问题描述
请参阅此处发布的解决方案()
我要实现的是,Web挂接应该等待,直到我从api调用获得响应为止.P.S:API正在起作用,只是机器人不等待响应到来.任何帮助将不胜感激.谢谢
Referring to the solution posted here (DialogFlow V2 Webhook - Expects Speech responses Immediately and not after async requests)
What I want to achieve is that the web hook should wait until I get a response from my api call.P.S: The API is working, its just that the bot doesn't wait for the response to come.Any help would be greatly appreciated. Thanks
const rp = require('request-promise');
function convert(params){
return rp('https://data.fixer.io/api/convert?access_key=[my key]&from='+
params['currency-from']+'&to='+params['currency-to']+'&amount='+params.amount)
.then((data) => {
let responseData = JSON.parse(data);
let message = responseData.result;
console.log('Success');
return Promise.resolve(message);
}).catch((err)=> {
return Promise.reject(err);
});
}
function currencyConversion(agent) {
let params = request.body.result.parameters;
return convert(params)
.then((message)=> {
agent.add(`${params.amount} ${params['currency-from']} is ${message} ${params['currency-to']}`);
return Promise.resolve()
})
.catch((err) => {
console.log(err);
agent.add("Uh oh, something happened.");
return Promise.resolve();
})
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('currency.convert', currencyConversion);
推荐答案
您没有说明要在哪个环境中运行,但是鉴于您的代码以及我上面概述的agent.parameters
更改,我能够复制您的将Firebase Cloud Functions与node.js 6.14结合使用时出现问题.
You didn't state what environment you were running in, but given your code, and the agent.parameters
change I outlined above, I was able to duplicate your problem using Firebase Cloud Functions with node.js 6.14.
我可以通过使用request-promise-native
软件包而不是request-promise
使其工作.顾名思义,它使用本机Promises而不是Bluebird Promise软件包,但除此之外,您进行的调用是相同的.
I was able to get it to work by using the request-promise-native
package instead of request-promise
. As the name suggests, this uses native Promises instead of the Bluebird Promise package, but otherwise the calls you're making are identical.
这篇关于在DialogFlow WebHook中使用异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!