NASANEWS: Serverless SMS Service with a Short Code in the cloud

60

As we’ve seen in previous posts like “2-Hour Hackathon and now you can send SMS from Hubot“, hackathons are always a great way to get together with your team and play around with your favorite toys. But also, they free your mind to create wonderful things. In this post we’re going to see an example of such a thing.

Meet NASANEWS, an SMS service accessible via a short code and that lives completely in the cloud, combining the power of our HTTP API with Amazon Web Services. That’s right, no servers needed at all to handle your subscriptions and messages, and the operational cost for this is less than $0.60/month. Neat, uh? 🙂

If you are in the US, you can subscribe right now to it by sending NASANEWS to the number 32458. And of course, the terms and conditions can be found here https://goo.gl/HiH5DC.

Full Open Source Code

For the impatient geeks, let us start by saying that we have made the code completely open source and available in our GitHub account at https://github.com/PortaText/nasanews-shortcode-serverless/ so yeah, you can skip everything else and go ahead and check it (or clone it) out and start hacking if you want to 🙂

Disclaimer

This is just a mere example of an SMS service that uses an RSS feed from NASA to send daily breaking news about NASA and its activities. The National Aeronautics and Space Administration (NASA) does not endorse, sponsor, promote or is affiliated in any way with PortaText or this service, and it’s also not responsible for it in any way.

That being said, we can continue our journey 🙂

What it does

This is an SMS Service that “lives” in the short code 32458 under the keyword NASANEWS, and it’s available for the US only.

It is a single opt-in service that will subscribe you as soon as you text that keyword and will send you breaking news taken from the official NASA RSS feed at https://www.nasa.gov/rss/dyn/breaking_news.rss when available. You will receive at most 1 message per day.

General Architecture: what you need to host your own SMS Short Code Service in the cloud

Servers? Where we're going, we don't need servers.

Servers? Where we’re going, we don’t need servers.

We are going to use NodeJS code running in AWS Lambda, and the full code is in the index.js file. Overall, here’s what’s needed:

How it Works

  • Every day, index.fetch will be run by a CloudWatch Event and will download and parse the RSS feed, saving the latest in DynamoDB
  • When a new item is detected, an event is sent through SNS that will be handled by index.createCampaign and use the PortaText API to create a new campaign
  • When a new campaign is created, PortaText will notify via SNS and index.handleNotifications will start this new campaign, so the news is sent to all the subscribers
  • When a user sends NASANEWS to 32458, PortaText sends an opt-in notification via SNS
  • index.handleNotifications will notice this and will grab the latest item from DynamoDB and send it to this new subscriber. This lambda will handle the needed PortaText set of notifications for interesting events.

Deploying the service

The next section will describe the gory details of how to setup everything to have it up and running. If you’re not a “techie guy/girl” you can call it a day and go spend some time with the family now 🙂

Create an SNS topic to receive the events from PortaText

Create an AWS SNS topic in your AWS account in order to receive notifications from PortaText via an SNS Topic. Let’s call this one “PortaTextEvents“. Create a key/secret for a user with access to publish in that topic.

PortaText Account configuration

First of all, you should have a PortaText account and take note of your API key in user panel.

Then, request your SMS service for the given short code and keyword. Don’t forget to first write your terms and conditions somewhere, for this service these are available at https://www.portatext.com/terms-and-conditions/nasa-news.html. Once the service is approved, take note of the SMS Service ID, we’re going to use this one when sending messages from AWS Lambda.

Create a template and include the variables {{link}} and {{title}} in it. Take note of the template ID.

Once everything’s in place, setup your account to receive SNS notifications, as described in our previous post “Receive notifications with Amazon SNS“. The topic, key, and secret should be the ones created earlier.

Get a Google Application API Key to use Google URL Shortener

Follow the instructions at Getting Started guide for Developers for Google URL Shortener in order to create a project and get an API key allowed to use the Google URL Shortener service.

Create a DynamoDB table

You can now create a table in DynamoDB named nasanews_settings that will be used to save the information of the latest breaking news in the feed and to know when a new item has been detected. The capacity can be 1 read unit and 1 write unit, because it won’t be accessed that much, and we are only going to save 1 item.

The partition key will be named “key”, and there’s no need to define a sort key.

Create an SNS topic to notify when a new item has been detected

We are going to use a special SNS topic that will receive a message every time a new item is detected, let’s call it “NasaNewsCreateCampaign“. The only subscription will be from a Lambda based on the function index.createCampaign.

Deploy the code to an Amazon S3 bucket

Here’s what you should in order to download the code and have it ready for deployment:

$ git clone https://github.com/PortaText/nasanews-shortcode-serverless
$ cd nasanews-shortcode-serverless/

# Edit the file index.js and setup the needed variables:
# googleApp: Google Project ID
# googleKey: Google Project API Key
# portatextKey: PortaText API Key
# smsServiceId: SMS Service ID in PortaText
# templateId: Message Template ID in PortaText
# SNSTopic: The ARN of the SNS Topic called NasaNewsCreateCampaign.

$ bin/package.sh

After running bin/package.sh you should get a file called nasa_news.zip, upload this one to an S3 bucket. We're going to use it when creating our AWS Lambdas.

Create the Lambda needed to handle PortaText notifications

Go your AWS Lambda console and create a new function, using NodeJS 4.3 as the runtime, call it sns_handler and use index.handleNotifications as the handler. The trigger should be the SNS Topic PortaTextEvents created earlier.

Create the Feed Parser Lambda

Go to your AWS Lambda Console and create a new function, using NodeJS 4.3 as the runtime, call it nasanews_feedparser, and use index.fetch as the handler.

Give it a role with a policy that allows it to access the DynamoDB table created before and choose to upload the code from an S3 bucket, you will need the full link to the nasa_news.zip file created above.

Add a CloudWatch Event as the trigger with a rate of 1 day.

If you click the "Test" button after creating the Lambda, you should be able to see its activity log in CloudWatch, and in case of success a new item will be created in your DynamoDB table called "last_item".

Create the Lambda used to create and launch new SMS Campaigns on breaking news

Go to your AWS Lambda Console and create a new function, using NodeJS 4.3 as the runtime, call it nasanews_campaign_creation, and use index.createCampaign as the handler. The trigger should be the SNS Endpoint created earlier named "NasaNewsCreateCampaign".

Add some CloudWatch alerts to monitor the service

You can also add some monitoring to the service so you can know when something's not right. Follow the steps described here in "Using CloudWatch to Monitor your AWS Lambda and send Alerts on Errors" so you can add everything needed in the lambdas created earlier.

That's it 🙂 You can now manage your own SMS service with your own short code and keyword, and without any servers at all, you can react to opt-ins, opt-outs, etc. Don't hesitate to get in touch with us if you have any questions.

Have fun!

-- The PortaText Team.