0

So I've recently started working on building out some Firebase Cloud Functions for Dialogflow agent fulfillment. I implemented an onboarding function in Firebase project, but I get an error when attempting to interact with Firestore.

I've double-checked to ensure that my package.json and local dev environments are identical, and yet I continue to see this error.

Any help is appreciated. Doesn't seem to matter if I'm using the test Dialogflow console or Twilio, I get the same exception. Somehow there is a mishandling of promises. It's definitely happening with the promise returned by liceRef.get(), but I have no idea why.

'use strict';

const firefunc = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
const serviceAccount = require('./service_account.json');

process.env.DEBUG = 'dialogflow:debug'; 
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

const db = admin.firestore();
const settings = {timestampsInSnapshots: true};
db.settings(settings);

exports.dfWebhook = firefunc.https.onRequest(async (request, response) => {

  const agent = new WebhookClient({ request, response });

  var uid;
  if(agent.requestSource === 'twilio') {
    uid = request.body.originalDetectIntentRequest.payload.data.From;
  } else {
    uid = 'TESTER';
  }
  console.log("USER ID : ", uid);

  async function onboarding(agent) {
    const { name, email, license } = agent.parameters;

    console.log("-----STARTING ONBOARDING FUNCTION-----");

    try {
        const liceRef = db.collection('license').doc(license);
        const getLice = await liceRef.get(); 

        if(!getLice.exists) {
            agent.add("I'm sorry, this license does not exist. blah blah");
        } else {
            if (getLice.data().uid !== uid && getLice.data().uid) {
                agent.add("This license has already been activated for another user etc...");
            } else {
                await liceRef.set({
                    name: name,
                    email: email,
                    uid: uid
                });
                agent.add("blah blah success message");
            }
        }
    } catch (err) {
        console.error(err);
        agent.add("I'm sorry, an error occured and I wasn't able to activate your license. Please try again later or contact support.");
    }
  }

... INTENT HANDLING CODE ETC ...
}

Without fail I get this error after my log statements:

8:11:13.748 AM - dfWebhook - Unhandled rejection
8:11:13.748 AM - dfWebhook - Error: No responses defined for platform: twilio
    at V2Agent.sendResponses_ (/srv/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13)
    at WebhookClient.send_ (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17)
    at promise.then (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:38)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

The only thing I can think of is that there must be some discrepancy between the Firebase configurations, perhaps Enterprise Google Cloud/Firebase is different than Personal? Honestly, I'm at a loss. I'm either missing something insanely obvious, or there's something else going on, but it seems like this code should work.

Thanks in advance for the help.

0 Answers0