简体   繁体   English

如何使用DialogFlow在线编辑器从Firebase检索数据

[英]How to retrieve data from Firebase using DialogFlow Inline Editor

I am pretty new to DialogFlow. 我是DialogFlow的新手。 I am wondering how can I retrieve data from Firebase through the Inline Editor of DialogFlow. 我想知道如何通过DialogFlow的内联编辑器从Firebase检索数据。 Hope you can help me! 希望你能帮我!

Thats how you can communicate with firebase from dialogflow 那就是您如何从dialogflow与firebase通信

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

Initialize Firebase Admin SDK. 初始化Firebase Admin SDK。

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

Interaction with firebase Collection users in fulfillment function 在实现功能中与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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM