【问题标题】:Dialogflow data to intentDialogflow 数据到意图
【发布时间】:2018-12-04 04:09:49
【问题描述】:

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

【问题讨论】:

  • 有一个额外的右大括号和括号不匹配。你可以发布你的整个代码吗?并重新发布您的完整错误消息?
  • 我重新发布了!错误只是说未处理的拒绝。如果没有要检索的数据,它还会创建该错误吗?
  • 不应该。 (一般来说,发布日志的屏幕截图并没有那么有用,顺便说一句。)我看到您正在记录请求正文。您能否更新问题以包含正在记录的正文的全文(作为文本),因为它可能有助于解释正在发生的事情。
  • “未处理的拒绝”表示您没有正确处理错误情况。请参阅我关于 Promises 的答案部分。
  • 我取消了我的信息并添加了我的请求正文。我的数据使用一个函数保存并且它可以工作,但我正在尝试检索数据以使用一组不同的数据发送到我的意图。我正在检索的数据在 firebase 中并存储在 firebase 中,但来自不同的应用程序。

标签: firebase dialogflow-es dialogflow-es-fulfillment


【解决方案1】:

错误消息表明您未能注册此 Intent 处理程序。使用 dialogflow-fulfillment 时,您通常有这样的代码来将 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 失败:第一个参数必须是有效的事件类型 = “value”、“child_added”、“child_removed”、“child_changed”或“child_moved”。

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

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

})

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

【讨论】:

  • intentMap.set('this', This); agent.handleRequest(intentMap);这就是我所拥有的,但仍然得到 unhandle 错误
  • 在注释中添加代码真的很难阅读。相反,请确保您更新了问题(在这种情况下,使用地图中的代码以及相关 Intent 的屏幕截图)并在 cmets 中注明您已经这样做了。
  • 好的,我让一切正常,但您是否见过机器人重复训练问题的问题。这是当您询问培训问题时,它会第二次回复我的功能。
  • 如果答案有帮助,接受和/或支持它总是值得赞赏的。如果您发现其他问题,最好尽可能详细地提出新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多