dialogflow与firebase实时数据库交互

时间:2017-10-18 10:47:18

标签: firebase firebase-realtime-database dialogflow

是否可以使用dialogflow从firebase数据库中获取一些数据?我是对话流的新手,所以我还在做一些研究。

例如,我想询问我的聊天机器人是否有医生,然后聊天机器人将访问firebase数据库以检查该特定医生是否可用,或者让我说安排我使用doc X进行预约,因此对话框流程将执行允许的功能将输入数据库的计划对象

感谢。

1 个答案:

答案 0 :(得分:2)

您可以使用Firebase功能来完成Dialogflow代理和Firestore数据库以存储数据。使用Dialogflow的Google智能助理集成的一个示例如下:

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

// Initialize Firebase Admin SDK.
firebaseAdmin.initializeApp(functions.config().firebase); 

exports.dialogflowFulfillment = functions.https.onRequest((req, res) => {
  // Log headers and body
  console.log('Request headers: ' + JSON.stringify(req.headers));
  console.log('Request body: ' + JSON.stringify(req.body));

  // Create a new Dialgoflow app request handler
  let app = new DialogflowApp({request: req, response: res});

  // welcome function handler
  function start(app) {
      // Get user ID from the Google Assistant through Action on Google
      let userId = app.getUser().userId;
      // Check if the user is in our DB
      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);
});