agent.add无法正常工作,但console.log可正常工作

时间:2019-10-18 11:16:10

标签: google-cloud-firestore dialogflow actions-on-google dialogflow-fulfillment

在下面的代码中,“ agent.add”不起作用,但“ console.log”起作用。我用决心和拒绝增加了诺言,但仍然行不通。我尝试了不同的方法,但是来自Firestore的多个响应无法将其发送给用户。可以在firebase中查看日志,但不能在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);
          });
      });
    }

1 个答案:

答案 0 :(得分:0)

问题已解决。在最后一个agent.add中添加了return语句,它正在工作。谢谢。

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