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ã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��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ã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úmero do protocolo do atendimento é $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() ) ); } }