Adding incoming callers to a call queue

In this recipe, we're going to set up a simple call queue. We're going to have this work with two separate numbers: one number is the number that the callers will call on and a second number will handle the agents who call in.

Callers will get placed in a queue, and when an agent calls the agent number, they will be connected to the first caller who is waiting to speak to someone.

Getting started

The complete code for this recipe can be found in the Code/Recipe1/ folder.

How to do it…

OK, let's set up our listener.php file to place all incoming calls in a queue and agent.php to connect our agents to callers by performing the following steps:

  1. Download the Twilio Helper Library available at https://github.com/twilio/twilio-php/zipball/master and unzip it.
  2. Upload the Services/ folder to your website.
  3. Upload config.php to your web server using the following code:
      <?php
        $accountsid = '';  //  YOUR TWILIO ACCOUNT SID
        $authtoken = '';  //  YOUR TWILIO AUTH TOKEN
        $callqueue = 'Twilio Cookbook'; //  YOUR CALL QUEUE
      ?>
  4. Upload listener.php to your web server using the following code:
    <?php
      include("config.php");
    
      # Include Twilio PHP helper library.
      require('Services/Twilio.php'),
    
      # Tell Twilio to expect some XML
      header('Content-type: text/xml'),
    
      # Create response object.
      $response = new Services_Twilio_Twiml();
    
      $response->enqueue( $callqueue );
    
      # Print TwiML
      print $response;
  5. Create a file called agent.php using the following code:
    <?php
      include("config.php");
    
      # Include Twilio PHP helper library.
      require('Services/Twilio.php'),
      
      # Tell Twilio to expect some XML
      header('Content-type: text/xml'),
      
      # Create response object.
      $response = new Services_Twilio_Twiml();
      
      # Dial into the Queue we placed the caller into to connect agent to
      # first person in the Queue.
      $dial = $response->dial();
      $dial->queue( $callqueue );
      
      # Print TwiML
      print $response;
  6. Next, we have to point your Twilio phone number to listener.php, as shown in the following screenshot:
    How to do it…
  7. Insert the URL in the box for Voice, which is shown on the page in the preceding screenshot. Then, any calls that you receive on this number will be processed via listener.php.
  8. Finally, we have to point your agent's Twilio phone number to agent.php as shown in the following screenshot:
    How to do it…
  9. Insert the URL in the box for Voice on this page. Then, any calls that you receive on this number will be processed via agent.php.

How it works…

In steps 1 and 2, we downloaded and set up our Twilio PHP library.

In step 3, we set up our config.php file with our Twilio settings.

In step 4, we uploaded listener.php, which handles calls from our customers and places them into a call queue.

In step 5, we uploaded agent.php, which handles calls from agents and connects them to customers waiting in the call queue.

In step 6, we set up our Twilio phone number that receives incoming calls and adds them to a queue.

Finally, in steps, 7, 8, and 9, we added agent.php to a phone number to connect our agents to any callers waiting in a queue.

This is a two-prong system. Firstly, any incoming caller is placed inside a call queue. Secondly, any agent who calls is connected to the first caller waiting to talk to an agent.

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

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