Now that you know how to place and disconnect calls from the browser, it's time for your browser to start receiving incoming calls. By the end of this recipe, you will be able to make a call into your browser from your phone.
We're going to set up our Twilio Client app to accept incoming calls. This involves making changes to our Twilio app in our Twilio account.
To begin receiving incoming calls, we have to set up the Twilio Client app by performing the following steps:
The complete code for this recipe can be found in the Recipe1
folder under Code
.
We will be implementing the following steps to set up our own app to allow incoming calls into our browser:
client.php
on your web server as shown in the following screenshot:Services/
folder to your website.config.php
to your web server using the following code:<?php $accountsid = ''; // YOUR TWILIO ACCOUNT SID $authtoken = ''; // YOUR TWILIO AUTH TOKEN $appsid = ''; // YOUR TWILIO APP SID $appname = ''; // YOUR CLIENT NAME ?>
hello.php
to your web server using the following code:<?php include("config.php"); include 'Services/Twilio/Capability.php'; $capability = new Services_Twilio_Capability($accountsid, $authtoken); $capability->allowClientOutgoing($appsid); $capability->allowClientIncoming($appname); $token = $capability->generateToken(); ?> <!DOCTYPE html> <html> <head> <title>Hello Client</title> <script type="text/Javascript" src= "//static.twilio.com/libs/twiliojs/1.1/twilio.min.js"> </script> <script type="text/Javascript" src="https:// ajax.googleapis.com/ajax/libs/jquery/ 1.6.2/jquery.min.js"> </script> <?php include("clientjs.php"); ?> </head> <body> <button class="call" onclick="call();">Call</button> <button class="hangup" onclick="hangup();">Hangup </button> <div id="log"></div> </body> </html>
We've once again set up our client interface. When we set up our token, we also specified to allow the app to accept incoming calls
clientjs.php
to your web server using the following code:<script type="text/javascript"> Twilio.Device.setup("<?php echo $token; ?>"); Twilio.Device.ready(function (device) { $("#log").text("Ready"); }); Twilio.Device.error(function (error) { $("#log").text("Error: " + error.message); }); Twilio.Device.connect(function (conn) { $("#log").text("Successfully established call"); }); Twilio.Device.disconnect(function (conn) { $("#log").text("Call ended"); }); Twilio.Device.incoming(function (conn) { $("#log").text("Incoming connection from " + conn.parameters.From); conn.accept(); }); function call() { Twilio.Device.connect(); } function hangup() { Twilio.Device.disconnectAll(); } </script>
This JavaScript code may look similar to the last recipe, but we've also added a connection for incoming calls, which will display a message in the log div
and also accept the call.
client.php
to your web server using the following code:<?php include("config.php"); header('Content-type: text/xml'), ?> <Response> <Dial> <Client><?php echo $appname; ?></Client> </Dial> </Response>
In steps 1, 2, and 3, we set up a TwiML app inside our Twilio account.
In steps 4 and 5, we downloaded and set up our Twilio Helper Library.
In step 6, we set up our config.php
file with our Twilio settings.
In step 7, we uploaded hello.php
, which is the interface for our Twilio Client app.
In step 8, we uploaded clientjs.php
, which handles the process of talking to Twilio from the browser.
In step 9, we uploaded client.php
, which receives incoming calls and connects to our browser session.
In step 10, we connected a Twilio number to our application.
Now, whenever a user calls on the number we have set up, it will connect to our Twilio app, which will in turn connect to the Twilio Client running in our browser and display the incoming call on your browser.