-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
87 lines (69 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'
const Botkit = require('botkit')
const _ = require('lodash')
const defaultLogger = require('skellington-logger')('skellington')
const logger = require('./lib/logger').setLogger(defaultLogger)
const debugLogger = require('./lib/debug-logger')
const server = require('./lib/server')
const help = require('./lib/help')
const botkitDefaults = {
debug: false,
stats_optout: true,
logger: require('skellington-logger')('botkit')
}
module.exports = (cfg) => {
const instance = {}
const config = _.cloneDeep(cfg)
if (config.logger) {
logger.setLogger(config.logger)
}
formatConfig(config)
const controller = Botkit.slackbot(getSlackbotConfig(config))
// start debugging before help listeners are added
if (config.debug) {
instance.__config = config // expose internal config during debug mode
debugLogger.addLogger(controller, config.debugOptions)
}
help.addHelpListeners(controller, config.plugins)
if (config.port) {
server.start(controller, config)
}
if (config.isSlackApp) {
require('./lib/slack-app').start(controller, config)
} else {
require('./lib/single-team-bot').start(controller, config)
}
return instance
}
/**
* Gets the config object for Botkit.slackbot
* @param config
*/
function getSlackbotConfig (config) {
return _.defaults(config.botkit, {debug: !!config.debug}, botkitDefaults)
}
/**
* Formats config values so they are normalized, will exit the process with an error if required config is missing
*
* @param config
*/
function formatConfig (config) {
_.defaults(config, {debug: false, plugins: []})
config.debugOptions = config.debugOptions || {}
config.connectedTeams = new Set()
if (!Array.isArray(config.plugins)) {
config.plugins = [config.plugins]
}
config.scopes = _.chain(config.plugins)
.map('scopes')
.flatten()
.concat(_.isArray(config.scopes) ? config.scopes : [])
.uniq()
.remove(_.isString.bind(_))
.value()
config.isSlackApp = !config.slackToken
if (!config.slackToken && !(config.clientId && config.clientSecret && config.port)) {
logger.error(`Missing configuration. Config must include either slackToken AND/OR clientId, clientSecret, and port`)
process.exit(1)
}
}