|
|
<?php |
|
|
|
|
|
namespace app\Core; |
|
|
|
|
|
use app\Core\Commands; |
|
|
use app\Models\Message; |
|
|
use app\Providers\Crypt; |
|
|
use app\Controllers\QueueController; |
|
|
use app\Controllers\AgentController; |
|
|
use app\Controllers\BilheteController; |
|
|
use app\Controllers\SystemMessageController; |
|
|
use app\Interfaces\IApiMedia; |
|
|
use app\Models\Atendimento; |
|
|
use app\Models\Evento; |
|
|
use app\Models\ListaNegraPalavras; |
|
|
use app\Models\SupervisorModel; |
|
|
use websocket\WsInterface; |
|
|
|
|
|
/** |
|
|
* Description of WhatsApp |
|
|
* |
|
|
* @author Lucas Awade |
|
|
*/ |
|
|
class CoreMedia |
|
|
{ |
|
|
/** @var IApiMedia $api api do provedor de mensagens */ |
|
|
protected $api; |
|
|
|
|
|
/** @var Commands $commands classe de comandos do sistema*/ |
|
|
protected $commands; |
|
|
|
|
|
/** @var QueueController $queue controller da queue*/ |
|
|
protected $queue; |
|
|
|
|
|
/** @var AgentController $agente controller do agente*/ |
|
|
protected $agente; |
|
|
|
|
|
/** @var ListaNegraPalavras $palavroes model de palavras banida*/ |
|
|
protected $palavroes; |
|
|
|
|
|
/** @var WsInterface $ws classe de comandos do sistema*/ |
|
|
protected $ws; |
|
|
|
|
|
/** @var Atendimento $atendimento model de atendimentos*/ |
|
|
protected $atendimento; |
|
|
|
|
|
/** @var SupervisorModel $supervisor model de supervisor*/ |
|
|
protected $supervisor; |
|
|
|
|
|
/** @var Evento $eventos model de eventos*/ |
|
|
protected $eventos; |
|
|
|
|
|
/** @var BilheteController $eventos controller dos bilhetess*/ |
|
|
protected $bilheteController; |
|
|
|
|
|
/** @var Message $message model de mensagens*/ |
|
|
protected $message; |
|
|
|
|
|
/** @var SystemMessageController $systemController controller de mensagens de sistema*/ |
|
|
protected $systemController; |
|
|
|
|
|
/** @var Crypt $crypt core de criptografia*/ |
|
|
protected $crypt; |
|
|
|
|
|
public function __construct() |
|
|
{ |
|
|
$this->ws = new WsInterface(); |
|
|
$this->queue = new QueueController(); |
|
|
$this->commands = new Commands(); |
|
|
$this->agente = new AgentController(); |
|
|
$this->crypt = new Crypt(); |
|
|
$this->atendimento = new Atendimento(); |
|
|
$this->message = new Message(); |
|
|
$this->palavroes = new ListaNegraPalavras(); |
|
|
$this->supervisor = new SupervisorModel(); |
|
|
$this->eventos = new Evento(); |
|
|
$this->bilheteController = new BilheteController; |
|
|
$this->systemController = new SystemMessageController(); |
|
|
} |
|
|
public function setApi(IApiMedia $api) |
|
|
{ |
|
|
$this->api = $api; |
|
|
} |
|
|
/** |
|
|
* Send the file to the active contact |
|
|
* |
|
|
* @return null |
|
|
*/ |
|
|
public function inicia($data, IApiMedia $providere) |
|
|
{ |
|
|
/** Validate $data */ |
|
|
$this->api = $providere; |
|
|
$this->build($data); |
|
|
|
|
|
if (!$this->api->getIsValidMessage()) { |
|
|
return null; |
|
|
} |
|
|
if (!$this->api->baixarMidia()) { |
|
|
$this->api->enviarMsg($this->api->getPhone(), "N<EFBFBD>o foi possivel entregar o aquivo para o agente. Envie novamente!"); |
|
|
} |
|
|
// VERIFICA SE ? UM COMANDO |
|
|
if ($this->commands->isCommand($this->api)) { |
|
|
return null; |
|
|
} |
|
|
//verifica se tem atendimento em aberto, se tiver ja manda msg para o agente via ws |
|
|
$atendiment = $this->atendimento->findAtenEmAberto($this->api->getPhone()); |
|
|
|
|
|
if ($atendiment) { |
|
|
$this->enviaMensagemAgente($atendiment); |
|
|
return null; |
|
|
} |
|
|
//verifica se tem atendimento em espera, se tiver ja mostra a sua posi<EFBFBD><EFBFBD>o na fila |
|
|
$atende = $this->atendimento->getAtendimentoByCliente($this->api->getPhone()); |
|
|
if (empty($atende)) { |
|
|
$atende = $this->atendimento->getAtendimentoByCliente($this->api->getPhone(), CONF_EVENT_ERRO_ATEND); |
|
|
} |
|
|
if ($atende) { |
|
|
$this->mostraPosiscaoNaFila($this->api->getPhone(), $atende->fila); |
|
|
return null; |
|
|
} |
|
|
//verifica se o cliente escolheu uma fila caso n<EFBFBD>o escolheu manda as filas |
|
|
$filas = $this->queue->listAllQueueWhatsApp($this->api->getMessage()); |
|
|
if ($filas['LIST']) { |
|
|
//manda as mensagens personalizada do moment CONF_MOMENT_SAUDACAO |
|
|
$this->systemController->sendMessageSystem( |
|
|
CONF_MOMENT_SAUDACAO, |
|
|
[["nome" => "@cliente_name", "valor" => $this->api->getProfile()]], |
|
|
$this->api, |
|
|
$this->api->getPhone() |
|
|
); |
|
|
$this->api->enviarMsg($this->api->getPhone(), $filas['LIST']); |
|
|
return null; |
|
|
} |
|
|
// cria a atendimento |
|
|
$this->criaAtendimento($filas['QUEUE'], $this->api->getPhone()); |
|
|
} |
|
|
/** |
|
|
* Set variables class |
|
|
* @param array $data |
|
|
* @return boolean |
|
|
*/ |
|
|
public function build($data) |
|
|
{ |
|
|
$this->api->setHook($data); |
|
|
$this->validaPalavroes(); |
|
|
} |
|
|
|
|
|
public function validaPalavroes() |
|
|
{ |
|
|
try { |
|
|
$msg = $this->api->getMessage(); |
|
|
$palavras = $this->palavroes->getAll(); |
|
|
foreach ($palavras as $key => $value) { |
|
|
$pattern = "/\b($value->palavra)\b/i"; |
|
|
$msg = preg_replace($pattern, '*' . str_repeat('*', strlen($value->palavra)) . '*', $msg); |
|
|
} |
|
|
$this->api->setMessage($msg); |
|
|
} catch (\Exception $th) { |
|
|
} |
|
|
return; |
|
|
} |
|
|
public function criaAtendimento($fila, $numero, $uniqueid = null) |
|
|
{ |
|
|
try { |
|
|
if (empty($uniqueid)) { |
|
|
$uniqueid = $this->atendimento->createAtendimento(null, $numero, 'E', $this->api->getchannel(), $this->api->getProfile()); |
|
|
if ($uniqueid) { |
|
|
$this->eventos->createEvento($uniqueid, 'EMESPERA', date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), $fila); |
|
|
} |
|
|
} |
|
|
$agentes = $this->agente->infoAgentes($fila); |
|
|
if (empty($agentes)) { |
|
|
$this->systemController->sendMessageSystem( |
|
|
CONF_MOMENT_ENTRAR_FILA_SEM, |
|
|
[], |
|
|
$this->api, |
|
|
$numero, |
|
|
$fila |
|
|
); |
|
|
return null; |
|
|
} |
|
|
$agent = $this->supervisor->findAgentByQueue($fila, 'LIVRE'); |
|
|
if (empty($agent)) { |
|
|
$this->systemController->sendMessageSystem( |
|
|
CONF_MOMENT_ENTRAR_FILA_COM, |
|
|
[], |
|
|
$this->api, |
|
|
$numero, |
|
|
$fila |
|
|
); |
|
|
$this->mostraPosiscaoNaFila($numero, $fila); |
|
|
return null; |
|
|
} |
|
|
$retServe = $this->atendimento->updAtendimento($uniqueid, $agent[0]->matricula); |
|
|
if (empty($retServe)) { |
|
|
$this->api->enviarMsg( |
|
|
$numero, |
|
|
'Erro ao gerar atendimento!' |
|
|
); |
|
|
return null; |
|
|
} |
|
|
$protocol = $this->bilheteController->generateProtocol($uniqueid); |
|
|
//cria o evento de inicio de atendimento e criar o protocolo |
|
|
if ($protocol) { |
|
|
$event = $this->eventos->getStatusAtendimento($uniqueid); |
|
|
$retCria = $this->eventos->createEvento( |
|
|
$uniqueid, |
|
|
CONF_EVENT_START, |
|
|
date('Y-m-d H:i:s'), |
|
|
date('Y-m-d H:i:s'), |
|
|
$fila, |
|
|
$agent[0]->matricula |
|
|
); |
|
|
if (!empty($retCria) && $event->evento != CONF_EVENT_START) { |
|
|
$atendimentosAbertos = $this->atendimento->getAtendimentoAbertoByAgente($agent[0]->matricula); |
|
|
$sup = new SupervisorModel(); |
|
|
$param = $this->atendimento->getQuantiAtendimentSimultaneos(); |
|
|
$ws = new WsInterface(); |
|
|
if ((count($atendimentosAbertos)) >= $param->prm_media_simultaneo) { |
|
|
$sup->updateAgent($agent[0]->matricula, CONF_AGENT_STATUS_OCUPADO); |
|
|
} |
|
|
$this->systemController->sendMessageSystem( |
|
|
CONF_MOMENT_INICIAR_ATENDIMENTO, |
|
|
[["nome" => "@agente_name", "valor" => utf8_encode($agent[0]->nome)]], |
|
|
$this->api, |
|
|
$numero, |
|
|
$fila |
|
|
); |
|
|
$this->api->enviarMsg( |
|
|
$numero, |
|
|
"N<EFBFBD>mero do protocolo do atendimento <EFBFBD> $protocol\n" |
|
|
); |
|
|
try { |
|
|
if ($event->evento == CONF_EVENT_ERRO_ATEND) { |
|
|
$ws->enviaMsg($ws->enviaActions( |
|
|
'Este atendimento foi realocado para sua responsabilidade.', |
|
|
're_start', |
|
|
$agent[0]->matricula, |
|
|
$uniqueid |
|
|
)); |
|
|
$this->message->addMessage( |
|
|
$uniqueid, |
|
|
$agent[0]->matricula, |
|
|
$agent[0]->matricula, |
|
|
're_start', |
|
|
'Este atendimento foi realocado para sua responsabilidade.', |
|
|
$agent[0]->nome, |
|
|
$this->api->getchannel(), |
|
|
'read' |
|
|
); |
|
|
} else { |
|
|
$ws->enviaMsg($ws->enviaActions('Atendimento iniciado!', 'start', $agent[0]->matricula, $uniqueid)); |
|
|
} |
|
|
} catch (\Exception $th) { |
|
|
return $ws->enviaActions('Atendimento iniciado!', 'start', $agent[0]->matricula, $uniqueid); |
|
|
} |
|
|
} |
|
|
} |
|
|
return; |
|
|
} catch (\Exception $th) { |
|
|
logger('criaAtendimento')->info($th->getMessage()); |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
public function mostraPosiscaoNaFila($numero, $fila) |
|
|
{ |
|
|
$clientfila = $this->queue->clientQueueVerify($numero, $fila); |
|
|
if ($clientfila['MESSAGE']) { |
|
|
$this->api->enviarMsg($numero, $clientfila['MESSAGE']); |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
public function retornaConteudo() |
|
|
{ |
|
|
switch ($this->api->getType()) { |
|
|
case 'text': |
|
|
return $this->api->getMessage(); |
|
|
case 'image': |
|
|
case 'sticker': |
|
|
case 'video': |
|
|
case 'voice': |
|
|
case 'audio': |
|
|
case 'document': |
|
|
return $this->api->getId(); |
|
|
case 'contacts': |
|
|
return null; |
|
|
case 'location': |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
function enviaMensagemAgente($atendiment) |
|
|
{ |
|
|
|
|
|
$this->message->addMessage( |
|
|
$atendiment->uniqueid, |
|
|
$this->api->getPhone(), |
|
|
$atendiment->matricula, |
|
|
$this->api->getType(), |
|
|
$this->retornaConteudo(), |
|
|
empty($this->api->getProfile()) ? '' : $this->api->getProfile(), |
|
|
$this->api->getchannel(), |
|
|
"sended", |
|
|
$this->api->getMimetype(), |
|
|
$this->api->retornaTituloDocument(), |
|
|
$this->api->getId() |
|
|
); |
|
|
$this->ws->enviaMsg( |
|
|
$this->ws->convertToWebsocket( |
|
|
$this->retornaConteudo(), |
|
|
$atendiment->matricula, |
|
|
$atendiment->uniqueid, |
|
|
$this->api->getType(), |
|
|
$this->api->getProfile(), |
|
|
$this->api->getPhone(), |
|
|
time(), |
|
|
$this->api->getId(), |
|
|
$this->api->getMimetype(), |
|
|
$this->api->retornaTituloDocument(), |
|
|
$this->api->getchannel() |
|
|
) |
|
|
); |
|
|
} |
|
|
} |