-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
295 lines (217 loc) · 8.02 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require('dotenv').config();
require('moment-timezone');
var bodyParser = require('body-parser');
var meetup = require('./meetup-api.js');
var slack = require('./slack.js');
var striptags = require('striptags');
var moment = require("moment");
var express = require('express');
var hbs = require('hbs');
var reddit = require('./reddit.js');
// Instantiate a new express app
var app = express();
var response_url = "";
// Search topics for meetup
var engineeringTopics = ["computer-programming","ios","ios-development","swift-language","objective-c","android","android-developers","java","mobile-development","javascript","reactjs","nodejs","angularjs","php","internet-of-things","dotnet","ruby","amazon-web-services","big-data","clojure","css","drupal","wordpress","sql","user-experience","ui-design","virtual-reality","agile-project-management","saas-software-as-a-service","opensource","softwaredev"];
var designTopics = ["design", "sketch", "photoshop", "illustrator", "graphic design", "ux", "user experience", "dribble"];
// Configure the express app
app.set('port', 3000);
app.set('view engine', 'html');
// Specify handlebars for templating
app.engine('html', hbs.__express);
// Mount bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Set up the GET route
app.get('/meetup', function(req, res) {
res.send("Meetup");
});
// Set up the POST route
app.post('/meetup', function(req, res, next) {
// Store the response URL for Slack
if (req.body.response_url !== undefined) {
// Make sure we have a valid request
if (req.body.token !== process.env.SLACK_TOKEN && req.body.token !== process.env.DESIGN_SLACK_TOKEN) {
res.send("Slack token is bad");
return;
}
response_url = req.body.response_url;
} else {
res.send("There is no body");
}
// Build an empty parameter object
var param = meetup.baseParameter();
// Build the topics search query string and set it
var topics = Array()
if (req.body.team_id == "T03E1AWDP") // Orlando Devs
topics = engineeringTopics.join();
else if (req.body.team_id == "T03EDNQMH") { // Orlando Designer
topics = designTopics.join();
}
param.topic = topics;
// Set the time limit if one is available
// This could be the current day, week, or month
var time = getDateForText(req.body.text);
if (time !== undefined) {
param.time = moment() + "," + time;
}
// Load the events from Meetup using the URL parameter
meetup.events(param, receivedEvents);
// Spit out an empty response
res.send("");
});
app.post('/r', function(req, res, next) {
var validRequest = isValidRequest(req, process.env.SLACK_R_TOKEN);
if (validRequest) {
response_url = req.body.response_url;
var text = req.body.text;
var args = text.split(" ");
var sub = args[0];
var mid = args[1];
var limit = args[2];
var sort;
if (args.length === 0) {
sub = "all";
mid = "hot";
}
var query = "/r/";
if (sub) {
query += sub;
if (!isNumeric(mid)) {
sort = mid;
}
if (limit === undefined) {
limit = 20;
}
if (sort !== undefined) {
query += "/" + sort;
}
var params = reddit.baseParameter();
params.sub = sub;
params.sort = sort;
params.limit = limit;
console.log("params");
console.log(params);
reddit.posts(params, parseRedditRSS);
}
res.send("");
} else {
console.log("invalid request");
res.send("");
return;
}
});
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function isValidRequest(req, token) {
if (req.body.response_url !== undefined) {
console.log("Req token: " + req.body.token);
console.log("Actual token: " + token);
if (req.body.token !== token) {
console.log("Wrong token");
return false;
}
return true;
}
console.log("undefined request");
return false;
}
// Route for the index
app.get('/', function(req, res) {
res.render('index');
});
// Given a string, return the appropriate unix timestamp using Moment.js
function getDateForText(text) {
if (text === "") {
return;
}
if (text === "today") {
return moment().tz("America/New_York").endOf("day");
}
if (text === "week") {
return moment().tz("America/New_York").endOf("week");
}
if (text === "month") {
return moment().tz("America/New_York").endOf("month");
}
}
// Callback after Meetup API call returns
// Parses response and sends it into slack
function receivedEvents(body) {
// Specify we want a new ephemeral message
var message = {response_type: "ephemeral"};
// Set the correct response URL
message.response_url = response_url;
var attachments = [];
// Notify the user if we don't have any results
if (body.results === undefined || body.results.length === 0) {
message.text = "There are no meetups scheduled. How did that happen?";
slack.postMessageToChannel(message);
return;
}
// Loop through each meetup event result
for (var i = 0; i < body.results.length; i++) {
var text = "";
var result = body.results[i];
// Start a new attachment
var attachment = {};
var date = new Date(result.time);
// Build the attachment title
title = "<" + result.event_url + "|" + result.name + ">" + " hosted by <http://www.meetup.com/" + result.group.urlname + "|" + result.group.name + ">\n";
// Specify the date of the event and display the description without any HTML tags
text += moment(date).tz("America/New_York").format("dddd, MMMM Do h:mm a") + "\n";
text += striptags(result.description) + "\n\n";
// Set a few other properties for the event
attachment.title = title;
attachment.text = text;
attachment.color = "#A5C8D8";
// Add the attachment object to the array
attachments.push(attachment);
}
// Set the messages attachments
// Each one of these messages is highlighted as a result
message.attachments = attachments;
// Send the message into slack
slack.postMessageToChannel(message);
}
function parseRedditRSS(results) {
// Specify we want a new ephemeral message
var message = {response_type: "ephemeral"};
// Set the correct response URL
message.response_url = response_url;
var attachments = [];
// Notify the user if we don't have any results
if (results === undefined || results.length === 0) {
message.text = "There are no posts available. How did that happen?";
slack.postMessageToChannel(message);
return;
}
for (var i = 0; i < results.length; i++) {
var result = results[i];
var text = "";
var attachment = {};
// Build the attachment title
title = "<https://www.reddit.com/" + result.data.permalink + "|" + result.data.title + "> posted by <https://www.reddit.com/u/" + result.data.author + "|" + result.data.author + ">";
// Specify the date of the event and display the description without any HTML tags
text += "<" + result.data.url + "|Direct Link> ";
text += "▲: " + result.data.ups;
// Set a few other properties for the event
attachment.title = title;
attachment.text = text;
attachment.color = "#A5C8D8";
// Add the attachment object to the array
attachments.push(attachment);
}
// console.log(message);
message.attachments = attachments;
// Send the message into slack
message.text = "";
slack.postMessageToChannel(message);
}
/* Start Server */
app.listen(process.env.PORT || 3000, function() {
console.log("Node app is running on port " + app.get('port'));
});