You can use your web browser as a telephone using Twilio Client. This HowTo shows how you can place outgoing calls to any phone using a usb headset or microphone and also create a virtual dialpad to send touch tone digits. You may also optionally provision a traditional phone number to ring in the browser when called.
A user enters a phone number they want to call and clicks the 'Start Call' button. Twilio will call the specified phone number and setup a call between that phone number and the browser. The call status and a disconnect button are displayed on the page and a browser based dialpad is available for touch tone entry. The parties may talk until one of them disconnects. If an incoming call is detected, the browser will prompt the user to accept the call, and make the connection if confirmed.
This HowTo demonstrates browser calling using Twilio Client, specifying an outgoing connection to a Twilio Application and creating TwiML call logic using the <Dial> verb.
Create a capability token to securely bind Twilio Client to your account and also give the device the ability to make outbound calls to a Twilio Application, as well as accept incoming connections to the name 'alice'.
include 'Services/Twilio/Capability.php';
$accountSid = 'ACxxxxxxxxxxxxxxx';
$authToken = 'xxxxxxxxxxxxxxxxx';
$token = new Services_Twilio_Capability($accountSid, $authToken);
$token->allowClientOutgoing('APxxxxxxxxxxxxxxx');
$token->allowClientIncoming("alice");
<script type="text/javascript" src="http://static.twilio.com/libs/twiliojs/1.0/twilio.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
Twilio.Device.setup("<?php echo $token->generateToken();?>");
var connection=null;
$("#call").click(function() {
params = { "tocall" : $('#tocall').val()};
connection = Twilio.Device.connect(params);
});
$("#hangup").click(function() {
Twilio.Device.disconnectAll();
});
Twilio.Device.ready(function (device) {
$('#status').text('Ready to start call');
});
Twilio.Device.incoming(function (conn) {
if (confirm('Accept incoming call from ' + conn.parameters.From + '?')){
connection=conn;
conn.accept();
}
});
Twilio.Device.offline(function (device) {
$('#status').text('Offline');
});
Twilio.Device.error(function (error) {
$('#status').text(error);
});
Twilio.Device.connect(function (conn) {
$('#status').text("Successfully established call");
toggleCallStatus();
});
Twilio.Device.disconnect(function (conn) {
$('#status').text("Call ended");
toggleCallStatus();
});
function toggleCallStatus(){
$('#call').toggle();
$('#hangup').toggle();
$('#dialpad').toggle();
}
$.each(['0','1','2','3','4','5','6','7','8','9','star','pound'], function(index, value) {
$('#button' + value).click(function(){
if(connection) {
if (value=='star')
connection.sendDigits('*')
else if (value=='pound')
connection.sendDigits('#')
else
connection.sendDigits(value)
return false;
}
});
});
});
</script>
Number to call: <input type="text" id="tocall" value="">
<input type="button" id="call" value="Start Call"/>
<input type="button" id="hangup" value="Hangup Call" style="display:none;"/>
<div id="status">
Offline
</div>
<div id="dialpad" style="display:none;">
<table>
<tr>
<td><input type="button" value="1" id="button1"></td>
<td><input type="button" value="2" id="button2"></td>
<td><input type="button" value="3" id="button3"></td>
</tr>
<tr>
<td><input type="button" value="4" id="button4"></td>
<td><input type="button" value="5" id="button5"></td>
<td><input type="button" value="6" id="button6"></td>
</tr>
<tr>
<td><input type="button" value="7" id="button7"></td>
<td><input type="button" value="8" id="button8"></td>
<td><input type="button" value="9" id="button9"></td>
</tr>
<tr>
<td><input type="button" value="*" id="buttonstar"></td>
<td><input type="button" value="0" id="button0"></td>
<td><input type="button" value="#" id="buttonpound"></td>
</tr>
</table>
</div>
<?php
header('Content-type: text/xml');
?>
<Response>
<Dial callerId="NNNNNNNNNN"><?php echo htmlspecialchars($_REQUEST["tocall"]); ?></Dial>
</Response>
Twilio processes the TwiML we sent and calls the specified phone number and successfully connects the two parties.
<?xml version="1.0" encoding="UTF-8"?> <Response> <Dial><Client>alice</Client></Dial> </Response>