NotificacionController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if($nu->plantilla) {
  39. $template = $nu->plantilla;
  40. }
  41. // $template = "informe_de_eventos";
  42. try {
  43. $wa = (new Whatsapp())
  44. ->set_access_token($wab["access-token"])
  45. ->msg_from($wab["phone-id"])
  46. ->msg_to($msgTo)
  47. ->template($template);
  48. foreach ($nu['parametros'] as $param) {
  49. $wa->add_body_param($param['text'], $param['type']);
  50. }
  51. $resultado = $wa->send_template_message();
  52. if($resultado === null) {
  53. $this->stdout("Ocurrió un error al enviar el mensaje\n");
  54. return;
  55. }
  56. $nu->detalle = $resultado;
  57. $nu->save();
  58. // $this->stdout("Proceso terminado\n");
  59. } catch (\Exception $e) {
  60. $this->stdout("Error al enviar el mensaje: {$e->getMessage()}\n");
  61. }
  62. }
  63. $notificacion->estatus = Notificacion::ESTATUS_TERMINADO;
  64. $notificacion->save();
  65. } catch(\Exception $e) {
  66. $notificacion->estatus = Notificacion::ESTATUS_ERROR;
  67. $notificacion->detalle = "Ocurrió un error en el servidor al generar la notificación {$e->getMessage()} {$e->getCode()}";
  68. $notificacion->save();
  69. $this->stdout($e->getMessage() . "\n");
  70. }
  71. }
  72. public function actionFecha() {
  73. $domingo = 0;
  74. $fecha = strtotime('2023-05-07');
  75. $dia = intval(date("w", $fecha));
  76. if($dia !== $domingo) {
  77. $fecha = strtotime('next Sunday');
  78. }
  79. $siguienteDomingo = date("Y-m-d", $fecha);
  80. $this->stdout("{$siguienteDomingo}\n");
  81. }
  82. }