-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathzoomParser.js
365 lines (316 loc) · 11.3 KB
/
zoomParser.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const request = require("request-promise");
const url = require("url");
const { get } = require("lodash");
const regex = /(.*).*[.,\s].*Meeting ID:[\s]*([\d\s]+)?/gm;
const phoneNumberRegex = /[+][\s,0-9].*/gm;
const urlRegex = /.*https:\/\/.*[.]*zoom.us\/j\/(\d+)/gm;
const passwordRegex = /Passcode:[\s]*([\d]+)/gim;
const joiningInstructionsRegex = /Joining instructions:(.*)/gim;
const standardPhoneNumbers = { US: ["+16465588656", "+14086380968"] };
const keys = ["Meeting ID:", "Joining instructions", "zoom.us/j/"];
const getPhoneNumbers = async (content, resetRegex = true) => {
if (resetRegex) {
phoneNumberRegex.lastIndex = 0;
}
let match;
const phoneNumbers = [];
let country = null;
while ((match = phoneNumberRegex.exec(content)) !== null) {
const splitStr = match[0].replace(/[\\,]+/g, " ").split(" ");
//Add check for length of phone number
splitStr[0].length > 9 &&
phoneNumbers.push(splitStr[0].trim().replace(/ /g, ""));
if (splitStr.length >= 3 && !country) country = splitStr[2].trim();
}
return {
country: country,
phoneNumbers: phoneNumbers,
};
};
const getJoiningInstructions = async (joiningInstructionsUrl) => {
if (joiningInstructionsUrl) {
const zoomJoiningInstructionsUrl = get(
url.parse(joiningInstructionsUrl, true),
"query.q",
null
);
if (zoomJoiningInstructionsUrl) {
const requestOptions = {
uri: zoomJoiningInstructionsUrl,
method: "GET",
resolveWithFullResponse: true,
};
const response = await request(requestOptions);
if (response && response.statusCode === 200) {
return response.body;
} else {
return null;
}
} else {
logger.info("Joining Instructions URL not found", {
joiningInstructionsUrl,
});
}
}
return null;
};
const isNumeric = (str) => {
return /^\d+$/.test(str);
};
const getPasswordFromPayload = async (payload, resetRegex = true) => {
if (resetRegex) {
passwordRegex.lastIndex = 0;
}
let meetingPasswordData;
while ((meetingPasswordData = passwordRegex.exec(payload)) != null) {
if (
meetingPasswordData &&
meetingPasswordData.length === 2 &&
isNumeric(meetingPasswordData[1])
) {
return meetingPasswordData[1];
}
}
};
const constructJoiningDetails = async ({
country,
phoneNumbers,
content,
meetingPassword,
meetingId,
}) => {
let extraStandardNumbers = standardPhoneNumbers[country] || [];
extraStandardNumbers = extraStandardNumbers.filter(
(number) => !phoneNumbers.includes(number)
);
if (extraStandardNumbers.length > 0) {
extraStandardNumbers.forEach((number) => phoneNumbers.push(number));
}
const dtmfFromUrl = urlRegex.exec(content);
let dtmf =
dtmfFromUrl && dtmfFromUrl.length === 2
? dtmfFromUrl[1].trim().includes("?")
? dtmfFromUrl[1].trim().split("?")[0].concat("#")
: dtmfFromUrl[1].trim().concat("#")
: meetingId.trim().replace(/ /g, "").concat("#");
if (meetingPassword) {
dtmf = dtmf.concat(`,,${meetingPassword}#`);
}
return {
joiningDetails: [
{
country,
phoneNumbers,
dtmf,
},
],
};
};
const zoomParser = () => {
const patternToParser = {};
patternToParser[keys[0]] = async (content) => {
if (content) {
const result = regex.exec(content);
if (result && result.length === 3) {
let { country, phoneNumbers } = await getPhoneNumbers(content, false);
let meetingPassword = await getPasswordFromPayload(content, false);
const joiningInstructionsUrl = get(
joiningInstructionsRegex.exec(content),
"[1]",
null
);
let joiningInstructions = null;
if (joiningInstructionsUrl)
joiningInstructions = await getJoiningInstructions(
joiningInstructionsUrl
);
if (
(!phoneNumbers || phoneNumbers.length <= 0) &&
joiningInstructions
) {
const data = await getPhoneNumbers(joiningInstructions);
phoneNumbers = data.phoneNumbers;
country = data.country;
}
if (!meetingPassword) {
meetingPassword = await getPasswordFromPayload(joiningInstructions);
}
if (!country) country = "US";
return await constructJoiningDetails({
country,
phoneNumbers,
meetingPassword,
content,
meetingId: result[2],
});
}
}
return null;
};
patternToParser[keys[1]] = async (content) => {
if (content) {
let joiningInstructions = null;
const joiningInstructionsUrl = get(
joiningInstructionsRegex.exec(content),
"[1]",
null
);
if (joiningInstructionsUrl) {
joiningInstructions = await getJoiningInstructions(
joiningInstructionsUrl
);
if (joiningInstructions) {
let meetingPassword = await getPasswordFromPayload(
joiningInstructions,
false
);
let { phoneNumbers, country } = await getPhoneNumbers(
joiningInstructions,
false
);
if (!country) country = "US";
const meetingId = get(regex.exec(joiningInstructions), "[2]", null);
return await constructJoiningDetails({
country,
phoneNumbers,
meetingPassword,
content: joiningInstructions,
meetingId,
});
}
}
}
return null;
};
patternToParser[keys[2]] = async (content) => {
if (content) {
const result = urlRegex.exec(content.trim());
if (result && result.length === 2) {
let dtmf = result[1].trim();
if (dtmf.includes("?")) {
dtmf = dtmf.split("?")[0];
}
dtmf = dtmf.concat("#");
const meetingPasswordData = passwordRegex.exec(content);
let meetingPassword =
meetingPasswordData && meetingPasswordData.length === 2
? meetingPasswordData[1]
: null;
if (meetingPassword) {
dtmf = dtmf.concat(`,,${meetingPassword}#`);
}
return {
joiningDetails: [
{
country: "US",
phoneNumbers: standardPhoneNumbers["US"],
dtmf,
},
],
};
}
return null;
}
};
return {
isValid(content) {
return content && keys.filter((key) => content.includes(key)).length > 0;
},
async parse(content) {
if (content) {
let parser = null;
keys.some((key, index) => {
if (content.toLowerCase().includes(key.toLowerCase())) {
parser = patternToParser[keys[index]];
return true;
}
return false;
});
if (parser) {
regex.lastIndex = 0;
urlRegex.lastIndex = 0;
phoneNumberRegex.lastIndex = 0;
passwordRegex.lastIndex = 0;
joiningInstructionsRegex.lastIndex = 0;
return await parser(content);
}
} else {
return null;
}
},
};
};
// const sample = "Aditya W is inviting you to a scheduled Zoom meeting.\n" +
// "\n" +
// "Join Zoom Meeting\n" +
// "https://zoom.us/j/996291789\n" +
// "\n" +
// "One tap mobile\n" +
// "+16465588656,,996291787# US (New York)\n" +
// "+14086380968,,996291787# US (San Jose)\n" +
// "\n" +
// "Dial by your location\n" +
// " +1 646 558 8656 US (New York)\n" +
// " +1 408 638 0968 US (San Jose)\n" +
// "Meeting ID: 996 291 787" +
// "Find your local number: https://zoom.us/u/acLLoulOb";
// const parser = zoomParser();
// if (parser.isValid(sample)) {
// const res = parser.parse(sample);
// console.log(res);
// }
module.exports = zoomParser;
// const sample = `Hi there,
//
// Toshish Jawale is inviting you to a scheduled Zoom meeting.
//
// Join from PC, Mac, Linux, iOS or Android: https://zoom.us/j/476452611
//
// Or iPhone one-tap :
// US: +16465588665,,476452611# or +14086380986,,476452611#
// Or Telephone:
// Dial(for higher quality, dial a number based on your current location):
// US: +1 646 558 8665 or +1 408 638 0986
// Meeting ID: 476 452 611
// International numbers available: https://zoom.us/u/yeuSXkkN`;
//
// const parser = zoomParser();
// if (parser.isValid(sample)) {
// const res = parser.parse(sample);
// console.log(res);
// }
//
// const sample1 = `You have been invited to the following event.\n\nTitle: Again + with pass+ pro\nWhen: Mon Apr 6, 2020 4pm – 5pm India Standard Time - Kolkata\n\nJoining info: Join Zoom Meeting\nhttps://zoom.us/j/6237467470?pwd=dmdZUGVCZGRQMGpaTzVFanFzY0ZmUT09 (ID: 6237467470, password: abhay)\n\nJoin by phone\n(US) +1 312-626-6799\n\nJoining instructions: https://www.google.com/url?q=https://applications.zoom.us/addon/invitation/detail?meetingUuid%3DM6vt6D0HTyO52941hhamBg%253D%253D%26signature%3Daa6880d40d38c68cbbc61fd1caad26b3dd428a9ee160ff8be2f6b74dc116c85a&sa=D&usg=AOvVaw3ur1l5U69uEB2MWcCPJcLx
// \n\nJoining notes: Password: abhay\n\nCalendar: [email protected]\nWho:\n * [email protected] - organizer\n * [email protected]\n * [email protected] - optional\n\nYour attendance is optional.\n\nEvent details: https://www.google.com/calendar/event?action=VIEW&eid=NmswbG40MnEyMTJlZXZkamlvOW1raHAzNTAgbWVldGluZ2luc2lnaHRzQG1lZXQtaHViLnN5bWJsLmFp&tok=MjAjYWJoYXkuZGFsdmlAc3ltYmwuYWkwNTc3NGUwZTdjZjUwOWRjOGE2M2EzYzFkYmU3ZjAxYzYyMjdlZWJh&ctz=Asia%2FKolkata&hl=en&es=0\n\nInvitation from Google Calendar: https://www.google.com/calendar/\n\nYou are receiving this courtesy email at the account [email protected] because you are an attendee of this event.\n\nTo stop receiving future updates for this event, decline this event. Alternatively you can sign up for a Google account at https://www.google.com/calendar/ and control your notification settings for your entire calendar.\n\nForwarding this invitation could allow any recipient to send a response to the organizer and be added to the guest list, or invite others regardless of their own invitation status, or to modify your RSVP. Learn more at https://support.google.com/calendar/answer/37135#forwarding\n" {"htmlDecodedContent":"You have been invited to the`;
// const sample1 = `Zoom logo
// Arjun Chouhan is inviting you to a scheduled Zoom meeting.
//
// Topic: This is a calendar meeting
// Time: Apr 6, 2020 01:44 PM India
//
// Join Zoom Meeting
// https://zoom.us/j/222349200?pwd=VGFKNUZUMmYrT2NIbTFUdkxCc2hVdz09
//
// Meeting ID: 222 349 200
// Password: gh7zfz
//
// One tap mobile
// +12532158782,,222349200# US
// +13017158592,,222349200# US
//
// Dial by your location
// +1 253 215 8782 US
// +1 301 715 8592 US
// +1 312 626 6799 US (Chicago)
// +1 346 248 7799 US (Houston)
// +1 646 558 8656 US (New York)
// +1 669 900 9128 US (San Jose)
// Meeting ID: 222 349 200
// Password: 526261
// Find your local number: https://zoom.us/u/aNEntYu01`;
//
// if (parser.isValid(sample1)) {
// parser.parse(sample1).then(res => {
// console.log(res, res.joiningDetails[0].phoneNumbers)
// });
// }