如何在实时Firebase数据库上存储DialogFlow的所有参数值?

时间:2019-01-18 15:15:24

标签: firebase firebase-realtime-database dialogflow google-home dialogflow-fulfillment

我正在为Google Home采取行动。这是关于餐厅预订的,所以我必须从以下用户那里获取一些信息:

  1. 您要预订几位客人?
  2. 哪个日期?
  3. 什么时候?

然后,我必须将其存储在Firebase实时数据库中。对于guests参数,它正在将值写入数据库,但日期和时间参数未显示在数据库上

与来宾一起在数据库中存储时间和日期的代码是什么?

'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
 
 //initialize db connection
 const admin = require('firebase-admin');
 admin.initializeApp();
 
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 fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}
  function createBooking(agent) {
      
    let guests = agent.parameters.guests;
    let time = new Date(agent.parameters.time);
    let date = new Date(agent.parameters.date);
    let bookingDate = new Date(date);
    bookingDate.setHours(time.getHours());
    bookingDate.setMinutes(time.getMinutes());
    let now = new Date();
      const guestsParam = agent.parameters.guests;
     
      if (guests < 1){
        agent.add('You need to reserve a table for at least one person. Please try again!');
    } else if (bookingDate < now){
        agent.add(`You can't make a reservation in the past. Please try again!`);
    } else if (bookingDate.getFullYear() > now.getFullYear()) {
        agent.add(`You can't make a reservation for ${bookingDate.getFullYear()} yet. Please choose a date in ${now.getFullYear()}.`);
    } else {
        let timezone = parseInt(agent.parameters.time.toString().slice(19,22));
        bookingDate.setHours(bookingDate.getHours() + timezone);
        agent.add(`You have successfully booked a table for ${guests} guests on ${bookingDate.toString().slice(0,21)}`);
        agent.add('See you at the restaurant!');
    }
     return admin.database().ref('/guests').push({guests: guests}).then((snapshot) => {
    console.log('database write successful: ' + snapshot.ref.toString());
  });
  
        
 }
    
 
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('restaurant.booking.create', createBooking);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

1 个答案:

答案 0 :(得分:0)

好吧,简单的答案是您没有在数据库中看到它们,因为您没有将它们放在那里。

admin.database().ref('/guests').push({guests: guests})

只需与来宾一起存储文档即可。您对bookingDate不做任何事情。