NotificacionController.php 3.1 KB

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