Skip to content

Instantly share code, notes, and snippets.

@lmammino
Created October 10, 2017 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmammino/8dd69db55b21b06c37383239b5cbaf4f to your computer and use it in GitHub Desktop.
Save lmammino/8dd69db55b21b06c37383239b5cbaf4f to your computer and use it in GitHub Desktop.
Automated SMS notifications with AWS Lambda and Twilio APIs — Commented code

This is the code used the article Automated SMS notifications with AWS Lambda and Twilio APIs published on Twilio's blog

Here follows a detailed description of every part of the code:

  1. The first thing we do is to import the request-promise-native library.
  2. Then we declare our handler function by respecting the expected signature with event, context and callback as described previously.
  3. Inside the handler, we define some constants that we will use as configuration in the rest of the code. Be sure to fill in all the values that are specific to your account.
  4. At this point we are ready to perform the first API request to get the exchange rate from Fixer.io.
  5. Since request-promise-native returns a promise, we have to write a then and a catch method to handle the asynchronous control flow. Inside the then we have get the response data from the API call and we can use it to extract the current exchange rate. We also log the result, which is a good debug practice.
  6. Now we are ready to send the exchange rate to our mobile number using the Twilio API. We do this by using again request-promise-native. This time it’s a POST request and we need to provide the authentication and SMS details as we discussed above.
  7. Again, we have to deal with a promise, so we have to attach another then function to the chain of promises. Notice that in the previous step we used return with the result of the request. This allowed us to propagate the promise in the chain, so that we can share a single catch to handle errors and deal with every asynchronous step with dedicated then hooks. In this last then hook, we just need to log the result and invoke the callback to complete the execution of the Lambda.
  8. At the end of the promise chain we have our generic catch hook. It will be invoked only if any of the promises in the chain rejects (fails). In this case we can only log the error and terminate the execution of the lambda by invoking the callback and propagating the error to AWS by passing it as first parameter to the callback. This will mark the execution as failed.
  9. The last thing we need to do is to export our handler function so that it can be loaded by the AWS runtime. We name the export handler, so that we can later reference this function as index.handler.
// 1.
const request = require('request-promise-native')
// 2.
const handler = (event, context, callback) => {
// 3.
const CURRENCY_FROM = 'GBP'
const CURRENCY_TO = 'EUR'
const TWILIO_ACCOUNT = 'XXXXXX' // add here your Twilio account ID
const TWILIO_API_KEY = 'YYYYYY' // add here your Twilio API key
const SEND_SMS_FROM = '+00123456789' // add here your Twilio phone number
const SEND_SMS_TO = '+0013456789' // add here your Twilio phone number
// 4.
request.get({
url: `http://api.fixer.io/latest?symbols=${CURRENCY_TO}&base=${CURRENCY_FROM}`,
json: true
})
.then((data) => {
// 5.
const rate = data.rates[CURRENCY_TO]
console.log(`Fetched exchange rate for ${CURRENCY_FROM} -> ${CURRENCY_TO}`, rate)
// 6.
return request.post({
url: `https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT}/Messages.json`,
json: true,
'auth': {
'user': TWILIO_ACCOUNT,
'pass': TWILIO_API_KEY
},
form: {
From: SEND_SMS_FROM,
To: SEND_SMS_TO,
Body: `Today you get ${rate} ${CURRENCY_TO} for 1 ${CURRENCY_FROM}`
}
})
})
.then((data) => {
// 7.
console.log(`Rate successfully sent through SMS to ${SEND_SMS_TO}`)
return callback(null, true)
})
.catch((err) => {
// 8.
console.error(err)
return callback(err)
})
}
// 9.
module.exports = { handler }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment