I like Twilio. They’ve made programming voice fun, something it certainly wasn’t when Dialogic boards were the platform of choice. I’m using Twilio for a new service I’m working on. More on this in another post.
When you start playing with Twilio, a few common needs arise. For me, the first two were:
- Forward a Twilio phone number to another phone number. In testing, I have to switch between lots of different numbers. Twilio uses Web hooks, i.e., maps phone and SMS events to HTTP requests. I found myself tweaking scripts a few times per day.
- Registering a Twilio phone number for CallerID. Twilio wants to validate that you control the number you want to register for CallerID. Therefore, they give you a call and ask you to enter a random PIN. That’s not easy to do when the verification call to the Twilio number will result in an HTTP request to some app.
It was the problem of registering a CallerID for a Twilio number that led me to a simple hack that solved both problems. My idea was to forward the Twilio number to my cell phone and enter the PIN validation when I got the verification call. To make things a little easier, I made the forwarding endpoint accept the forwarding number as a parameter.
You use it like this:
The link is live so feel free to point your Twilio numbers to it. Remember to enter the phone number with the country code.
The code is trivial:
<?php header("content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; if(!$to = $_REQUEST['to']) $to = "18005551212"; ?> <Response> <Dial>+<?php echo $to ?></Dial> <Say>The call failed or the remote party hung up. Goodbye.</Say> </Response>
One of the Twilio numbers I set up for CallerID is going to be used for outbound calls only. What was going to happen when someone called it directly? That led to another quick Twilio hack–email me instead–which will greet callers and tell them to email you at a given address.
Side note: when doing speech to text, you have to write the text in a way that helps the speech engine pronounce it correctly. Note the commas I added to insert short pauses and the mis-spelling of Inc as ink, which I found made the “k” sound a little stronger.
Go ahead, try this by calling (917) 300-0268.
Another “twivial” code snippet:
<?php header("content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; if(!$name = $_REQUEST['name']) $name = ""; if(!$email = $_REQUEST['email']) $email = "the feedback address on our site"; ?> <Response> <Say voice="woman">Thank you for calling <?php echo $name ?>.</Say> <Pause length="1"/> <Say voice="woman">Please, email us at <?php echo $email ?>.</Say> <Pause length="2"/> <Say voice="woman">Goodbye.</Say> </Response>
I wish Twilio did three things:
- Allow for static TwiML to be associated with a phone number. I’d love to just be able to enter some markup in a text box to handle simple cases like the ones above without having to deploy any server-side code.
- Automatically allowed us to set up CallerID on Twilio numbers we own. That’s just common sense.
- Allowed localhost targets using some type of automatic gateway software (in-page applet or browser plugin or Adobe AIR app). This will make testing so much easier.
Other than that, thanks, guys!