-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.js
63 lines (61 loc) · 1.97 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
var AWS = require("aws-sdk");
console.log('Loading function');
exports.handler = (event, context, callback) => {
var number = (event.query.number === undefined ? "" : event.query.number);
var sns = new AWS.SNS();
var params = {
Protocol: 'sms', /* required */
TopicArn: 'main-arn', /* required */
Endpoint: number,
};
sns.subscribe(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
var welcomeSubParams = {
Protocol: 'sms',
TopicArn: 'welcome-arn', //input welcome arn
Endpoint: number,
};
sns.subscribe(welcomeSubParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
var welcomeParams = {
Message: "welcome to Cat Facts! to unsubscribe visit examle.com",
Subject: "Top Gun Quotes",
TopicArn: "welcome-arn" //input welcome arn
};
sns.publish(welcomeParams,function(err,data){
if (err) console.log(err, err.stack); // an error occurred
else {
removeWelcome()
} // successful response
});
} // successful response
});
function removeWelcome(){
var removeWelcomeParams = {
TopicArn: 'welcome-arn', /* enter your arn */
//NextToken: 'moreSubs'
};
sns.listSubscriptionsByTopic(removeWelcomeParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
for (var i = 0; i<data.Subscriptions.length;i++){
removeARN = data.Subscriptions[i].SubscriptionArn;
unsubscribeSMS(removeARN);
break;
}
}
});
}
function unsubscribeSMS(arn){
var unsubParams = {
SubscriptionArn: arn
};
sns.unsubscribe(unsubParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
};