623 lines
18 KiB
PHP
623 lines
18 KiB
PHP
<?php
|
|
|
|
ini_set('error_reporting', E_ALL);
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
class TelphinApiClass extends MangoApiClass {
|
|
private $api_key;
|
|
private $api_pass;
|
|
private $base_url = "https://apiproxy.telphin.ru/";
|
|
private $token;
|
|
private $metod;
|
|
private $post;
|
|
private $client_id;
|
|
private $header;
|
|
private $users = array();
|
|
private $phone_type;
|
|
private $phone_types = [0 => 'telphinAbonentNoAnswer',
|
|
1 => 'telphinAbonentAnswer',
|
|
2 => 'abonentTelphinAnswer',
|
|
3 => 'abonentTelphinNoAnswer'];
|
|
private $client_stage = 0;
|
|
private $subscription_url = '';
|
|
private $event_subscription_type = array('dial-in','dial-out', 'hangup', 'answer');
|
|
private $dbpg = null;
|
|
|
|
public function __construct($api_key, $api_pass){
|
|
$this->api_key = $api_key;
|
|
$this->api_pass = $api_pass;
|
|
$this->dbpg = new MysqlPdo(hstpg, ndbpg, userpg, passpg, false, '5432', true);
|
|
|
|
}
|
|
|
|
public function set_users($users){
|
|
$this->users = $users;
|
|
}
|
|
|
|
public function setUrlSubscription($url){
|
|
$this->subscription_url = $url;
|
|
}
|
|
public function checkSubscription(){
|
|
|
|
$users=json_decode($this->getUsers());
|
|
|
|
foreach($users as $user){
|
|
if($user_id = $this->checkUser($user)){
|
|
$this->checkSubscriptionUser($user, $user_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function checkUser($user){
|
|
$check = false;
|
|
$id = $user->name;
|
|
$phone = '';
|
|
$extra_params = json_decode($user->extra_params, true);
|
|
//var_dump($extra_params);
|
|
if(isset($extra_params['phone_work'])){
|
|
$phone = $extra_params['phone_work'];
|
|
}
|
|
$sql = "SELECT id, phone FROM users WHERE vpbx_id='".$id."'";
|
|
if(!empty($phone)){
|
|
$sql .= " or vpbx_id = '".$phone."'";
|
|
}
|
|
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q)>0){
|
|
$r = mysql_fetch_assoc($q);
|
|
if(in_array((int)$r['id'], $this->users)){
|
|
$check = (int)$r['id'];
|
|
}
|
|
}
|
|
return $check;
|
|
}
|
|
|
|
private function checkSubscriptionUser($user, $user_id){
|
|
if($events = $this->getSubscriptionUser($user)){
|
|
foreach($this->event_subscription_type as $name){
|
|
if(!in_array($name, $events)){
|
|
$this->setSubscriptionUser($user, $user_id, $name);
|
|
}
|
|
}
|
|
} else {
|
|
$this->setSubscriptionUser($user, $user_id);
|
|
}
|
|
}
|
|
|
|
private function getSubscriptionUser($user){
|
|
|
|
$result = false;
|
|
$this->metod = 'api/ver1.0/extension/'.$user->id.'/event/';
|
|
$res = $this->sendCurl(false, true);
|
|
$data = json_decode($res);
|
|
if(!isset($data->status)){
|
|
foreach ($data as $event){
|
|
if(strpos($event->url, $this->subscription_url) !== false){
|
|
$result[] = $event->event_type;
|
|
}
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
private function setSubscriptionUser($user, $user_id, $name = null){
|
|
|
|
if(empty($name)){
|
|
foreach ($this->event_subscription_type as $nameEvent){
|
|
$this->sendSetSubscriptionUser($user, $user_id, $nameEvent);
|
|
}
|
|
} else {
|
|
//echo $name;
|
|
$this->sendSetSubscriptionUser($user, $user_id, $name);
|
|
}
|
|
}
|
|
|
|
private function sendSetSubscriptionUser($user, $user_id, $name){
|
|
$this->metod = 'api/ver1.0/extension/'.$user->id.'/event/';
|
|
$this->post = json_encode(array(
|
|
'event_type' => $name,
|
|
'method' => 'POST',
|
|
'url'=> $this->subscription_url.'?ident='.$user_id)
|
|
);
|
|
|
|
$res=$this->sendCurl(true,true);
|
|
var_dump($res);
|
|
}
|
|
|
|
|
|
private function sendCurl($post = false, $header = false){
|
|
$response = 0;
|
|
$url = $this->base_url . $this->metod;
|
|
//echo $url;
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
if($header){
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
|
|
curl_setopt($ch, CURLOPT_HEADER,0);
|
|
}
|
|
if($post){
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post);
|
|
}
|
|
$response = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
return $response;
|
|
}
|
|
|
|
public function auth(){
|
|
$this->metod = 'oauth/token';
|
|
$result = true;
|
|
|
|
$this->post = array(
|
|
'grant_type' => 'client_credentials',
|
|
'client_id' => $this->api_key,
|
|
'client_secret'=> $this->api_pass
|
|
);
|
|
|
|
$res = json_decode($this->sendCurl(true));
|
|
//var_dump($res);
|
|
if(isset($res->access_token)){
|
|
$this->token = $res->access_token;
|
|
|
|
$this->header = ['Content-type:application/json',
|
|
'Authorization:Bearer '.$this->token];
|
|
$this->getClient();
|
|
} else {
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function getClient(){
|
|
$this->metod = 'api/ver1.0/user/';
|
|
$res = json_decode($this->sendCurl(false, true));
|
|
$this->client_id = $res->client_id;
|
|
}
|
|
|
|
public function getUsers(){
|
|
$this->metod = 'api/ver1.0/client/'.$this->client_id.'/extension/';
|
|
$res = $this->sendCurl(false, true);
|
|
return $res;
|
|
}
|
|
|
|
public function calls($start_datetime, $end_datetime){
|
|
$this->metod = 'api/ver1.0/client/'.$this->client_id.'/call_history/?start_datetime='.$start_datetime.'&order=asc&end_datetime='.$end_datetime.'&per_page=100000&page=1';
|
|
$res = $this->sendCurl(false, true);
|
|
return $res;
|
|
}
|
|
|
|
public function get_record($record_uuid){
|
|
$this->metod = 'api/ver1.0/client/'.$this->client_id.'/record/'.$record_uuid;
|
|
$res = $this->sendCurl(false, true);
|
|
return $res;
|
|
}
|
|
|
|
private function type_phone($call){
|
|
//call);
|
|
$result = '';
|
|
$find_user = '';
|
|
$result = $call->result;
|
|
|
|
if($call->flow == 'in'){
|
|
if(!empty($call->bridged_username)){
|
|
$find_user = $call->bridged_username;
|
|
} else {
|
|
foreach ($call->cdr as $cdr) {
|
|
if($cdr->extension_type == 'phone'){
|
|
$find_user = $cdr->to_username;
|
|
}
|
|
}
|
|
}
|
|
|
|
} else if($call->flow == 'out'){
|
|
$find_user = $call->from_username;
|
|
|
|
}
|
|
|
|
if($call->flow == 'in'){
|
|
|
|
if(($result == 'answered' || $result == 'bridged') && !empty($call->bridged_username)){
|
|
$type = 2;
|
|
} else {
|
|
$type = 3;
|
|
}
|
|
} else {
|
|
if(($result == 'answered' || $result == 'bridged') && !empty($call->bridged_username)){
|
|
$type = 1;
|
|
} else {
|
|
$type = 0;
|
|
}
|
|
}
|
|
|
|
|
|
$this->phone_type = $this->phone_types[$type];
|
|
// echo $find_user.'*****<br>';
|
|
return $find_user;
|
|
}
|
|
|
|
public function get_phone_type($phone){
|
|
$find_user = $this->type_phone($phone);
|
|
return json_encode(array('type'=>$this -> phone_type, 'find_user'=>$find_user));
|
|
}
|
|
|
|
//Установка стадии клиента
|
|
private function set_client_stage($stage){
|
|
$this -> client_stage = $stage;
|
|
}
|
|
|
|
public function find_user($phone, $agency_id=0){
|
|
$user_id = 0;
|
|
$phone = $this->ver_phone($phone);
|
|
|
|
$sql_user = "SELECT id FROM users WHERE phone = '".$phone."'";
|
|
|
|
|
|
$q_user = mysql_query($sql_user);
|
|
if(mysql_num_rows($q_user) > 0){
|
|
$r_user = mysql_fetch_assoc($q_user);
|
|
$user_id = $r_user['id'];
|
|
}
|
|
|
|
return $user_id;
|
|
}
|
|
|
|
public function find_client($phone, $no = false){
|
|
$this -> set_client_stage(0);
|
|
$client_id = 0;
|
|
$phone_client = $this -> ver_phone($phone);
|
|
|
|
$sql_cl = "SELECT `id`, `stage` FROM `clients` WHERE cancel=0 AND (phone = '$phone_client' OR id in (SELECT client_id FROM clients_phone2 WHERE phone = '$phone_client')) and (who_work in (".implode(',', $this->users).") or id_agent in (".implode(',', $this->users)."))";
|
|
if($no){
|
|
$sql_cl = "SELECT `id`, `stage` FROM `clients` WHERE cancel=0 AND (phone = '$phone_client' OR id in (SELECT client_id FROM clients_phone2 WHERE phone = '$phone_client'))";
|
|
|
|
}
|
|
//echo $sql_cl.'****************';
|
|
$q_cl = mysql_query($sql_cl);
|
|
if(mysql_num_rows($q_cl) > 0){
|
|
$r_cl = mysql_fetch_assoc($q_cl);
|
|
$client_id = $r_cl['id'];
|
|
$this -> set_client_stage($r_cl['stage']);
|
|
}
|
|
return $client_id;
|
|
}
|
|
|
|
|
|
|
|
public function recordSave($p){
|
|
if (!is_dir($_SERVER['DOCUMENT_ROOT'] . "/server/telphin")) {
|
|
mkdir($_SERVER['DOCUMENT_ROOT'] . "/server/telphin", 0777);
|
|
}
|
|
$records = array();
|
|
|
|
foreach ($p as $rec) {
|
|
|
|
if(!empty($rec->record_uuid) && !in_array($rec->record_uuid, $records) && $rec->extension_type != 'queue'){
|
|
|
|
$records[] = $rec->record_uuid;
|
|
$file = trim($this->get_record($rec->record_uuid));
|
|
$name = $rec->record_uuid.'.mp3';
|
|
if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/server/telphin/'.$name)){
|
|
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/server/telphin/'.$name, $file);
|
|
}
|
|
//var_dump($file);
|
|
}
|
|
}
|
|
return json_encode($records);
|
|
}
|
|
|
|
public function find_user_comment($call, $type){
|
|
|
|
$to_number = $call->to_username;
|
|
if($type == 'to'){
|
|
if(!empty($call->bridged_username)){
|
|
$to_number = $call->bridged_username;
|
|
} else {
|
|
foreach ($call->cdr as $cdr) {
|
|
if($cdr->extension_type == 'phone'){
|
|
$to_number = $cdr->to_username;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$from_number = $call->from_username;
|
|
|
|
$number = $to_number;
|
|
if($type == 'from'){
|
|
$number = $from_number;
|
|
}
|
|
$mes = '';
|
|
$sql = "SELECT fio FROM mango_call_users WHERE number_sip = '".$number."'";
|
|
//echo $sql;
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) > 0){
|
|
$r = mysql_fetch_assoc($q);
|
|
$mes = '(Сотрудник '.$r['fio'].')';
|
|
}
|
|
|
|
return $mes;
|
|
}
|
|
|
|
private function checkUUId($uuid){
|
|
//var_dump($uuid);
|
|
$res = 0;
|
|
$sql = "SELECT id FROM telphin_uuid WHERE uuid = '$uuid'";
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) == 0){
|
|
$sql_i = "INSERT INTO telphin_uuid SET uuid = '$uuid'";
|
|
if(mysql_query($sql_i)){
|
|
$res = mysql_insert_id();
|
|
}
|
|
} else {
|
|
$r = mysql_fetch_assoc($q);
|
|
$res = (int)$r['id'];
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
public function stat($call, $agency_id, $only_widget=0){
|
|
$id = 0;
|
|
$idpg = 0;
|
|
//var_dump($call);
|
|
$type = json_decode($this->get_phone_type($call))->type;
|
|
// echo $call->from_username.' - '.$type.'<br>';
|
|
$to_number = $call->to_username;
|
|
$from_number = $call->from_username;
|
|
$start_time_gmt = 0;
|
|
|
|
$id_uuid = $this->checkUUId($call->call_uuid);
|
|
$record = $this->recordSave($call->cdr);
|
|
|
|
$answerResult = 'not answered';
|
|
$answer = 0;
|
|
|
|
if(!empty($call->start_time_gmt)){
|
|
if($type == 'abonentTelphinAnswer' || $type == 'telphinAbonentAnswer'){
|
|
$answer = (strtotime($call->start_time_gmt)+3*60*60);
|
|
$answerResult = 'answered';
|
|
}
|
|
}
|
|
$from_extension = '';
|
|
if($call->flow == 'out'){
|
|
$from_extension = $call->from_username;
|
|
}
|
|
if($call->flow == 'in'){
|
|
if(!empty($call->bridged_username)){
|
|
$to_number = $call->bridged_username;
|
|
} else {
|
|
foreach ($call->cdr as $cdr) {
|
|
if($cdr->extension_type == 'phone'){
|
|
$to_number = $cdr->to_username;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$start = (strtotime($call->init_time_gmt)+3*60*60);
|
|
$finish = (strtotime($call->hangup_time_gmt)+3*60*60);
|
|
|
|
//echo $to_number.'<br>';
|
|
$sql = "SELECT id FROM mango_calls WHERE operator_id=3 and operator_call_id = '".$id_uuid."'";
|
|
$q = mysql_query($sql);
|
|
$row = mysql_num_rows($q);
|
|
|
|
$qpg = $this->dbpg->query($sql);
|
|
$rowpg = $this->dbpg->num_rows($qpg);
|
|
/* if($row == 0 && $only_widget == 0){
|
|
|
|
$in = "INSERT INTO `mango_calls` SET
|
|
`records`='".$record."',
|
|
`start`= $start,
|
|
`finish`=$finish,
|
|
`answer`=$answer,
|
|
`from_extension`='".$from_extension."',
|
|
`from_number`= '".$from_number."',
|
|
`to_extension`='".$call->to_username."',
|
|
`to_number`='".$to_number."',
|
|
`disconnect_reason`='".$answerResult."',
|
|
`entry_id`='".$call->to_username."',
|
|
`line_number`='telphin',
|
|
`operator_id`=3,
|
|
`location`='',
|
|
`operator_call_id`=$id_uuid,
|
|
`uuid`='".$call->call_uuid."',
|
|
`agency_id`=$agency_id
|
|
";
|
|
// echo $in;
|
|
mysql_query($in);
|
|
$id = mysql_insert_id();
|
|
} else if($row > 0) {
|
|
$r = mysql_fetch_assoc($q);
|
|
$id = (int)$r['id'];
|
|
$id_uuid = $this->checkUUId($call->call_uuid);
|
|
$in = "UPDATE `mango_calls` SET
|
|
`records`='".$record."',
|
|
`start`= $start,
|
|
`finish`=$finish,
|
|
`answer`=$answer,
|
|
`from_extension`='".$from_extension."',
|
|
`from_number`= '".$from_number."',
|
|
`to_extension`='".$call->to_username."',
|
|
`to_number`='".$to_number."',
|
|
`disconnect_reason`='".$answerResult."',
|
|
`entry_id`='".$call->to_username."',
|
|
`line_number`='telphin',
|
|
`agency_id`=$agency_id
|
|
WHERE
|
|
operator_id=3 and operator_call_id = '".$id_uuid."'
|
|
";
|
|
//echo $in;
|
|
mysql_query($in);
|
|
}*/
|
|
if($rowpg == 0 && $only_widget == 0){
|
|
|
|
$inpg = "INSERT INTO mango_calls (
|
|
records,
|
|
start,
|
|
finish,
|
|
answer,
|
|
from_extension,
|
|
from_number,
|
|
to_extension,
|
|
to_number,
|
|
disconnect_reason,
|
|
entry_id,
|
|
line_number,
|
|
operator_id,
|
|
location,
|
|
operator_call_id,
|
|
uuid,
|
|
agency_id) VALUES
|
|
('".$record."',
|
|
$start,
|
|
$finish,
|
|
$answer,
|
|
'".$from_extension."',
|
|
'".$from_number."',
|
|
'".$call->to_username."',
|
|
'".$to_number."',
|
|
'".$answerResult."',
|
|
'".$call->to_username."',
|
|
'telphin',
|
|
3,
|
|
'',
|
|
'{$id_uuid}',
|
|
'".$call->call_uuid."',
|
|
$agency_id)
|
|
";
|
|
//file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_sql.txt", $inpg."\n", FILE_APPEND);
|
|
$this->dbpg->query($inpg);
|
|
$idpg = $this->dbpg->insert_id();
|
|
} else if($rowpg > 0) {
|
|
$r = $this->dbpg->fetch_assoc($qpg);
|
|
$idpg = (int)$r['id'];
|
|
$id_uuid = $this->checkUUId($call->call_uuid);
|
|
$inpg = "UPDATE mango_calls SET
|
|
records='".$record."',
|
|
start= $start,
|
|
finish=$finish,
|
|
answer=$answer,
|
|
from_extension='".$from_extension."',
|
|
from_number= '".$from_number."',
|
|
to_extension='".$call->to_username."',
|
|
to_number='".$to_number."',
|
|
disconnect_reason='".$answerResult."',
|
|
entry_id='".$call->to_username."',
|
|
line_number='telphin',
|
|
agency_id=$agency_id
|
|
WHERE
|
|
operator_id=3 and operator_call_id = '".$id_uuid."'
|
|
";
|
|
// file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_sql.txt", $inpg."\n", FILE_APPEND);
|
|
$this->dbpg->query($inpg);
|
|
}
|
|
$result['id'] = $id;
|
|
$result['idpg'] = $idpg;
|
|
return $result;
|
|
|
|
}
|
|
|
|
public function saveUser($user, $agency_id){
|
|
|
|
if($user->status = 'active' && !empty($user->label)){
|
|
$user_id = $user->id;
|
|
|
|
$extra_params = json_decode($user->extra_params);
|
|
//var_dump($extra_params);
|
|
$phone_home = '';
|
|
if(isset($extra_params->phone_work)){
|
|
$phone_home = $extra_params->phone_work;
|
|
}
|
|
|
|
$sql = "SELECT id FROM mango_call_users WHERE user_id='".$user_id."'";
|
|
|
|
$q = mysql_query($sql);
|
|
|
|
|
|
//Записываем
|
|
if(mysql_num_rows($q) == 0){
|
|
|
|
$in = "INSERT INTO `mango_call_users` SET
|
|
user_id = '".$user_id."',
|
|
fio = '".$user->label."',
|
|
number_phone = '".$phone_home."',
|
|
number_sip = '".$user->name."',
|
|
agency_id = $agency_id
|
|
";
|
|
mysql_query($in);
|
|
} else {
|
|
$up = "UPDATE `mango_call_users` SET
|
|
fio = '".$user->label."',
|
|
number_phone = '".$phone_home."',
|
|
number_sip = '".$user->name."',
|
|
agency_id = $agency_id
|
|
WHERE user_id = '".$user_id."'";
|
|
//echo $up.'<br>';
|
|
mysql_query($up);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function findSource($phone){
|
|
$source = 0;
|
|
$sql = "SELECT * FROM advertising_phone WHERE phone = '".$phone."'";
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) > 0){
|
|
$r = mysql_fetch_assoc($q);
|
|
$sql_v = "SELECT id FROM advertising_sources WHERE id = ".$r['adv_id']." AND user_id in (".implode(', ',$this->users).")";
|
|
$q_v = mysql_query($sql_v);
|
|
|
|
if(mysql_num_rows($q_v) > 0){
|
|
$r_v = mysql_fetch_assoc($q_v);
|
|
$source = $r_v['id'];
|
|
}
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
|
|
public function save_user_call($id_call, $user_find, $main_user, $agency_id=0){
|
|
$user_id = 0;
|
|
|
|
if($id_call > 0){
|
|
$user_id = (int)$this->find_user($user_find);
|
|
if($user_id == 0){
|
|
$user_id = (int)$main_user;
|
|
}
|
|
}
|
|
|
|
if($user_id > 0){
|
|
$sql = "SELECT id FROM user_call WHERE call_id = {$id_call}";
|
|
$q = mysql_query($sql);
|
|
if(mysql_num_rows($q) == 0){
|
|
$sql_in = "INSERT INTO user_call SET call_id = {$id_call}, user_id = {$user_id}";
|
|
mysql_query($sql_in);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function save_user_call_pg($id_call, $user_find, $main_user, $agency_id=0){
|
|
$user_id = 0;
|
|
|
|
if($id_call > 0){
|
|
$user_id = (int)$this->find_user($user_find);
|
|
if($user_id == 0){
|
|
$user_id = (int)$main_user;
|
|
}
|
|
}
|
|
|
|
if($user_id > 0){
|
|
$sql = "SELECT id FROM user_call WHERE call_id = {$id_call}";
|
|
$q = $this->dbpg->query($sql);
|
|
if($this->dbpg->num_rows($q) == 0){
|
|
$sql_in = "INSERT INTO user_call (call_id, user_id) VALUES ({$id_call}, {$user_id})";
|
|
//file_put_contents($_SERVER['DOCUMENT_ROOT']."/webhooks/log_call_sql.txt", $sql_in."\n", FILE_APPEND);
|
|
$this->dbpg->query($sql_in);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|