[英]Dialogflow Intent
在 google dialogflow 中進行 API 調用時遇到問題。 我想在第一個意圖 (getAPIData) 中調用 API,然后同時調用第二個意圖 (followOne) 和第三個意圖 (followTwo)。 (不想等待API調用完成)。 在第三個意圖中,我想要我們在第一個意圖中調用的 API 調用的結果,並將 API 響應添加到 dialogflow 代理。 請查看代碼以獲取更多詳細信息,並讓我知道問題所在。 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”在第三個意圖中未定義。
有幾種方法可以實現這一點。
followTwo
Intent webhook 觸發時,您可以從 Redis 獲取數據並存儲在上下文中,並清空 Redis 以進行其他查詢。 因為 Redis 是內存數據庫,有內存限制。盡量不要使用全局變量。 改用任何數據庫。
您可以使用輸出上下文將變量意圖傳遞給后續意圖,
agent.context.set({ "name": 'test-followup', "lifespan": 5, "parameters": {"name": value}});
此輸出上下文作為輸入上下文傳遞給后續意圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.