06.12.09
About four months ago I wrote a PHP Helper library to help output valid TwiML. While the library worked well enough, it had no error checking and the overall design was poorly thought-out. But no worries, I have just released version 2 of twilio-libresponse. It’s nothing like the first version, so let’s see some code.
A example TwiML response using libresponse.
$r = new Response();
$r->append(new Say("Hello World");
$r->Respond();
which generates
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Hello World</Say>
</Response>
Let’s add some attributes
$r = new Response();
$r->append(new Play("www.example.com/rickroll.mp3", array("loop" => "5")));
$r->Respond();
which generates
<Response>
<Play loop="5">www.example.com/rickroll.mp3</Play>
</Response>
Adding nested elements inside another verb, usually difficult to do, is quite easy. Just append and go
$r = new Response();
$g = $r->append(new Gather());
$g->append(new Say("Hello again, World"));
$r->Respond();
<Response>
<Gather>
<Say>Hello again, World</Say>
</Gather>
</Response>
Attribute handling has also been upgraded. Only valid attributes are allowed to be added to verbs; an exception is raised when invalid attributes or attribute values are added. However, putting __ in front of any attribute disables this error checking. See below.
$r = new Response();
$r->append(new Redirect(array("__crazy" => "delicious")));
$r->Respond();
<Response>
<Redirect crazy="delicious"/>
</Response>
During my first two weeks here at Twilio, the library has come in handy. You can download a copy a github. If you need me any help, send me an email.
06.08.09
For the last two semesters, I have been taking an advanced animation course here at Berkeley. Taught by Dan Garcia and Brian Barsky, CNM 190 mixes computer science and art, allowing undergraduate students from both majors to take the course. Our group, the Fighting Wireframes, consists of 12 students, most with a computer science background.
After nine months of arduous work, our animation is finally finished. Check it out on Youtube or find it in my Portfolio.
03.16.09
After my interview with Twilio, I wanted to get my hands dirty and write some simple Twilio apps. After an hour of writing XML using PHP and print statements (yuck) I knew there had to be a better way. Thanks to Jon Coulter, I was able to utilize his Twilio PHP response library to facilitate XML creation. However, after using it for a couple of days, I was unhappy with having to create arrays inside function calls, which caused function calls to span multiple lines. So I set out to create my own library.
PHP 5 includes a great new feature for dealing with XML documents, SimpleXML. Instead of having to print out raw XML, I took advantage of SimpleXML’s addChild() and addAttribute() functions, using them to generate valid XML without having to write any myself. After a few hours of work, I am proud to present my own Twilio PHP response library, twilio-libresponse. I utilized some of Jon’s helper functions, mainly encode() and Respond() to format my XML properly. Let’s see how to implement some basic Twilio code.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Hello World</Say>
</Response>
Using twilio-response, this code becomes:
<?php
$rsp = new TwilioResponse();
$rsp->Say("Hello world");
$rsp->Respond();
?>
A more complicated example
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="woman" loop="2">Hello</Say>
</Response>
Adding attributes is easy. Just add the attributes you wish to add to the function call itself, with the attribute name followed by it’s value
<?php
$rsp = new TwilioResponse();
$rsp->Say("Hello world", "voice", "woman", "loop", 2);
$rsp->Respond();
?>
A more complicated example involves nesting commands inside a Gather
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="/process_gather.php" method="GET">
<Say>
Please enter your account number,
followed by the pound sign
</Say>
</Gather>
</Response>
This however, becomes very easy. Just save the gather tag in a variable and pass it along as the first value to the function
<?php
$rsp = new TwilioResponse();
$gather = $rsp->Gather("action", "/process_gather.php", "method", "GET");
$rsp->Say($gather, "Please enter your account number,followed by the pound sign");
$rsp->Respond();
?>
Note that only the Gather and Dial command return values, as those are currently the only tags that allow nesting. I am thinking of adding error checking to the code so that only code Twilio understands is allowed. The code is free for all to use.
03.10.09
The “Start-up Experience” job fair was held today on campus and I was lucky enough to attend. Roughly fifteen companies had tables set up, with informative and energetic staff behind each table. I loved talking directly to the engineers and developers behind these companies instead of pushy public relation types. Twilio, WebFam, and Loomia were the coolest of the start ups there. Here’s hoping for an internship.
On a website related note, my first portfolio entry has been added. After working on the back end for the last few days, it’s nice to simply log on and easily add an entry, no MySQL hacking required. Look forward to more entries in the coming weeks.
02.28.09
After my first redesign of the site a couple months back, I was never really happy with how the site turned out. I was going for minimalism, but instead my design was simply boring. This version contains a bit more visual flair, but I think I have done a good job keeping it to a minimum. Internally, I have decided that the size and scope of this site are too small to justify using Drupal, so I have switched to Wordpress to handle the blogging side of things. For my portfolio, I am just going to roll my own PHP / MySQL database, which really shouldn’t be too much of a hassle to put together.
I have also made the decisions to turn off comments of the site. I really don’t expect high readership, and I don’t want to have to deal with spam, trackbacks, and the like. So instead, if you like an article and want to post some feedback, a Twitter reply will suffice. Speaking of blog posts, with my switch to Wordpress, writing entries has become much easier. I look forward to staying up to date with my posts. It’s 6:17 AM, I should probably get to bed.
02.28.09
Having recently completed this checklist of the IMDB Top 250 movies, I wanted a more permanent place to record my checklist. Instead of just using a simple text document, I thought I would spend a day or two and write a Facebook application so that other people could join in the fun.
A week later, my application is complete. I just finished the last of the code today after taking a three day hiatus to move all my stuff back down to Berkeley. The best part about the project is that is is open source! Install the app on Facebook or head over to Github to view the source code.