【发布时间】:2020-09-28 18:18:55
【问题描述】:
我在 Google 对话流中进行 API 调用时遇到问题。我想在第一个意图 (getAPIData) 中调用 API,然后同时调用第二个意图 (followOne) 和第三个意图 (followTwo)。 (不想等待完成 API 调用)。在第三个意图中,我想要我们在第一个意图中调用的 API 调用的结果,并将 API 响应添加到对话流代理。请查看代码以获取更多详细信息,并让我知道问题所在。 API 响应时间超过 11 秒。
const {WebhookClient} = require('dialogflow-fulfillment');
const axios = require('axios');
const {Card, Suggestion, List, BrowseCarousel, BrowseCarouselItem} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug';
exports.GetAPIDataHandle = functions.https.onRequest((request, response) => {
let dialogflowResponse = '';
const agent = new WebhookClient({ request, response });
async function getAPIData(){
axios.get('https://apiurl.com')
.then((resp) => {
dialogflowResponse = resp.data.data;
})
doTimeOut(3.5);
agent.setFollowupEvent('followUpOne');
}
function followOne(agent){
doTimeOut(4);
agent.setFollowupEvent('followUpTwo');
}
function followTwo(agent){
doTimeOut(4);
if(dialogflowResponse != ''){
agent.add('Here is the search result.');
dialogflowResponse.map(result => {
agent.add(result.word);
})
}else{
agent.add(`Please try again later`);
}
}
let intentMap = new Map();
intentMap.set('getFunnyWords', getAPIData);
intentMap.set('followupeventone', followOne);
intentMap.set('followupeventtwo', followTwo);
agent.handleRequest(intentMap);
});
function doTimeOut(numsec){
var dt1 = new Date();
dt1.setSeconds( dt1.getSeconds() + numsec);
do{
}while((new Date()).getSeconds() < dt1.getSeconds());
}
根据当前代码,我们得到全局变量“dialogflowResponse”在第三个意图中未定义。
【问题讨论】:
-
您是否启用了计费功能?由于外部 API 调用需要在 firebase 中启用计费
-
是的,我们已经启用了计费功能。
标签: node.js dialogflow-es actions-on-google