0

Below is my fulfilment inline code

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

// initialise DB connection
const admin = require('firebase-admin');
admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: 'ws://rochechatbot.firebaseio.com/',
});

process.env.DEBUG = 'dialogflow:debug';

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 handleData(agent) {
    const Product_name = agent.parameters.Product_name;
    agent.add('Hello');

    return admin.database().ref('rochechatbot').once("value").then((snapshot) => {
      var StockData = snapshot.child("Stock").val();
      agent.add(StockData);
    });
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('StockLevelEnquiry', handleData);
  agent.handleRequest(intentMap);
});

Below is my firebase DB

enter image description here

Below is my dialogflow, webhook is enabled

enter image description here

Wonder why even 'hello" is not being returned? how to read the data from the firebase?

Fire-In-D-Hole
  • 270
  • 2
  • 13
  • I'm curious to know where you got the idea to use `databaseURL: 'ws://rochechatbot.firebaseio.com/'` in your admin config? Normally you call initializeAll with no parameters. – Doug Stevenson Feb 09 '19 at 16:40
  • i learned it from https://www.youtube.com/watch?v=xWpHVbMnBXI&t=103s ; what is the expected code i need to adjust to make the initialisation work? – Mike Kwok Feb 10 '19 at 03:38

0 Answers0