Send SMS Messages with Processing and Twilio
Processing is a relatively new framework used for creating rich visual-based applications in Java, Android, Javascript, and more. It's free, open source, portable, extensible, and well documented.
In a recent effort to try out Processing I decided to see how difficult it would be to integrate Twilio into a sketch. Although there are many third-party libraries available to Processing, there is currently no Twilio library, official or otherwise. The simplest use of Twilio would be to send an outgoing SMS with Twilio's REST API. This post will step through accomplishing that task.
Step One: Making HTTP Requests
In order to use the Twilio API there needs to be some way for the client to make GET and POST requests to certain Twilio endpoints. I found a simple but powerful Processing library called HTTP-Requests-for-Processing.
First import it at the top of the sketch:
import http.requests.*;
Making GET requests is extremely easy, as seen below:
GetRequest get = new GetRequest("http://twilio.com");
get.send();
println("Reponse Content: " + get.getContent());
Similarly, POST requests can be easily crafted to send various data, as seen below:
PostRequest post = new PostRequest("http://twilio.com");
post.addData("Name", "Brodan");
post.send();
println("Reponse Content: " + post.getContent());
Step Two: Basic Authentication
I realized that this library did not include any method for authentication, which is required to use the Twilio REST API. I did what any anxious programmer would do, and forked the repository so I could add the feature myself.
Now, it is possible to add a Basic authentication header to any request using the method seen below:
post.addUser("username", "password");
Step Three: Crafting an API Request
With authentication out of the way, sending an outgoing SMS via Twilio's REST API became much less intimidating. A sample request would look like this:
PostRequest post = new PostRequest("https://api.twilio.com/2010-04-01/Accounts/{ACCOUNT_SID}/Messages");
post.addData("From", "+15555555555");
post.addData("To", "+15555555555");
post.addData("Body", "Hello from Processing!");
post.addUser("YOUR_ACCOUNT_SID", "YOUR_AUTH_TOKEN");
Finally, here's an example sketch that will send an outgoing SMS when you run it:
import http.requests.*;
String ACCOUNT_SID = "XXXXXXXXXXXXXXXXXXXXXXX";
String AUTH_TOKEN = "YYYYYYYYYYYYYYYYYYYYYYY";
PostRequest post = new PostRequest("https://api.twilio.com/2010-04-01/Accounts/"
+ ACCOUNT_SID + "/Messages");
void setup() {
post.addData("From", "+15555555555");
post.addData("To", "+15555555555");
post.addData("Body", "Hello from Processing!");
post.addUser(ACCOUNT_SID, AUTH_TOKEN);
post.send();
println("Reponse Content: " + post.getContent());
}
Remember to replace the values above with your Twilio credentials and your phone numbers.
You can also find the complete code for this sketch in this Gist.
Wrapping Up
Thanks to Twilio's simple and intuitive REST API we can quickly send an outgoing SMS using Processing. Changing the POST request's endpoint and data will allow you to use the rest of Twilio's API just as easily.
If you found this post helpful or would like to give feedback, feel free to reach out to me on Twitter @brodan_.