【问题标题】:Dialogflow Intent对话流意图
【发布时间】: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


【解决方案1】:

有几种方法可以实现这一点。

  1. 这里是示例how you can declare global variable in Node JS
  2. 如果您不使用 Dialogflow 的内联编辑器,那么最好使用 Redis 数据库,它真的很快。您可以根据您的请求逐个会话管理它。当followTwo intent webhook 触发时,您可以从 Redis 获取数据并存储在上下文中,并清空 Redis 以进行其他查询。因为 Redis 是内存数据库,存在内存限制。
  3. 使用任何其他数据库来存储请求数据。

尽量不要使用全局变量。改用任何数据库。

【讨论】:

  • 我已经使用 Redis 来保存响应,但我没有收到响应。我在 getAPIData 函数中插入了以下代码。我在 redisClient.set(sessionKey, dfRes); 之后放置了 console.log("Fun 0"); sessionKey = 'rsessionid'; redisClient.set(sessionKey, dfRes);以下代码在 followTwo 函数中,但在 DialogFlow 的模拟器中没有显示响应。我在 redisClient.get 之后插入了 console.log("Fun 2")。 sessionKey = 'rsessionid';redisClient.get(sessionKey, function (er, reply) {agent.add(reply); });在 Logging 中,我收到了“Fun 2”然后是“Fun 0”。
【解决方案2】:

您可以使用输出上下文将变量意图传递给后续意图,

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

这个输出上下文作为输入上下文传递给后续意图

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多