DD *_* DD 5 javascript chatbot node.js firebase dialogflow-es
我有关于webhook连接的问题.
我通常通过对话框流程的内联编辑器进行编辑.
但现在我想在我的本地编辑.
所以我做了一些设置,看了两个例子.
https://chatbotsmagazine.com/creating-nodejs-webhook-for-dialogflow-2b050f76cd75
https://github.com/dialogflow/fulfillment-temperature-converter-nodejs
[1]我发了文件,
(1)用户/ a/firebase.js
(2)用户/ a/functions/index.js(带包模块)
(3)ngrok的webhook服务器.
(4)我在dialogflow webhook上附加了这个链接' https:// ngrok ~~/webhook '
[2] firebase.js有
{}
Run Code Online (Sandbox Code Playgroud)
[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...");
});
Run Code Online (Sandbox Code Playgroud)
服务器在本地启动ngrok port 3000.
我写server.listen了我的代码.
但似乎它在我的代码中没有webhook帖子.
所以,总而言之,当我在对话框流中写'hello'时,ngrok会404 not found出错.
It looks like you have several things that you've mixed together.
Your node.js program is using two different methods to listen on a port, a way that is designed to use Cloud Functions for Firebase and a way that uses the express library, but you haven't indicated that you're running the program using either. Let's look at each thing that is (or should be) running
ngrok
ngrok is a port forwarder, so it assumes that there is another program that is running that is listening on the port you've specified. It does not start anything listening on that port itself.
Cloud Functions for Firebase
The portion starting with exports.dialogflowFirebaseFulfillment is for Cloud Functions for Firebase. Most examples from Google use this because it is easy to setup and use for beginners, scales well in production, and can use the monthly cloud credit from Google once you've deployed your Action. All the code inside this block is your webhook handler.
为 Firebase Cloud Functions 编写的代码通常在 Google 的服务器上运行,但为了进行测试,您可以使用该firebase serve --only functions命令在本地运行它。
快捷图书馆
您已经编写了开始侦听端口 3000 和特定路径 ( /getName) 的代码,但您返回的内容不会调用您之前拥有的任何 Webhook 代码。
和参数与 Cloud Functions 部分中的和参数相匹配(云函数仅在底层使用express),因此如果需要,您可以将意图处理代码移至此express 处理程序内部req。resrequestresponse
使用express 库编写的代码是使用该node命令启动的。当您要发布代码时,您将需要一个公共服务器 - 不要尝试通过 ngrok 到您的家庭网络连接运行生产级服务器。
| 归档时间: |
|
| 查看次数: |
513 次 |
| 最近记录: |