Send and Receive Tweets using SMS with Twilio: Part 1

March 19, 2015
Written by
Phil Nash
Twilion

3310

You may have just read the title and thought, “What? Can’t you already send and receive tweets over SMS?”

You would be right to think that, Twitter has a load of shortcodes for different countries for sending and receiving tweets. But since February 4th in the UK, O2, one of the major mobile operators, announced to their users (by SMS ironically) that they were dropping this support. Users will no longer be able to tweet or receive notifications via the UK 86444 shortcode.

Hopefully this doesn’t cause other networks to drop their support too, though according to the list above, Vodafone does not support the shortcode either. Many people find it useful to be able to use Twitter via SMS, either when abroad to avoid high data costs or simply because they don’t have a smartphone. There has been a lot of complaining about this choice, on Twitter naturally, so I thought I’d try to do something about it.

O2 will stop supporting SMS notifcations by SMS

With the Twilio and Twitter APIs at my hands, I’ve put together a Twitter by SMS service that you can host yourself. The code is available on GitHub and in this post I’m going to walk you through how to setup and deploy this application and give you your own personal Twitter by SMS service. In part 2 I will walk through how I built the app.

Here’s how to get started.

Things you’ll need

You’re not going to get very far if you don’t have a Twitter account with a mobile number verified for your account. If you’ve been receiving SMS notifications, you probably already do, but I’m just checking! You will also need a free Twilio account for sending and receiving text messages. Finally, you will need a Heroku account, although you can hold off signing up until later if you want to, it will be a part of our install process.

Setting up Twilio

All you need from Twilio is a phone number that can send SMS messages. Here’s one I bought earlier.

Save the number from your Twilio dashboard.

Make a note of the number as you will need it later, along with your Account SID and Auth Token available on your account dashboard.

You can find your Account Sid and Auth Token on your Twilio dashboard

That’s our Twilio set up, now we need to create a Twitter app.

Setting up a Twitter app

To allow us to post to and receive notifications from Twitter via SMS, we are going to need a Twitter app. You can set one up by visiting https://apps.twitter.com/. Click “Create New App” and fill in the form.

The Website field needs to point to a URL, but it doesn’t really matter what it is. You do not need to enter a Callback URL.

Create the app on Twitter and fill in a name, description and URL

Accept the developer terms and conditions and you have yourself a Twitter app. There are some more bits we need to do before the app is ready though.

Go to the Permissions tab and change the access from “Read only” to “Read, Write and Access direct messages” and click “Update Settings”. This will allow you to create new tweets and get sent your direct messages.

Change the access to "Read, write and direct messages"

Next go to the “Keys and Access Tokens” tab. Scroll to the bottom and click the button that says “Create my access token”.

Press the "Create my access token" button

Once you’ve done that, the page will show you your application keys and your access token. The important bits here are the Consumer Key, Consumer Secret, Access Token and Access Token Secret.

You will need the Consumer Key and Consumer Secret and the Access Token and Access Token Secret

With all our credentials now in hand all we need to do is deploy the application.

Deploying the app

We’re going to deploy this application to Heroku and to do so is very easy due to the “Deploy to Heroku” button. All you need to do is click the deploy button below. If you’re signed in to Heroku you will start the deploy process straight away. Otherwise log in or sign up to Heroku and you’ll be able to continue with the deploy.


Deploy

Once you click the button heroku.com should load and you should see a screen like the one below:

Create the app on Heroku

Firstly, pick a name for your app. I’ve called mine “twitter-sms”. When the form tells you that your name is available, copy it to the field below titled “HEROKU_APP_NAME”. Then fill in the rest of the fields with the various IDs and keys that you have been setting as we go through this process.

Once all the fields are filled, press the “Deploy for Free” button at the bottom of the form. You can watch the lights turn green as the process completes the steps or you could use this time to make a cup of tea or think about your first tweet that you’ll be delivering via your own SMS service. Once all the steps are complete your service is live!

When all the steps complete, your app is launched.

Monitoring

We’ll want to make sure the service stays alive so that we keep getting notifications. I have included a health check endpoint that you can check with an uptime monitoring service that can let you know if it goes down. Set the monitoring service to check https://your-app-name.herokuapp.com/heath and notify you if it goes down. I have used UptimeRobot for this:

Set up monitoring with a check on the /health endpoint

Texting is back

Now it doesn’t matter whether your network supports tweeting by SMS or not, you have your own personal number to access Twitter the way you want to. The service allows you to:

  • Send an SMS to your own personal number to post tweets to Twitter
  • Receive notifications of direct messages or @mentions from Twitter

Try it out now, just send an SMS to the number you bought. You’ll see it appear on Twitter in no time.

A tweet I sent by SMS

Sending and receiving SMS Tweets

If you want to know how it works, you can dive into the code on GitHub or stay tuned for part two where I’ll take you through building the app step-by-step. The application is written in Ruby on the Sinatra framework using the Twilio, Twitter and TweetStream gems. Feel free to hit me up with feature requests or pull requests if you want something added.

If you have any trouble deploying the application, or just want to share your disappointment that O2 dropped their Twitter SMS support, please get in touch on GitHub, on Twitter or by email.

Happy tweet texting!