Some fun with SMS from Twilio

My favorite radio station is Radio X2. Many many times, I’m listening to the radio and asking myself “What’s the name of that song?” When I’m near a computer I just look at their What’s playing widget, but most of the times I’m in my car and I can’t access the web…

Then it hit me, why not make a SMS bridge using Twilio?

I fired up my text editor and wrote this simple perl script and put it somewhere on my web site:

#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => 'http://rms.radiox.com/player/PlayerWebService.asmx/GetPlayingDescription');
$req->header("Content-Type", "application/json; charset=utf-8");
$req->content('{"context":"CHXX","api":"flash2"}');
my $response = $ua->request($req);
print "Content-Type: text/plain; charset=utf-8\n\n";
if ($response->is_success) {
if ($response->decoded_content =~ m/\{"d":"([^\$]+)\!\$/) { # "
print $1;
} else {
print "Sorry, can't find song name.";
}
} else {
print $response->status_line;
}

Browsing to it will just output some text:

TMZ - KROME

Crazily enough, that’s just what it takes for Twilio to work!

On the Twilio website, I just directed the phone number (316) 251-0939 to this web page and whenever I send an SMS to this number it answers me in a few seconds with the name of the song that’s currently playing! Freakin’ Sweet!

There are a few caveats, however. First, Radio X2 announces the name of the song a few seconds before it actually starts, so if you send the SMS at the end of the song, you might end up with the name of the next one. A way to fix this would be to answer with the name of the last two or three songs played.

Also, Radio X2 is in Québec City, with area code 418/581 and while Twilio has phones numbers in these area codes, SMS don’t work with them. This basically means that it costs me 25¢ to send an SMS to their 316 area code number. Not a big deal, but you should be aware of that if you want to try it out.

Overall, this was a quick fun project that took me about an hour to program — most of it was spent in WireShark figuring out what headers their GetPlayingDescription API wanted.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>