1

I've written into an array from input data on Dialogflow using Firestore, like this.

But I don’t know how to fetch data from Firestore to display in Dialogflow, and there's my programming below it.

Here is what I want to achieve. When I input number in Dialogflow, I would like to get an array of number from the Firestore instance.

const db = admin.firestore();
db.settings({timestampsInSnapshots: true}); 
.
.
.
const sessionId = request.body.session.split("/").reverse()[0]; // or agent.session

  function read(agent){
    const number = agent.parameters.number;  // when I input the number in Fialogflow
    const docRef = db.collection('orders').doc(sessionId);

    return docRef.get()
      .then(doc => {
        if (!doc.exists) {
          agent.add('No data found in the database!');
          console.log(doc);
        } else {
          agent.add(doc.data().orders);
        }
        return Promise.resolve('Read complete');
      }).catch(() => {
        agent.add('Error reading entry from the Firestore database.');
        agent.add('Please add a entry to the database first by saying, "Write <your phrase> to the database"');
      });
  }

  function writeOrderToDb (newOrder) {
    const docRef = db.collection('orders').doc(sessionId);

    return db.runTransaction(t => {
      return t.get(docRef)
      .then(doc => {
        t.update(docRef, {
          orders: admin.firestore.FieldValue.arrayUnion(newOrder)
        }, {merge: true});
        /*if (!doc.data()) {
          t.set(docRef, { orders: [newOrder] });
        }
        else {
          t.update(docRef, {
            orders: admin.firestore.FieldValue.arrayUnion(newOrder)
          });
        }*/
      });
    }).catch(err => {
      console.log(`Error writing to Firestore: ${err}`);
    });
  }


  function confirmOrder(agent) {
    const order = agent.context.get('order'),
          amount = order.parameters.amount,
          size = order.parameters.size,
          type = order.parameters.type;

    agent.add(`Confirming ${amount} ${type} in ${size}`);

    // important to return, otherwise console.logs will not appear and non-deterministic behavior will ocurr
    return writeOrderToDb({
      "type": type,
      "size": size,
      "amount": amount
    });
  }
李宇澄
  • 53
  • 1
  • 2
  • 7
  • I think here [1] you will be able to find a good example about connecting dialog flow with Firestore. [1] https://stackoverflow.com/questions/51692357/how-to-connect-dialogflow-to-cloud-firestore-via-the-inline-editor-in-dialogflow/51715153 – Oqueli A. Martinez Nov 15 '19 at 01:12
  • I know what you mean. Although we fetch data on Dialogflow from using Firestore, I would like to fetch "sessionId" of every array. For example, I input number one on Dialogflow, and then I will get value of number one array. – 李宇澄 Nov 15 '19 at 08:33

1 Answers1

0

I have created the same data model as yours:

enter image description here

So with the following code, you can pass any number within the range of your array "orders" into the variable my_num. It will return the data inside that part of the array.

var my_num = 1;

db.collection('dial').doc('dial1').get().then(function(doc) {
  console.log(doc.data().orders[my_num]);
});

Output:

enter image description here

Waelmas
  • 1,894
  • 1
  • 9
  • 19
  • I did the same Firestore structure as yours. However, it showed mistake up when I inputted one number in Dialogflow. "webhookStatus": { "code": 14, "message": "Webhook call failed. Error: UNAVAILABLE." } – 李宇澄 Jan 09 '20 at 07:38
  • This is related to Dialogflow only. Make sure you are properly setting up your webhook. You can follow this link https://cloud.google.com/dialogflow/docs/fulfillment-overview – Waelmas Jan 09 '20 at 08:24
  • I'm developing one Chatbot with using Dialogflow. Because I could put the data into Firestore, so I'd like to get data from it. – 李宇澄 Jan 09 '20 at 17:17