如何在index.js中将函数拆分为另一个文件

时间:2019-07-28 01:02:35

标签: dialogflow dialogflow-fulfillment

我想使用Firebase中托管的webhook将函数拆分为不同的js文件。

因为我希望我将来会编写更多的函数。

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

  function hello(agent) {
      console.log("hello);
  }


  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('hello',hello);
  agent.handleRequest(intentMap);
});

1 个答案:

答案 0 :(得分:0)

您的index.js

const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
const  welcome  = require('./welcome')
const  fallback  = require('./fallback')

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));
    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
    agent.handleRequest(intentMap);
});

您可以拆分welcomefallback等文件

=> welcome.js

const welcome = (agent) => {
    agent.add(`Welcome to my agent!`);
}
module.exports = welcome

=> fallback.js

const fallback = (agent) => {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}
module.exports  =  fallback