3

In below code, "agent.add" is not working but "console.log" is working. I have added promise with resolve and reject, but still its not working. I have tried different ways but multiple response from firestore, not able to send it to user. Able to see logs in firebase but not in dialogflow.

    const {Firestore} = require('@google-cloud/firestore');
    const functions = require('firebase-functions');
    const {WebhookClient} = require('dialogflow-fulfillment');
    const {Card, Suggestion} = require('dialogflow-fulfillment');
    const admin = require('firebase-admin');

    admin.initializeApp();

    process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging 
     statements
        const firestore = new Firestore();
        const settings = {/* your settings... */ 
                          timestampsInSnapshots: true};
    firestore.settings(settings);

    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));
     const db = admin.firestore();

    ```
    function welcome(agent) {
      agent.add(`Welcome to my agent!`);  // not working
      console.log("Welcome Agent");
      const num = agent.parameters.number;

      let usersRef = db.collection('add');
      let query = usersRef.where('Number', '==', num);
      return new Promise((resolve,reject)=>{   // not working
        return query.get()
          .then(querySnapshot => {
            if (querySnapshot.empty) {/*
            const timestamp = querySnapshot.get('created_at');
            const date = timestamp.toDate();*/
              console.log('No matching documents.');
              agent.add(`No Matching Documents`);
              return;
            }
            querySnapshot.forEach(doc => {
              const line1 = doc.get('Line1');
              const line2 = doc.get('Line2');
              const explain = doc.get('explanation');
              console.log('Line1: ', line1);  //this works
              console.log('Line2: ', line2);  //this works
              console.log('explain: ', explain); //this works
              agent.add(`your response is ` +doc.get('Line1')); //not working
              agent.add(`Final Response -  ${line2}`); //not working
              agent.add(doc.get('explanation')); //not working
            });
            resolve('resolved');
          })
          .catch(err => {
            console.log('Error getting documents', err);
            reject(err);
          });
      });
    }

Dinesh BR
  • 31
  • 4
  • When you say "not working", how are you testing it to see if it works? Are you getting errors somewhere? If this is being done through the simulator, can you update your question to show what the simulator shows? What library are you using (or show the rest of the code to show the library being loaded and how you register the intent handler)? – Prisoner Oct 18 '19 at 23:15
  • @Prisoner, I am testing it in dialogflow getting "Default Response" as Not Available but in firebase console logs am able to see logs getting populated correctly. able to see welcome intent is getting triggered. Added the code.. – Dinesh BR Oct 20 '19 at 02:55
  • Issue is resolved now. Added return statement in the last agent.add and it is working. Thanks. agent.add(`your response is ` +doc.get('Line1')); agent.add(`Final Response - ${line2}`); return agent.add(doc.get('explanation')); – Dinesh BR Oct 22 '19 at 11:40
  • I'm trying to analyze the execution of my fulfillment but `console.log`s don't show up in any logs. How do I go about that? – Csaba Toth Mar 07 '20 at 05:43

1 Answers1

0

Issue is resolved now. Added return statement in the last agent.add and it is working. Thanks.

agent.add(your response is +doc.get('Line1')); 
agent.add(Final Response - ${line2}); 
return agent.add(doc.get('explanation')); 
Dinesh BR
  • 31
  • 4