【发布时间】: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-east1,Trigger type 设置为 HTTP 并 允许未经身份验证的调用。然后按如下所述保存、更新 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"
}
}
【问题讨论】:
-
请分享您尝试过的代码,即。 e.将其添加到描述中。
-
我在上面的问题中直接添加了代码
-
不听
port可以试试这个功能吗?看起来 Cloud Functions 已经默认使用 8080
标签: google-cloud-functions dialogflow-cx