Created
October 10, 2019 08:19
-
-
Save nutch31/62ccd7c1e977120c1c430eba1112dc58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
require 'db.php'; | |
global $agi,$conn; | |
require 'phpagi.php'; | |
$agi = new AGI(); | |
$agi->answer(); | |
$array_url = getUrl(); | |
//$agi->verbose("*****$url"); | |
$calldate = $argv[1]; | |
$path = $argv[2]; | |
$endtime = $argv[3]; | |
$answertime = $argv[4]; | |
$clid = $argv[5]; | |
$src = $argv[6]; | |
$dst = $argv[7]; | |
$dcontext = $argv[8]; | |
$channel = $argv[9]; | |
$dstchannel = $argv[10]; | |
$duration = $argv[11]; | |
$billsec = $argv[12]; | |
$disposition = $argv[13]; | |
$uniqueid = $argv[14]; | |
$heronum = $argv[15]; | |
$sms_email = $argv[16]; | |
$camp_name = $argv[17]; | |
$camp_id = $argv[18]; | |
$recording_url = $argv[20]; | |
// Check Sms Email Configuration | |
list($sms, $email) = checkSmsEmail($sms_email); | |
// Get Client Number | |
$client_number = getClientNumber($path); | |
// Get Time Duration Call | |
$time = getTime($disposition); | |
// Prepare Array Data send to webhook | |
$data = prepareData($answertime, $heronum, $camp_id, $camp_name, $client_number, $clid, $disposition, $time, $recording_url); | |
// Send Array Data to webhook | |
$status_error = sendData($data, $array_url); | |
// status_error = true error, false = not error | |
if($status_error) { | |
exit(-1); | |
} else { | |
exit(0); | |
} | |
// Get Webhook API | |
function getUrl() { | |
$qry = "select * from `url_master`"; | |
$query = mysql_query($qry); | |
$info = mysql_fetch_array($query); | |
return [ | |
$info['url'], | |
'https://xxxx.com/api/calls?token=xxxx', | |
'https://xxxx.co.th/PbxCallService?token=xxxxx' | |
]; | |
} | |
// Check Configuration Sms Email | |
function checkSmsEmail($sms_email) { | |
if ($sms_email == "1") { | |
$sms = "no"; | |
$email = "yes"; | |
} elseif ($sms_email == "2") { | |
$sms = "yes"; | |
$email = "no"; | |
} elseif ($sms_email == "3") { | |
$sms = "no"; | |
$email = "no"; | |
} else { | |
$sms = "yes"; | |
$email = "yes"; | |
} | |
return [ | |
$sms, | |
]; | |
} | |
// Get Client Number | |
function getClientNumber($path) { | |
$array = explode("-", $path); | |
return $array[1]; | |
} | |
// Get Time Duration Call | |
function getTime($disposition) { | |
// $time = gmdate("H:i:s",$duration); | |
if ($disposition == "ANSWER" || $disposition == "ANSWERED") { | |
//$time = "00:00:00"; | |
$time = gmdate("H:i:s", $duration); | |
} else { | |
//$time = gmdate("H:i:s",$duration); | |
$time = "00:00:00"; | |
} | |
return $time; | |
} | |
// Prepare Array Data to webhook | |
function prepareData ($answertime, $heronum, $camp_id, $camp_name, $client_number, $clid, $disposition, $time, $recording_url = '') { | |
return [ | |
'timestamp' => $answertime, | |
'heronumber' => $heronum, | |
'campaign_id' => $camp_id, | |
'campaign_name' => $camp_name , | |
'client_number' => $client_number, | |
'caller_id' => $clid, | |
'status' => $disposition, | |
'duration' => $time, | |
'recording_url' => $recording_url | |
]; | |
} | |
// Send Data to webhook | |
function sendData($data, $array_url) { | |
// Create status error, Default false | |
$status_error = false; | |
// Array to Json | |
$val = json_encode($data); | |
// Create the multiple cURL handle | |
$mh = curl_multi_init(); | |
// Create ch_list array | |
$ch_list = array(); | |
foreach ($array_url as $url) { | |
// Create both cURL resources | |
$ch_list[$url] = curl_init($url); | |
// set URL and other appropriate options | |
curl_setopt($ch_list[$url], CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch_list[$url], CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch_list[$url], CURLOPT_VERBOSE, true); | |
curl_setopt($ch_list[$url], CURLOPT_POSTFIELDS, $val); | |
curl_setopt($ch_list[$url], CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch_list[$url], | |
CURLOPT_HTTPHEADER, | |
array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($val) | |
) | |
); | |
// add the two handles | |
curl_multi_add_handle($mh, $ch_list[$url]); | |
} | |
// execute the multi handle | |
do { | |
$status = curl_multi_exec($mh, $active); | |
if ($active) { | |
curl_multi_select($mh); | |
} | |
} while ($active && $status == CURLM_OK); | |
// Check for errors | |
if ($status != CURLM_OK) { | |
// Display error message | |
// echo "ERROR!\n " . curl_multi_strerror($status); | |
$status_error = true; | |
fwrite(STDERR, "ERROR!\n " . curl_multi_strerror($status)); | |
} | |
foreach ($array_url as $url) { | |
//close the handles | |
curl_multi_remove_handle($mh, $ch_list[$url]); | |
} | |
curl_multi_close($mh); | |
return $status_error; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment