【问题标题】:How to connect webhook of my local to dialogflow?如何将我本地的 webhook 连接到 dialogflow?
【发布时间】:2019-05-17 01:53:09
【问题描述】:

我有一个关于 webhook 连接的问题。
我通常通过对话框流的内联编辑器进行编辑。
但现在我想在本地编辑。
所以我做了一些设置,看两个例子。

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) 我在 dialogflow webhook 上附加了这个链接“https://ngrok~~/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 port 3000 启动。

我在我的代码中写了server.listen
但它似乎在我的代码中没有 webhook 帖子。

因此,总而言之,当我在 dialogflow 中写下 'hello' 时,ngrok 会给出 404 not found 错误。

【问题讨论】:

  • 您能否更新您的问题以澄清您所说的“没有 webhook 输出”是什么意思。你在尝试什么,当你尝试时你看到了什么?你是如何在本地启动服务器的?
  • 请更新问题 - 不要试图在 cmets 中做广泛的答案,只需在更新问题后回复即可。 cmets 最好要求澄清,以便我们可以创建一个问题,为您和可能有类似问题的其他人解释您的情况。
  • 我编辑了我的问题并删除了 cmets。非常感谢您提供有关如何使用 stackoverflow 的建议! :)

标签: javascript node.js firebase dialogflow-es chatbot


【解决方案1】:

看起来你有几个混合在一起的东西。

您的 node.js 程序正在使用两种不同的方法来侦听端口,一种是设计为使用 Cloud Functions for Firebase 的方法,另一种是使用 express library 的方法,但您没有表明您正在运行该程序使用任何一个。让我们看看正在(或应该)运行的每一件事情

ngrok

ngrok 是一个端口转发器,因此它假定正在运行的另一个程序正在侦听您指定的端口。它不会启动对该端口本身的任何侦听。

Firebase 的云函数

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

为 Cloud Functions for Firebase 编写的代码通常在 Google 的服务器上运行,但为了进行测试,您可以使用 firebase serve --only functions 命令在本地运行它。

快递库

您已经编写了开始侦听端口 3000 和特定路径 (/getName) 的代码,但是您在那里返回的内容不会调用您之前拥有的任何 webhook 代码。

reqres 参数与 Cloud Functions 部分中的 requestresponse 参数匹配(Cloud Functions 只是在后台使用 express),因此您可以将您的意图处理代码移动到此 express 中处理程序,如果你愿意。

使用 express 库编写的代码使用 node 命令启动。当您发布代码时,您需要一个公共服务器 - 不要尝试通过 ngrok 运行生产级服务器到您的家庭网络连接。

【讨论】:

  • 非常感谢你,囚徒。我发现我必须研究有关服务器的更多基本知识。我只是复制人。
  • 希望我提供的链接可以帮助您开始使用两种不同的服务器技术。 (我建议使用 Cloud Functions for Firebase - 它们很容易上手和使用。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 2019-01-12
  • 2019-11-15
相关资源
最近更新 更多