Webhook呼叫失败。错误:500内部服务器错误|对话流|火力基地

时间:2019-03-20 17:28:43

标签: node.js firebase dialogflow

我正在尝试创建一个Firebase函数,以使用实现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

日志

RAW 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日志中看来,我必须付费。我必须吗?

1 个答案:

答案 0 :(得分:2)

如果使用的是DialogueFlow 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功能日志记录,是的,您需要付费,

请注意,firebase函数仅允许调用google api,而不允许免费配额中使用第三方api,因此您需要将其计划“随用随付”,因为这是所有方法中最便宜的

或者,您可以使用Express服务器制作Webhook,也可以将其部署在heroku云上,他们允许在免费计划中调用第三方api