-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
75 lines (65 loc) · 2.07 KB
/
utils.js
File metadata and controls
75 lines (65 loc) · 2.07 KB
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
var libphonenumber = require('google-libphonenumber');
var AWS = require('aws-sdk');
var ms = require('ms');
// Change it as per AWS SNS config
// AWS.config.region = '<aws.region>';
// AWS.config.update({
// accessKeyId: '<aws.accessKeyId>',
// secretAccessKey: '<aws.secretAccessKey>',
// });
// var sns = new AWS.SNS();
// sns.setSMSAttributes(
// {
// attributes: {
// DefaultSMSType: "Transactional"
// }
// },
// function (error) {
// if (error) {
// console.log(error);
// } else {
// console.log("AWS SNS instance initiated!");
// }
// }
// );
function normalizePhoneNumber(phone) {
return new Promise((resolve, reject) => {
if (phone) {
var isValid;
var normalize;
try {
var phoneNumber = libphonenumber.PhoneNumberUtil.getInstance().parse(phone, "IN"); //with default country
isValid = libphonenumber.PhoneNumberUtil.getInstance().isValidNumber(phoneNumber);
normalize = libphonenumber.PhoneNumberUtil.getInstance().format(phoneNumber, libphonenumber.PhoneNumberFormat.E164);
} catch (e) {
console.error("NumberParseException was thrown: " + e.toString());
}
if (isValid && normalize) {
resolve(normalize);
} else {
reject("Invalid phone number formatting!");
}
}
});
}
function sendSmsUsingAwsSns(phone, msg) {
return new Promise((resolve, reject) => {
var params = {
Message: msg,
MessageStructure: 'string',
PhoneNumber: phone
};
sns.publish(params, (err, data) => {
if (err) {
console.log(err, err.stack); // an error occurred
reject(err);
} else {
console.log(data); // successful response
resolve(data);
}
});
});
}
module.exports = {
normalizePhoneNumber: normalizePhoneNumber,
};