【问题标题】:Error when deploying DialogFlow CX webhook on Google Cloud Functions: "Error: listen EADDRINUSE: address already in use :::8080"在 Google Cloud Functions 上部署 DialogFlow CX webhook 时出错:“错误:监听 EADDRINUSE:地址已在使用 :::8080”
【发布时间】:2021-07-02 07:57:08
【问题描述】:

我拼命尝试为我的 DialogFlow CX 代理实现一个简单的 Webhook。以前从未这样做过,所以我只是将在下一页找到的 index.js 和 package.json 代码复制粘贴到我的 Google Cloud 函数中:DialogFlow CX calculate values

但这似乎不起作用。尝试部署 Cloud Function 时出现错误“错误:监听 EADDRINUSE:地址已在使用 :::8080”。

如果我使用此示例代码,也会发生同样的情况:Dialogflow CX webhook for fulfilment to reply user using nodejs

我做错了什么?我正在编辑代码并尝试将其直接部署在 Google Cloude Web 控制台中,而不是通过命令提示符工具。

更多详情:

Google Cloud Function 设置:我通过 Google Cloud Console 点击Create Function 设置了一个新的 Google Cloud Function。我将 Region 设置为 us-east1Trigger type 设置为 HTTP允许未经身份验证的调用。然后按如下所述保存、更新 index.jspackage.json,然后单击 Deploy。结果是部署无法完成,因为Error: listen EADDRINUSE: address already in use :::8080

这是我放入 index.js 的代码:

'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

var port = process.env.PORT || 8080;

app.use(
    bodyParser.urlencoded({
      extended: true
    })
);
  
app.use(bodyParser.json());

app.post('/BMI', (req, res) => processWebhook4(req, res));

var processWebhook4 = function(request, response ){

    const params = request.body.sessionInfo.parameters;
    
    var heightnumber = params["height.number"];
    var weightnumber = params["weight.number"];
    var heightunit = params["height.unit-height"]
    var weightunit = params["weight.unit-weight"]
    var computedBMI;

    if (heightunit == "cm" && weightunit == "kg") { //using metric units
        computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
    } else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
        computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
    }

    const replyBMI = {
        'fulfillmentResponse': {
            'messages': [
                {
                    'text': {
                        'text': [
                            'This is a response from webhook! BMI is ' + computedBMI
                        ]
                    }
                }
            ]
        }
    }
    response.send(replyBMI);
}

app.listen(port, function() {
    console.log('Our app is running on http://localhost:' + port);
});

这里是我放入 package.json 的代码:

{
   "name": "cx-test-functions",
   "version": "0.0.1",
   "author": "Google Inc.",
   "main": "index.js",
   "engines": {
       "node": "8.9.4"
   },
   "scripts": {
       "start": "node index.js"
   },
   "dependencies": {
       "body-parser": "^1.18.2",
       "express": "^4.16.2"
   }
}

【问题讨论】:

  • 请分享您尝试过的代码,即。 e.将其添加到描述中。
  • 我在上面的问题中直接添加了代码
  • 不听port可以试试这个功能吗?看起来 Cloud Functions 已经默认使用 8080

标签: google-cloud-functions dialogflow-cx


【解决方案1】:

您分享的 StackOverflow 帖子中的代码在 Heroku 等其他平台上运行。

您遇到的错误“Error: listen EADDRINUSE: address already in use :::8080”是因为代码函数正在监听端口8080。请注意,您需要检查并编辑您提供的示例代码,并查看所使用的库是否在 Google Cloud Functions 中受支持(例如:express js),以及这些库是否兼容以便在 Google Cloud Functions 中使用它。

这是来自 StackOverflow Post 的 Cloud Functions 工作代码:

exports.helloWorld = (req, res) => {
 
  const params = req.body.sessionInfo.parameters;
  
   var heightnumber = params.height.number;
   var weightnumber = params.weight.number;
   var heightunit = params.height.heightUnit;
   var weightunit = params.weight.weightUnit;
   var computedBMI;
 
  if (heightunit == "cm" && weightunit == "kg") { //using metric units
      computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
   } else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
       computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
   }
 
   const replyBMI = {
       'fulfillmentResponse': {
           'messages': [
               {
                   'text': {
                       'text': [
                           'This is a response from webhook! BMI is ' + computedBMI
                          
                       ]
                   }
               }
           ]
       }
 }
 res.status(200).send(replyBMI);
};

结果如下:

此外,这里有一个示例代码,您也可以使用它在 Cloud Function 中进行部署:

index.js

exports.helloWorld = (req, res) => {
 
 let fulfillmentResponse = {
          "fulfillmentResponse": {
              "messages": [{
                  "text": {
                      "text": [
                          "This is a sample response"
                      ]
                  }
              }]
          }
  };
  res.status(200).send(fulfillmentResponse);
};

package.json

{
  "name": "sample-http",
  "version": "0.0.1"
}

部署示例代码后,您可以执行以下操作以使用 webhook:

  1. 转到管理 > Webhook > 创建
  2. 添加显示名称和 Webhook URL(Cloud Functions 中的触发器 URL)
  3. 点击保存
  4. 转到构建 > 流程 > 起始页
  5. 选择任意路由并添加 webhook
  6. 在模拟器中测试

结果应该是这样的:

【讨论】:

  • 定义明确的答案。帮助我设置了代理响应的基本功能!
猜你喜欢
  • 1970-01-01
  • 2021-02-11
  • 2020-11-09
  • 2020-04-08
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
相关资源
最近更新 更多