NotificacionController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\commands;
  3. use app\components\NotificacionController as Controller;
  4. use app\components\Whatsapp;
  5. use app\models\Notificacion;
  6. use app\models\NotificacionUsuario;
  7. class NotificacionController extends Controller {
  8. public function actionParticipacion() {
  9. $notificacion = $this->consultarNotificacion();
  10. if ($notificacion === null) {
  11. $this->stdout("No hay notificaciones pendientes\n");
  12. return;
  13. }
  14. $this->stdout("Procesando notificación {$notificacion->id}\n");
  15. $params = \Yii::$app->params;
  16. $wab = null;
  17. if(isset($params["wab"])) {
  18. $wab = $params["wab"];
  19. }
  20. if($wab === null) {
  21. $this->stdout("No existe la configuración\n");
  22. }
  23. try {
  24. /** @var \app\models\Notificacion $notificacion */
  25. $notificacion->estatus = Notificacion::ESTATUS_ENVIANDO;
  26. $notificacion->save();
  27. $notificacionUsuario = NotificacionUsuario::find()
  28. ->andWhere(["idNotificacion" => $notificacion->id]);
  29. foreach ($notificacionUsuario->each() as $nu) {
  30. $this->stdout("Enviando mensaje a ".$nu->telefono."\n");
  31. /** @var \app\models\NotificacionUsuario $nu */
  32. // $msgTo = "526624473145";
  33. $msgTo = $nu->telefono;
  34. if (strlen($nu->telefono) === 10) {
  35. $msgTo = "52".$nu->telefono;
  36. }
  37. $template = "laud_resumen_eventos";
  38. // $template = "informe_de_eventos";
  39. try {
  40. $wa = (new Whatsapp())
  41. ->set_access_token($wab["access-token"])
  42. ->msg_from($wab["phone-id"])
  43. ->msg_to($msgTo)
  44. ->template($template);
  45. foreach ($nu['parametros'] as $param) {
  46. $wa->add_body_param($param['text'], $param['type']);
  47. }
  48. $resultado = $wa->send_template_message();
  49. if($resultado === null) {
  50. $this->stdout("Ocurrió un error al enviar el mensaje\n");
  51. return;
  52. }
  53. $nu->detalle = $resultado;
  54. $nu->save();
  55. // $this->stdout("Proceso terminado\n");
  56. } catch (\Exception $e) {
  57. $this->stdout("Error al enviar el mensaje: {$e->getMessage()}\n");
  58. }
  59. }
  60. $notificacion->estatus = Notificacion::ESTATUS_TERMINADO;
  61. $notificacion->save();
  62. } catch(\Exception $e) {
  63. $notificacion->estatus = Notificacion::ESTATUS_ERROR;
  64. $notificacion->detalle = "Ocurrió un error en el servidor al generar la notificación {$e->getMessage()} {$e->getCode()}";
  65. $notificacion->save();
  66. $this->stdout($e->getMessage() . "\n");
  67. }
  68. }
  69. public function actionFecha() {
  70. $fecha = \Yii::$app->db->createCommand("select now()")
  71. ->queryAll();
  72. var_dump($fecha);
  73. }
  74. }