簡體   English   中英

對話流意圖

[英]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”在第三個意圖中未定義。

有幾種方法可以實現這一點。

  1. 所以這里是如何在 Node JS 中聲明全局變量的示例。
  2. 如果您不使用 Dialogflow 的內聯編輯器,那么最好使用 Redis 數據庫,它真的很快。 您可以針對您的請求逐個會話管理它。 followTwo Intent webhook 觸發時,您可以從 Redis 獲取數據並存儲在上下文中,並清空 Redis 以進行其他查詢。 因為 Redis 是內存數據庫,有內存限制。
  3. 使用任何其他數據庫來存儲請求數據。

盡量不要使用全局變量。 改用任何數據庫。

您可以使用輸出上下文將變量意圖傳遞給后續意圖,

 agent.context.set({ "name": 'test-followup', "lifespan": 5, "parameters": {"name": value}});

此輸出上下文作為輸入上下文傳遞給后續意圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM