- What are the mandatory components in index.js?
- How to include codes or call functions in index.js from other files?
- How the index.js file can be kept as small as possible?
- How to manage a complex bot?
- How to handle many intents and their corresponding fulfillment codes or functions?
'use strict'
const functions = require('firebase-functions')
const { WebhookClient } = require('dialogflow-fulfillment')
// custom functions
const { findWord } = require('./find-word')
const { getResult } = require('./k-score') // wisdom check, should be moved to k-test
const { initKTest, kTest } = require('./k-test') // knowledge test
const { userDetailsGiven, verifyOtp } = require('./user-details') // session only user management
const { makeAppo } = require('./appointment')
const { commonYes } = require('./common-yes')
process.env.DEBUG = 'dialogflow:debug' // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
try {
const agent = new WebhookClient({ request, response }); const intentMap = new Map()
intentMap.set('Default Welcome Intent', welcome) // fulfillment not yet activated
intentMap.set('Default Fallback Intent', fallback) // fulfillment not yet activated
intentMap.set('Rhyming Words', findWordHandler)
intentMap.set('Sentence to word', findWordHandler)
intentMap.set('Wisdom test', knowledgeCheck)
intentMap.set('User identity details', userDetailsGiven)
intentMap.set('User identity verification', verifyOtp)
intentMap.set('D365 Sales knowledge test', initKTest)
intentMap.set('D365 Sales Q1', kTest)
intentMap.set('Appointment', makeAppo)
intentMap.set('Common Yes', commonYes)
agent.handleRequest(intentMap)
} catch (error) {
console.log(`Error in index.js dialogflowFirebaseFulfillment: ${error}`)
}
})
async function knowledgeCheck(agent) {
const { topic, qid, answer } = agent.parameters
agent.add(await getResult(topic, qid, answer))
}
async function findWordHandler(agent) {
const { input, type } = agent.parameters
const result = await findWord(input, type)
agent.add(`For '${input}' here are the words: ${result}.`)
}
function welcome(agent) {
agent.add(`Welcome, myself NDRobo, how can I help you?`)
}
function fallback(agent) {
agent.add(`I didn't understand`)
agent.add(`I'm sorry, can you try again?`)
}
Refer to the lines of intentMap.set where the intents are mapped with their corresponding functions. Some of those functions are in index.js file itself and some are from other files as you find in the require section at the beginning of the code. Keeping the functions/codes distributed in various files makes the fulfillment code more manageable.
Post a Comment