forked from azharb/SlackVote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
176 lines (142 loc) · 4.73 KB
/
Copy pathapp.js
File metadata and controls
176 lines (142 loc) · 4.73 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
/*
* Express Dependencies
*/
var express = require('express');
var nconf = require('nconf');
nconf.argv()
.env()
.file({ file: './config.json' });
var app = express();
var port = 3000;
/*
* Mongo DB
*/
var mongo = require('mongodb');
var monk = require('monk');
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'localhost:27017/VoteDB';
var db = monk(mongoUri);
/*
* Slack configuration
*/
var Slack = require('slack-node');
var appAccessToken = nconf.get('appAccessToken');
// For gzip compression
app.use(express.compress());
// Make our db accessible to our router
app.use(function(req,res,next){
req.db = db;
next();
});
// Configure app
app.configure(function(){
app.use(express.bodyParser());
app.use(app.router);
});
/*
* Routes
*/
// Index Page
app.get('/', function(request, response, next) {
response.render('index');
});
// Outgoing webhooks from Slack
app.post('/outgoing', function(req, res, next) {
var votes = req.db.get('votes');
if (req.body.token != nconf.get('outgoingToken')) {
res.json({ text : 'Invalid token' });
return;
}
var trigger_word = req.body.trigger_word;
var trigger_text = req.body.text;
// config for slack api call
var slack = new Slack(appAccessToken);
var channelID = req.body.channel_id;
// Trigger is to start vote
if (trigger_word == 'startvote') {
//get all members in channel
slack.api("channels.info", { 'channel' : channelID}, function(err, response) {
//expect their responses
response.channel.members.forEach(function(m) {
votes.update(
{ 'userID' : m, 'channelID' : channelID },
{ 'userID' : m, 'username': '', 'channelID' : channelID, 'status' : 0, 'vote': ''},
{ upsert: true },
function (err, doc) {
if (err) throw err;
console.log(doc);
}
);
});
});
//respond asking for votes from everyone
res.json({text: 'everyone reply with "/vote <youranswer>"'});
} else if (trigger_word == 'reveal') {
var returnText = 'Votes:\n';
// get channel members
slack.api('channels.info', { 'channel' : channelID }, function(err, response) {
var params = { userID : { $in : response.channel.members }, channelID : channelID, status : 1 };
votes.find(
params,
function(err, results){
if (err) throw err;
if (results.length > 0) {
results.forEach(function(r) {
if (trigger_text.indexOf('anon') < 0) {
returnText += r.username + " votes ";
}
returnText += r.vote + "\n";
});
res.json({ text: returnText });
} else {
res.json({ text : "No votes found." });
}
}
);
});
} else if (trigger_word == 'votecount') {
// get channel members
slack.api('channels.info', { 'channel' : channelID }, function(err, response) {
var params = { userID : { $in : response.channel.members }, channelID : channelID, status : 1 };
votes.find(
params,
function(err, results){
if (err) throw err;
res.json({ text : results.length + " votes casted" });
}
);
});
} else {
res.json({ text : 'Unknown trigger' });
}
});
// Slack command - vote from a user
app.post('/vote', function(req, res, next) {
var votes = req.db.get('votes');
var input = req.body;
console.log(input);
if (input.token != nconf.get('commandToken')) {
res.json('Invalid token');
return;
}
votes.update(
{ userID : input.user_id, channelID : input.channel_id },
{ $set: { status : 1, vote : input.text, username : input.user_name }},
function(err, doc) {
console.log(err);
console.log(doc);
if (err) {
res.json('Something went wrong. Your vote was not recorded.');
} else {
res.json('Your vote \'' + input.text + '\' has been recorded.');
}
}
);
});
/*
* Start it up
*/
app.listen(process.env.PORT || port);
console.log('Express started on port ' + port);