/
bot.js
54 lines (50 loc) · 1.89 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
var SlackBot = require('slackbots');
var parseMessage = require('./parse');
var quotes = require('./quotes');
var config = require('./config');
// create a bot
var bot = new SlackBot({
token: config('TOKEN'),
name: 'Quotbot'
});
bot.on('start', function() {
console.info('bot connected');
// more information about additional params https://api.slack.com/methods/chat.postMessage
// var params = {
// icon_emoji: ':cat:'
// };
// // define channel, where bot exist. You can adjust it there https://my.slack.com/services
// bot.postMessageToChannel('general', 'meow!', params);
// // define existing username instead of 'user_name'
// bot.postMessageToUser('user_name', 'meow!', params);
// // If you add a 'slackbot' property,
// // you will post to another user's slackbot channel instead of a direct message
// bot.postMessageToUser('user_name', 'meow!', {
// slackbot: true,
// icon_emoji: ':cat:'
// });
// // define private group instead of 'private_group', where bot exist
// bot.postMessageToGroup('private_group', 'meow!', params);
});
bot.on('message', data => {
if (data.type === 'message') {
parseMessage(data.text)
.then(response => {
if (response) {
if (process.env.NODE_ENV !== 'development') {
if (response.length < 1000) {
// avoid spamming the channel with big messages
bot.postMessage(data.channel, response, {});
} else {
bot.postMessage(data.user, response, {});
}
}
console.info(`quote delivered: ${response}`);
}
})
.catch(err => console.error(err.stack));
}
});
bot.on('error', err => {
console.error(err.message);
});