Friday, March 18, 2016

Twilio Voice call integration using PHP and JS


First step is to signup on twilio using a valid mobile number - which will get a verification code which is used to verify the mobile number.

Selection_120.png

www.twilio.com/try-twilio

On clicking the Get Your First Twilio Phone Number, a sample phone number will be provided by twilio which will be used to generate calls (From number).

Selection_121.png

Download the sdk and setup the code to autoload the sdk in the project.
Go to Dashboard www.twilio.com/user/account/voice/dashboard click on ‘view API credentials’ to get the id and token.

Now go to www.twilio.com/user/account/voice/dev-tools/twiml-apps and create a sample TwiML app. On the voice section provide an accessible url from which twilio will be fetching the from and to numbers for connecting (will explain in detail below).
E.g. http://DOMAIN/twilioCall.php

Generate required files for initiating calls.

demoCall.php : Display the dial-er page.

<?php
include 'Services/Twilio/Capability.php';
 
// put your Twilio API credentials here
// https://www.twilio.com/user/account/settings
$accountSid = 'AccountId';
$authToken  = 'token';
 
// put your Twilio Application Sid here
// https://www.twilio.com/user/account/voice/dev-tools/twiml-apps and select an app to get the app id
$appSid     = 'TwiMLAppID';
 
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming('incommingTest');
$token = $capability->generateToken();
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Call Demo</title>
    <script type="text/javascript"
      src="//media.twiliocdn.com/sdk/js/client/v1.3/twilio.min.js"></script>
    <script type="text/javascript"
      src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <link href="//static0.twilio.com/resources/quickstart/client.css"
      type="text/css" rel="stylesheet" />
    <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);
        // accept the incoming connection and start two-way audio
        conn.accept();
      });

      function call() {
        // get the phone number to connect the call to
        params = {"PhoneNumber": $("#number").val()};
        Twilio.Device.connect(params);
      }

      function hangup() {
        Twilio.Device.disconnectAll();
      }
    </script>
  </head>
  <body>
    <button class="call" onclick="call();">
      Call
    </button>

    <button class="hangup" onclick="hangup();">
      Hangup
    </button>

    <input type="text" id="number" name="number" placeholder="Enter a phone number to call"/>
    <div id="log">Loading ...</div>
  </body>
</html>

twilioCall.php

<?php
header('Content-type: text/xml');
if (isset($_REQUEST['PhoneNumber'])) {
    $number = htmlspecialchars($_REQUEST['PhoneNumber']);
}?>

<Response>
    <Dial callerId="<?php echo $callerId ?>">
 <Number>"<?php echo $number ?>"</Number>
    </Dial>
</Response>

Key elements required for connection are Account SId, Token (www.twilio.com/user/account/settings) and appId (www.twilio.com/user/account/voice/dev-tools/twiml-apps and select an app to get the app id).

Now its ready to be tested. Note that since this is a demo account from and to numbers cannot be changed (From should be the number provided by Twilio during signup and to should be the verified number on the account).

Logs will be available over here : www.twilio.com/user/account/log/calls , on clicking the timestamp it will direct to the details of request and response.

www.twilio.com/user/account/monitor/alerts, this one provide alerts created during our test failures.

Selection_123.png

Alert logs
Selection_122.png

No comments:

Post a Comment