UNPKG

solar-chat-bot

Version:

Agnostic micro-framework for dialogflow fulfillment

39 lines (31 loc) 1.12 kB
const { WebhookClient } = require('dialogflow-fulfillment'); module.exports = class Webhook{ constructor(config, http){ this.intentsPath = config.intentsPath; this.intentsFolder = config.intentsFolder; this.headerLog = config.headerLog; this.bodyLog = config.bodyLog; this.http = http; } logger(){ if(this.headerLog) console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers)); if(this.bodyLog) console.log('Dialogflow Request body: ' + JSON.stringify(request.body)); } intentMapper(){ const intents = require(this.intentsPath); let intentMap = new Map(); for(let intent of intents){ intentMap.set(intent.name, require(this.intentsFolder+'/'+intent.handler)); } return intentMap; } run(){ const { request, response } = this.http; const agent = new WebhookClient({ request, response }); this.logger(); const intents = this.intentMapper(); agent.handleRequest(intents); } }