-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusershandler.js
505 lines (492 loc) · 19.6 KB
/
usershandler.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
var RC = require('ringcentral')
var fs = require('fs')
var async = require("async");
const RCPlatform = require('./platform.js')
require('dotenv').load()
function User(id, mode) {
this.id = id;
this.extensionId = 0;
this.accountId = 0
this.userName = ""
this.phoneNumbers = []
this.sendReport = {
"sendInProgress": false,
"successCount": "Sending 0/0",
"failedCount" : "",
"invalidNumbers" : []
}
this.mainCompanyNumber = ""
this.detailedReport = []
this.recipientArr = []
this.fromNumber = ""
this.sendMessage = ""
this.sendCount = 0
this.failedCount = 0
this.index = 0
this.delayInterval = 1510
this.intervalTimer = null
this.rc_platform = new RCPlatform(this, mode)
return this
}
var engine = User.prototype = {
setExtensionId: function(id) {
this.extensionId = id
},
setUserName: function (userName){
this.userName = userName
},
getUserId: function(){
return this.id
},
getExtensionId: function(){
return this.extensionId
},
getUserName: function(){
return this.userName;
},
getPlatform: function(){
return this.rc_platform.getSDKPlatform()
},
loadSendSMSPage: function(req, res){
res.render('sendsmspage', {
userName: this.getUserName(),
phoneNumbers: this.phoneNumbers,
sendReport: this.sendReport
})
},
login: function(req, res, callback){
var thisReq = req
if (req.query.code) {
var rc_platform = this.rc_platform
var thisUser = this
rc_platform.login(req.query.code, function (err, extensionId){
if (!err){
thisUser.setExtensionId(extensionId)
req.session.extensionId = extensionId;
callback(null, extensionId)
var thisRes = res
rc_platform.getPlatform(function(err, p){
if (p != null){
p.get('/account/~/extension/~/')
.then(function(response) {
//console.log(response)
var jsonObj = response.json();
//console.log(JSON.stringify(jsonObj))
//thisUser.rc_platform.setAccountId(jsonObj.account.id)
//thisRes.send('login success');
thisUser.accountId = jsonObj.account.id
/*
if (jsonObj.permissions.admin.enabled){
thisUser.setAdmin(true)
}
*/
var fullName = jsonObj.contact.firstName + " " + jsonObj.contact.lastName
thisUser.setUserName(fullName)
engine.readPhoneNumber(thisUser, callback, thisRes)
})
.catch(function(e) {
console.log("Failed")
console.error(e);
callback("error", e.message)
});
}else{
console.log("CANNOT LOGIN")
callback("error", thisUser.extensionId)
}
})
}else {
console.log("USER HANDLER ERROR: " + thisUser.extensionId)
callback("error", thisUser.extensionId)
}
})
} else {
res.send('No Auth code');
callback("error", null)
}
},
readPhoneNumber: function(thisUser, callback, thisRes){
thisUser.rc_platform.getPlatform(function(err, p){
if (p != null){
thisUser.phoneNumbers = []
var endpoint = '/account/~/extension/~/phone-number'
p.get(endpoint, {
"perPage": 1000,
"usageType": ["MainCompanyNumber", "CompanyNumber", "DirectNumber"]
})
.then(function(response) {
//console.log(response)
var jsonObj =response.json();
var count = jsonObj.records.length
for (var record of jsonObj.records){
//console.log("recordid: " + JSON.stringify(record))
if (record.paymentType == "TollFree") {
//if (record.usageType == "DirectNumber"){
if (record.type == "VoiceFax" || record.type == "VoiceOnly"){
for (var feature of record.features){
if (feature == "SmsSender"){
var item = {
"format": formatPhoneNumber(record.phoneNumber),
"number": record.phoneNumber,
"type": "Toll-Free Number"
}
thisUser.phoneNumbers.push(item)
break;
}
}
}
}
if (record.usageType == "MainCompanyNumber"){
thisUser.mainCompanyNumber = formatPhoneNumber(record.phoneNumber)
}
}
thisRes.send('login success');
})
.catch(function(e) {
console.log("Failed")
console.error(e.message);
thisRes.send('login success');
});
}else{
console.error(e.message);
thisRes.send('login failed');
}
})
},
sendSMSMessageSync: function(req, res){
this.recipientArr = []
if (req.file != undefined){
var currentFolder = process.cwd();
var tempFile = currentFolder + "/" + req.file.path
var fs = require('fs');
var content = fs.readFileSync(tempFile, 'utf8');
content = content.trim();
this.recipientArr = content.split("\n")
this.recipientArr.shift()
fs.unlinkSync(tempFile);
}
var tempArr = req.body.recipients.split("\r\n")
for (var number of tempArr){
number = number.trim()
if (number != "")
this.recipientArr.unshift(number)
}
this.fromNumber = req.body.fromNumber
this.sendMessage = req.body.message
this.sendCount = 0
this.failedCount = 0
this.index = 0
this.detailedReport = []
this.sendReport = {
sendInProgress: true,
successCount: "Sent 0/" + this.recipientArr.length,
failedCount : "Failed 0",
invalidNumbers: []
}
res.render('sendsmspage', {
userName: this.getUserName(),
phoneNumbers: this.phoneNumbers,
sendReport: this.sendReport
})
if (this.recipientArr.length > 0){
console.log("CONTINUE PROSESSING")
engine.sendMessages(this)
}
},
sendMessages: function(thisUser){
var currentIndex = thisUser.index
var totalCount = thisUser.recipientArr.length
thisUser.intervalTimer = setInterval(function() {
if (currentIndex == thisUser.index){
// this will prevent sending a new message while the previous message was not sent
currentIndex++
if (thisUser.index >= totalCount){
clearInterval(thisUser.intervalTimer);
thisUser.intervalTimer = null
console.log('ALL RECIPIENT!');
thisUser.sendReport['sentInfo'] = "Completed."
thisUser.sendReport['sendInProgress'] = false
return
}
//console.log("index: " + thisUser.index)
//console.log("recipient: " + thisUser.recipientArr[thisUser.index])
var recipient = thisUser.recipientArr[thisUser.index].trim()
var unsentCount = totalCount - thisUser.index
var timeLeft = formatEstimatedTimeLeft(unsentCount * (thisUser.delayInterval/1000))
/*
var remainMinutesToSend = (unsentCount * (thisUser.delayInterval/1000)) / 60
var timeLeft = "00 hour, "
if (remainMinutesToSend >= 60){
timeLeft = Math.floor((remainMinutesToSend / 60)) + " hours and "
remainMinutesToSend %= 60
timeLeft += Math.ceil(remainMinutesToSend).toString() + " minutes."
}else if (remainMinutesToSend < 1){
var round = Number(remainMinutesToSend).toFixed(2);
timeLeft = (round * 60) + " seconds."
}else{
timeLeft = Math.ceil(remainMinutesToSend).toString() + " minutes."
}
*/
thisUser.rc_platform.getPlatform(function(err, p){
if (p != null){
var params = {
from: {'phoneNumber': thisUser.fromNumber},
to: [{'phoneNumber': recipient }],
text: thisUser.sendMessage
}
p.post('/account/~/extension/~/sms', params)
.then(function (response) {
var jsonObj = response.response().headers
/*
console.log("limitLimit " + jsonObj['_headers']['x-rate-limit-limit'][0])
console.log("limitRemaining " + jsonObj['_headers']['x-rate-limit-remaining'][0])
console.log("limitWindow" + jsonObj['_headers']['x-rate-limit-window'][0])
var limitLimit = parseInt(jsonObj['_headers']['x-rate-limit-limit'][0])
var limitRemaining = parseInt(jsonObj['_headers']['x-rate-limit-remaining'][0])
var limitWindow = parseInt(jsonObj['_headers']['x-rate-limit-window'][0])
if (limitRemaining == 0){
console.log("out of limit")
thisUser.delayInterval = 60000
}else
thisUser.delayInterval = ((limitWindow/limitLimit) * 1000) + 100
console.log(thisUser.delayInterval)
*/
var jsonObj = response.json()
var item = {
"id": jsonObj.id,
"uri": jsonObj.uri,
"creationTime": jsonObj.creationTime,
"from": thisUser.fromNumber,
"status": jsonObj.messageStatus,
"smsDeliveryTime": jsonObj.smsDeliveryTime,
"smsSendingAttemptsCount": jsonObj.smsSendingAttemptsCount,
"to": recipient
}
thisUser.detailedReport.push(item)
thisUser.sendCount++
thisUser.index++
thisUser.sendReport['sentInfo'] = "Estimated time to finish " + timeLeft
thisUser.sendReport['successCount'] = "Sent " + thisUser.sendCount + " out of " + totalCount
//console.log(thisUser.sendReport['successCount'])
if (thisUser.index >= totalCount){
console.log('DONE SEND MESSAGE!');
//thisUser.sendReport['sendInProgress'] = false
}
})
.catch(function(e){
thisUser.index++
thisUser.failedCount++
thisUser.sendReport['failedCount'] = "Failed " + thisUser.failedCount
thisUser.sendReport['sentInfo'] = "Estimated time to finish " + timeLeft
var reason = ""
if (e.message.indexOf("Parameter [to.phoneNumber] value") != -1){
reason = "Invalid recipient number."
var item = {
"number": recipient,
"reason": reason
}
//thisUser.sendReport['invalidNumbers'].push(item)
}else if (e.message.indexOf("Parameter [from] value") != -1){
reason = "Invalid sender number."
var item = {
"number": thisUser.fromNumber,
"reason": "Invalid sender number."
}
//thisUser.sendReport['invalidNumbers'].push(item)
console.log('STOP SENDING BECAUSE OF INVALID FROM NUMBER!');
clearInterval(thisUser.intervalTimer);
thisUser.intervalTimer = null
console.log('ALL RECIPIENT!');
thisUser.sendReport['sendInProgress'] = false
return
}else if (e.message.indexOf("Account limits exceeded. Cannot send the message.") != -1){
if (thisUser.intervalTimer != null){
console.log("Force to pause sending due to exceeding limits!")
clearInterval(thisUser.intervalTimer);
}
}else{
reason = e.message
var item = {
"number": "N/A",
"reason": reason
}
//thisUser.sendReport['invalidNumbers'].push(item)
}
var item = {
"id": 0,
"uri": "",
"creationTime": new Date().toISOString(),
"from": thisUser.fromNumber,
"status": reason,
"smsDeliveryTime": "",
"smsSendingAttemptsCount": 0,
"to": recipient
}
thisUser.detailedReport.push(item)
if (thisUser.index >= totalCount){
console.log('DONE SEND MESSAGE!');
}
})
}
})
}
else{
console.log("not sending while previous message status is pending")
}
}, thisUser.delayInterval);
},
setDelayInterVal: function(req, res){
this.intervalTimer = req.query.interval
res.send({"status":"ok", "message":"set interval"})
},
pauseMessageSending: function(req, res){
if (this.intervalTimer != null){
console.log("pauseMessageSending")
clearInterval(this.intervalTimer);
res.send({"status":"ok", "message":"pause sending"})
}else
res.send({"status":"failed", "message":"cannot pause sending"})
},
resumeMessageSending: function(req, res){
if (this.intervalTimer != null){
console.log("resumeMessageSending")
engine.sendMessages(this)
res.send({"status":"ok", "message":"resume sending"})
}else
res.send({"status":"failed", "message":"cannot resume sending"})
},
cancelMessageSending: function(req, res){
if (this.intervalTimer != null){
console.log("cancelMessageSending")
clearInterval(this.intervalTimer);
this.intervalTimer = null
}
res.send({"status":"ok", "message":"cancel timer"})
},
getSendSMSResult: function(req, res){
if (this.recipientArr.length == 0){
this.sendReport = {
sendInProgress: false,
sentInfo: "Nothing to send!",
successCount: "Sent 0/0",
failedCount : "Failed 0",
invalidNumbers: []
}
}
res.send(this.sendReport)
},
downloadSendSMSResult: function(req, res){
var fs = require('fs')
var dir = "reports/"
if(!fs.existsSync(dir)){
fs.mkdirSync(dir)
}
var fullNamePath = dir + this.getExtensionId()
var fileContent = ""
if (req.query.format == "JSON"){
fullNamePath += '.json'
fileContent = JSON.stringify(this.detailedReport)
}else{
fullNamePath += '.csv'
fileContent = "id,uri,creationTime,fromNumber,status,smsSendingAttemptsCount,toNumber"
for (var item of this.detailedReport){
fileContent += "\n"
fileContent += item.id + "," + item.uri + "," + item.creationTime + "," + item.from + "," + item.status + "," + item.smsSendingAttemptsCount + "," + item.to
}
}
try{
fs.writeFileSync('./'+ fullNamePath, fileContent)
var link = "/downloads?filename=" + fullNamePath
res.send({"status":"ok","message":link})
//res.send(link)
}catch (e){
console.log("cannot create report file")
res.send({"status":"failed","message":"Cannot create a report file! Please try gain"})
}
},
logout: function(req, res, callback){
console.log("LOGOUT FUNC")
var p = this.rc_platform.getPlatform(function(err, p){
if (p != null){
p.logout()
.then(function (token) {
console.log("logged out")
//p.auth().cancelAccessToken()
//p = null
callback(null, "ok")
})
.catch(function (e) {
console.log('ERR ' + e.message || 'Server cannot authorize user');
callback(e, e.message)
});
}else{
callback(null, "ok")
}
})
},
postFeedbackToGlip: function(req){
post_message_to_group(req.body, this.mainCompanyNumber, this.accountId)
}
}
module.exports = User;
function formatEstimatedTimeLeft(timeInSeconds){
var duration = ""
if (timeInSeconds > 3600){
var h = Math.floor(timeInSeconds / 3600)
timeInSeconds = timeInSeconds % 3600
var m = Math.floor(timeInSeconds / 60)
m = (m>9) ? m : ("0" + m)
timeInSeconds = Math.floor(timeInSeconds % 60)
var s = (timeInSeconds>9) ? timeInSeconds : ("0" + timeInSeconds)
return h + ":" + m + ":" + s
}else if (timeInSeconds > 60){
var m = Math.floor(timeInSeconds / 60)
timeInSeconds = Math.floor(timeInSeconds %= 60)
var s = (timeInSeconds>9) ? timeInSeconds : ("0" + timeInSeconds)
return m + ":" + s
}else{
var s = (timeInSeconds>9) ? timeInSeconds : ("0" + timeInSeconds)
return "0:" + s
}
}
function formatPhoneNumber(phoneNumberString) {
var cleaned = ('' + phoneNumberString).replace(/\D/g, '')
var match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/)
if (match) {
var intlCode = (match[1] ? '+1 ' : '')
return [intlCode, '(', match[2], ') ', match[3], '-', match[4]].join('')
}
return phoneNumberString
}
function post_message_to_group(params, mainCompanyNumber, accountId){
var https = require('https');
var message = params.message + "\n\nUser main company number: " + mainCompanyNumber
message += "\nUser account Id: " + accountId
message += "\nSalesforce lookup: https://rc.my.salesforce.com/_ui/search/ui/UnifiedSearchResults?str=" + accountId
message += "\nAI admin lookup: https://admin.ringcentral.com/userinfo/csaccount.asp?user=XPDBID++++++++++" + accountId + "User"
var body = {
"icon": "http://www.qcalendar.com/icons/" + params.emotion + ".png",
"activity": params.user_name,
"title": "SMS Toll-Free app user feedback - " + params.type,
"body": message
}
var post_options = {
host: "hooks.glip.com",
path: "/webhook/ab875aa6-8460-4be2-91d7-9119484b4ed3",
method: "POST",
headers: {
'Content-Type': 'application/json'
}
}
var post_req = https.request(post_options, function(res) {
var response = ""
res.on('data', function (chunk) {
response += chunk
});
res.on("end", function(){
console.log(response)
});
});
//console.log(data)
post_req.write(JSON.stringify(body));
post_req.end();
}