repositório com os arquivos utilizados para integração entre o sistema SimplesIP e diversos sistemas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

210 lines
7.4 KiB

<?php
require_once 'Integracao.php';
include_once 'config.php';
/*
* Documentacao https://training.bitrix24.com/rest_help/scope_telephony/
* gerar Inbound webhook na aba recursos para desenvolvedores, other
* dar as permissoes de CRM, telephony, Users
*
*/
class Bitrix24 extends Integracao {
private $token;
private $url_api;
private $selfSigned;
private $metodo;
########################################################################
## VARIAVEIS DA CLASSE ##
########################################################################
private $query;
private $curl;
private $post = false;
private $params = array();
private $debug;
########################################################################
## RECURSOS DA API ##
########################################################################
/*
* UF_PHONE_INNER => ramal configurado no bitrix para identificacao com a central
*/
function consultaUsuario($ramal) {
$this->debug = debug_backtrace();
if ($this->getArgs(func_get_args())) {
$this->params = array(
"UF_PHONE_INNER" => "$ramal"
);
$this->setMetodo('user.get');
return $this->setParams();
} else {
return false;
}
}
/* USER_PHONE_INNER => ramal configurado no bitrix para identificacao com a central
* USER_ID => buscar o id do usuario pelo ramal usando o metodo consultaUsuario
* PHONE_NUMBER => numero que esta ligadno
* TYPE => 1 - outbound, 2 - inbound. 1 - saida, 2 - entrada
* CALL_START_DATE = data e hora do inicio da liga<EFBFBD><EFBFBD>o
* CRM_CREATE = [0/1] Option to create or not a new lead. 0 - nao cria novos leads, 1 - cria novos leads
*/
function registrarChamada($ramal, $id_usuario, $numero, $tipo, $dataHoraInicio, $crmCreate = 1) {
$this->debug = debug_backtrace();
if ($this->getArgs(func_get_args())) {
$this->params = array(
"USER_PHONE_INNER" => "$ramal",
"USER_ID" => $id_usuario,
"PHONE_NUMBER" => "$numero",
"TYPE" => $tipo,
"CALL_START_DATE" => $dataHoraInicio,
"CRM_CREATE" => $crmCreate
);
$this->setMetodo('telephony.externalcall.register');
return $this->setParams();
} else {
return false;
}
}
/* CALL_ID => id gerada no metodo registrarChamada
* USER_ID => buscar o id do usuario pelo ramal usando o metodo consultaUsuario
* DURATION => duracao da chamda em segundos
*/
function finalizarChamada($callId, $id_usuario, $duracao, $audio) {
$this->debug = debug_backtrace();
if ($this->getArgs(func_get_args())) {
$this->params = array(
"CALL_ID" => "$callId",
"USER_ID" => $id_usuario,
"DURATION" => $duracao,
"RECORD_URL" => $audio,
"ADD_TO_CHAT" => 1
);
$this->setMetodo('telephony.externalcall.finish');
return $this->setParams();
} else {
return false;
}
}
/*
* CALL_ID => id gerada no metodo registrarChamada
* FILENAME => nome do arquivo para cadastro
* FILE_CONTENT => url do audio para upload wav ou mp3
*/
function anexarAudio($callId, $audioUrl) {
$ext = (strstr($audioUrl, '.mp3')) ? ".mp3" : ".wav";
$this->debug = debug_backtrace();
if ($this->getArgs(func_get_args())) {
$this->params = array(
"CALL_ID" => "$callId",
"FILENAME" => base64_encode($callId) . $ext,
"RECORD_URL" => $audioUrl
);
$this->setMetodo('telephony.externalCall.attachRecord');
return $this->setParams();
} else {
return false;
}
}
########################################################################
## FUNCOES DO SISTEMA ##
########################################################################
function __construct($token, $url_api, $log = false, $selfSigned = true) {
$this->token = $token;
$this->url_api = "$url_api/rest/1/$token/";
$this->selfSigned = $selfSigned;
$this->setLog($log);
$this->log->info("Iniciando integracao com Bitrix", debug_backtrace());
}
private function setMetodo($metodo) {
$this->metodo = $metodo;
}
/**
* Escreve a query para ser passada para o curl
*
* @param string $query
*/
private function setQuery($query) {
return $this->query .= $query;
}
/**
* retorna a string pronta da query do curl e limpa a variavel.
*
* @return string $query
*/
private function getQuery() {
$query = $this->query;
unset($this->query);
return $query;
}
/**
* Habilita requisicao via POST
*
* @return boolean
*/
private function getPost() {
return $this->post = true;
}
private function strCurl() {
$this->curl = "curl -s -k ";
$this->curl .= sprintf("-H \"Content-Type: application/json\" -X POST %s ", $this->query ? '-d' : '');
$this->curl .= $this->getQuery();
$this->curl .= "{$this->url_api}{$this->metodo}.json";
$this->log->debug("Curl: {$this->curl}", debug_backtrace());
}
/**
* Recebe array de forma de indice e valor
*
* @example array("qtype" => 'test_api', "query" => '123', "oper" => '=')
*
* @param type $params
*/
private function setParams() {
$count = 0;
if ($this->params) {
$this->setQuery("'{");
foreach ($this->params as $key => $param) {
$count++;
$this->setQuery("\"{$key}\": \"$param\"");
if ($count == count($this->params)) {
$this->setQuery("}' ");
} else {
$this->setQuery(",");
}
}
}
unset($this->params);
$this->strCurl();
$result = $this->exec();
$ret = json_decode($result, true);
return $ret;
}
function exec() {
$exec = shell_exec($this->curl);
return $this->response($exec);
}
private function response($data) {
$this->log->debug("Reponse: " . print_r(json_decode($data, true), true), $this->debug);
if ($data) {
return $data;
} else {
return false;
}
}
}