对话流数据意图

时间:2018-12-04 04:09:50

标签: firebase dialogflow dialogflow-fulfillment

Intent in question

Error I get 这是我得到的唯一错误,它登录了Firebase。它并不能告诉我太多,所以我发送了我得到的错误的屏幕截图。

Error: Query.once failed: First argument must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".
    at validateEventType (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1593:19)
    at Reference.Query.once (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:4825:9)
    at This (/user_code/index.js:40:5)
    at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:88:9)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /var/tmp/worker/worker.js:724:7
    at /var/tmp/worker/worker.js:707:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

请求正文

Dialogflow请求正文:

{
  "responseId": "6c61001c-7b4c-4d1d-9790-6774dde5a821",
  "queryResult": {
    "queryText": "what is the number",
    "action": "This",
    "parameters": {
      "number": "number"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/bot-tmnvld/agent/intents/0f19a151-afbb-4c28-b948-ced1f3b56d6e",
      "displayName": "This"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en-us"
  },
  "originalDetectIntentRequest": {
    "source": "GOOGLE_TELEPHONY",
    "payload": {
      "telephony": {
        "caller_id": "REDACTED_IN_STANDARD_TIER_AGENT"
      }
    }
  },
  "session": "projects/bot-tmnvld/agent/sessions/doP4-dClQBiAl_WDixhMkg",
  "alternativeQueryResults": [
    {
      "queryText": "what is the number",
      "languageCode": "en-us"
    }
  ]
}

代码:

'use strict';

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

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

//const serviceAccount = require("path/to/serviceAccountKey.json");
firebase.initializeApp();

process.env.GCLOUD_PROJECT;
process.env.FIREBASE_CONFIG;
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

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));

 function Data(agent){
     const allParam = agent.parameters.all;
     const all = allParam.replace(/\+/g, "").replace(/-/g,'').replace(/ /g,'');

     //agent.add(number + number + number);
     return firebase.database().ref('/all').push({all: all}).then((snapshot) => {
     console.log('database write successful: ' + snapshot.ref.toString());
 });
}

功能This(agent){

 const db = firebase.database();
 const ref = db.ref("my/database/my/data");
  return ref.once("value") 
 .then( snapshot => {
  console.log(snapshot.val());
    // Don't forget to return something to Dialogflow

}, 
ref.once('unhandledRejection', err => {
    console.log('Unhandled rejection:', err);

}));
}


  let intentMap = new Map();
  //intentMap.set('Default Intent', welcome);
 // intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('Data', Data);
  intentMap.set('This', This);

  agent.handleRequest(intentMap);
});

1 个答案:

答案 0 :(得分:0)

该错误消息表明您未能注册此Intent Handler。当使用dialogflow-fillfillment时,通常会有这样的代码将Intent名称映射到要用作该Intent处理程序的函数:

  let intentMap = new Map();
  intentMap.set('IntentName', intentHandler);
  agent.handleRequest(intentMap);

您似乎需要为此功能添加这样的行。

但是,您的处理程序还存在其他问题:

  • 由于您正在执行异步函数(它具有回调),因此需要返回Promise。
  • on()函数可能更合适时(您不想获取更新-您只想要数据的单个快照),您还使用了once()函数。
  • 我也不确定为什么将“ / all”用作on()的第一个参数,因为这应该是事件类型,通常使用“值”的类型。所以我不确定您认为它应该在做什么。

幸运的是,如果您不使用回调,once()命令将返回一个Promise。像这样的东西可能是您想要的,但是很难说:

return ref.once("value")
  .then( snapshot => {
    console.log(snapshot.val());
    // Don't forget to return something to Dialogflow
  })
  .catch( err => {
    console.log(err);
    // Dialogflow should say something went wrong
  });

更新:在您刚发布的错误消息中,关键行是

  

Query.once失败:第一个参数必须是有效的事件类型=“值”,“ child_added”,“ child_removed”,“ child_changed”或“ child_moved”。

您当前发布的代码在语法上看起来无效,但是此行绝对是错误的:

ref.once('unhandledRejection', err => {
    console.log('Unhandled rejection:', err);

})

我不确定应该怎么做(应该是.catch()块吗?)但是正如错误所暗示的那样,您几乎可以肯定希望对ref.once()的调用是ref.once('value'),如我上面的代码所示。