node.js - Webhook 调用失败。错误 : 500 Internal Server Error | Dialogflow | Firebase

标签 node.js firebase dialogflow-es

我正在尝试创建一个 firebase 函数以使用 fulfillment webhook 将 API 响应发送到 dialogflow,但我遇到了错误:500。

const functions = require('firebase-functions');
var request = require('request')

exports.webhook = functions.https.onRequest((request, response) => {

        console.log("request.body.result.parameters: ", request.body.result.parameters);
        let params = request.body.result.parameters;

        var options = {
            url: `https://islam360api.herokuapp.com/${params.find}`,
            json:true
        }
        request(options, function(error, response, body){
            if(error) response.send(error);
            else response.send(body);
        });
    });

我在部署时得到的警告是

  13:26  warning  Unexpected function expression  prefer-arrow-callback

✖ 1 problem (0 errors, 1 warning)
  0 errors and 1 warning potentially fixable with the `--fix` option.

例如,如果 params.findevoke 那么解析 API 响应应该给出这个结果

https://islam360api.herokuapp.com/evoke

{"speech":"In Surat-ul-Fateha, Ayat Number: 7, Quran says:  not of those who have evoked [Your] anger or of those who are astray. ","displayText":"In Surat-ul-Fateha, Ayat Number: 7, Quran says:  not of those who have evoked [Your] anger or of those who are astray. "}

编辑: 这是我在部署 firebase 函数时得到的响应

C:\Users\mnauf\Desktop\IOT\islam360\firebase>firebase deploy --only functions

=== Deploying to 'islam360-3cf18'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint C:\Users\mnauf\Desktop\IOT\islam360\firebase\functions
> eslint .


C:\Users\mnauf\Desktop\IOT\islam360\firebase\functions\index.js
  13:26  warning  Unexpected function expression  prefer-arrow-callback

✖ 1 problem (0 errors, 1 warning)
  0 errors and 1 warning potentially fixable with the `--fix` option.

+  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (37.98 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 6 function webhook(us-central1)...
+  functions[webhook(us-central1)]: Successful update operation.

+  Deploy complete!

Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/islam360-3cf18/overview

日志

原始 API 响应

{
  "id": "ed1108a6-a938-4cfb-8fe1-7a1fa0c856e4",
  "timestamp": "2019-03-20T18:02:22.676Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "where does Quran talk about evoke",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "find": "evoke"
    },
    "contexts": [],
    "metadata": {
      "isFallbackIntent": "false",
      "webhookResponseTime": 129,
      "intentName": "find",
      "intentId": "dd549c44-25bd-48fa-915f-4810759968eb",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false"
    },
    "fulfillment": {
      "speech": "Something went wrong",
      "messages": [
        {
          "type": 0,
          "speech": "Something went wrong"
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 206,
    "errorType": "partial_content",
    "errorDetails": "Webhook call failed. Error: 500 Internal Server Error"
  },
  "sessionId": "4d887eaa-2dc1-649d-9ae6-238a6d79f085"
}

履行请求

{
  "id": "ed1108a6-a938-4cfb-8fe1-7a1fa0c856e4",
  "timestamp": "2019-03-20T18:02:22.676Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "where does Quran talk about evoke",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "find": "evoke"
    },
    "contexts": [],
    "metadata": {
      "isFallbackIntent": "false",
      "webhookResponseTime": 129,
      "intentName": "find",
      "intentId": "dd549c44-25bd-48fa-915f-4810759968eb",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false"
    },
    "fulfillment": {
      "speech": "Something went wrong",
      "messages": [
        {
          "type": 0,
          "speech": "Something went wrong"
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 206,
    "errorType": "partial_content",
    "errorDetails": "Webhook call failed. Error: 500 Internal Server Error"
  },
  "sessionId": "4d887eaa-2dc1-649d-9ae6-238a6d79f085"
}

履行响应

Error: could not handle the request

履行状态

Webhook call failed. Error: 500 Internal Server Error

控制台截图

enter image description here

Firebase 日志

enter image description here

从 firebase 日志看来,我必须付款。我必须这样做吗?

最佳答案

如果您使用的是 dialogflow v2:

 request(options, function(error, response, body){
     if(error) 
         response.send({ fulfillmentText: "error in api call"});
     else 
         response.send({ fulfillmentText: body.speech });
 });

如果您使用的是 dilogflow v1(即将弃用)

 request(options, function(error, response, body){
     if(error) 
         response.send({ speech: "error in api call"});
     else 
         response.send({ speech: body.speech });
 });

要检查/更改 dialogflow 的版本,转到 dilogflow 控制台(https://console.dialogflow.com), 按齿轮图标,并在常规部分检查它,不要忘记按保存按钮以防您更改它

更新

根据您的 firebase 功能日志,是的,您需要付费,

或者,您可以使用 express server 来制作 webhook,您可以将其部署在 heroku cloud 上,他们的免费计划中允许第三方 api 调用

关于node.js - Webhook 调用失败。错误 : 500 Internal Server Error | Dialogflow | Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266870/

相关文章:

node.js - 为什么 MongoDB 会忽略我的一些更新?

android - 如何在 Android 中使用 Firebase?

android - 如何在 1 节点 Android Firebase 下读取特定值

node.js - node.js 中函数调用后 API 的响应范围

javascript - 在 Node js中获取谷歌日历事件ID

javascript - 如何避免用作快速路由回调的函数中的 'this' 问题

javascript - NodeJs JSON 解析问题

node.js - Express Restful api 需要 json 数组

android - 无法安装约束布局:1.0.2

dialogflow-es - Google Assistant 中有没有办法询问可用的训练短语?