【问题标题】:How to retrieve data from Firebase using DialogFlow Inline Editor如何使用 DialogFlow 内联编辑器从 Firebase 检索数据
【发布时间】:2019-02-10 12:53:29
【问题描述】:

我对 DialogFlow 很陌生。我想知道如何通过 DialogFlow 的内联编辑器从 Firebase 检索数据。希望你能帮助我!

【问题讨论】:

  • 一般来说 - 你会像对任何其他 Firebase Cloud Function 那样做。如果您遇到问题,更新您的问题以显示您尝试过的代码以及遇到的错误,这会让我们为您提供更好的帮助。

标签: javascript actions-on-google dialogflow-es


【解决方案1】:

这就是你如何通过对话流与 Firebase 进行通信

const functions = require('firebase-functions');
const firebaseAdmin = require('firebase-admin');
const DialogflowApp = require('actions-on-google').DialogflowApp;

初始化 Firebase Admin SDK。

firebaseAdmin.initializeApp(functions.config().firebase); 

在履行功能中与 firebase Collection users 的交互

let userId = app.getUser().userId;
admin.firestore().collection('users').where('userId', '==', userId).limit(1).get()
.then(snapshot => {
      let user = snapshot.docs[0]
        if (!user) {
          // If user is not in DB, its their first time, Welcome them!
          app.ask('Welcome to my app for the first time!');
          // Add the user to DB
          firebaseAdmin.firestore().collection('users').add({
            userId: userId
          }).then(ref => {
              console.log('Added document with ID: ', ref.id);
          });
        } else {
          // User in DB
          app.ask('Welcome back!')
        }    
      });
  }

  // Map function hanlder to Dialogflow's welcome intent action 'input.welcome'
  const actionMap = new Map('input.welcome', start)
  app.handleRequest(actionMap);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多