-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (76 loc) · 2.49 KB
/
server.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
const express = require('express')
const AWS = require('aws-sdk')
const app = express()
app.use(express.json())
AWS.config.update({ region: 'us-east-1' });
const sns = new AWS.SNS({ apiVersion: '2010-03-31' })
const TOPIC_ARN = 'arn:aws:sns:us-east-1:971410147123:sms-topic'
app.get('/', async (req, res) => {
const result = await sns.listSubscriptionsByTopic({ TopicArn: TOPIC_ARN }).promise()
return res.json(result)
})
app.post('/register/:number', async (req, res) => {
const { number } = req.params
const params = {
PhoneNumber: number,
};
await sns.createSMSSandboxPhoneNumber(params).promise()
return res.json({ success: true })
})
app.post('/confirm-register/:number', async (req, res) => {
const { number } = req.params
const { Token } = req.body
console.log({Token})
await sns.verifySMSSandboxPhoneNumber({
PhoneNumber: number,
OneTimePassword: Token
}).promise()
return res.json({ success: true })
})
app.post('/subscribe/:number', async (req, res) => {
const { number } = req.params
console.log({ number })
const params = {
Protocol: 'sms',
TopicArn: TOPIC_ARN,
Endpoint: number
}
await sns.subscribe(params).promise()
return res.json({ success: true })
})
app.post('/unsubscribe/:number', async (req, res) => {
const { number } = req.params
try {
const result = await sns.listSubscriptionsByTopic({ TopicArn: TOPIC_ARN }).promise()
const subscriptionArn = result.Subscriptions.find(sub => String(sub.Endpoint) === number).SubscriptionArn
if(!subscriptionArn) throw 'Subscription not found'
await sns.unsubscribe({
SubscriptionArn:subscriptionArn
}).promise()
return res.json({ success: true })
} catch(err) {
return res.json({ success: false, error: err })
}
})
app.post('/send', async (req, res) => {
const Message = req.body.Message
await sns.publish({
Message: Message,
TopicArn: TOPIC_ARN,
}).promise()
return res.json({ success: true })
})
app.post('/send/:number', async (req, res) => {
const Message = req.body.Message
const PhoneNumber = req.params.number
console.log({ Message, PhoneNumber })
await sns.publish({
Message: Message,
PhoneNumber: PhoneNumber,
}).promise()
return res.json({ success: true })
})
const PORT = 8080
app.listen(PORT, () => {
console.log(`APP runing in port ${PORT}`)
})