node.js - 我可以仅使用 Dialogfow 的默认履行 Webhook 代码作为 Azure 上的起点吗?

标签 node.js azure dialogflow-es

我正在 Node.js 中为 Google Dialogflow 中的 Fullfillment 片段开发一个 Webhook。我将在 Azure 上托管 Webhook。

我的问题是,我是否可以仅采用/使用 Dialogflow 中的默认 Webhook/fullfillment 代码并将其复制/托管在 Azure 上并将其用作起始位置?代码段“exports.dialogflowFirebaseFulfillment =functions.https.onRequest((request,response)?

”怎么样?
    'use strict';

    const functions = require('firebase-functions');
    const {WebhookClient} = require('dialogflow-fulfillment');
    const {Card, Suggestion} = require('dialogflow-fulfillment');


    process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

    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 welcome(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?`);
    }

        agent.add(`Thank you...`);

        return admin.database().ref('AgeInfo').once("value").then((snapshot) => {
        var averageAge = snapshot.child("RunningAverage").val();
        agent.add(`Our recorded average age is ` + averageAge);
        });
    }


    // Run the proper function handler based on the matched Dialogflow intent name
    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
    intentMap.set('AskAge', handleAge);
    // intentMap.set('your intent name here', yourFunctionHandler);
    // intentMap.set('your intent name here', googleAssistantHandler);
    agent.handleRequest(intentMap);
    });

最佳答案

如果您在 Azure 上使用类似 Express 的服务,那么应该不会遇到太多问题。您需要做两件事

  1. 为您的 Express 应用设置 body-parser 中间件,它将 JSON 文本正文转换为对象并将其存储在 request.body 中。

  2. exports.dialogflowFirebaseFulfillment =functions.https.onRequest( 部分替换为 POST 请求的快速路由到“/dialogflowFirebaseFulfillment”(或选择一个不同的名称,就像我在下面所做的那样) - 您只需更改 Webhook 的 URL)。

我还没有测试过,但它可能看起来像这样:

var expressApp = express();
var bodyParser = require('body-parser');

expressApp.use( bodyParser.json() );

expressApp.post( '/dialogflowFulfillment', (request, response) => {
  // Continue with the rest of the code here that was inside the
  // onRequest handler

关于node.js - 我可以仅使用 Dialogfow 的默认履行 Webhook 代码作为 Azure 上的起点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52899224/

相关文章:

Azure IoT 边缘队列深度

javascript - 无法将 Dialogflow 响应解析为 AppResponse

node.js - NodeJS 有没有办法在不回调的情况下按顺序执行命令

node.js - 创建付款时结帐时出现此错误 "Bad request - parameter error"

node.js - 在 Mongoose 模式验证中执行查询

azure - 如何在从 Azure Key Vault 更新 secret 时自动重新启动 Pod/部署

javascript - 将定义添加到 Typescript 中的现有模块

azure - 如何对 Microsoft 发出的有关更新 API 的消息使用react

android - Kotlin 将 Dialogflow 与 Fuel 结合用于 Android 上的简单聊天机器人

android - 无法使用 Dialogflow Android 客户端从dialogflow webhook 访问 SimpleResponse。无法访问有效负载