我可以仅使用Dialogfow的默认实现Webhook代码作为Azure的起点吗?

时间:2018-10-19 20:05:52

标签: node.js azure dialogflow

我正在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);
    });

1 个答案:

答案 0 :(得分:1)

如果您在Azure上使用类似快递的服务,则应该不会有太多问题。您需要做两件事

  1. 为您的快速应用程序设置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