【问题标题】:Dialogflow v2 use Firebase Cloud Function to return Firestore database dataDialogflow v2 使用 Firebase Cloud Function 返回 Firestore 数据库数据
【发布时间】:2019-02-28 12:08:17
【问题描述】:

我对 DialogFlow 完全陌生.. 我想创建一个聊天机器人,我可以向它提问,它会以从我的 Firebase Firestore 数据库中检索到的值进行响应。

我已经创建了必要的意图 (GetPopulationInCity) 并选择了Enable webhook call for this intent

我最好将 DialogFlow Fulfillment 与我的其他 CloudFunction 应用一起使用。

我使用了以下示例中的代码:

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

function GetPopulationInCity(agent) {
//Search Firestore for value, if found =>
    agent.add(`There are 10,000 people living in XXX`); //should it be ask or something like send or return?
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Get Population', GetPopulationInCity);
  intentMap.set('Default Fallback Intent', fallback);
  agent.handleRequest(intentMap);
});

但我不知道如何为我的意图创建一个处理程序并返回一个值。有没有人可以帮帮我?

【问题讨论】:

    标签: node.js firebase google-cloud-functions dialogflow-es


    【解决方案1】:

    首先,确保您尝试为其编写处理程序的 Intent 的名称与 intentMap.set() 部分中的名称匹配。 (在您的描述中,您不清楚对话流中的意图名称与函数名称是什么。)

    处理程序本身需要做一些事情:

    1. 获取可能在 Intent 中设置的任何参数的值。

      你可以从agent.parameters得到这个。

    2. 查询数据库中的值。

    3. 返回一个 Promise,表示正在异步处理结果。
    4. 作为结果处理的一部分,使用结果调用agent.add()

      这些将使用类似于您现在进行 Firebase 调用的代码来完成。您尚未展示您是如何执行此操作的,或者您的数据库的结构是什么样的,但假设您使用的是 Firebase 管理库,您的 webhook 处理程序可能如下所示:

      function GetPopulationInCity( agent ){
        var cityName = agent.parameters.cityName;
        return db.collection('cities').doc(cityName).get()
          .then( doc => {
            var value = doc.data();
            agent.add(`The population of ${cityName} is ${value.population}.`);
          });
      }
      

    最后,顺便说一句,您的代码提出了使用add()ask() 的问题。

    • 您正在使用 Dialogflow 实现库,该库(按照惯例)将发送到处理程序的参数命名为“代理”。向响应添加消息类型的函数是agent.add()

    • 还有另一个库,actions-on-google 库,它与 dialogflow-fulfillment 库的工作方式类似。 a-o-g 库的约定是传递带有会话信息的“conv”参数。向代理添加响应的函数是conv.ask()conv.close()

    • 为了增加一些混淆,如果您正在使用 Actions(而不是 Dialogflow 工作的其他代理之一与)。

    【讨论】:

      【解决方案2】:

      我建议看看 Actions on Google 提供的这个操作示例:https://github.com/actions-on-google/dialogflow-updates-nodejs

      按照自述文件中的步骤操作,或者只查看 functions/index.js 文件,您将了解该示例如何处理 Firestore。

      【讨论】:

        猜你喜欢
        • 2019-07-05
        • 2019-01-05
        • 2021-06-19
        • 2020-07-02
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 2019-08-07
        • 1970-01-01
        相关资源
        最近更新 更多