Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created August 24, 2018 00:45
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 astrotars/c20037e56908cf4f1c5fa4f8c95d100b to your computer and use it in GitHub Desktop.
Save astrotars/c20037e56908cf4f1c5fa4f8c95d100b to your computer and use it in GitHub Desktop.
// Dependencies
const AWS = require('aws-sdk');
const sns = new AWS.SNS();
const moment = require('moment');
exports.handler = (event, context, callback) => {
// Parse incoming event payload
const payload = JSON.parse(event.Records[0].body)[0].new[0];
// This should be your database
const cache = {
actors: ['Nick'],
verbs: {
tweet: 'tweeted'
}
};
const actor = cache.actors[0]; // Hardcoding user, should be a database lookup
const verb = cache.verbs[payload.verb]; // Hardcoding verb (should be a ternary or if/else statement)
const tweet = payload.tweet; // The example always contains a tweet
const time = moment(payload.time).format('MMMM Do YYYY [at] h:mm:ss A'); // Time is ISO-8601 formatted
// Build params
const params = {
Message: `${actor} ${verb} "${tweet}" on ${time}`, // SMS message that gets sent to the subscriber (AKA your device)
TopicArn: 'YOUR_AWS_SNS_TOPIC_ARN' // Only works on localhost (hardcode value for AWS deployments)
};
// Send a SMS via AWS SNS
sns.publish(params, (err, data) => {
if (err) {
return callback(err.stack);
} else {
callback(null, data);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment