如何将我本地的webhook连接到对话流? [英] How to connect webhook of my local to dialogflow?

查看:154
本文介绍了如何将我本地的webhook连接到对话流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于webhook连接的问题。

我通常是通过对话框流程的内联编辑器编辑的。

但现在我想在我的本地编辑。

所以我做了一些设置,看了两个例子。


https://chatbotsmagazine.com/creating-nodejs-webhook-for-dialogflow-2b050f76cd75


https://github.com/dialogflow/fulfillment-temperature-converter-nodejs

I have a question about webhook connect.
I edited usually by inline editor of dialogflow.
But now I want to edit in my local.
So I did some setting watching two examples.

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

[1] I made file,
      (1) Users/a/firebase.js
      (2) Users/a/functions/index.js (with package module)
      (3) webhook server by ngrok.
      (4) I attached this link 'https://ngrok~~/webhook' on dialogflow webhook

[2] firebase.js有

[2] firebase.js has

{}

[3] index.js有

[3] index.js has

'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 port 3000

我在我的代码中写了 server.listen

但似乎它在我的代码中没有webhook帖子。

I've written server.listen in my code.
But it seems that it doesn't have webhook post in my code.

所以,总之,当我在对话框流中写'hello'时,ngrok给出 404找不到错误。

So, in conclusion, when I write 'hello' in my dialogflow , ngrok gives a 404 not found error.

推荐答案

看起来你有好几件事吗?已经混合在一起。

It looks like you have several things that you've mixed together.

你的node.js程序正在使用两种不同的方法来监听端口,这种方式设计为使用 Firebase的云功能以及使用快速库,但您没有表明您正在使用其中任何一个运行程序。让我们看一下(或应该)正在运行的每件事

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 是一个端口转发器,因此它假定正在运行的另一个程序正在侦听您指定的端口。它不会启动任何正在侦听该端口本身的内容。

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.

Firebase的云功能

exports.dialogflowFirebaseFulfillment 开头的部分适用于Firebase的Cloud Functions。 Google的大多数示例都使用此功能,因为它很容易为初学者设置和使用,在生产中很好地扩展,并且一旦部署了Action,就可以使用Google提供的每月云额信用。此块中的所有代码都是您的webhook处理程序。

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创建的代码代码通常在Google服务器上运行,但是对于测试,您可以使用 firebase serve --only functions 命令在本地运行。

Code written for Cloud Functions for Firebase generally run on Google's servers, but for testing you can use the firebase serve --only functions command to run it locally.

快递库

你写的代码是开始侦听端口3000和特定路径( / getName ),但是你在那里返回的内容并没有调用你以前的任何webhook代码。

You've written code that starts listening on port 3000 and for a particular path (/getName), but what you're returning there doesn't call any of the webhook code that you have previously.

req res 参数匹配请求响应云功能部分中的参数(云功能只需使用快递),这样你就可以移动你的意图如果需要,可以将代码处理到此express处理程序中。

The req and res parameters match the request and response parameters in the Cloud Functions section (Cloud Functions just use express under the hood), so you could move your intent handling code to inside this express handler if you wanted.

使用快速库编写的代码使用节点命令。当你去发布你的代码时,你需要一个公共服务器 - 不要试图通过ngrok运行生产级服务器到你的家庭网络连接。

Code that is written using the express library is started using the node command. You'll need a public server when you go to release your code - don't try to run a production-level server through ngrok to your home network connection.

这篇关于如何将我本地的webhook连接到对话流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆