-1

So I figured out how the fulfillment connects with a real-time Firebase database. The issue I am having is structuring the data. I have a db with say a 100 collections that contain the following info per doc...

{
 brandName: "Coca cola",
 prodName: "Coke Zero",
 price: 1.50
}

Researching all the tutorials I have to use the following command to fulfill in dialogflow

function getFromFirebase(agent) {
    return firebaseAdmin.database().ref('prodName').once("value").then((snapshot) => {
      var prodName = snapshot.val();
    })

  }

The issue is, I want the prodName's value to match the entity in dialogflow, and then return the whole collection. Since I want the bot reply to be the price of the product. I am guessing I have to query the value first and then pull the collection, but can't find a good example of this. Any advice is greatly appreciated

mad
  • 11
  • 2

1 Answers1

0

I have an example where using an intent with a parameter

I created an intent called ‘GetFromFirebase’ with a training phrase ‘Price of’ and the parameter ‘prodname’ and enabled Webhook call for this intent.

In Firebase I structured the data as follows:

},
"Coke Zero" : {
  "brandName" : "Coca Cola",
  "price" : 1.5
},

Then in fulfillment Inline Editor I tried the next code:

//Function to get the product collection
 function getFromFirebase(agent){
   const prodname = agent.parameters.prodname;
    return admin.database().ref(prodname).once('value').then((snapshot) => {
    const dataprod = snapshot.val();
    const brandName = dataprod.brandName;
    const price = dataprod.price;

      if(dataprod!==null){
      agent.add('The price of ' + prodname + ' is ' + price );
      }
    });
  }


//Map the intent
 let intentMap = new Map();
  intentMap.set('GetFromFirebase', getFromFirebase);
  agent.handleRequest(intentMap);

With this implementation I tried in the “Try it now” box the phrase “Price of” and after that I entered a the product name “Coke Zero”to get all the product collection to the constants and the bot replied the next message:

{
  "fulfillmentText": "The price of Coke Zero is 1.5",
  "outputContexts": []
}
joserobertog
  • 109
  • 4
  • See the issue is I can't change the Firebase data structure. The product name is in the node like I have before. – mad Aug 09 '21 at 12:47
  • I tried to replicate your scenario, but I couldn't do the exercise with your data structure, it doesn't seem like it can be done like this, I recommend that you adjust the Firebase data structure as I suggested in the answer, that is because each node needs a parent for a search if you want to retrieve the entire collection by looking only for the product name – joserobertog Aug 14 '21 at 01:07
  • Have you ever done it with Firestore? I managed to get a connection but again when querying the data I am running in to some issues – mad Aug 15 '21 at 01:51
  • The original question of this post is about a real-time Firebase database, the answer that I posted is focused on that kind of database, then the connection that you mentioned is in Firebase right? If it’s Firestore I suggest you to edit the current question or post a new one to have a proper answer to the use case scenario. – joserobertog Aug 16 '21 at 23:40
  • Well I am trying both options. But with the RTDB did you every try .orderByChild() and .equarlTo() – mad Aug 18 '21 at 13:19
  • I tried .orderByChild () and .equarlTo (), but it doesn't works for your data structure, this because the Firebase realtime database is just a big Json, so the parent of each node must be configured correctly for your use case, so I recommend to change your data structure as is suggested in the answer. – joserobertog Aug 18 '21 at 15:41