This repository has been archived by the owner on Sep 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (124 loc) · 3.42 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const token = process.env.token;
const logger = require('./lib/logger.js');
if (!token) {
logger.error('Error: Specify token in environment');
process.exit(1);
}
const fs = require('fs');
const Path = require('path');
const help = require('./lib/help.js');
const storage = require('./lib/storage.js');
const EventHook = require('./lib/event_hook.js');
const controller = require('botkit').slackbot({
storage: storage({ path: 'data' })
});
// FIXME: ensure callback to be always executed
controller.hooks = new EventHook(['ambient'], hooks => {
Object.keys(hooks.events).forEach(e => {
controller.on(e, (bot, message) => {
logger.debug(`hooks ${e}`);
controller.hooks.trigger(e, cb => cb(bot, message));
});
});
});
// load config if json exists
const configFile = './config.json';
if (fs.existsSync(configFile)) {
Object.assign(controller.config, require(configFile));
}
// load all scripts
const paths = [];
['before', '', 'after'].forEach(d => {
const scriptsPath = Path.join(__dirname, 'scripts', d);
fs.access(scriptsPath, 'r', err => {
if (err) {
return;
}
fs.readdirSync(scriptsPath).forEach(file => {
const path = `${scriptsPath}/${file}`;
fs.stat(path, (err, stat) => {
if (err || stat.isDirectory()) {
return;
}
paths.push(path);
require(path)(controller);
});
});
});
});
const bot = controller
.spawn({
token
})
.startRTM((err, bot) => {
if (err) {
return logger.error(err);
}
// register cron jobs
const cronPath = Path.join(__dirname, 'cron');
fs.readdirSync(cronPath).forEach(file => {
require(`${cronPath}/${file}`)(bot);
});
});
bot.botkit
.on('rtm_open', () => {
logger.info('rtm_open');
// parse help message
paths.forEach(path => {
help.parse(path, bot.identity.name);
});
})
.on('rtm_close', () => {
logger.info('rtm_close');
process.exit(1);
});
// load and save team, member and channel data from slack
require('./lib/slack_sync.js')(bot).all();
/*
* WIP: slack commands or outgoing web hooks
*/
const fetchVariable = (key, defaults) => {
return process.env[key] || controller.config[key] || defaults;
};
const clientId = fetchVariable('SLACK_APP_CLIENT_ID');
const clientSecret = fetchVariable('SLACK_APP_CLIENT_SECRET');
const port = fetchVariable('port', '3978');
const redirectUri = fetchVariable('uri');
if (clientId && clientSecret && redirectUri) {
controller.configureSlackApp({
clientId,
clientSecret,
redirectUri,
scopes: [
'incoming-webhook',
'team:read',
'users:read',
'channels:read',
'im:read',
'im:write',
'groups:read',
'emoji:read',
'chat:write:bot'
]
});
controller.setupWebserver(port, (err, webserver) => {
// set up web endpoints for oauth, receiving webhooks, etc.
controller
.createHomepageEndpoint(webserver)
.createOauthEndpoints(webserver, (err, req, res) => {
logger.log(err, req, res);
})
.createWebhookEndpoints(webserver);
const serverPath = Path.join(__dirname, 'server');
fs.readdirSync(serverPath).forEach(file => {
require(`${serverPath}/${file}`)(webserver, controller);
});
});
controller.on('slash_command', (bot, message) => {
logger.debug(message);
bot.replyPrivate(
message,
'Only the person who used the slash command can see this.'
);
});
}