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.
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.
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.
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.
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.
Next go to the “Keys and Access Tokens” tab. Scroll to the bottom and click the button that says “Create my access token”.
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.
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.
Once you click the button heroku.com should load and you should see a screen like the one below:
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!
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:
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.
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.
Happy tweet texting!
- Usage and Migration Guide for Twilio's Ruby Helper Library 5.x
- Send an SMS message during a phone call with Ruby and Sinatra
- Serverless Ruby on AWS Lambda with the Jets framework
- Call the Milk Wins Netbook Week #4 – This Week: Open Source It!
- Hacking Pixel Art on Twilio’s SIGNAL Conference Video Wall with Ruby and 23,000 Text Messages