dialogflow / dialogflow-fulfillment-nodejs

Dialogflow agent fulfillment library supporting v1&v2, 8 platforms, and text, card, image, suggestion, custom responses
Apache License 2.0
597 stars 281 forks source link

agent.add() Not showing inside .collection().doc().get() #299

Open EugeniaCasFrag opened 4 years ago

EugeniaCasFrag commented 4 years ago

I have the following code:

function welcome(agent){ console.log(voy a ver si existe el documento); agent.add(voy a ver si existe el documento); //THIS ISN'T SHOWING

return db.collection('names').doc(''+358599997).get()
  .then((doc) => {
    if (!doc.exists) {
      console.log(`Saved Telegram payload not found for user `);
      agent.add(`Saved Telegram payload not found for user`);  // THIS ISN'T SHOWING
    } else {
      console.log('Found Telegram profile: ', JSON.stringify(doc.data()));
      agent.add('Found Telegram profile: ');   //THIS ISN'T SHOWING
    }
  })
  .catch((err) => {
    console.error(err);
  });

}return welcome(agent)

I followed the following response trying to fix my issue:

if you are just calling your getList function in your action handler, that is the expected behaviour as the webhook response might be sent before the callback function is finished and it won't include the second message. If you want the webhook response be sent after the callback, you need to return a promise in your action handler so it will be sent after the promise is resolved. you can do this by simply changing the action handler code to return getList(agent);

Originally posted by @r-nasiri in https://github.com/dialogflow/dialogflow-fulfillment-nodejs/issues/203#issuecomment-450890839

However, the default response is showing and not the responses inside de get() call. I know that the function is correct becasue the log messages are showing in the firebase console... please help

ketan-lambat commented 4 years ago

I'm facing a similar issue. The fulfilment responses are shown in the dialogflow console, but not in the webhook integration I'm using (Twilio-whatsapp).

index.js


// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
"use strict";

const functions = require("firebase-functions"); const admin = require("firebase-admin"); const { WebhookClient } = require("dialogflow-fulfillment");

process.env.DEBUG = "dialogflow:debug"; // enables lib debugging statements admin.initializeApp(functions.config().firebase); const db = admin.firestore();

exports.dialogflowFirebaseFulfillment = functions.https.onRequest( (request, response) => { const agent = new WebhookClient({ request, response, });

function welcome(agent) {
  agent.add(`Welcome to my agent!`);
}

function saveUserDataHandler(agent) {
  const firstname = agent.parameters.firstname;
  const lastname = agent.parameters.lastname;
  const phoneno = agent.parameters.phonenumber;

  const dialogflowAgentRef = db.collection("users").doc();

  return db
    .runTransaction((t) => {
      t.set(dialogflowAgentRef, {
        firstname: firstname,
        lastname: lastname,
        phoneno: phoneno,
      });
      return Promise.resolve("Write complete");
    })
    .then((doc) => {
      agent.add(
        ` Wrote "${firstname} ${lastname}, ${phoneno}" to the Firestore database.`
      );
      console.log("wrote to db", firstname, lastname, phoneno);
    })
    .catch((err) => {
      console.log(`Error writing to firestore : ${err}`);
      agent.add(
        ` Failed to write "${firstname} ${lastname}, ${phoneno}" to the Firestore database.`
      );
    });
}

function readUserDataHandler(agent) {
  const dialogflowAgentDoc = db.collection("users");

  return dialogflowAgentDoc
    .get()
    .then((snapshot) => {
      snapshot.forEach((doc) => {
        if (!doc.exists) {
          agent.add("No Data found in the database");
        } else {
          agent.add(doc.data().firstname);
          agent.add(doc.data().lastname);
          agent.add(doc.data().phoneno);
          console.log(doc.data());
        }
        return Promise.resolve("Read complete");
      });
    })
    .catch((err) => {
      agent.add("Error reading entry from the Firestore database.");
      console.log(err);
    });
}

function fallback(agent) {
  agent.add(`I didn't understand`);
  agent.add(`I'm sorry, can you try again?`);
}

// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set("Default Welcome Intent", welcome);
intentMap.set("Default Fallback Intent", fallback);
intentMap.set("Get User Data", saveUserDataHandler);
intentMap.set("Read User Data", readUserDataHandler);

agent.handleRequest(intentMap);

} );

> package.json

{ "name": "dialogflowFirebaseFulfillment", "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase", "version": "0.0.1", "private": true, "license": "Apache Version 2.0", "author": "Google Inc.", "engines": { "node": "8" }, "scripts": { "start": "firebase serve --only functions:dialogflowFirebaseFulfillment", "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment" }, "dependencies": { "actions-on-google": "^2.5.0", "dialogflow": "^4.0.3", "dialogflow-fulfillment": "^0.6.1", "firebase-admin": "^6.4.0", "firebase-functions": "^2.1.0" } }



In the twilio debugger I get a response that the message body is invalid.

`
MESSAGE
Message body must be specified
Invalid Body
Warning - 14103
Message Invalid Body
The Message body does not contain valid text or a media URL
`

Here I'm using the firebase database and both the read and write are working perfectly fine, in the dialogflow console interaction and the GCP console logs. It's simply that the agent.add() text is not being passed to the webhook. The response added from the intent section works as expected.

Am I missing something, or is it a bug ??
Praveen-Pandiyan commented 4 years ago

same here :( , any update ? i get -- Error - 11200 HTTP retrieval failure in twilio console what's that mean ?