简体   繁体   English

如何通过Dialogflow中的内联编辑器将Dialogflow连接到Cloud Firestore?

[英]How to connect Dialogflow to Cloud Firestore via the Inline Editor in Dialogflow?

I have a Cloud Firestore database that stores the number of inhabitants of all cities in England in 2017. 我有一个Cloud Firestore数据库,可存储2017年英格兰所有城市的居民人数。

Then I have a Dialogflow. 然后我有一个Dialogflow。 Whenever I tell the name of a city to Dialogflow, I want it to get the number of inhabitants in that city from Firestore and return it to Dialogflow. 每当我将一个城市的名称告诉Dialogflow时,我希望它从Firestore获取该城市的居民数量并将其返回给Dialogflow。

Specifically, I want to implement this via the Inline Editor. 具体来说,我想通过内联编辑器实现这一点。

Question : What lines of code do I need to add to the code below in order to make this happen? 问题 :我需要在下面的代码中添加哪些代码才能实现此目的?

So here is the code that I write in the Inline Editor in Dialogflow > Fulfillment > index.js: 所以这是我在Dialogflow> Fulfillment> index.js中的内联编辑器中编写的代码:

'use strict';

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

const App = require('actions-on-google').DialogflowApp;

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(`Hello and welcome!`);
  }

  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('Default Fallback Intent', fallback);

  agent.handleRequest(intentMap);
});

Here is some sample code showing how to connect Firebase's Firestore database to Dialogflow fulfillment hosting on Firebase functions: 以下是一些示例代码,演示如何将Firebase的Firestore数据库连接到Firebase函数上的Dialogflow履行主机:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function writeToDb (agent) {
    // Get parameter from Dialogflow with the string to add to the database
    const databaseEntry = agent.parameters.databaseEntry;

    // Get the database collection 'dialogflow' and document 'agent' and store
    // the document  {entry: "<value of database entry>"} in the 'agent' document
    const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
    return db.runTransaction(t => {
      t.set(dialogflowAgentRef, {entry: databaseEntry});
      return Promise.resolve('Write complete');
    }).then(doc => {
      agent.add(`Wrote "${databaseEntry}" to the Firestore database.`);
    }).catch(err => {
      console.log(`Error writing to Firestore: ${err}`);
      agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
    });
  }

  function readFromDb (agent) {
    // Get the database collection 'dialogflow' and document 'agent'
    const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

    // Get the value of 'entry' in the document and send it to the user
    return dialogflowAgentDoc.get()
      .then(doc => {
        if (!doc.exists) {
          agent.add('No data found in the database!');
        } else {
          agent.add(doc.data().entry);
        }
        return Promise.resolve('Read complete');
      }).catch(() => {
        agent.add('Error reading entry from the Firestore database.');
        agent.add('Please add a entry to the database first by saying, "Write <your phrase> to the database"');
      });
  }

  // Map from Dialogflow intent names to functions to be run when the intent is matched
  let intentMap = new Map();
  intentMap.set('ReadFromFirestore', readFromDb);
  intentMap.set('WriteToFirestore', writeToDb);
  agent.handleRequest(intentMap);
});

This came from Dialogflow's Firestore sample located here: https://github.com/dialogflow/fulfillment-firestore-nodejs 这来自Dialogflow的Firestore示例: https//github.com/dialogflow/fulfillment-firestore-nodejs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dialogflow - 来自 Cloud Firestore 的内联 webhook - Dialogflow - inline webhook from Cloud Firestore 如何在嵌入式编辑器dialogflow中发出发布请求? - how to make post request in inline editor dialogflow? 如何在 Google Dialogflow Fullfilment Inline Editor 中安装 npm 包 - How to install npm packages within Google Dialogflow Fullfilment Inline Editor 是否可以在dialogflow内联编辑器中使用外部库? - Is it possible to use external libraries in the dialogflow inline editor? 如何使用 dialogflow 内联编辑器 node.js 创建 facebook messeger DYNAMIC 轮播卡? - How to create a facebook messeger DYNAMIC carausel card using dialogflow inline editor node.js? 如何从参数获取实体并在Dialogflow内联编辑器中创建if条件以实现 - How to get entity from the argument and create if condition in Dialogflow Inline editor for fulfilment 如何在dialogflow的内联编辑器中进行第三方api调用 - How to make 3rd party api call in dialogflow's inline editor 无法在 Dialogflow Fulfillment(内嵌编辑器)中从 Firestore 获取数据 - Cannot bring data from Firestore in Dialogflow Fulfillment (In-line Editor) 您可以在dialogflow内联编辑器中实现两个不同的实现吗? - Can you have two different fulfillment's in the dialogflow inline editor? 我可以在 Inline Fulfillment Dialogflow 编辑器中跨对话存储计数器吗? - Can I store a counter across conversation in Inline Fulfillment Dialogflow editor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM