我有一个关于 webhook 连接的问题。
我通常使用 dialogflow 的内联编辑器进行编辑。
但是现在我想在本地编辑。
所以我看了两个例子做了一些设置。
https://chatbotsmagazine.com/creating-nodejs-webhook-for-dialogflow-2b050f76cd75
https://github.com/dialogflow/fulfillment-temperature-converter-nodejs
[1] 我制作了文件,
(1) 用户/a/firebase.js
(2) Users/a/functions/index.js(带包模块)
(3) ngrok 的 webhook 服务器。
(4) 我附加了这个链接' https://ngrok~~/webhook ' 在 dialogflow webhook 上
[2] firebase.js 有
{}
[3] index.js 有
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
const { dialogflow } = require('actions-on-google');
const app = dialogflow();
const admin = require('firebase-admin');
const server = express();
//admin.initializeApp();
process.env.DEBUG = 'dialogflow:debug';
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function hello(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('hello', hello);
intentMap.set('Default Fallback Intent', fallback);
agent.handleRequest(intentMap);
});
var port = process.env.PORT || 3000;
// create serve and configure it.
server.get('/getName',function (req,res){
res.send('Swarup Bam');
});
server.listen(port, function () {
console.log("Server is up and running...");
});
并且服务器以 ngrok 端口 3000
在本地启动。
我在我的代码中写了server.listen
。
但是我的代码中似乎没有 webhook post。
因此,总而言之,当我在 dialogflow 中写“你好”时,ngrok 给出了一个 404 not found
错误。
最佳答案
看起来你有几种东西混合在一起。
您的 node.js 程序使用两种不同的方法来监听端口,一种旨在使用 Cloud Functions for Firebase 的方法和一种使用 express library 的方法,但您没有表明您正在使用其中任何一个运行该程序。让我们看看每个正在(或应该)运行的东西
ngrok
ngrok
是一个端口转发器,因此它假定有另一个正在运行的程序正在监听您指定的端口。它不会启动任何监听该端口本身的东西。
Firebase 云函数
以 exports.dialogflowFirebaseFulfillment
开头的部分用于 Cloud Functions for Firebase。来自 Google 的大多数示例都使用它,因为它很容易为初学者设置和使用,在生产中可以很好地扩展,并且在您部署 Action 后可以使用来自 Google 的月度云积分。此 block 中的所有代码都是您的 webhook 处理程序。
为 Cloud Functions for Firebase 编写的代码通常在 Google 的服务器上运行,但为了进行测试,您可以使用 firebase serve --only functions
命令在本地运行它。
express 库
您已经编写了开始监听端口 3000 和特定路径 (/getName
) 的代码,但是您在那里返回的内容不会调用您拥有的任何 webhook 代码之前。
req
和 res
参数匹配 Cloud Functions 部分中的 request
和 response
参数(Cloud Functions只需在引擎盖下使用 express),这样您就可以根据需要将意图处理代码移动到此 express 处理程序中。
使用 express 库编写的代码是使用 node
命令启动的。当您发布代码时,您将需要一个公共(public)服务器 - 不要尝试通过 ngrok 运行生产级服务器到您的家庭网络连接。
关于javascript - 如何将我本地的 webhook 连接到 dialogflow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53802663/