dialogflow-es - 如何使用 actions-on-google 包的 "dialogFlow"模块创建我自己的 DialogFlow 实现网络 Hook ?

标签 dialogflow-es actions-on-google

我正在为 Google Actions 开发一个 Action,并使用 DialogFlow 将用户的语音作为意图进行处理。这些意图目前由作为 Google Cloud Function(即 Firebase)托管的 Node.js Web hook 来实现。但是,由于新的要求,网络 Hook 现在必须从 Firebase 移至客户端的服务器。

所以,我的问题是:如何调整 Web Hook 以在 Firebase 以外的服务器上运行?

下面是网络 Hook 的当前(简化)代码。所有意图的实现都相对简单,因此这个网络钩子(Hook)很大程度上基于 Google's sample code .

'use strict';

const {
  dialogflow,
  Suggestions
} = require('actions-on-google');
const functions = require('firebase-functions');

const app = dialogflow({debug: true});

app.intent('Default Fallback Intent', (conv) => {
    conv.ask('Please repeat');
});

app.intent('Default Welcome Intent', (conv) => {
    conv.ask('Hi, what do you wanna talk about?');
    conv.ask(new Suggestions(['fashion tips', 'celebrity news']));
});

// handlers for other intents...

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

我想保留尽可能多的当前代码(使用 dialogFlowSuggestionsactions-on-google 的其他模块code> 包)在适配中尽可能多,因为该操作有大量由网络钩子(Hook)实现的意图。

思考这个问题的另一种方法是:假设网络钩子(Hook)是使用express或http提供的,我如何使上述应用程序对象处理请求?那么如何写回复呢?

我找不到关于这个特定问题的任何示例、教程或问题。 This问题类似,但作者不使用DialogFlow,就像我的情况一样。 Google 的文档在这方面似乎也有点稀疏,重点关注将 Web hook 作为 Google Cloud Function 提供服务。

在上面的代码中,我尝试将最后一行 (exports.dialogFlow...) 替换为以下位:

const express = require('express');
const server = express();
server.post('/hook', app);
server.listen(3000, () => console.log('Server listening on port 3000.'))

但是,当使用模拟器的“请求”选项卡中显示的 JSON 手动执行对 Web Hook 的 POST 请求时,会出现错误“TypeError:无法将 undefined 或 null 转换为对象”,这让我认为某些 header 或者请求正文中缺少某些属性。

我错过了什么吗?我觉得我可能跳过了一两个指南,但似乎找不到它们。

最佳答案

你已经很接近了。您缺少的部分是您需要使用 body-parser 中间件来生成请求的 body 属性,该属性在将其解析为 JSON 后将包含正文.

您的代码可能如下所示:

const express = require('express');
const bodyParser = require('body-parser');
const server = express();
server.use(bodyParser.json());
server.post('/hook', app);
server.listen(3000, () => console.log('Server listening on port 3000.'))

关于dialogflow-es - 如何使用 actions-on-google 包的 "dialogFlow"模块创建我自己的 DialogFlow 实现网络 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066053/

相关文章:

google-cloud-functions - 类型错误 : handler is not a function using Firebase and DialogFlow with Google Actions

dialogflow-es - 格式错误的响应 : Failed to parse Dialogflow response into AppResponse because of empty speech response

javascript - 类型错误 : Cannot read property 'then' of undefined Dialogflow Promise

node.js - 提供权限请求建议 (Dialogflow V2)

mysql - 为什么我的 Node.Js 代码在从用户那里获取值(value)时没有在第一个请求中给出响应?

google-cloud-functions - 对话流 : Posting attchment to Facebook via Fullfillment via Firebase (inline) Cloud functions

bots - 如何将 Dialogflow 的实现代码与 Kommunicate 结合使用

node.js - 使用 Node.js 和 Express for Action on Google 进行同步调用?

oauth - Google 帐户链接操作 - 根据我们的帐户链接政策,Google 帐户不能用作身份验证端点 URL

node.js - 在我的在线服务器上部署我的Messenger chatBot