ParticipacionController.php 2.6 KB

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