Cannot able to add multiple fields to a document by saving the document name in dialogflow context

岁酱吖の 提交于 2019-12-12 19:44:54

问题


I have created a dialogflow bot to ask the users some questions and I need to save all the answers entered in Firestore database, I am using api v1 bot! I could able to add the fields name and answer1 by saving the Name of the document in Dialogflow context,

index.js:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp(functions.config().firebase);

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(
  (request, response) => {
    // console.log("function happening");

    let action = request.body.result.action;

    var Name = request.body.result.parameters.Name;

    let params = request.body.result.parameters;

    var Answer1 = request.body.result.parameters.Answer1;
    var Answer2 = request.body.result.parameters.Answer2;
    var Answer3 = request.body.result.parameters.Answer3;
    var Answer4 = request.body.result.parameters.Answer4;
    var Answer5 = request.body.result.parameters.Answer5;
    var Answer6 = request.body.result.parameters.Answer6;
    var Answer7 = request.body.result.parameters.Answer7;
    var Answer8 = request.body.result.parameters.Answer8;
    var Answer9 = request.body.result.parameters.Answer9;
    var Answer10 = request.body.result.parameters.Answer10;
    var Answer11 = request.body.result.parameters.Answer11;
    var Answer12 = request.body.result.parameters.Answer12;

    let query = request.body.result.resolvedQuery;

    const parameters = request.body.result.parameters;

    const inputContexts = request.body.result.contexts;

    console.log("Parameters is" + params);
    console.log("Answer1 is " + Answer1);
    console.log("Helllo");
    console.log("action is " + action);
    console.log("name is:", Name);

    if (action == "save.name") {
      admin
        .firestore()
        .collection("users")
        .doc(Name)
        .set({
          name: Name,
          answer1: "",
          answer2: "",
        })
        .then(ref => {
          console.log("name saving function");
          console.log("Name is added successfully", +Name);
          console.log("Added new user");
        });
    }

    if (action == "save.answer1") {
      var Name;
      for (
        var co = 0;
        inputContexts && co < inputContexts.length && !Name;
        co++
      ) {
        var context = inputContexts[co];
        console.log('The input context here is ', +inputContexts[co]);
        if (context.name == "question1") {
          console.log("The name of the document is", +context.parameters.Name);
          Name = context.parameters.Name;
        }
      }
      console.log("name in save.answer1", Name);
      admin
        .firestore()
        .collection("users")
        .doc(Name)
        .update({
          answer1: Answer1
        })
        .then(ref => {
          console.log("Updated user");
          console.log("Name is " + Name);
          console.log("ref id is:" + ref.id);
        });
    }
    if (action == "save.answer2") {
      var Name;
      for (
        var co = 0;
        inputContexts && co < inputContexts.length && !Name;
        co++
      ) {
        var context = inputContexts[co];
        console.log('The input context here is ', +context.name);
        if (context.name == "question2") {
          console.log("The context name here is", +context.name);
          console.log("The name of the document is", +context.parameters.Name);
          Name = context.parameters.Name;
          console.log('Name inside if', +Name);
        }
      }
      console.log("name in save.answer2", Name);
      admin
        .firestore()
        .collection("users")
        .doc(Name)
        .update({
          answer2: Answer2
        })
        .then(ref => {
          console.log("Updated user");
          console.log("Name is " + Name);
          console.log("ref id is:" + ref.id);
        });
    }
}
);

I am saving the name of the document in dialogflow context, by doing so I able to save the field name and answer1 to the document, but I can't able to save answer2 by following the same technique, it throws error like:

Argument "documentPath" is not a valid ResourcePath. Path must be a non-empty string.

My json in dialogflow after saving answer1 looks like:

{
  "id": "da818283-f8bf-44ee-8c86-80a69cf8128a",
  "timestamp": "2018-07-18T07:33:51.743Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "I am fine",
    "action": "save.answer1",
    "actionIncomplete": false,
    "parameters": {
      "Answer1": "I am fine"
    },
    "contexts": [
      {
        "name": "question2",
        "parameters": {
          "Answer1": "I am fine",
          "Answer1.original": "I am fine"
        },
        "lifespan": 1
      }
    ],
    "metadata": {
      "intentId": "cf4c0b5e-b848-402a-be22-62ca26ec1d00",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "isFallbackIntent": "false",
      "webhookResponseTime": 10000,
      "intentName": "Q-1"
    },
    "fulfillment": {
      "speech": "Oh Well, Can you tell What is the best thing about your Companion?",
      "messages": [
        {
          "type": 0,
          "speech": "Oh Well, Can you tell What is the best thing about your Companion?"
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 206,
    "errorType": "partial_content",
    "errorDetails": "Webhook call failed. Error: Request timeout."
  },
  "sessionId": "4967cb18-a192-0796-63ed-5ce3ec03acf9"
}

Json after inputting answer2:

{
  "id": "9a9425dc-44a0-4cf6-8fa5-8d483d552c74",
  "timestamp": "2018-07-18T08:40:10.86Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "I love to watch movies",
    "action": "save.answer2",
    "actionIncomplete": false,
    "parameters": {
      "Answer2": "I love to watch movies"
    },
    "contexts": [
      {
        "name": "question3",
        "parameters": {
          "Answer2": "I love to watch movies",
          "Answer2.original": "I love to watch movies"
        },
        "lifespan": 1
      }
    ],
    "metadata": {
      "isFallbackIntent": "false",
      "webhookResponseTime": 339,
      "intentName": "Q-2",
      "intentId": "d15366cc-93f0-4178-975b-af8b0e182498",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false"
    },
    "fulfillment": {
      "speech": "Oh that's cool! What is the worst thing about your partner?",
      "messages": [
        {
          "type": 0,
          "speech": "Oh that's cool! What is the worst thing about your partner?"
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 206,
    "errorType": "partial_content",
    "errorDetails": "Webhook call failed. Error: 500 Internal Server Error"
  },
  "sessionId": "4967cb18-a192-0796-63ed-5ce3ec03acf9"
}

回答1:


The problem is that you aren't storing name in any context, but you're trying to read it out of the context on the next time through. These contexts are what is storing information in between calls to your webhook, so you need to add any information you need the next time through.

Since the name of the document is something that you need to use in later responses, you need to save it.

You don't show how you're creating or sending the response, but make sure you create a context and include the name as one of the parameters.



来源:https://stackoverflow.com/questions/51396250/cannot-able-to-add-multiple-fields-to-a-document-by-saving-the-document-name-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!