-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot.js
executable file
·134 lines (112 loc) · 2.99 KB
/
bot.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
require('dotenv').load();
var async = require('async');
var botkit = require('./lib/Botkit.js');
var botkitAPI = require('./lib/Slack_web_api.js');
var botkitMongoStorage = require('./lib/storage/mongo_storage.js');
var logger = require('./lib/logger');
var SLACK_USERNAME = process.env.BOT_NAME || 'resourcebot';
var PACKAGE_INFORMATION = require('./package.json');
var PACKAGE_VERSION = PACKAGE_INFORMATION.version;
var LISTEN_ON = [
'direct_message',
'direct_mention',
'mention'
];
var mongoConfig = {
mongo_uri: process.env.MONGO_URI
};
var slackConfig = {
token: process.env.SLACK_TOKEN
};
var storage = botkitMongoStorage(mongoConfig);
var ObjectId = storage.ObjectId;
var controller = botkit.slackbot({
debug: false,
storage: storage,
logger: logger
});
var bot = controller.spawn(slackConfig);
var api = botkitAPI(controller, slackConfig);
var app = {
version: PACKAGE_VERSION,
LISTEN_ON: LISTEN_ON,
slack_username: SLACK_USERNAME,
mongo_config: mongoConfig,
slack_config: slackConfig,
storage: storage,
controller: controller,
bot: bot,
api: api,
command: function(commandDescription, func) {
'use strict';
return this.controller.hears(commandDescription, LISTEN_ON, func);
}
};
(function bootApp() {
'use strict';
function ensureResourceExists(resourceName, cb) {
controller.storage.resources.findOne({
name: resourceName
}, function(err, resource) {
if (err) { // Error? Bail!
return cb(err);
}
if (resource) { // Resource already exists? Nothing to do here.
logger.info('Found resource with name:', resourceName);
return cb();
}
var id = new ObjectId();
// Resource doesn't exist. Add it!
logger.info('Adding resource with name:', resourceName, ', id:', id);
controller.storage.resources.save(resourceName, {
created_at: new Date()
}, cb);
});
}
function ensureResourcesExist(cb) {
async.each([ 'staging', 'beta', 'demo' ], function(resourceName, next) {
ensureResourceExists(resourceName, next);
}, cb);
}
async.series([
ensureResourcesExist,
function(cb) {
bot.startRTM(cb);
}
], function(err) {
if (err) {
console.error('Failed booting app:', err);
process.exit(1);
}
});
}());
require('./commands/help')(app);
require('./commands/list')(app);
require('./commands/add')(app);
require('./commands/remove')(app);
require('./commands/claim')(app);
require('./commands/release')(app);
// Exit handling
var exitHandler = function(options, err) {
if (options.cleanup) {
logger.info('in exitHandler(), process.exit()');
}
if (err) {
logger.error(err.stack);
}
if (options.exit) {
logger.info('Process exiting');
process.exit();
}
};
process.on('exit', exitHandler.bind(null, {
cleanup: true
}));
// Catch Ctrl-c
process.on('SIGINT', exitHandler.bind(null, {
exit: true
}));
// Catch uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {
exit:true
}));