我正在使用Dialogflow构建聊天机器人,并且正在使用Axios进行发布并获取请求,但有时HTTP调用中的agent.add()无法正常工作。
我附上示例代码。
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
// Using some external libraries
//axios for using third party api hosted as http.
const axios = require('axios');
// xml2js for parsing xml output in response
const xml2js = require('xml2js');
const json = require('json');
// Accessing firebase admin
const admin = require('firebase-admin');
function name(agent){
return axios.post('http://some-api,
{"name": "Kyle"}).then((result) => {
console.log('Store datato api');
agent.add('What is your email id'); // Sometimes its working and sometimes its not
//return Promise.resolve(agent); // I tried this as well but the same issue.
});
}
可能是适当的更改,因为我在研究Stackoverflow上的所有其他问题时,我也尝试返回pomise,但没有用。
答案 0 :(得分:1)
您似乎没有正确构造Promise。这是我用来返回需要承诺的响应的格式:
function name(agent) {
const promise = new Promise(resolve => {
// logic goes here
resolve(agent.add(output));
});
return promise;
}
如果这不起作用,请检查其他地方是否可能失败-您确定您的网络挂钩在POST请求中没有失败吗?