classificacao = new ClassificacaoController(); $this->agentController = new AgentController(); $this->supervisor = new SupervisorModel(); $this->atendimento = new Atendimento(); $this->eventos = new Evento(); $this->pausasModel = new Pause(); $this->parametros = new Parametros(); } function router($rota, $request) { $request = json_decode($request, true); switch ($rota) { case 'entrar': $this->login($request); break; case 'listarFilas': $this->listaFilas($request); break; case 'classificarAtendimento': $this->classificarAtendimento($request); break; case 'enviarMensagem': $this->enviaMsg($request); break; case 'sair': $this->logoff($request); break; case 'listarAgentesDisponivel': $this->listaAgentesDisponivel(); break; case 'finalizarAtendimento': $this->finalizaAtendimento($request); break; case 'listarAtendimentoAgente': $this->listaAtendimentoAgent($request); break; case 'listarMensagem': $this->listaMensagem($request); break; case 'listarPausasAgente': $this->listaPausasAgente($request); break; case 'sairPausa': $this->saiPausa($request); break; case 'entrarPausa': $this->entrarPausa($request); break; case 'transferirAtendimento': $this->transfAtendimento($request); break; case 'marcarMensagemVista': $this->markMessegeRead($request); break; case 'statusAgente': $this->statusAgente($request); break; case 'listarAtendimentosFilas': $this->listarAtendimentosFilas($request); break; case 'listarAtendimentosAbandonado': $this->listarAtendimentosAbandonado($request); break; default: echo json_encode(['status' => '404']); break; } } function login($request) { try { $agente = $this->supervisor->findByAgent($request['matricula']); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } $filaModel = new Queue(); $fila = $filaModel->findQueueById($request['id_fila']); if (empty($fila)) { $this->retorno("Fila não encontrada"); return; } $ret = $this->agentController->login( $fila->nome, $request['matricula'] ); if (is_string($ret)) { $this->retorno($ret); return; } $this->retorno( $ret ? "Logado com sucesso" : "Erro", $ret ? $ret : null ); } catch (\Exception $th) { $this->retorno($th->getMessage()); } return null; } function logoff($request, $valida = true) { try { $agente = $this->supervisor->findAgentByMatricula($request['matricula']); //verifica se existe agente $atends = $this->atendimento->getAtendimentoAbertoByAgente($request['matricula']); if (!$valida) { foreach ($atends as $key => $atendimento) { $this->sinalisaErroAtendimento( $request['matricula'], $atendimento->uniqueid, $agente->fila, $atendimento->cliente_id ); } } if (!empty($atends) && $valida) { $this->retorno("Atendimentos em aberto, finalize os atendimentos para fazer logoff"); return; } if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } $ret = $this->agentController->logoff( $request['matricula'], $valida ); if (is_string($ret)) { $this->retorno($ret); return; } $this->retorno( $ret ? "Deslogado com sucesso" : "Erro", $ret ? $ret : null ); } catch (\Exception $th) { $this->retorno($th->getMessage()); } return null; } function listaAgentesDisponivel() {; try { $param = $this->atendimento->getQuantiAtendimentSimultaneos(); $ret = $this->supervisor->listaAgentesDisponivel('LIVRE'); $agentes = []; foreach ($ret as $key => $value) { if ($value->countAtendimentos < $param->prm_media_simultaneo) { array_push($agentes, $value); } } $this->retorno( $agentes ? "Sucesso" : "Nenhum agente disponivel", $agentes ? $agentes : null, $agentes ? $agentes : null ); } catch (\Exception $th) { $this->retorno($th->getMessage()); } return null; } function finalizaAtendimento($request) { try { $agente = $this->supervisor->findAgentByMatricula($request['matricula']); logger('debug')->info(var_export($agente, true)); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } $atendimento = $this->atendimento->findAtendId($request['uniqueid']); //verifica se existe atendimento if (empty($atendimento)) { $this->retorno("Atendimento não encontrado"); return; } $event = $this->eventos->findEventFinish($request['uniqueid']); //verifica se o atendimento ja foi finalizado if (!empty($event)) { $this->retorno("Atendimento já foi finalizado"); return; } $ret = $this->eventos->createEvento( $request['uniqueid'], CONF_EVENT_TIMERMINO_AGENTE, date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), $atendimento->fila, $request['matricula'] ); $ws = new WsInterface(); $mesg = "Atendimento finalizado pelo agente {$agente->nome}"; $ws->enviaMsg($this->enviaActions($mesg, 'finish', $agente->matricula, $request['uniqueid'])); $systemController = new SystemMessageController(); $provedor = returnChannel($atendimento->context); $systemController->sendMessageSystem( CONF_MOMENT_FINALIZAR_ATENDIMENTO, [["nome" => "@autor_name", "valor" => $agente->nome]], $provedor, $atendimento->cliente_id ); $messegeModel = new Message(); $messegeModel->addMessage( $request['uniqueid'], $agente->matricula, $agente->matricula, 'finish', $mesg, $agente->nome, $atendimento->context, 'read' ); if ($agente->status == CONF_AGENT_STATUS_INDISPONIVEL) { $atends = $this->atendimento->getAtendimentoAbertoByAgente($request['matricula']); if (empty($atends)) { $this->agentController->enterPause($request['matricula'], $agente->motivo_pausa); $ws->enviaMsg($this->enviaActions('Agente em pausa', 'att_status', $agente->matricula, $request['uniqueid'])); } } if ($agente->status == CONF_AGENT_STATUS_OCUPADO) { $atendimentosAbertos = $this->atendimento->getAtendimentoAbertoByAgente($agente->matricula); $param = $this->atendimento->getQuantiAtendimentSimultaneos(); if (count($atendimentosAbertos) < $param->prm_media_simultaneo) { $this->supervisor->updateAgent($agente->matricula, CONF_AGENT_STATUS_LIVRE); } } $this->retorno( $ret ? "Finalizado com sucesso" : "Erro", $ret ? $ret : null, $ret ? [$ret] : null ); } catch (\Exception $th) { $this->retorno($th->getMessage()); } return null; } function sinalisaErroAtendimento($matricula, $uniqueid, $fila, $client_id) { try { $ret = $this->eventos->createEvento( $uniqueid, CONF_EVENT_ERRO_ATEND, date('Y-m-d H:i:s'), date('Y-m-d H:i:s'), $fila, $matricula ); $atendimento = $this->atendimento->findAtendId($uniqueid); $systemController = new SystemMessageController(); $provedor = returnChannel($atendimento->context); $systemController->sendMessageSystem( CONF_MOMENT_ERRO_ATEND, [], $provedor, $client_id, $fila ); $this->retorno( $ret ? "Finalizado com sucesso" : "Erro", $ret ? $ret : null, $ret ? ["id" => $ret] : null ); } catch (\Exception $th) { $this->retorno($th->getMessage()); } return null; } public function listaFilas() { $queue = new QueueController(); $search = $queue->listaAllFilas(); $this->retorno( $search ? "Sucesso" : "Erro", $search ? $search : null, $search ? $search : null ); return null; } public function classificarAtendimento($request) { $agent = $this->agentController->getAgente($request['ramal']); if (!$agent) { $msg = "Faça o login para iniciar o atendimento!\n"; echo json_encode(['message' => $msg]); return null; } /** * VERIFICA CLASSIFICACAO DE ATENDIMENTO */ $chamadaSemClassificacao = $this->classificacao->agentClassificacaoPending($agent->matricula, $agent->dac); if ($chamadaSemClassificacao) { $classificacaoRegisterReturn = $this->classificacao->agentClassificacaoRegister( $agent->matricula, $agent->dac, $chamadaSemClassificacao, $request['option'] ); if ($classificacaoRegisterReturn) { echo json_encode(['message' => CONF_NAME_REPONSE . " : " . $this->classificacao->message()]); return null; } $classificacaoList = $this->classificacao->agentClassificacaoList($agent->dac); echo json_encode(['message' => CONF_NAME_REPONSE . " : " . $classificacaoList]); return null; } echo json_encode(['status' => "false"]); return null; } public function enviaMsg($request) { try { $mensagem = $request['event']['mensagem']; $contact = $request['event']['contact']; $status = $this->atendimento->getStatusAtendimento($mensagem['uniqueid']); if ($status->evento == CONF_EVENT_START) { $provider = returnChannel($mensagem['media']); $modelMensagem = new Message(); $msgTexto = $contact['name'] . ': ' . $this->validaPalavroes($mensagem['content']); if ($mensagem['type'] == 'text') { $retuno = $provider->enviarMsg( $mensagem['dst'], $msgTexto, false ); } else { $anmeArquivo = CONF_PATH_FILES . $mensagem['id_provedor']; $texto = base64_decode($mensagem['content']); file_put_contents($anmeArquivo, $texto); ConvertWavToMp3($anmeArquivo); $retuno = $provider->enviarMedia( $mensagem['dst'], CONF_MIDDLEWARE_LINKUPLOAD . $mensagem['id_provedor'] . '/' . base64_encode($mensagem['mimetype']), $mensagem['type'], $mensagem['file_name'] ); } $modelMensagem->addMessage( $mensagem['uniqueid'], $contact['number'], $mensagem['dst'], $mensagem['type'], $mensagem['type'] == 'text' ? $msgTexto : $mensagem['id_provedor'], $contact['name'], $mensagem['media'], $mensagem['status'], $mensagem['mimetype'], $mensagem['file_name'], $mensagem['id_provedor'] ); $this->retorno( $retuno ? "Sucesso" : "Erro", $retuno ? $retuno : null, $retuno ? [["message" => $retuno]] : null ); } else { $this->retorno('Atendimento já foi finalizado'); } return; } catch (\Exception $th) { $this->retorno($th->getMessage()); return; } } function retorno($mensagem, $status = null, $dados = null) { //{ "status": "success", "message": "register created!", "data": [ "id": 20 ] } $data = []; $data['message'] = utf8_encode($mensagem); if (!empty($status)) { $data['status'] = "success"; } else { $data['status'] = "error"; } if (!empty($dados)) { $data['data'] = $dados; } echo json_encode($data); } function listaAtendimentoAgent($request) { try { if ($request['matricula']) { $agente = $this->supervisor->findAgentByMatricula($request['matricula']); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } } $ret = $this->atendimento->findAtendAgent($request['matricula'], $request['quantidade']); $data = []; $data['message'] = utf8_encode("Sucesso"); $data['status'] = "success"; $data['data'] = $ret; echo json_encode($data); return null; } catch (\Exception $th) { $this->retorno($th->getMessage()); return; } } function listaMensagem($request) { try { $atend = $this->atendimento->findAtendId($request['uniqueid']); //verifica se existe agente if (empty($atend)) { $this->retorno("Não existe atendimento para esse uniqueid"); return; } $messageModel = new Message(); $retunr = $messageModel->findMessageByUniqueid($request['uniqueid']); $mensagenss = []; foreach ($retunr as $key => $value) { $mensagem = []; $mensagem["event"] = [ "type" => $value->type == 'finish' ? 'actions' : "mensagem", "contact" => [ "name" => utf8_encode($value->profile_name), "number" => $value->src, ], "mensagem" => [ "type" => $value->type, "content" => $value->content, "id_provedor" => $value->id_provedor, "dst" => $value->dst, "uniqueid" => $value->uniqueid, "media" => $value->media, "file_name" => utf8_encode($value->file_name), "datetime" => $value->msg_date, "status" => $value->status, 'mimetype' => $value->mimetype ] ]; array_push($mensagenss, $mensagem); } $data = []; $data['message'] = utf8_encode("Sucesso"); $data['status'] = "success"; $data['data'] = $mensagenss; echo json_encode($data); return; } catch (\Exception $th) { $this->retorno($th->getMessage()); return; } } function listaPausasAgente($request) { $agente = $this->supervisor->findAgentByMatricula($request['matricula']); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return null; } $param = $this->parametros->findProtocolByParams(); if (false) { $ret = $this->pausasModel->findPauseByGroupUser($request['matricula']); } else { $ret = $this->pausasModel->findAllPause(); } foreach ($ret as $key => $value) { $ret[$key] = removeAcentosArray((array) $value); } $this->retorno( $ret ? "Sucesso" : "Erro", $ret ? $ret : null, $ret ? $ret : null ); return null; } function entrarPausa($request) { $ws = new WsInterface(); $agente = $this->supervisor->findAgentByMatricula($request['matricula']); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } $pausa = $this->pausasModel->findPauseById($request['id_pausa']); if (empty($pausa)) { $this->retorno("Pausa não encontrado"); return; } if ($agente->status == CONF_AGENT_STATUS_PAUSA || $agente->status == CONF_AGENT_STATUS_INDISPONIVEL) { $this->retorno('Agente precisa estar livre para entrar em pausa!'); return; } $atends = $this->atendimento->getAtendimentoAbertoByAgente($request['matricula']); if (!empty($atends)) { $ret = $this->agentController->indisponivelAtendimento($request['matricula'], $pausa->motivo); if (is_string($ret)) { $this->retorno($ret); } else { $ws->enviaMsg($this->enviaActions('Agente em pausa', 'att_status', $agente->matricula)); $this->retorno( "Agente em 'indisponivel'", $ret ); } return; } $ret = $this->agentController->enterPause($request['matricula'], $pausa->motivo); $ws->enviaMsg($this->enviaActions('Agente em pausa', 'att_status', $agente->matricula)); $this->retorno( $ret ? "Agente em 'pausa'" : "Erro", $ret ? $ret : null ); return null; } function saiPausa($request) { $ws = new WsInterface(); $agente = $this->supervisor->findAgentByMatricula($request['matricula']); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } $ret = $this->agentController->exitPause($request['matricula']); if (is_string($ret)) { $this->retorno($ret); } else { $ws->enviaMsg($this->enviaActions('Agente em pausa', 'att_status', $agente->matricula)); $this->retorno( "Agente 'livre'", $ret ); } return; } function transfAtendimento($request) { $retunr = $this->agentController->transfer( $request['matricula_origem'], $request['matricula_destino'], $request['uniqueid'] ); if (is_string($retunr)) { $this->retorno($retunr); } else { $this->retorno( $retunr ? "Sucesso" : "Erro", $retunr ? $retunr : null ); } return null; } function markMessegeRead($request) { $atend = $this->atendimento->findAtendId($request['uniqueid']); //verifica se existe agente if (empty($atend)) { $this->retorno("Não existe atendimento para esse uniqueid"); return; } $modelMensagem = new Message(); $ret = $modelMensagem->markMessege($request['uniqueid'], 'read'); $this->retorno( $ret ? "Sucesso" : "Erro", $ret ? $ret : null ); return null; } function statusAgente($request) { try { $agente = $this->supervisor->findAgentByMatricula($request['matricula']); //verifica se existe agente if (empty($agente)) { $this->retorno("Agente não encontrado"); return; } $this->supervisor->updateAgent($agente->matricula, $agente->status, $agente->motivo_pausa); $ret = $this->supervisor->statusAgente($request['matricula']); $ret = removeAcentosArray((array) $ret); $this->retorno( $ret ? "Sucesso" : "Erro", $ret ? $ret : null, $ret ? [$ret] : null ); } catch (\Exception $th) { $this->retorno($th->getMessage()); } } function enviaActions($msg, $tipo, $destino, $uniqueid = null) { try { $mensagem = []; $mensagem["event"] = [ "type" => 'actions', "contact" => [ "name" => 'Sistema', "number" => '0' ], "mensagem" => [ "type" => $tipo, "dst" => $destino, "uniqueid" => $uniqueid, "content" => utf8_encode($msg) ], ]; return json_encode($mensagem); } catch (\Exception $th) { logger('monitora')->info($th->getMessage()); } } public function validaPalavroes($msg) { try { $palavroes = new ListaNegraPalavras(); $palavras = $palavroes->getAll(); foreach ($palavras as $key => $value) { $pattern = "/\b($value->palavra)\b/i"; $msg = preg_replace($pattern, '*' . str_repeat('*', strlen($value->palavra)) . '*', $msg); } } catch (\Exception $th) { logger('telegram')->info(print_r($th, true), true); } return $msg; } public function listarAtendimentosFilas($request) { try { $filaModel = new Queue(); $fila = $filaModel->findQueueById($request['id_fila']); if (empty($fila)) { $this->retorno("Fila não encontrada"); return; } $retunr = $this->atendimento->getAtendFila($fila->nome); $data = []; $data['message'] = utf8_encode("Sucesso"); $data['status'] = "success"; $data['data'] = $retunr; echo json_encode($data); return null; } catch (\Exception $th) { return $this->retorno($th->getMessage()); } } public function listarAtendimentosAbandonado($request) { try { $filaModel = new Queue(); if ($request['id_fila']) { $fila = $filaModel->findQueueById($request['id_fila']); if (empty($fila)) { $this->retorno("Fila não encontrada"); return; } } $retunr = $this->atendimento->getAtendAbandonado($fila->nome); $data = []; $data['message'] = utf8_encode("Sucesso"); $data['status'] = "success"; $data['data'] = $retunr; echo json_encode($data); return null; } catch (\Exception $th) { return $this->retorno($th->getMessage()); } } }