1. == connect_company.php // after customer click call me it goes here ==
  2. <?php
  3. require 'twilio.php';
  4.  
  5. $ApiVersion = "2008-08-01";
  6. $AccountSid = "AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  7. $AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
  8.  
  9. // Database gets a list of phone numbers to call
  10. $phone_numbers = array('XXXXXXXXXX', 'XXXXXXXXXX', 'XXXXXXXXXX');
  11. $customer_number =$_REQUEST['customer_number'];
  12.  
  13. $client = new TwilioRestClient($AccountSid, $AuthToken);
  14.  
  15. /* make Twilio REST request to initiate outgoing call */
  16. $response = $client->request("/$ApiVersion/Accounts/$AccountSid/Calls",
  17. "POST", array(
  18. "Caller" => $phone_numbers[0],
  19. "Called" => $outgoing,
  20. "Url" => $url . "start_call.php?customer_number={$customer_number}&company_numbers=".implode(',', $phone_numbers).'&current_order=0'
  21. ));
  22. ?>
  23.  
  24. == start_call.php // initializes TwiML. This also tracks the current number we are calling. ==
  25. <?php
  26. $phone_numbers = explode(',', $_REQUEST['phone_numbers']);
  27.  
  28. // Adds spaces between numbers for voice
  29. $voice_phone = implode(' ', str_split($_REQUEST['customer_number']));
  30. ?>
  31. <Response>
  32. <!-- Gather is used to screen voicemail here -->
  33. <Gather action="connect_customer.php?customer_number=<?php echo $_REQUEST['customer_number'] ?>">
  34. <Say>Press 1 to accept a call from a customer with phone number <?php echo $voice_phone ?>.</Say>
  35. </Gather>
  36.  
  37. <!-- Make sure no out of bounds in array error -->
  38. <?php if($_REQUEST['current_order'] + 1 < count($phone_numbers)): ?>
  39. <!-- Redirect if no answer // Remember to escape your & as &amp; in XML -->
  40. <Redirect url="start_call.php?customer_number=<?php echo $_REQUEST['customer_number'] ?>&amp;company_numbers=<?php echo $_REQUEST['company_numbers'] ?>&amp;current_order=".($_REQUEST['current_order'] + 1) />
  41.  
  42. <!-- No more numbers available. Could redirect here to call company voicemail. -->
  43. <?php else: ?>
  44. <Say>Sorry, no one available to answer your call. We will get back to you shortly.</Say>
  45. <?php endif; ?>
  46. </Response>