Dialogflow 数据到意图

Dialogflow data to intent

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

函数这个(代理){

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

错误信息提示您注册此Intent Handler失败。使用 dialogflow-fulfillment 时,您通常会使用这样的代码将 Intent 名称映射到您要用作该 Intent 的处理程序的函数:

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

看来你需要为这个功能添加这样一行。

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

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

幸运的是,如果您不使用回调,once() 命令 return 是一个 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 failed: First argument must be a valid event type = "value", "child_added", "child_removed", "child_changed", or "child_moved".

您目前发布的代码在语法上看起来不正确,但这行肯定是错误的:

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

})

我不确定那应该做什么(那应该是 .catch() 块吗?)但是正如错误提示的那样,您几乎肯定希望调用 ref.once()就像我在上面的代码中说明的那样 ref.once('value')