<?php
//Call center Automation System
// Created by Kewnhyps
// Email:Kenedymuthui1@gmail.com

//1. Receive POST from AT
  $isActive  = $_POST['isActive'];
  $callerNumber =$_POST['callerNumber'];
  $dtmfDigits = $_POST['dtmfDigits'];
  $sessionId = $_POST['sessionId'];

  //2. Check if isActive=1 to act on the call or isActive=='0' to store the result
  if($isActive=='1'){
    //2a. Switch through the DTMFDigits
    switch ($dtmfDigits) {
      case "0":
          //2b. Talk to secretary...
        $response  = '<?xml version="1.0" encoding="UTF-8"?>';
        $response .= '<Response>';
        $response .= '<Say voice="woman">Please wait on the line while we connect you to the Secretary. The call may be recorded for quality purposes. As a result, we may ask you a few questions regarding the quality of services offered</Say>'; 
        $response .= '<Dial record="true" sequential="true" phoneNumbers="+254710468569" ringbackTone="http://customercare.papemus.co.ke/media/extreme.mp3" />';
    
       $response .= '<Play url="http://customercare.papemus.co.ke/media/kalimba.mp3"/>';
        $response .= '</Response>';  
        
        // Print the response onto the page so that our gateway can read it
        header('Content-type: text/plain');
        echo $response;
          break;
          
      case "1":
          //2c. School services ...
        $response  = '<?xml version="1.0" encoding="UTF-8"?>';
        $response .= '<Response>';
        $response .= '<Say voice="woman">Welcome to Kentech School of Computing customer care. We are happy to serve you. When we come together, great things happen </Say>'; 
        $response .= '<Play url="http://customercare.papemus.co.ke/media/kalimba.mp3"/>';
        $response .= '</Response>';
         
        // Print the response onto the page so that our gateway can read it
        header('Content-type: text/plain');
        echo $response;
          break;
          
      case "2":
          //2c. Check fee balance ...
            $balanceArr = array(
                 '+254704753346' => 12000,
                 '+254706792519' => 19000,
                 );
                 
         if ( array_key_exists($callerNumber, $balanceArr) ) {
            $balance = $balanceArr[$callerNumber];
            $text    = "Welcome Ken. Your fee balance is " . $balance . " shillings. Good bye.";
         } else {
            $text = "Sorry, your phone number is not registered for this service. Good Bye.";
         }
      
        $response  = '<?xml version="1.0" encoding="UTF-8"?>';
        $response .= '<Response>';
        $response .= '<Say voice="woman">'.$text.'</Say>';
        //$response .= '<Redirect>http://62.12.117.25/MF-Ussd-Live/voiceCall.php</Redirect>';
        $response .= '</Response>';

        // Print the response onto the page so that our gateway can read it
        header('Content-type: text/plain');
        echo $response;     
          break;
          case "3":
          //2b. Talk to secretary...
        $response  = '<?xml version="1.0" encoding="UTF-8"?>';
        $response .= '<Response>';
        $response .= '<Say voice="woman">Please wait on the line while we connect you with Ken. The call may be recorded for quality purposes. As a result, we may ask you a few questions regarding the quality of services offered</Say>'; 
        $response .= '<Dial record="true" sequential="true" phoneNumbers="+254704753346" ringbackTone="http://customercare.papemus.co.ke/media/extreme.mp3" />';
    
       $response .= '<Play url="http://customercare.papemus.co.ke/media/kalimba.mp3"/>';
        $response .= '</Response>';  
        
        // Print the response onto the page so that our gateway can read it
        header('Content-type: text/plain');
        echo $response;
          break;
      default:
        //2e. By default talk to support
      $response  = '<?xml version="1.0" encoding="UTF-8"?>';
      $response .= '<Response>';
      $response .= '<GetDigits timeout="30" finishOnKey="#">';
      $response .= '<Say voice="woman">Welcome to Kentech School of Computing customer care, For school services, press 1. For queries with your fee balance, press 2.To speek to Ken, press 3 followed by hash. To speak to the secretary press 0 followed by hash.
         </Say>';     
      $response .= '</GetDigits>';
      $response .='<Say>You have not pressed any key. Good bye</Say>';
    //   $msg="Jambo, we noticed you did not make any selection.Did you know you can access all Kentech School of Computing services at one point? visit https://simulator.africastalking.com:1517 then dial *384*10056#Ok or through Kentech App?Download it here http://bit.ly/kentech";
    //   $phone=$callerNumber;
    // $phone = (substr($phone, 0, 1) == "+") ? str_replace("+", "", $phone) : $phone;
    //  $baseUrl="http://bulksms.mobitechtechnologies.com/api/sendsms";
    // $ch = curl_init($baseUrl);
    // $data= array('api_key' =>'61cc2b1027738' ,
    // 'username' =>'ecites' ,
    // 'sender_id' =>'23107' ,
    // 'message' =>$msg,
    // 'phone' => $phone);
    //  $payload = json_encode($data);
    //  curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    //  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Accept:application/json'));
    // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // $result = curl_exec($ch);
      $response .= '</Response>';

       
      // Print the response onto the page so that our gateway can read it
      header('Content-type: text/plain');
      echo $response;    
    }

  }else{
    //3. Store the data from the POST
      $durationInSeconds=$_POST['durationInSeconds'];
      $direction=$_POST['direction'];
      $amount=$_POST['amount'];
      $callerNumber=$_POST['callerNumber'];
      $destinationNumber=$_POST['destinationNumber'];
      $sessionId=$_POST['sessionId'];
      $callStartTime=$_POST['callStartTime'];
      $isActive=$_POST['isActive'];
      $currencyCode=$_POST['currencyCode'];
    $status=$_POST['status'];
   unset($msg);
     $msg="Jambo,we are committed to serving you. You can now self-serve by vising https://simulator.africastalking.com:1517 followed by dialing *384*10056# or Chat Kentech chatbot on WhatsApp https://wa.me/+254704753346";
    
    $phone=$callerNumber;
    $phone = (substr($phone, 0, 1) == "+") ? str_replace("+", "", $phone) : $phone;
     $baseUrl="http://bulksms.mobitechtechnologies.com/api/sendsms";
    $ch = curl_init($baseUrl);
    $data= array('api_key' =>'61cc2b1027738' ,
    'username' =>'ecites' ,
    'sender_id' =>'23107' ,
    'message' =>$msg,
    'phone' => $phone);
     $payload = json_encode($data);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Accept:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    //3a. Store the data, write your SQL statements here...
  }

?>
