-
Notifications
You must be signed in to change notification settings - Fork 0
/
grzeslack-app.js
64 lines (51 loc) · 1.61 KB
/
grzeslack-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
var Botkit = require('botkit');
if (!process.env.clientId || !process.env.clientSecret || !process.env.PORT) {
console.log('Error: Specify clientId clientSecret and port in environment');
process.exit(1);
}
var controller = Botkit.slackbot({
// interactive_replies: true, // tells botkit to send button clicks into conversations
json_file_store: './db_slackbutton_bot/',
// rtm_receive_messages: false, // disable rtm_receive_messages if you enable events api
}).configureSlackApp(
{
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
scopes: ['bot'],
}
);
controller.setupWebserver(process.env.PORT,function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver,function(err,req,res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Success!');
}
});
});
// just a simple way to make sure we don't
// connect to the RTM twice for the same team
var _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}
controller.on('create_bot',function(bot,config) {
if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function(err) {
if (!err) {
trackBot(bot);
}
bot.startPrivateConversation({user: config.createdBy},function(err,convo) {
if (err) {
console.log(err);
} else {
convo.say('I am a bot that has just joined your team');
convo.say('You must now /invite me to a channel so that I can be of use!');
}
});
});
}
});