0

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

但这似乎行不通。尝试部署云功能时,我收到错误“错误:监听 EADDRINUSE:地址已在使用 :::8080”。

如果我采用此示例代码,也会发生同样的情况:Dialogflow CX webhook for履行使用nodejs回复用户

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

这里有更多细节:

谷歌云函数的设置:我通过谷歌云控制台设置了一个新的谷歌云函数,点击创建函数。我将Region设置为us-east1,将Trigger type设置为HTTPAllow unauthenticated invocations。然后我保存,更新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"
   }
}
4

1 回答 1

3

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

你遇到的错误“Error:listen EADDRINUSE:address already in use :::8080”是因为code函数监听了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);
};

包.json

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

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

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

结果应该是这样的:

在此处输入图像描述

于 2021-07-15T15:32:50.823 回答