我拼命尝试为我的 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设置为HTTP并Allow unauthenticated invocations。然后我保存,更新index.js和package.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"
}
}