-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
87 lines (68 loc) · 2.46 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
var Botkit = require('botkit');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var config = require('./src/config/config')[env];
const clientId = process.env.SLACK_CLIENT_ID;
const clientSecret = process.env.SLACK_CLIENT_SECRET;
if (!clientId || !clientSecret || !config.port) {
console.log('Error: Specify clientId clientSecret and PORT in environment');
process.exit(1);
}
const controller = Botkit.slackbot({
debug: false,
interactive_replies: true, // tells botkit to send button clicks into conversations
json_file_store: './alfred-bot-db/'
});
const slackbot = controller.spawn({
token: process.env.SLACK_TOKEN,
}).startRTM();
controller.configureSlackApp({
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
redirectUri: 'https://00c4ebb2.ngrok.io/oauth',
scopes: ['bot'],
debug: true
});
// set up a botkit app to expose oauth and webhook endpoints
controller.setupWebserver(config.port, function (err, webserver) {
// set up web endpoints for oauth, receiving webhooks, etc.
controller
.createHomepageEndpoint(controller.webserver)
.createOauthEndpoints(controller.webserver, function (err, req, res) {
})
.createWebhookEndpoints(controller.webserver);
});
//register middleware
require('./src/middlewares/leave-service-registration')(controller, config);
require('./src/middlewares/channel-subscription')(controller, config);
//register skills
require('./src/skills/initialize-brains')(controller, config);
controller.hears('', 'direct_mention,mention', function (bot, message) {
const user = message.user;
bot.startPrivateConversation({
user: user
}, function (err, convo) {
if (!err && convo) {
convo.say({
text: `Hello <@${user}> :wave:`,
attachments: [
{
title: 'I cannot talk on public channels. But I\'m all ears out here :simple_smile:',
text: 'Type `help` to learn more about me.',
mrkdwn_in: ['text']
}
]
});
}
});
});
controller.on('interactive_message_callback', function (bot, message) {
const callbackId = message.callback_id;
const handler = require('./src/skills/interactive-message-handler-factory')(callbackId);
if(!handler){
bot.replyInteractive(message, "Oops. Found myself in an unknown territory. :confused: ");
}
else {
const response = handler.handleResponse(message.actions[0].value);
bot.replyInteractive(message, response);
}
});