-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws-sms-v3.js
37 lines (31 loc) · 1.25 KB
/
aws-sms-v3.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
async function sendSMS(phoneNumber, Message) {
return new Promise((resolve, reject) => {
const {SNSClient, PublishCommand, SNS} = require("@aws-sdk/client-sns");
const regionObj = {region: "us-east-1"};
const sendMessageParams = {
Message,
PhoneNumber: phoneNumber,
MessageAttributes: {
"AWS.SNS.SMS.SenderID": {
DataType: "String",
StringValue: "Vadlerka"
},
},
};
const snsClient = new SNSClient(regionObj);
const sns = new SNS(regionObj);
sns.checkIfPhoneNumberIsOptedOut({phoneNumber}, async (err, data) => {
if (err) reject(err);
if (data.isOptedOut)
sns.optInPhoneNumber({phoneNumber}, async function (err, data) {
if (err) reject(err);
console.log('block');
resolve(await snsClient.send(new PublishCommand(sendMessageParams)).catch(reject));
});
else
resolve(await snsClient.send(new PublishCommand(sendMessageParams)).catch(reject));
});
});
}
sendSMS("+380509501469", 'This is a text message').then(console.log);
// "+380509501469"