我正在 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 的服务,那么应该不会遇到太多问题。您需要做两件事
为您的 Express 应用设置
body-parser
中间件,它将 JSON 文本正文转换为对象并将其存储在request.body
中。将
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/