This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
91 lines (78 loc) · 2.23 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
/* eslint no-console: ["warn", { allow: ["error", "info"] }] */
const Botkit = require('botkit');
const config = require('./src/config');
const firebase = require('./src/botkit-storage-firebase-admin');
const path = require('path');
const getNewsAndSend = require('./src/getNewsAndSend');
const printConfigTable = require('./src/printConfigTable');
const {
clientId,
clientSecret,
debugEnabled,
greetingText,
helpText,
port,
redirectUri,
} = config;
const activeBots = {};
if (debugEnabled) {
printConfigTable();
}
const controller = Botkit.slackbot({
debug: debugEnabled,
storage: firebase,
}).configureSlackApp({
clientId,
clientSecret,
redirectUri,
scopes: ['bot'],
});
controller.setupWebserver(port, (err, webserver) => {
webserver.get('/', (req, res) => {
res.sendFile(path.join(`${__dirname}/public/index.html`));
});
controller.createOauthEndpoints(webserver, (oauthErr, req, res) => {
if (oauthErr) {
res.status(500).send(`ERROR: ${oauthErr}`);
} else {
res.send('Success!');
}
});
});
controller.storage.teams.all((teams) => {
teams.forEach((team) => {
const parsedTeam = team.val();
const { name } = parsedTeam;
if (debugEnabled) { console.info(`Adding team: ${name}`); }
controller.spawn(parsedTeam.bot).startRTM((teamErr, bot) => {
if (teamErr) {
console.error(`Error connecting bot to Slack for team ${name}: ${teamErr}`);
} else if (activeBots[bot.config.token]) {
console.error(`Error: bot already active for team ${name}`);
} else {
activeBots[bot.config.token] = bot;
}
});
});
});
controller.on('create_bot', (bot) => {
if (activeBots[bot.config.token]) {
if (debugEnabled) { console.info(`Bot already active: ${bot.config.token}, skipping`); }
} else {
bot.startRTM((err) => {
if (!err) {
activeBots[bot.config.token] = bot;
}
});
}
});
controller.hears([''], 'direct_message,direct_mention,mention', (bot, message) => {
const { text } = message;
if (text === '' || text === 'help') {
bot.reply(message, helpText);
} else if (['hi', 'hello', 'hey', 'yo'].includes(text)) {
bot.reply(message, greetingText);
} else {
getNewsAndSend(message, bot);
}
});