Mejora codigo smsgateway.

se añade llamado de parametro configuracion
parent c1e7004a
...@@ -2,34 +2,26 @@ ...@@ -2,34 +2,26 @@
namespace BackendBundle\Controller; namespace BackendBundle\Controller;
//use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use AppBundle\Lib\Utiles; use AppBundle\Lib\Utiles;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Model\NotificaQuery; use AppBundle\Model\NotificaQuery;
use AppBundle\Model\Notifica; use AppBundle\Model\Notifica;
//use Aws\CloudWatchLogs\CloudWatchLogsClient;
require_once __DIR__.'/../../../vendor/autoload.php';
require_once __DIR__ . '/../../../vendor/autoload.php';
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class EnvioSmsController extends FOSRestController {
class EnvioSmsController extends FOSRestController
{
/** /**
* @Route("/usuario/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1}) * @Route("/usuario/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
* @Method({"GET"}) * @Method({"GET"})
*/ */
public function indexAction($id){ public function indexAction($id) {
$view = View::create(); $view = View::create();
$view->setData(array("id"=>$id,"nombre"=>"Usuario no autorizado")); $view->setData(array("id" => $id, "nombre" => "Usuario no autorizado"));
return $this->handleView($view); return $this->handleView($view);
} }
...@@ -37,20 +29,7 @@ class EnvioSmsController extends FOSRestController ...@@ -37,20 +29,7 @@ class EnvioSmsController extends FOSRestController
* @Route("/enviar") * @Route("/enviar")
* @Method({"POST"}) * @Method({"POST"})
*/ */
public function enviarSmsAction(){ public function enviarSmsAction() {
/* $resp = array(
'success' => true,
'content' => array(0, 'main_content' => $this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url)
)
);
$finalResponse = json_encode($resp);
$response = new Response($finalResponse);
$response->headers->set('Content-Type', 'application/json');
return $response;*/
$campana = $_POST['campana']; $campana = $_POST['campana'];
$telefono = $_POST['telefono']; $telefono = $_POST['telefono'];
...@@ -58,9 +37,6 @@ class EnvioSmsController extends FOSRestController ...@@ -58,9 +37,6 @@ class EnvioSmsController extends FOSRestController
$idmetodo = $_POST['idmetodo']; $idmetodo = $_POST['idmetodo'];
$log = Utiles::setLog('BackendBundle', 'backend/backend'); $log = Utiles::setLog('BackendBundle', 'backend/backend');
$log->debug(count(NotificaQuery::create()->filterByCampana($campana)->filterByTelefono($telefono)->find())); $log->debug(count(NotificaQuery::create()->filterByCampana($campana)->filterByTelefono($telefono)->find()));
$notifica = NotificaQuery::create()->filterByCampana($campana)->filterByTelefono($telefono)->find(); $notifica = NotificaQuery::create()->filterByCampana($campana)->filterByTelefono($telefono)->find();
...@@ -70,83 +46,62 @@ class EnvioSmsController extends FOSRestController ...@@ -70,83 +46,62 @@ class EnvioSmsController extends FOSRestController
$password = ""; $password = "";
$url = ""; $url = "";
if ($idmetodo==1){ //nrsgateway if ($idmetodo == 1) { //nrsgateway
$usuario = $this->container->getParameter('nrsgateway_user'); $usuario = Utiles::getParametroConfiguracion('nrsgateway_user');
$password = $this->container->getParameter('nrsgateway_pws'); $password = Utiles::getParametroConfiguracion('nrsgateway_pws');
$url = $this->container->getParameter('nrsgateway_url'); $url = Utiles::getParametroConfiguracion('nrsgateway_url');
// $view = View::create();
// $view->setData($this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url));
//return $this->handleView($view);
return $this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url); return $this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url);
} } else { //teltech
else{ //teltech if ($idmetodo == 2) {
if($idmetodo==2){ $usuario = Utiles::getParametroConfiguracion('teltech_user');
$usuario = $this->container->getParameter('teltech_user'); $password = Utiles::getParametroConfiguracion('teltech_pws');
$password = $this->container->getParameter('teltech_pws'); $url = Utiles::getParametroConfiguracion('teltech_url');
$url = $this->container->getParameter('teltech_url');
//$view = View::create();
//$view->setData($this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url));
//return $this->handleView($view);
return $this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url); return $this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url);
} } else { //Amazon
else{ //Amazon
return $this->enviarMensajeAmazon($telefono, $mensaje); return $this->enviarMensajeAmazon($telefono, $mensaje);
} }
} }
/*$view = View::create();
$view->setData($this->enviarMensaje($campana, $telefono, $mensaje, $usuario, $password, $url));
return $this->handleView($view);*/
} }
private function enviarMensaje($campana, $movil, $mensaje, $usuario, $password, $url) { private function enviarMensaje($campana, $movil, $mensaje, $usuario, $password, $url) {
try{ try {
$post['to'] = array($movil); $post['to'] = array($movil);
$post['text'] = $mensaje; $post['text'] = $mensaje;
$post['from'] = $movil; $post['from'] = $movil;
$post['dlr-url'] ="http://localhost/apirestsms/notifica/". $campana . "/%P/%d"; $post['dlr-url'] = Utiles::getParametroConfiguracion("url_base")."/apirestsms/notifica/" . $campana . "/%P/%d";
$user = $usuario; $user = $usuario;
$password = $password; $password = $password;
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_HEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json", "Content-Type: application/json",
"Authorization: Basic " . base64_encode($user.":".$password) "Authorization: Basic " . base64_encode($user . ":" . $password)
)); ));
$result = curl_exec($ch); $result = curl_exec($ch);
$array = json_decode($result, true); $array = json_decode($result, true);
$resp=""; $resp = "";
if (isset($array['error']['code'])) { if (isset($array['error']['code'])) {
$resp = array( $resp = array(
'success' => false, 'success' => false,
'statusCode' => $array['error']['code'] 'statusCode' => $array['error']['code']
); );
return $resp; return $resp;
} } else {
else{
$log = Utiles::setLog('BackendBundle', 'backend/backend'); $log = Utiles::setLog('BackendBundle', 'backend/backend');
$log->debug('entra: ' . count($notifica)); $log->debug('entra: ' . count($notifica));
$notifica=NotificaQuery::create()->filterByCampana($campana)->filterByTelefono($movil)->find(); $notifica = NotificaQuery::create()->filterByCampana($campana)->filterByTelefono($movil)->find();
$resp=""; $resp = "";
$estado = ""; $estado = "";
if(count($notifica)>0){ if (count($notifica) > 0) {
foreach ($notifica as $noti) { foreach ($notifica as $noti) {
$estado=$noti->getEstado(); $estado = $noti->getEstado();
} }
$resp = array( $resp = array(
'success' => false, 'success' => false,
...@@ -154,8 +109,7 @@ class EnvioSmsController extends FOSRestController ...@@ -154,8 +109,7 @@ class EnvioSmsController extends FOSRestController
); );
return $resp; return $resp;
} } else {
else{
$resp = array( $resp = array(
'success' => false, 'success' => false,
'statusCode' => $array['error']['code'] 'statusCode' => $array['error']['code']
...@@ -163,20 +117,18 @@ class EnvioSmsController extends FOSRestController ...@@ -163,20 +117,18 @@ class EnvioSmsController extends FOSRestController
return $resp; return $resp;
} }
} }
} catch (\Exception $e) {
}
catch(\Exception $e){
$log = Utiles::setLog('exception'); $log = Utiles::setLog('exception');
$log->debug('exception: ' .$e); $log->debug('exception: ' . $e);
} }
} }
private function enviarMensajeAmazon($telefono, $mensaje){ private function enviarMensajeAmazon($telefono, $mensaje) {
$sdk = new \Aws\Sns\SnsClient([ $sdk = new \Aws\Sns\SnsClient([
'region' => 'us-east-1', 'region' => Utiles::getParametroConfiguracion('region'),
'version' => 'latest', 'version' => Utiles::getParametroConfiguracion('version'),
'credentials' => ['key' => $this->container->getParameter('amazonkey'), 'secret' => $this->container->getParameter('amazonsecret') 'credentials' => ['key' => Utiles::getParametroConfiguracion('amazonkey'), 'secret' => Utiles::getParametroConfiguracion('amazonsecret')
]]); ]]);
$result = $sdk->publish([ $result = $sdk->publish([
'Message' => $mensaje, 'Message' => $mensaje,
...@@ -195,7 +147,7 @@ class EnvioSmsController extends FOSRestController ...@@ -195,7 +147,7 @@ class EnvioSmsController extends FOSRestController
* @Route("/buscarstatus") * @Route("/buscarstatus")
* @Method({"POST"}) * @Method({"POST"})
*/ */
public function buscarStatusAction(){ public function buscarStatusAction() {
$time = intVal(strVal(time() - 3600 + 6) . "000"); $time = intVal(strVal(time() - 3600 + 6) . "000");
$messageId = $_POST['messageid']; $messageId = $_POST['messageid'];
...@@ -205,13 +157,13 @@ class EnvioSmsController extends FOSRestController ...@@ -205,13 +157,13 @@ class EnvioSmsController extends FOSRestController
'credentials' => ['key' => $this->container->getParameter('amazonkey'), 'secret' => $this->container->getParameter('amazonsecret') 'credentials' => ['key' => $this->container->getParameter('amazonkey'), 'secret' => $this->container->getParameter('amazonsecret')
]]); ]]);
$loggroup = $cloud->describeLogGroups([ 'logGroupNamePrefix' => 'sns','limit' => 10 ]); $loggroup = $cloud->describeLogGroups(['logGroupNamePrefix' => 'sns', 'limit' => 10]);
$arrayGroup = json_decode($loggroup, true); $arrayGroup = json_decode($loggroup, true);
$g= $loggroup['logGroups']; $g = $loggroup['logGroups'];
$val=strVal($messageId); $val = strVal($messageId);
$cloud2 = new \Aws\CloudWatchLogs\CloudWatchLogsClient([ $cloud2 = new \Aws\CloudWatchLogs\CloudWatchLogsClient([
'region' => 'us-east-1', 'region' => 'us-east-1',
...@@ -219,31 +171,24 @@ $val=strVal($messageId); ...@@ -219,31 +171,24 @@ $val=strVal($messageId);
'credentials' => ['key' => $this->container->getParameter('amazonkey'), 'secret' => $this->container->getParameter('amazonsecret') 'credentials' => ['key' => $this->container->getParameter('amazonkey'), 'secret' => $this->container->getParameter('amazonsecret')
]]); ]]);
$result = $cloud2->filterLogEvents([ $result = $cloud2->filterLogEvents([
'filterPattern' => '{ $.notification.messageId = '. $val . ' }', 'filterPattern' => '{ $.notification.messageId = ' . $val . ' }',
'logGroupName' => $g[0]['logGroupName'], // REQUIRED 'logGroupName' => $g[0]['logGroupName'], // REQUIRED
//'logStreamName' => '9a6f5bec-731e-4150-abd1-87f9a94ce3ee', // REQUIRED
// 'logStreamNamePrefix' => '<string>',
// 'logStreamNames' => ['<string>', ...],
// 'nextToken' => '<string>',
'startTime' => $time, 'startTime' => $time,
]); ]);
if($result['events']){ if ($result['events']) {
return $result; return $result;
} else {
}
else{
$result = $cloud2->filterLogEvents([ $result = $cloud2->filterLogEvents([
'filterPattern' => '{ $.notification.messageId = '. $val . ' }', 'filterPattern' => '{ $.notification.messageId = ' . $val . ' }',
'logGroupName' => $g[1]['logGroupName'], // REQUIRED 'logGroupName' => $g[1]['logGroupName'], // REQUIRED
'startTime' => $time, 'startTime' => $time,
]); ]);
if($result['events']){ if ($result['events']) {
return $result; return $result;
} } else {
else{
$resp = array( $resp = array(
'success' => true, 'success' => true,
'statusCode' => '-1' 'statusCode' => '-1'
...@@ -252,21 +197,18 @@ $val=strVal($messageId); ...@@ -252,21 +197,18 @@ $val=strVal($messageId);
return $resp; return $resp;
} }
} }
} }
/** /**
* @Route("/notifica/{idenvio}/{tel}/{estado}") * @Route("/notifica/{idenvio}/{tel}/{estado}")
*/ */
public function notificaAction($idenvio, $tel,$estado){ public function notificaAction($idenvio, $tel, $estado) {
$notifica = new Notifica(); $notifica = new Notifica();
$notifica->setCampana($idenvio); $notifica->setCampana($idenvio);
$notifica->setTelefono($tel); $notifica->setTelefono($tel);
$notifica->setEstado($estado); $notifica->setEstado($estado);
$notifica->save(); $notifica->save();
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment