-
Notifications
You must be signed in to change notification settings - Fork 2
/
subscribe.js
80 lines (73 loc) · 2.18 KB
/
subscribe.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
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
AWS.config.update({ region: "REGION" });
var sns = new AWS.SNS({ apiVersion: "2010-03-31" });
var number = "";
function subscribeByEmail(email) {
// Create subscribe/email parameters
var params = {
Protocol: "EMAIL" /* required */,
TopicArn: "arn:aws:sns:ap-south-1:639661757204:Test-Topic" /* required */,
Endpoint: email,
};
// Create promise and SNS service object
var subscribePromise = new AWS.SNS({ apiVersion: "2010-03-31" })
.subscribe(params)
.promise();
// Handle promise's fulfilled/rejected states
subscribePromise
.then(function (data) {
console.log("Subscription ARN is " + data.SubscriptionArn);
})
.catch(function (err) {
console.error(err, err.stack);
});
return;
}
function sendOtp(phoneNumber) {
this.number = phoneNumber;
var params = {
PhoneNumber: `"+91" + ${phoneNumber}` /* required */,
LanguageCode: "en-US",
};
sns.createSMSSandboxPhoneNumber(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data);
console.log("Otp Send");
} // successful response
});
// return "Otp Send";
}
function subscribeByPhoneNumber(phoneNumber, otp) {
var params = {
OneTimePassword: otp /* required */,
PhoneNumber: `"+91" + ${number}` /* required */,
};
sns.verifySMSSandboxPhoneNumber(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data);
var params = {
Protocol: "SMS" /* required */,
TopicArn:
"arn:aws:sns:ap-south-1:639661757204:Test-Topic" /* required */,
// Attributes: {
// '<attributeName>': 'STRING_VALUE',
/* '<attributeName>': ... */
//},
Endpoint: number,
ReturnSubscriptionArn: true || false,
};
sns.subscribe(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
} // successful response
});
}
module.exports = {
subscribeByEmail,
subscribeByPhoneNumber,
sendOtp,
};