forked from 18F/standup-slack-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (104 loc) · 3.31 KB
/
app.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
'use strict';
require('./env');
var log = require('./getLogger')('app');
var Botkit = require('botkit');
var schedule = require('node-schedule');
var botLib = require('./lib/bot');
var startWebServer = require('./lib/web/start');
var cfenv = require('cfenv');
var appEnv = cfenv.getAppEnv();
// Database setup
var models = require('./models');
models.sequelize.sync(
// Set to true to reset db on load
{force: false}
);
if (!process.env.SLACK_TOKEN) {
log.error('SLACK_TOKEN not set in environment.');
process.exit(1);
}
var bkLogger = require('./getLogger')('botkit');
function bkLog(level) {
var args = [ ];
for(var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
// Remap botkit log levels
if(level === 'debug') {
return;
}
else if(level === 'info') {
level = 'verbose';
} else if(level === 'notice') {
level = 'info';
}
var fn, thisObj;
if(bkLogger[level]) {
fn = bkLogger[level];
thisObj = bkLogger;
} else {
fn = console.log;
thisObj = console;
args.unshift('[' + level + ']');
}
fn.apply(thisObj, args);
}
var controller = Botkit.slackbot({
debug: false,
logger: { log: bkLog },
webserver: {
static_dir: __dirname + '/lib/web/static'
}
});
// Initialize the bot
controller.spawn({
token: process.env.SLACK_TOKEN,
retry: 5
}).startRTM(function(err, bot) {
if (err) {
log.error(err);
throw new Error(err);
} else {
log.info('Connected to RTM');
bot.identifyBot(function(err,identity) {
// identity contains...
// {name, id, team_id}
log.info('Bot name: ' + identity.name);
// Set up cron job to check every minute for channels that need a standup report
schedule.scheduleJob('* * * * 1-5', botLib.getReportRunner(bot));
schedule.scheduleJob('* * * * 1-5', botLib.getReminderRunner(bot));
// TODO: method to set standup frequency
// TODO: add usage messages
botLib.giveHelp(controller, identity.name);
// Set yourself OOO for some time. Put this above getStandupInfo
// because getStandupInfo catches anything that starts with "#channel",
// so catch the more precise
botLib.setOutOfOffice(controller);
botLib.getStandupInfo(controller);
// TODO: remind people to do standup?
botLib.setReminder(controller);
// Message for when the bot is added to a channel
botLib.joinChannel(controller, identity.name);
// Create a standup in a channel
botLib.createStandup(controller);
// Add or change a standup message for today in a DM with the bot
botLib.getUserStandupInfo(controller);
// DM a user when they ask to be interviewed or
// they react to a reminder DM
botLib.startInterview(controller);
botLib.startDmEmoji(controller, identity.id);
// Remove a standup
botLib.removeStandup(controller);
// Set a standup audience to a user group
botLib.setAudience(controller);
// Get a weekly user report
botLib.userReport(controller);
// I think that these aren't necessary because channel & user are stored as
// unique id rather than display name
// TODO: update channel name if it changes
// TODO: update user name if it changes
log.verbose('All bot functions initialized');
});
startWebServer(controller);
}
});