-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
233 lines (204 loc) · 8.06 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
var express = require('express');
var bodyParser = require("body-parser");
var axios = require('axios');
var FormData = require('form-data');
var meetup = require('meetup-api')({
key: '3e565b1e1e4d466c5714f69145a264b'
});
var app = new express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
var path = require("path");
var date = new Date();
var topicsMap = {
ui: 'javascript,redux,react,angularjs,angular2,angular-2,angular-3,angular-4,angular-5,angular-6,angular-7,vue',
blockchain: 'blockchain',
};
var fillTemplate = function(templateString, templateVars){
return new Function("return `"+templateString +"`;").call(templateVars);
}
var messageTemplates = {
blockchain: ["Hi, ${this.personName} how are you?",
"It was great to see you at the ${this.meetupName} meetup at ${this.meetupLocationName} on ${this.meetupDate} and I would like to connect you with a startup company that its platform is based in Blockchain. The company is on a rise right now, and recently finished a very successful funding round, and they looking for expand, and we need a talented developers who are experts in Blockchain. They are offering amazing benefits (including Bitcoin) and great salaries too! For more details about this position, please check the following link: https://bit.ly/2XdhZpG",
"Waiting for update, ",
"${this.meetupHostName}",
"${this.meetupGroupName}.",
].join('\n'),
ui: ["Hello ${this.personName}! How are you? My name is Sharon and I am a sourcing specialist who working with the lead Tech companies today. I got your detailed from ${this.meetupHostName}, who was really impressed from your knowledge at the ${this.meetupName} meetup in ${this.meetupLocationName} you attended on ${this.meetupHostName}, and I would like to offer you in a position that might be perfect for you! The company using the latest technologies (you can find more find details here: https://bit.ly/2XdhZpG). ",
"Waiting to hear from you, Sharon."
].join('\n')
};
var fromTemplates = {
blockchain: "${this.meetupHostEmailName}@meetup.com",
ui: "[email protected]"
};
fromNameTemplates = {
blockchain: "${this.meetupHostName}",
ui: "Sharon"
};
var subjectTemplates = {
blockchain: "Blockcahin meetup - ${this.meetupDate} – Opportunities suggestions",
ui: "${this.meetupName} -${this.meetupDate} – job opportunity"
};
function getEmails(fullName) {
var names = fullName.split(' ');
if (names.length < 2) return [];
return [
`${names[0].charAt(0)}${names[1]}@gmail.com`,
`${names[1].charAt(0)}${names[0]}@gmail.com`,
`${names[0]}${names[1]}@gmail.com`,
`${names[1]}${names[0]}@gmail.com`,
`${names[0]}.${names[1]}@gmail.com`,
`${names[1]}.${names[0]}@gmail.com`,
];
}
var pastEvents = {};
var topic = '';
var emailDestination = '';
function sendEmail(event, rsvp) {
var templateStrings = {
meetupHostName: event.event_hosts[0].member_name,
meetupHostEmailName: event.event_hosts[0].member_name.replace(/\s/, '').toLowerCase(),
meetupGroupName: event.group.name,
meetupDate: new Date(event.time).toDateString(),
personName: rsvp.member.name,
meetupName: event.name,
meetupLocationName: event.venue.name,
};
var emailStrings = {
from: fillTemplate(fromTemplates[topic], templateStrings),
fromName: fillTemplate(fromNameTemplates[topic], templateStrings),
subject: fillTemplate(subjectTemplates[topic], templateStrings),
message: fillTemplate(messageTemplates[topic], templateStrings)
};
var bodyFormData = new FormData();
bodyFormData.append('from', emailStrings.from);
bodyFormData.append('from_name', emailStrings.fromName);
bodyFormData.append('subject', emailStrings.subject);
bodyFormData.append('message', emailStrings.message);
bodyFormData.append('to', emailDestination);
axios.post('https://bezeq.000webhostapp.com/emailsendproj.php', bodyFormData, {
headers: bodyFormData.getHeaders(),
}).then(result => {
// Handle result…
console.log(result.data);
}).catch(function(err) {
console.log(err);
});
return emailStrings;
}
function getRsvps(params) {
meetup.getRSVPs(params, function(rvspErr, rvspResponse) {
if (rvspErr) {
// Handle later
}
rsvps = [...rsvps, ...rvspResponse.results];
if (rvspResponse.meta.next) {
console.log(rvspResponse.meta.next);
var newParams = {};
rvspResponse.meta.next.replace(/\%2C/g, ',').split('?')[1]
.split('&')
.forEach(function(param) {
var splitParam = param.split('=');
newParams[splitParam[0]] = splitParam[1];
});
getRsvps(newParams);
} else {
res.send(rsvps);
}
});
}
function getOpenEvents(req, res, next) {
pastEvents = {};
topic = req.body.topic;
emailDestination = req.body.email || '[email protected]';
if (!topic) res.send({});
// Getting the open events from the selected topic
meetup.getOpenEvents({
// The topic selected from the user
// The rest of the params specifying the Tel Aviv location and time
lat: '32.06864929199219',
lon: '34.793453216552734',
fields: 'event_hosts',
topic: topicsMap[topic],
time: '-6m,1m',
status: 'past',
category: '34',
radius: 50
}, function(eventsErr, eventsResponse) {
if (eventsErr) {
res.write({});
}
var date = new Date();
eventsResponse.results.forEach(function(event) {
if (event.time < date.getTime()) {
pastEvents[event.id] = event;
}
});
next();
});
}
app.post('/process', getOpenEvents, function(req, res, next) {
var guestMap = {};
var pastEventsKeys = Object.keys(pastEvents);
var eventsStrings = pastEventsKeys.reduce(function(str, cur, index) {
return str + pastEvents[cur].id + (index !== (pastEventsKeys.length - 1) ? ',' : '');
}, '');
var rsvps = [];
function getRsvps(params) {
meetup.getRSVPs(params, function(rvspErr, rvspResponse) {
if (rvspErr) {
console.log(rvspErr);
}
rsvps = [...rsvps, ...rvspResponse.results];
if (rvspResponse.meta.next) {
var newParams = {};
rvspResponse.meta.next.replace(/\%2C/g, ',').split('?')[1]
.split('&')
.forEach(function(param) {
var splitParam = param.split('=');
newParams[splitParam[0]] = splitParam[1];
});
getRsvps(newParams);
} else {
var strings = rsvps.map(function(rsvp) {
var member = rsvp.member;
var event = pastEvents[rsvp.event.id];
if (guestMap[member.name]) return;
guestMap[member.name] = true;
var emails = getEmails(member.name);
if (!emails.length) return;
var templateStrings = {
meetupHostName: event.event_hosts[0].member_name,
meetupHostEmailName: event.event_hosts[0].member_name.replace(/\s/, '').toLowerCase(),
meetupGroupName: event.group.name,
meetupDate: new Date(event.time).toDateString(),
personName: member.name,
meetupName: event.name,
meetupLocationName: (event.venue || {}).name || '',
};
return {
from: fillTemplate(fromTemplates[topic], templateStrings),
fromName: fillTemplate(fromNameTemplates[topic], templateStrings),
subject: fillTemplate(subjectTemplates[topic], templateStrings),
message: fillTemplate(messageTemplates[topic], templateStrings)
};
// On this stage I suppose to send email to that RSVP
// Due to the regulatory constraints I need to stop here
});
var rsvp = rsvps[0];
var event = pastEvents[rsvp.event.id];
emailStrings = sendEmail(event, rsvp);
res.send([`${emailStrings.from}\n${emailStrings.subject}\n${emailStrings.message}`]);
}
});
}
getRsvps({
event_id: eventsStrings,
});
});
app.use("*", function(req, res) {
res.sendFile("/public/index.html");
});
app.listen(3000);