PABX da Simples IP
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.

231 lines
7.2 KiB

#!/usr/bin/php -q
<?php
require("funcoes/shared.php");
require("phpagi/phpagi.php");
define("DIR_BASE_APL", "/var/lib/asterisk/scripts/uraativa/");
define("DIR_BASE_AUDIO", "/var/lib/asterisk/sounds/");
define("TTS_EXE", "/var/lib/asterisk/scripts/uraativa/ttsimples");
$tempo = time();
$nomeApl = 'ura_atende_simples';
$stdlog = fopen("/var/lib/asterisk/scripts/uraativa/$nomeApl.log", "a");
/*
* Arquivo de log.
*/
$opc = trim($argv[1]);
$uid = isset($argv[2]) ? trim($argv[2]) : null;
$msg = "Iniciando-> Opc: $opc Iid: $uid";
/*
* Interage com agi.
*/
$agi = new AGI("/var/lib/asterisk/scripts/uraativa/phpagi.conf");
/*
* Cria conex<EFBFBD>o para o banco de dados.
*/
$conStr = false;
//$conStr = sprintf("host='%s' port='%s' dbname='%s' user='%s' password='%s'", '192.168.115.11', '5432', 'teste', 'contacte', 'ctepgSQL');
/*
* Se a configura<EFBFBD><EFBFBD>o n<EFBFBD>o foi definida manualmente retorna a configura<EFBFBD><EFBFBD>o padr<EFBFBD>o.
*/
if ($conStr === false) {
$conStr = GetDefStrDb();
}
/*
* Inicia a conex<EFBFBD>o com banco de dados.
*/
$conn = pg_connect($conStr);
if ($conn)
Grava_log('Banco conectado.');
else
Grava_log('Banco n<EFBFBD>o conectado.');
/*
* Seta valor de parametros passados no $argv[3], no seguinte formato
*/
GetVariableCmd();
if ($opc == 'VALIDA_CADASTRO') {
$query = "select nome from clientes where '$fone' in(fone, fone1, fone2)";
$result = pg_query($query);
if (!pg_num_rows($result)) {
$agi->set_variable("RESULT", 0);
$msg = sprintf("%s\, seja bem vindo a central de relacionamento Simples Ip<EFBFBD>! Se voc<EFBFBD> j<EFBFBD> <EFBFBD> nosso cliente digite seu cpf ou pressione 9 para ser direcionado a um de nossos atendentes. ", Sauda());
$agi->set_variable("MSG", 1);
$agi->set_variable("MENSAGEM", $msg);
$agi->set_variable("RESULT", 0);
$agi->set_variable("VDTMF_RESULT", "");
$agi->set_variable("SOM_READ", "");
$agi->set_variable("GOTO", "ncad_fone");
} else {
$dados = pg_fetch_array($result);
$msg = sprintf("%s senhor \"%s\", bem vindo a Simples Ip<EFBFBD> , escolha uma das op<EFBFBD><EFBFBD>es apresentadas a seguir. ", Sauda(), $dados["nome"]);
$agi->set_variable("RESULT", 1);
$agi->set_variable("MSG", 1);
$agi->set_variable("MENSAGEM", $msg);
$agi->set_variable("GOTO", "atendimento");
}
}
if ($opc == 'GET_MENU') {
$msg = "Abrir um chamado para reparo t<EFBFBD>cnico digite 1.Agendar uma visita digite 2.Se voc<EFBFBD> quer saber seu saldo do cart<EFBFBD>o fidelidade digite 3.Cancelar o seu contrato digite 4.Fazer um elogio sugest<EFBFBD>o ou reclama<EFBFBD><EFBFBD>o digite 5.Conhecer nossas ofertas e promo<EFBFBD><EFBFBD>es digite 6.Falar com um de nossos atendentes digite 9.E caso queira ouvir novamente este menu digite 0.";
$agi->set_variable("MENSAGEM", $msg);
}
/*
* Procura o cad
*/
if ($opc == 'CLIENTE_CPF') {
$query = "select nome, fone, fone1, fone2 from clientes where cpf = '$cpf'";
$result = pg_query($query);
if (!pg_num_rows($result)) {
$agi->set_variable("RESULT", 0);
$msg = sprintf("O n<EFBFBD>mero digitado n<EFBFBD>o foi encontrado em nossa base de dados, ap<EFBFBD>s o bipe digite pausadamente o n<EFBFBD>mero do seu cpf.", Sauda());
$agi->set_variable("MSG", 1);
$agi->set_variable("MENSAGEM", $msg);
$agi->set_variable("GOTO", "cad_cpf");
$agi->set_variable("SOM_READ", "beep");
$agi->set_variable("VDTMF_RESULT", "");
} else {
$dados = pg_fetch_array($result);
$msg = sprintf("Senhor \"%s\", encontramos o seu cadastro. Se deseja adicionar este telefone ao seu cadastro digite 1 ap<EFBFBD>s o beep ou aguarde para ser redirecionado ao menu de op<EFBFBD><EFBFBD>es. ", Sauda(), $dados["nome"]);
$agi->set_variable("RESULT", 1);
$agi->set_variable("MSG", 1);
$agi->set_variable("MENSAGEM", $msg);
$agi->set_variable("GOTO", "cad_numero");
$agi->set_variable("SOM_READ", "beeb");
$agi->set_variable("VDTMF_RESULT", "");
$fones = sprintf("%s;%s;%s", $dados["fone"], $dados["fone1"], $dados["fone2"]);
$agi->set_variable("FONES", $fones);
}
}
if ($opc == 'CAD_CPF') {
$fones = $agi->get_variable("FONES");
$fones = explode(';', $fones[data]);
$nomeFone = "fone2";
if (!trim($fones[0]))
$nomeFone = "fone";
else if (!trim($fones[1]))
$nomeFone = "fone1";
$query = "update clientes set $nomeFone = '$fone' where cpf = '$cpf'";
$result = pg_query($query);
$msg = "O novo telefone foi incluido ao seu cadastro, isto tornar<EFBFBD> mais agil o atendimento da proxima vez que no ligar";
$agi->set_variable("RESULT", 1);
$agi->set_variable("MSG", 1);
$agi->set_variable("MENSAGEM", $msg);
$agi->set_variable("GOTO", "atendimento");
$agi->set_variable("SOM_READ", "");
$agi->set_variable("VDTMF_RESULT", "");
}
$agi = null;
Grava_log('Opeera<EFBFBD><EFBFBD>o finalizado');
Grava_log('-------------------------------------------');
exit(0);
/* ----------------------------------------------------Funcoes do arquivo---------------------------------------------------- */
function Grava_log($log) {
global $stdlog, $nomeApl, $tempo;
$log = (trim($log) . " Tempo de execu<EFBFBD><EFBFBD>o: " . (time() - $tempo) . " Data Atual: " . date('Y-m-d H:i:s-z'));
$tempo = time();
//if(!$stdlog)$stdlog = fopen("/var/lib/asterisk/scripts/uraativa/$nomeApl.log", "a");
fputs($stdlog, "Log: $log\n");
}
function GetVariableCmd() {
global $argv;
if (!isset($argv[3]))
return;
$params = explode('@', $argv[3]);
foreach ($params as $param) {
if (trim($param) != '') {
$var = explode('#', $param);
$varName = trim($var[0]);
if ($varName) {
$varValue = trim($var[1]);
$GLOBALS[$varName] = $varValue;
}
}
}
}
function Sauda($caixa = 'l', $cType = "f") {
$hora = (int) date('H');
if (($hora >= 6) && ($hora < 12))
$sauda = "bom dia";
else if (($hora >= 12) && ($hora < 18))
$sauda = "boa tarde";
else
$sauda = "boa noite";
if ($caixa == 'U') {
$sauda = strtoupper($sauda);
}
if ($caixa == 'C') {
$sauda = $cType == "f" ? ucfirst($sauda) : ucwords($sauda);
}
return $sauda;
}
function GetAudio($txtAudio, $fileName) {
$fileName = DIR_BASE_AUDIO . $fileName;
$cmd = sprintf("%s %s", TTS_EXE, $txtAudio, $fileName);
exec($cmd, $ret, $result);
return file_exists($fileName) ? $fileName : false;
}
/*
Log: Uid: 1395866313.166 Cpf: 33333333333 Audio: 200 result=1 (bem_vindo.alaw)
Log: Cpf: 200 result=1 (33333333333)
Log: QUALQUER: 200 result=0
Log: Agivar: Array
(
[agi_request] => /var/lib/asterisk/scripts/uraativa/validacliente.php
[agi_channel] => SIP/1004-0000005d
[agi_language] => en
[agi_type] => SIP
[agi_uniqueid] => 1395866313.166
[agi_callerid] => 1004
[agi_calleridname] => Amarildo
[agi_callingpres] => 0
[agi_callingani2] => 0
[agi_callington] => 0
[agi_callingtns] => 0
[agi_dnid] => 36145555
[agi_rdnis] => unknown
[agi_context] => ura-126
[agi_extension] => rec
[agi_priority] => 7
[agi_enhanced] => 0.0
[agi_accountcode] =>
)
Log: Uid: 1395866313.166 Cpf: 33333333333
Log: select nome, saldo_cartao from clientes where cpf = '33333333333'
Log: Senhor Alan Pablo, seu saldo at<EFBFBD> o momento <EFBFBD> de R$ 245.35
*/
?>