Friday, May 10, 2013

Using Twilio to send an SMS message



I've been asked from time to time if it is possible to send an SMS message from your application. SMS stands for Short Message Service and is also commonly referred to as a Text Message or just a Text. SMS messages can contain up to 160 characters. To send someone an SMS, you just need their mobile phone number.

So is it possible to send an SMS message from your apps? As is the case with most things, the answer is "Yes, if you know how". And often many of these things can be accomplished using a web service of some kind.

One of the easiest ways to interface with telephones, including SMS, is using a web service called Twilio.

With Twilio, you can do all kinds of things related to telephones, including making and receiving phone calls and text messages. Twilio is not a free service, but you can get a free developer account with a credit to test it out.

To start, create a free developer account at Twilio.com. I'll wait for you to come back...

Twilio has a lot of documentation on how to use their various services. To send an SMS message, you want to take a look at their SMS REST API which is here:


On this page you will see buttons with examples for a variety of languages. You can just click XML to see how to do this using XML and curl, which you can easily incorporate into your app.

When Xojo is released, it will include a sample project that demonstrates how to do this using just a few lines of a code and a handful of controls.

Sample Application

When you run the app, you enter your Twilio Account SID and AuthCode, your Twilio phone number, the phone number you want to send the SMS to and your message. You can get the Account SID and AuthCode on the main Dashboard page:

Twilio Dashboard Page

The app uses curl (from a Shell) to send the SMS so it will only work on OS X and Linux. The code is entirely contained (all 6 lines of it) in the Action event handler of SendButton.

This code takes what was entered in the window and creates the curl command (as described on the Twilio page) and then runs the command using a Shell. The XML results are displayed after the message is sent.

This is the message as it appears on my iPhone:
SMS Message appearing on iPhone

You can also download the sample project from here.







4 comments:

john said...

It's already easy to send SMS from Xojo
A lot of the SMS gateways provide http and https APIs so all you need to do to send a message is build a URL in code and stick that in as the parameter in e.g the LoadURL method of the htmlviewer

Norman Palardy said...

Some providers let you format it like an email to the user.
But you then have to know what carrier they are with to make that work.

cfitzsimmons said...

Twilio is probably a better SMS portal provider solution than many others. This was not lost on ATT when they partnered with Twilio because of their vast cross-carrier integration which translates to faster SMS delivery or at least less hassle for developers. Please reference http://gigaom.com/2012/09/26/with-twilios-help-att-opens-up-sms-voice-to-developers/

Anthony Papillion said...

This can also be made to work on Microsoft Windows with a few modifications. cURL is available for Windows from here

http://curl.haxx.se/download.html