Scheduling a conference call

The conference call scheduler is going to let you enter details to schedule a conference call. This will let you set up participants, the moderator, and when the call will take place.

Getting ready

The complete source code for this recipe can be found in the Chapter4/ folder.

How to do it...

Ok, ready? For our first recipe, we're going to build the scheduling and conference management sections.

  1. Download the Twilio Helper Library from (https://github.com/twilio/twilio-php/zipball/master) and unzip it.
  2. Upload the Services/ folder to your website.
  3. Add sql.sql to your database.
  4. Upload config.php to your website and make sure the following variables are set:
    <?php
    $accountsid = '';  //  YOUR TWILIO ACCOUNT SID
    $authtoken = '';   //  YOUR TWILIO AUTH TOKEN
    $fromNumber = '';  //  PHONE NUMBER CALLS WILL COME FROM
    $conferenceNumber = '';  //  Number to call into.
    $dbhost = '';  //  YOUR DATABASE HOST
    $dbname = '';  //  YOUR DATABASE NAME
    $dbuser = '';  //  YOUR DATABASE USER
    $dbpass = '';  //  YOUR DATABASE PASS
    ?>
  5. Upload pdo.class.php to your website.
  6. Create a file on your website called schedule.php and add the following code to it:
    <?php
    include("config.php");
    include("pdo.class.php");
    include 'Services/Twilio.php';
    include("functions.php");
    
    $action = isset($_GET['action']) ? $_GET['action'] : null;
    
    switch($action){
      case 'save':
           extract($_POST);
           $timestamp = strtotime( $timestamp );
           $sql = "INSERT INTO conference SET`name`='{$name}',`timestamp`='{$timestamp}'";
           $pdo = Db::singleton();
           $pdo->exec($sql);
           $qid = $pdo->lastInsertId();
           if( isset($qid) && !empty($qid) ){
                foreach($call_name as $k=>$cname){
                        $cphone = $call_phone[$k];
                        $cstatus = $call_status[$k];
                        $sql = "INSERT INTO callers SET conference_id = '{$qid}',`name` = '{$cname}',`phone_number' = '{$cphone}',status='{$cstatus}'";
                        $pdo->exec($sql);
                }
           }
           break;
      case 'addnew':
           include("form.php");
           break;
      default:
           include("home.php");
           break;
    }
  7. Now let's create a file on your website called functions.php and add the following code to it:
    <?php
    functiongetRecording($caSID){
    global $accountsid,$authtoken;
        $version = '2010-04-01';
        $url = "https://api.twilio.com/2010-04-01/Accounts/{$accountsid}/Calls/{$caSID}/Recordings.xml";
        $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, "{$accountsid}:{$authtoken}");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
    curl_close($ch);
        $output = simplexml_load_string($output);
    echo "<table>";
    foreach ($output->Recordings->Recording as $recording) {
    echo "<tr>";
    echo "<td>".$recording->Duration." seconds</td>";
    echo "<td>".$recording->DateCreated."</td>";
    echo '<td><audio src="https://api.twilio.com/2010-04-01/Accounts/'.$sid.'/Recordings/'.$recording->Sid.'.mp3" controls preload="auto" autobuffer></audio></td>';
    echo "</tr>";
        }
    echo "</table>";
    }
    ?>
  8. Now, we'll create home.php, which will let us display conference calls, and either monitor or review recordings. Add the following code to it:
    <ahref="schedule.php?action=addnew">Schedule new conference</a><hr />
    <h2>Conferences</h2>
    <table width=100%>
    <?php
      $res = $pdo->query("SELECT * FROM conference ORDER BY `timestamp`");
      while( $row = $res->fetch() ){
             $conference = $client->account->conferences->getIterator(0, 50, array("FriendlyName" =>$row['ID']));
    ?>
            <tr>
              <td><?=$row['name']?></td>
              <td><?=date("m-d-Y ",$row['timestamp'])?></td>
            <td>
    <?php  if( $conference->status == "in-progress") { ?>
      <ahref="monitor.php?room=<?=$row['ID']?>">Monitor</a> | 
      <ahref="view.php?room=<?=$row['ID']?>">View Listeners</a>
    <?php  }else if( $conference->status == 'completed') {
                    getRecording( $conference->sid );
    }else{  ?>
                 Not Yet Started
    <?php  }    ?>
              </td>
            </tr>
    <?php
      }
    ?>
      </table>
      <br />
  9. Finally, we'll create form.php, which is the actual form used to schedule conference calls. Add the following code to it:
    <h2>Prepare your conference</h2>
    <form method="POST" action="schedule.php?action=save">
    <table>
    <tr>
      <td>Name</td>
      <td><input type="text" name="name" /></td>
    </tr>
    <tr>
      <td>Date & Time</td>
      <td>
      <input type="text" name="timestamp" placeholder="DD/MM/YY HH:MM"/>
      </td>
    </tr>
    </table>
    <h2>Add Participants</h2>
    <table>
    <?php
      $limit = 6;
      for($i = 0;$i< $limit;$++){
    ?>
      <tr>
          <td>Name</td>
          <td><input type="text" name="call_name[]" /></td>
          <td>Phone Number</td>
          <td><input type="text" name="call_phone[]" /></td>
          <td>Moderator?</td>
          <td>
                 <select name="call_status[]">
                       <option value="0">No</option>
                       <option value="1">Yes</option>
                 </select>
          </td>
      </tr>
    <?php
      }
    ?>
    </table>
    <button type="submit">Save</button>
    </form>

How it works...

In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP; this library is at the heart of your Twilio-powered apps. In step 3, we loaded our database schema into our database.

In step 4, we uploaded config.php, which contains our authentication information to talk to Twilio's API. In step 5, we uploaded pdo.class.php, which is our class that talks to the database.

Finally, in steps 6 and 7, we created schedule.php. This shows you your scheduled conference calls and lets you add new conferences.

In the list of scheduled conference calls, it will check the conference status with Twilio and let you monitor or mute conferences that are in progress or let you view recordings on completed conference calls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset