-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
326 lines (302 loc) · 11.1 KB
/
server.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// server.js
// where your node app starts
// init project
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
var moment = require('moment');
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});
app.post('/meetupbot', function (req, res) {
/* /meetupbot calls the meetupbot and response is list of commands */
var userName = req.body.user_name;
var reply = {
"text": "Hello, " +userName+ " I am a MeetupBot. I show list of meetups going on near your location.\n Try following commands :",
"attachments": [
{
title: "1) /meetupbot-find <location> (& <group>)",
text: `use this to find local meetup-groups based on your location \nfor ex: /meetupbot-find New-York
\noptional: add parameter for your interests: '/meetupbot-find New-York & <interest>'`,
color: "#764FA5"
},
{
title: "2) /meetupbot-show <location> & <interest>",
text: "use this to find meetup-events based on your location and interests \nfor ex: /meetupbot-show Mumbai & Javascript (Don't forget to use ampersand (&).)",
color: "#764FA5"
}
]
};
res.json(reply);
});
app.post('/meetupbot-show', function(req, res) {
/* /meetupbot-show <Location> <Category/Interest> */
var userName = req.body.user_name;
var commandText = req.body.text;
var area = commandText.split('&')[0];
var interest = commandText.split('&')[1];
var reply = {};
var location={};
var attachment=[];
if(!commandText || commandText == undefined){
reply.text = 'Please provide a location along with the command @'+userName+'\nFor ex: /meetupbot-show Mumbai & Javascript.';
return res.json({text: reply.text});
} else if(interest =='' || interest == undefined){
reply.text = 'Please provide a location & search term along with the command @'+userName+'\nFor ex: /meetupbot-show Mumbai & Javascript.\nIt helps in filtering the list of meetups according to your interest :blush:.'+'\nActually it will be difficult to show a long list of meetups for me right now :stuck_out_tongue_winking_eye:.';
res.json(reply);
} else {
reply.text = 'Hey '+userName+',\nThis is the list of meetups near '+area+'.';
getGeoCode(area)
.then((data) => {
location.lat = data.lat;
location.lon = data.lng;
getMeetupEvents(location, interest)
.then((events) =>{
if(events.length == 0){
reply.text = 'No Meetups found in '+area+' :sleuth_or_spy: .\nMake sure the location you entered is correct and try again.:slightly_smiling_face:';
return res.json(reply);
}
events.forEach((event) => {
var status = (event.status != undefined) ? ('Status - '+event.status) : 'Status - visible only to Members';
var date = new Date(event.time + event.utc_offset);
date = moment(date).format('lll');
var venue = (event.venue != undefined) ? event.venue.address_1 : 'Only visible to members';
attachment.push({
title: 'Group - '+event.group.name,
text: '<'+event.link+'| Event - '+event.name+'>',
author_name: status,
title_link: 'https://www.meetup.com/'+event.group.urlname,
color: "#764FA5",
fields: [
{ "title": "Date", "value": date, "short": true },
{ "title": "Venue", "value": venue, "short": true },
{ "title": "RSVP Count", "value": event.yes_rsvp_count, "short": true }
]
});
});
reply.attachments = attachment;
return res.json(reply);
}) // catch for getMeetupEvents
.catch((e) => {
console.log("error occured in getMeetupEvents promise as "+e);
return res.json({text: 'Ops something went wrong. Please try again :blush:'});
});
}) // catch for getGeoCode
.catch((e) => {
console.log("error in geocode as "+e);
return res.json({text: 'Ops something went wrong. Please try again :blush:'});
});
}
});
app.post('/meetupbot-find', function(req, res) {
var location = {};
var commandText = req.body.text.replace(" ", "");
var userName = req.body.user_name;
var reply = {};
var area,interest;
if(commandText.includes("&")) {
area = commandText.split("&")[0];
interest = commandText.split("&")[1];
} else {
area = commandText;
interest = "";
}
if(!commandText || commandText == undefined){
reply.text = '@' + userName + ` Please provide a location along with the command \nFor ex: /meetupbot-find London
(& <category>)`;
return res.json(reply);
} else if(interest === undefined) {
reply.text = '@' + userName + ' Please use the correct syntax: /meetupbot-find <location> & <interest>';
return res.json(reply);
} else {
reply.text = "Hey @" + userName +"\nThose are the groups near " + area;
if(interest) reply.text += " for" + interest;
getGeoCode(area)
.then(function(data) {
location.lat = data.lat;
location.lon = data.lng;
findMeetupGroups(location, interest)
.then(function(groups) {
if (groups.length === 0) {
reply.text = 'No groups found near '+area+` :sleuth_or_spy: .\nMake sure the location you entered is correct
and you didn\'t forget the "&". \nPlease try again.:slightly_smiling_face:`;
return res.json(reply);
} else if (groups.length > 20) {
reply.attachments = [];
for (var i = 0; i <21; i++) {
var current = groups[i];
composeAttachments(reply.attachments, current);
}
return res.json(reply);
} else {
reply.attachments = [];
groups.forEach(function(group) {
composeAttachments(reply.attachments, group);
});
return res.json(reply);
}
})
.catch(function() { //catch for findMeetupGroups
return res.json({text: 'Ops something went wrong. Please try again :blush:'});
});
})
.catch(function() { // catch for getGeoCode
return res.json({text: 'Ops something went wrong. Please try again :blush:'});
});
}
});
/*
*function to get the Geocode from google geocode API.
*/
function getGeoCode(location){
return new Promise(function(resolve, reject) {
var options = {
method: 'GET',
url: 'http://maps.googleapis.com/maps/api/geocode/json',
qs: { address: location }
};
request(options, function (error, res, body) {
if (error) {
console.log("Error occured in getGeoCode as "+error);
reject();
} else {
body = JSON.parse(body);
var loc = body.results[0].geometry.location;
resolve(loc);
}
});
});
}
/*
*function to get meetups near your city/town/location using meetup API
*/
function getMeetupEvents(location, interest) {
var key = process.env.SECRET;
return new Promise((resolve, reject) => {
var options = { method: 'GET',
url: 'https://api.meetup.com/find/events',
qs: {
key: key,
lat: location.lat,
lon: location.lon,
text: interest,
radius: 10
}
};
console.log(options);
request(options, function (error, response, body) {
if (error) {
console.log("error occured in getMeetupEvents as "+error);
reject();
} else {
body = JSON.parse(body);
console.log(body.length);
resolve(body);
}
});
});
}
/*
*function to get meetup-groups near your city/town/location using meetup API
*/
function findMeetupGroups (location, interest){
var key = process.env.SECRET;
return new Promise(function(resolve, reject){
var options = {
method: "GET",
url: "https://api.meetup.com/find/groups",
qs: {
key: key,
lat: location.lat,
lon: location.lon,
radius: 10
}
};
if(!(interest === undefined || interest === "")) options.qs.text = interest;
//api-request
request(options, function(error, response, body) {
if(error) {
console.log("error occured in findMeetupGroups: " + error);
reject();
} else {
body = JSON.parse(body);
resolve(body);
}
});
});
}
//function to help compose reply
function composeAttachments(arr, obj){
arr.push({
title: obj.name,
text: /*obj.description,*/removeHtml(obj.description),
color: "#764FA5",
mrkdwn_in: ["text", "attachments"],
fields: [
{"title": "Link","value": obj.link,"short": true},
{"title": "Members","value": obj.members,"short": true},
]
});
}
function removeHtml(str) {
var tags = /<\/?\w+>/g, bold = /<\/?b>/g, entities = /&\w+;/g, italics = /<\/?i>/g;
var linebreak = /<\/?br>/g, imgAndA = /<(?:a\s|img\s).+>/g;
var descr;
// cut str to 300 characters
if (str.length>300) descr=str.substr(0, 300);
else descr = str;
var description = descr.replace(bold, "*").replace(italics, "_").replace(entities, "").replace(linebreak, "\n");
var result = description.replace(tags, "").replace(imgAndA, "").replace(/<\w*$/, "");
return result + "...";
}
app.get('/redirected', function(req,res){
//route for redirection for the add to slack button
doOauth(req.query)
.then((data)=>{
//got the tooken i.e successfully installed the MeetupBot on slack channel.
console.log('doOauth then block with data as '+JSON.stringify(data));
res.redirect('/');
})
.catch((err)=>{
res.json({'error': 'Ops Something went wrong! Please try again.'});
console.log('error as '+err);
});
});
function doOauth(data){
return new Promise((resolve, reject) => {
var options = {
method: 'POST',
headers:{
'content-type': 'application/x-www-form-urlencoded',
charset: 'utf-8',
},
url: 'https://slack.com/api/oauth.access',
form: {
'client_id': process.env.slackClientId,
'client_secret': process.env.slackSecret,
'code': data.code
},
json: true,
'redirect_uri':'https://meetupbot.glitch.me/redirected'
};
console.log(options);
request(options, function (error, response, body) {
if (error) {
console.log("error occured in doOauth as "+error);
reject();
} else{
resolve(body);
}
});
});
}
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});