Last active
January 13, 2022 00:17
-
-
Save tofusoup429/9e8c07750befb7a0fb4c6bedb9f159c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require('aws-sdk'); | |
AWS.config.update({region: 'us-east-1'}); //Set Region | |
exports.handler = (event) => { | |
return new Promise((res, rej)=>{ | |
const sns = new AWS.SNS(); | |
console.log('event', event); | |
let {phonenumber, message, requestContext} = JSON.parse(event.body); | |
let {http} = event.requestContext; | |
if(http.method ==='POST'){ | |
if(http.path.includes('request-six-digit-confirmation-code')){ // executed differently depending on path | |
let randomSixDigits = Math.floor(100000 + Math.random() * 900000); // 6 digits must start with 1-9, not 0 | |
let messageWrapper = `${message} [${randomSixDigits}]` // app decides what message is, lambda decides randomSixDigits | |
let params = { | |
"PhoneNumber":phonenumber, | |
"Message":messageWrapper | |
}; | |
sns.publish(params, function(err, data) { | |
if (err){ | |
console.log('err', err); | |
rej({statusCode:'500'}) | |
} // an error occurred | |
else res({statusCode:'200',message:data}) // successful response | |
}) | |
} | |
} | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment