Moderators: gerski, enjay, williamconley, Op3r, Staydog, gardo, mflorell, MJCoate, mcargile, Kumba, Michael_N
#!/usr/bin/php -q
<?php
/***
* The purpose of this script is to force agents to become ready when certain conditions are met (e.g. too much time in PAUSE or DEAD calls)
Just save it anywhere and run it from command line or crontab.
Author: Christian Cabrera (christian AT enlaza DOT mx)
Last modify date: 2015-09-25
*/
// **************
// Config options
// **************
// API authentication
$vici_api_server = '127.0.0.1';
$vici_api_user = 'apiuser';
$vici_api_pass = 'apipass';
$vici_api_url = "http://$vici_api_server/agc/api.php?source=api&user=$vici_api_user&pass=$vici_api_pass";
// Database authentication
$db_server = '127.0.0.1';
$db_dbname = 'asterisk';
$db_user = 'cron';
$db_pass = '1234';
// WHERE conditions. You can add as many as you want to the array and they will be glued together using 'OR'.
// We recommend you enclose each in ( ) to avoid syntax errors
// WARNING: DO NOT LEAVE EMPTY OR YOU MAY HANGUP ALL YOUR CALLS!
// Match agents with more than 30 secons inside an unspecified pause code (e.g. Click pause, leave their seat and don´t select a code)
$where_clauses[] = "(status = 'PAUSED' AND last_state_change <= ADDDATE(NOW(),INTERVAL -10 SECOND) AND pause_code = '')";
// Match agents inside dead calls who haven´t selected a disposition status for more than 15 seconds
$where_clauses[] = "(status = 'DEAD' AND last_state_change <= ADDDATE(NOW(),INTERVAL -15 SECOND)) ";
// What to do if conditions are met
$api_disposition_status = 'DISMX'; // Disposition status to set
$api_force_pause_code = 'BRK'; // What pause code to use when agents do not enter one. Be sure to use a valid pause code.
// Echo everything we do.
$debug = TRUE;
// Enable test mode. Do not submit any API events. Useful for testing if your queries are OK before going live
$test = FALSE;
/* END OF CONFIGURATION.
DO NOT CHANGE ANYTHING BELOW THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING
*/
// First we check to see which agents are in the need to be logged out
if (!$db = mysql_connect($db_server,$db_user,$db_pass))
die('Cannot connect to the database: ' . mysql_error());
if (!mysql_select_db($db_dbname))
die('Unable to select database: ' . mysql_error());
$queryBase = "SELECT user,`status`,pause_code,UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(last_state_change) AS last_change_secs FROM vicidial_live_agents WHERE %s";
$query = sprintf($queryBase,implode(' OR ',$where_clauses));
if ($debug)
echo "Query: $query\n";
$result = mysql_query($query,$db);
if (!$result)
die("Could not run query ($query) from DB: " . mysql_error());
elseif (mysql_num_rows($result) == 0) {
echo "No agents found. Exiting.";
exit;
}
// Here´s the query result
$agents = $rows = array();
while ($row = mysql_fetch_assoc($result))
$agents[] = $row;
if ($debug) {
echo sprintf("Found %d agents matching your conditions.\n",count($agents) );
print_r($agents);
}
// Now we need to logout all agents that are inside the $agents array.
foreach ($agents as $agent) {
// First we hangup their calls.
// EXAMPLE: http://server/agc/api.php?source=test&user=6666&pass=1234&agent_user=1000&function=external_hangup&value=1
$options = array(
'function' => 'external_hangup',
'agent_user' => $agent['user'],
'value' => 1
);
api_call($options,$debug);
// Now we dispose them
// EXAMPLE: http://server/agc/api.php?source=test&user=6666&pass=1234&agent_user=1000&function=external_status&value=A
$options = array(
'function' => 'external_status',
'agent_user' => $agent['user'],
'value' => $api_disposition_status
);
api_call($options,$debug);
// In case agent left the pause code option window open, then FORCE a pause code
if (($agent['status'] == 'PAUSED') && ($agent['pause_code'] == '')) {
$options = array(
'function' => 'pause_code',
'agent_user' => $agent['user'],
'value' => $api_force_pause_code
);
api_call($options,$debug);
}
if ($agent['status'] == 'PAUSED') {
// In case the agent was just paused (meaning: no calls or disposition to do), just resume them
// EXAMPLE: http://server/agc/api.php?source=test&user=6666&pass=1234&agent_user=1000&function=external_pause&value=RESUME
$options = array(
'function' => 'external_pause',
'agent_user' => $agent['user'],
'value' => 'RESUME'
);
api_call($options,$debug);
}
}
/*
Create an easy way to call the API.
*/
function api_call($options) {
global $vici_api_url,$debug,$test;
$url = $vici_api_url;
foreach ($options as $key => $value)
$url .= '&' . $key . '=' . $value;
if ($debug)
echo "API URL: " . $url . "\n";
if (!$test) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
}
else
$response = 'Disable test mode to get real results';
if ($debug)
echo "Response: " .$response . "\n";
return $response;
}
?>
Users browsing this forum: No registered users and 88 guests