-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
47 lines (40 loc) · 1.26 KB
/
index.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
var config = require('./config')
var model = require('./model');
config.bot.on('start', function() {
console.log('bot started.');
// define channel, where bot exist. You can adjust it there https://my.slack.com/services
//bot.postMessageToChannel('general', 'meow!', { icon_emoji: ':cat:' });
});
// slack handle: reaction
config.bot.on('message', function(data) {
var votes = {
reaction_added: 1,
reaction_removed: -1,
};
console.log('message:', JSON.stringify(data));
if (data.reaction !== config.REACTION_NAME) return;
if (data.item.type !== 'message') {
console.warn('type !== message'); // TODO
return;
}
var query = { type: data.item.type, id: data.item.ts };
var incr = votes[data.type];
if (incr) {
var update = {
$inc: { votes: incr },
channel: data.item.channel
};
model.Message.findOneAndUpdate(query, update, { upsert: true, new: true }, function(err, doc){
console.log('findOneAndUpdate=> ', err || doc);
});
}
});
// dummy web server (required by heroku)
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(process.env.PORT || 3000, function () {
console.log('listening on port ' + (process.env.PORT || 3000));
});