Skip to content

Instantly share code, notes, and snippets.

@tofusoup429
Last active January 13, 2022 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tofusoup429/9e8c07750befb7a0fb4c6bedb9f159c0 to your computer and use it in GitHub Desktop.
Save tofusoup429/9e8c07750befb7a0fb4c6bedb9f159c0 to your computer and use it in GitHub Desktop.
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