-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
160 lines (146 loc) · 5.4 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
// 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`.
app.use(bodyParser.urlencoded({extended: true}));
/*
* /meetupbot calls the meetupbot and response is a greeting along with list of commands
*/
app.post('/meetupbot', function (req, res) {
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 command :",
"attachments": [
{
title: "1) /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);
});
/*
* /meetupbot-show <Location> <Category/Interest>
* gives you list of meetup events happening nearby your location based on your interest
*/
app.post('/meetupbot-show', function(req, res) {
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;
return location;
})
.catch(e => {
console.log("error in geocode as "+e);
return res.json({text: 'Ops something went wrong. Please try again :blush:'});
})
.then(data => getMeetupEvents(data, 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(e => {
console.log("error occured in getMeetupEvents promise as "+e);
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);
}
});
});
}
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});