@abbott-platform/abbott-framework
Version:
Abbott Framework is a framework to bring productivity and abstractions to help you to build awesome chatbots.
127 lines (101 loc) • 3.02 kB
JavaScript
const path = require('path');
const coreUtils = require('../core/utils');
const logging = require('../logging');
/**
* Controller Base Class for chat engines.
* @name BaseController
* @class
*/
module.exports = class BaseController {
get logger() {
if (!this.__logger) {
this.__logger = logging(`abbott-framework:controllers:${this.key}`);
}
return this.__logger;
}
get botkitType() {
return null;
}
get hearsMentionEvents() {
return [];
}
get hearsTriggerEvents() {
return [];
}
get triggerEventsNLPMap() {
return {
};
}
get hearsMessageEvents() {
return [];
}
get config() {
return this.abbottCore.options.platforms[this.key];
}
get nlpProcessor() {
return this.abbottCore.nlpProcessor;
}
get webserver() {
return this.abbottCore.webserver.server;
}
constructor(key, abbottCore, globals) {
this.key = key;
this.abbottCore = abbottCore;
this.abbottCore.options.platforms[key] = this.abbottCore.options.platforms[key] || {};
this.globals = globals || {};
this.globals.__dirname = this.globals.__dirname || __dirname;
if (this.abbottCore.options.storage) {
this.config.storage = JSON.parse(JSON.stringify(this.abbottCore.options.storage));
}
this.logger.debug('BaseController -> initialized!');
}
getBotkitOptions() {
let botkitOpts = {
debug: false
};
var storeNameSpace = this.abbottCore.options.botName + '-' + this.key;
if (this.config.storage.datastore) {
var namespace = null;
if (this.config.storage.datastore.namespace) {
namespace = this.config.storage.datastore.namespace + '-' + this.key;
}
this.config.storage.datastore.namespace = namespace || storeNameSpace;
botkitOpts.storage = require('@abbott-platform/botkit-storage-datastore')(this.config.storage.datastore);
} else if (this.config.storage.local) {
botkitOpts.json_file_store = path.join(this.config.storage.local, storeNameSpace); // store user data in a simple JSON format
}
return botkitOpts;
}
loadComponents(componentsPath) {
this.loadLibsFrom(componentsPath, (comp) => {
comp(this);
});
}
loadSkills() {
var customSkills = coreUtils.requireIfExits(this.globals.__dirname + '/skills');
if (!customSkills) {
var skillsGeneric = coreUtils.requireIfExits(__dirname + '/generic/skills');
if (skillsGeneric) {
skillsGeneric(this);
}
} else {
customSkills(this);
}
if (this.nlpProcessor) {
var nlpProcessorSkills = coreUtils.requireIfExits(__dirname + '/generic/skills/nlp/' + this.nlpProcessor.key);
if (nlpProcessorSkills) {
nlpProcessorSkills(this);
}
}
}
loadLibsFrom(libDir, eachCallback) {
require('fs').readdirSync(libDir).forEach((file) => {
if (eachCallback) {
eachCallback(require(path.join(libDir, file)));
}
});
}
start() {
this.loadSkills();
}
};