Archive

Archive for the ‘twilio’ Category

Using twilio without a public web server howto

October 7, 2012 Leave a comment

Why bother:
Most of the twilio tutorials on how to make phone calls require a twiml url and (optionally) status call back url, which might not be desired because either 1) not easy to set up a publicly accessible web server 2) for security reasons it’s better not to to expose some urls to the public even they are just intended for twilio to access. If your application doesn’t require a status call back check, you can follow this post to use twilio for phone calls without setting up a web server.

Basic concept:
1) prepare a twiml url through twimlet echo call
2) create a twilio service object
3) make a call with the service object using the url in step 1, use null as StatusCallBack
4) get the sid for the call made in step 3)
5) wait for a period of time which is enough for step 3) to finish
6) get the status for the call through sid obtain in step 4

Example code (in PHP):
1) go to https://www.twilio.com/labs/twimlets/echo and put in the following xml code into the textarea:

<Response>
<Say>Testing 123.</Say>
<Hangup/>
</Response>

and save the resulting url for the next step
2)

<?php
define('ACCOUNT_SID', 'your_twilio_account_sid');
define('AUTH_TOKEN', 'your_twilio_auth_token');
// from step 1
define('TWIML', 'http://twimlets.com/echo?Twiml=%3CResponse%3E%0A%3CSay%3ETesting%20123.%3C%2FSay%3E%0A%3CHangup%2F%3E%0A%3C%2FResponse%3E&');
require_once 'Path/To/Services/Twilio.php';

$sleep=30;  // seconds to pause before checking the call status
$from='###'; // nbr verified through twilio
$to='###';  // nbr to call
$tw=new Services_Twilio(ACCOUNT_SID, AUTH_TOKEN);
$opts=array(
	'timeout'=>'8',
);

// make a phone call
$call=$tw->account->calls->create($from, $to, TWIML, $opts);

// get its sid
$sid=$call->sid;

// sleep for some seconds
printf("sleeping for %d seconds now...\n", $sleep);
sleep($sleep);

// now check the call status
$call_check=$tw->account->calls->get($sid);
$status=$call_check->status;
printf( "status for [%s] is %s.",  $sid, $call_check->status );

// todo: do something with the $status

Conclusion:
As you can see from the above code, the trick is to take advantage of the twimlet echo service (basically we are using that to provide a twiml url which is hosted by twilio.com).

Categories: twilio