I had a few hours to waste the other day and I started to fool around Twilio Cloud Service. First of all, I must make a note to self that from now on I will read the documentation thoroughly before I start doing things.
First of all let me start off, with what exactly is Twilio. From what I’ve figured out so far, Twilio provides a layer between Telephony and the Application Layer. From what it appears one could easily set up a phone number, and from there write a few scripts on his favourite programming language to command the Telephony Layer into doing various stuff for him. In few words looks like it provides a more abstract ${CURL()} asterisk function.
As probably some of you, when I come across something that intrigues me, I don’t waste much time reading and I want to get my hands dirty this instant. Well what happened today is a perfect example why you should read beforehand. Well, I created an account on Twilio, downloaded the PHP helper and I was going through the documentations. Twilio is using an XML syntax which they call it TwilML which looks like a small version of Voice XML, making it possible for an external application hosted on your servers to communicate with Twilio’s Telephony Servers.
So I was reading the XML structure and I was thinking to myself, “well, I definitely will not write XML directly into my PHP scripts! Hmm, “, I thought to myself, ” it would be nice to have a small library to create the actual XML results programmatically”, since I am a (control) freak of nature! So I started coding my library for TwilML. (The ones familiar with Twilio, have already understood my point)
While I was half way there, I noticed that the PHP library supplied by Twilio, already has such a library implemented. Well I had already wasted an hour or so, so I decided to keep going and finish it, just to have something to write in this blog! Well anyhow after a few hours I have my version of the TwilML XML structure for PHP. I decided to make something that looks like a SimpleXMLElement for each of the TwilML elements, and at the same time add a methodology to create the XML structure are a series of associative arrays.
Well here is the result. I don’t know if it is easier however:
Creating a Twilio Response as an array
$response = new TwilML( array( 'Response' => array( 'nouns' => array( 'Say' => array( 'nouns' => array( 'Body' => array( 'element' => new TwilML_Element_Body('Welcome') ) ) ), 'Gather' => array( 'element' => new TwilML_Element_Gather('/twilio/verify_pin.php', TwilML_Element_Abstract::METHOD_POST, 5, '#', 6) 'nouns' => array( 'Say' => array( array( 'element' => new TwilML_Element_Body('Please enter your six digit PIN') ) ) ) ), 'Say' => array( 'nouns' => array( 'Body' => array( 'element' => new TwilML_Element_Body('You didn't enter anything. Goodbye') ) ) ) ) ) ) ); echo $response;
The array structure is rather simple. Each element is an associative array on itself and there are two ways of defining the element. Passing an instance of the element:
array( 'element' => new TwilML_Element_<Element>(), 'nouns' => array( /* Noun Elements that should be included under this element */ ) )
Or passing the textual representation of the element:
'Say' => array( 'options' => array( /* Element options */ ), 'nouns' => array( /* Noun Elements that should be included under this element */ ) )
When eventually the TwilML object is echoed, all the elements return their textual representation and construct the XML result.
It is also possible to create the result as such:
$gather = new TwilML_Element_Gather(...); $say = new TwilML_Element_Say(...); $say->addNoun( new TwilML_Element_Body("Please don't call me again!") ); $gather->addNoun($say); echo $gather;
Here is the library for the not-faint-hearted!
I have even created a demo Two-Stage-Dialing application using Twilio and this version of TwilML. I haven’t test it yet. But will post it here in a while anyway! I am pretty sure the logic behind it is correct!
Any interest in creating a twilio wordpress plugin? I’ve got an idea that I know is possible, just don’t have the coding skills
I am always open to a good idea