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.
 
 
 
 
 
 

196 lines
7.7 KiB

#!/bin/bash
#Funcoes
cria_arquivo()
{
ORIGEM=$1
CONTEXTO=$2
CONTEXTO_DESTINO=$3
DESTINO=$4
CAMPANHA=$5
ID_CLIENTE=$6
IDENTIFICADOR_CLI=$7
PRIORIDADE=1
CALLFILE=$(cat <<-EOF1
Channel: Local/$ORIGEM@$CONTEXTO
Callerid: $ORIGEM
MaxRetries: 0 ; Numero de Tentativas de discagem
RetryTime: 15 ; Intervalo entre as tentativas de discagem
WaitTime: 100 ; Tempo de Ring
Context: $CONTEXTO_DESTINO
Extension: $DESTINO
Priority: $PRIORIDADE
Set: PERMISSAO=OK
Set: ID_CLI=$ID_CLIENTE
Set: IDENTIFICADOR=$IDENTIFICADOR_CLI
Set: TIPOLIGACAO=ATIVA
Archive: Yes
EOF1
)
DATA=$(date +%F)
DIR_CALL="/var/spool/asterisk/outgoing/"
RAND=$(echo $($fortune ; w ; ps; date +%s) | cksum | cut -f 1 -d" ")
DIR_FILE_NAME="/var/log/asterisk/"
FILE_NAME=$(date +%F-%H-%M-%S)-$ORIGEM-$DESTINO-$RAND.call
echo "$CALLFILE" >> $DIR_FILE_NAME$FILE_NAME
chown pbx:pbx $DIR_FILE_NAME$FILE_NAME
chmod --recursive u=rwX,g=rX,o= $DIR_FILE_NAME$FILE_NAME
mv $DIR_FILE_NAME$FILE_NAME $DIR_CALL
}
#Base de dados
HOST="127.0.0.1"
BD="pbx"
USUARIO="contacte"
SENHA="ctepgSQL"
#Variaveis
CONTEXTO_DESTINO="ext-campanha"
CAMPANHA="$1"
ID_CAMPANHA="$2"
CONTEXTO="$3"
TRONCOS_PA="$4"
echo "TRONCOS_PA => $TRONCOS_PA"
echo "1" > /tmp/$ID_CAMPANHA.txt
if [ -z "$CAMPANHA" -o -z "$ID_CAMPANHA" -o -z "$CONTEXTO" ] ; then
echo "Faltando parametros"
echo "parametro CAMPANHA => $CAMPANHA"
echo "parametro ID_CAMPANHA => $ID_CAMPANHA"
echo "parametro CONTEXTO => $CONTEXTO"
exit
fi
# Guarda PID da campanha
PID=$$
export PGPASSWORD=$SENHA
psql -c "update pbx_campanha set cmp_pid = '$PID' where cmp_id = '$ID_CAMPANHA' " -h $HOST -d $BD -U $USUARIO
while [ "TRUE" ] ; do
while [ "TRUE" ] ; do
#Verifica status da campanha
export PGPASSWORD=$SENHA
CMP_STATUS=$(psql -Atc "select count(*) from pbx_campanha where cmp_status = '1' and cmp_id = '$ID_CAMPANHA' " -h $HOST -d $BD -U $USUARIO)
if [ "$CMP_STATUS" = "0" ]; then
export PGPASSWORD=$SENHA
psql -c "update pbx_campanha set cmp_pid = '0' where cmp_id = '$ID_CAMPANHA' " -h $HOST -d $BD -U $USUARIO
exit;
fi
#Verifica se h<EFBFBD> agentes disponiveis
export PGPASSWORD=$SENHA
LIVRES=$(psql -Atc "select count(*) as livres from pbx_supervisor_agentes where upper(status) = upper('livre') and upper(dac) = upper('$CAMPANHA') and status_agente = '1' " -h $HOST -d $BD -U $USUARIO)
#echo "Agentes livres === $LIVRES"
if [ "$LIVRES" = "0" ]; then
#sleep 2
echo "N<EFBFBD>o h<EFBFBD> agentes disponiveis para essa campanha!!!"
echo "Agentes LIVRES = $LIVRES"
break;
fi
export PGPASSWORD=$SENHA
CONTATOS=$(psql -Atc "select a.cont_id,translate(a.cont_identificador,'|','-') as cont_identificador from pbx_campanha_contato a where a.cont_discado = '0' and a.cmp_id = '$ID_CAMPANHA' and a.list_id in (select list_id from pbx_campanha_lista where cmp_id = '$ID_CAMPANHA' and list_status = '1') order by a.cont_peso desc , a.cont_id limit '$(($LIVRES * $TRONCOS_PA))'" -h $HOST -d $BD -U $USUARIO)
#echo "$CONTATOS"
#sleep 1
echo "$CONTATOS" | while read LINHA; do
#for LINHA in $CONTATOS
#do
ID=$(echo "$LINHA" | cut -d "|" -f1)
IDENTIFICADOR=$(echo "$LINHA" | cut -d "|" -f2)
CONT=1
if [ -z "$ID" ] ; then
echo "N<EFBFBD>o h<EFBFBD> registro para continuar com a campanha!!"
echo "FINALIZA => $(cat /tmp/$ID_CAMPANHA.txt)"
if [ "$(cat /tmp/$ID_CAMPANHA.txt)" -ge "3" ]; then
AGENDADOS=$(psql -Atc "select count(*) from pbx_campanha_agendamento where cmp_id = '$ID_CAMPANHA' and agd_status = '0' " -h $HOST -d $BD -U $USUARIO)
if [ "$AGENDADOS" = "0" ]; then
export PGPASSWORD=$SENHA
psql -Atc "update pbx_campanha set cmp_status = '5' where upper(cmp_descricao) = upper('$CAMPANHA')" -h $HOST -d $BD -U $USUARIO
exit 1;
else
export PGPASSWORD=$SENHA
psql -Atc "update pbx_campanha set cmp_status = '6' where upper(cmp_descricao) = upper('$CAMPANHA')" -h $HOST -d $BD -U $USUARIO
exit 1;
fi
fi
sleep 10
echo "Dormindo!!"
echo "FINALIZA => $(cat /tmp/$ID_CAMPANHA.txt)"
echo "$(($(cat /tmp/$ID_CAMPANHA.txt) + 1))" > /tmp/$ID_CAMPANHA.txt
break;
fi
echo "1" > /tmp/$ID_CAMPANHA.txt
export PGPASSWORD=$SENHA
DISCANDO=$(psql -Atc "select count(*) as discando from pbx_campanha_contato_fone where cmp_id = '$ID_CAMPANHA' and conf_status_lista = 1 and upper(conf_status) in ('DISCANDO','ATENDIDA') and round(extract(epoch from age(now(),conf_data))) <= '20' and list_id in (select list_id from pbx_campanha_lista where cmp_id = '$ID_CAMPANHA' and list_status = '1') " -h $HOST -d $BD -U $USUARIO)
sleep 1
export PGPASSWORD=$SENHA
#LIVRES=$(psql -Atc "select count(*) as livres from pbx_supervisor_agentes where upper(status) = upper('livre') and upper(dac) = upper('$CAMPANHA') and status_agente = '1' " -h $HOST -d $BD -U $USUARIO)
RESULT=$(psql -Atc "select status,count(*) from pbx_supervisor_agentes where upper(status) in ('OCUPADO','PAUSA') and upper(dac) = upper('$CAMPANHA') group by status union select status,count(*) from pbx_supervisor_agentes where upper(status) in ('LIVRE') and upper(dac) = upper('$CAMPANHA') and status_agente = '1' group by status order by status" -h $HOST -d $BD -U $USUARIO)
for STATUS in $RESULT
do
case $(echo "$STATUS" | cut -d "|" -f1) in
LIVRES)
LIVRES=$(echo "$STATUS" | cut -d "|" -f2)
;;
OCUPADO)
OCUPADO=$(echo "$STATUS" | cut -d "|" -f2)
;;
PAUSA)
PAUSA=$(echo "$STATUS" | cut -d "|" -f2)
;;
esac
done
echo "Chamadas Discando ==> $DISCANDO"
echo "Agentes Livres ==> $LIVRES"
echo "Troncos por PA ==> $TRONCOS_PA"
if [ "$DISCANDO" -ge "$(($LIVRES * $TRONCOS_PA))" ]; then
#sleep 2
echo "N<EFBFBD>o h<EFBFBD> agentes livres para essa campanha!!!"
echo "Numeros Discando => $DISCANDO"
echo "Agentes LIVRE => $LIVRES"
echo "Agentes em PAUSA => $PAUSA"
echo "Agentes OCUPADO => $OCUPADO"
break;
fi
FONE=$(psql -Atc "select conf_fone from pbx_campanha_contato_fone where cont_id = '$ID' and conf_status = '0' and conf_status_lista = '1' and cmp_id = '$ID_CAMPANHA' and list_id in (select list_id from pbx_campanha_lista where cmp_id = '$ID_CAMPANHA' and list_status = '1') order by conf_id limit 1" -h $HOST -d $BD -U $USUARIO)
if [ -z "$FONE" ] ; then
psql -c "update pbx_campanha_contato set cont_discado = '1' where cont_id = '$ID' and cmp_id = '$ID_CAMPANHA' and list_id in (select list_id from pbx_campanha_lista where cmp_id = '$ID_CAMPANHA' and list_status = '1') " -h $HOST -d $BD -U $USUARIO
else
cria_arquivo "$FONE" "$CONTEXTO" "$CONTEXTO_DESTINO" "$ID_CAMPANHA" "$CAMPANHA" "$ID" "$IDENTIFICADOR"
export PGPASSWORD=$SENHA
psql -c "update pbx_campanha_contato set cont_discado = '1' where cont_id = '$ID' and cmp_id = '$ID_CAMPANHA' and list_id in (select list_id from pbx_campanha_lista where cmp_id = '$ID_CAMPANHA' and list_status = '1') " -h $HOST -d $BD -U $USUARIO
psql -c "update pbx_campanha_contato_fone set conf_callfile = '$FILE_NAME', conf_status = 'DISCANDO', conf_data = 'now()' where cont_id = '$ID' and conf_fone = '$FONE' and cmp_id = '$ID_CAMPANHA' and list_id in (select list_id from pbx_campanha_lista where cmp_id = '$ID_CAMPANHA' and list_status = '1') " -h $HOST -d $BD -U $USUARIO
CONT=$(($CONT + 1))
fi
done
done
done