| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace app\commands;
- use app\components\NotificacionController as Controller;
- use app\components\Whatsapp;
- use app\models\Notificacion;
- use app\models\NotificacionUsuario;
- class NotificacionController extends Controller {
- public function actionParticipacion() {
- $notificacion = $this->consultarNotificacion();
- if ($notificacion === null) {
- $this->stdout("No hay notificaciones pendientes\n");
- return;
- }
- $this->stdout("Procesando notificación {$notificacion->id}\n");
- $params = \Yii::$app->params;
- $wab = null;
- if(isset($params["wab"])) {
- $wab = $params["wab"];
- }
- if($wab === null) {
- $this->stdout("No existe la configuración\n");
- }
- try {
- /** @var \app\models\Notificacion $notificacion */
- $notificacion->estatus = Notificacion::ESTATUS_ENVIANDO;
- $notificacion->save();
- $notificacionUsuario = NotificacionUsuario::find()
- ->andWhere(["idNotificacion" => $notificacion->id]);
- foreach ($notificacionUsuario->each() as $nu) {
- $this->stdout("Enviando mensaje a ".$nu->telefono."\n");
- /** @var \app\models\NotificacionUsuario $nu */
- // $msgTo = "526624473145";
- $msgTo = $nu->telefono;
- if (strlen($nu->telefono) === 10) {
- $msgTo = "52".$nu->telefono;
- }
- $template = "laud_resumen_eventos";
- // $template = "informe_de_eventos";
- try {
- $wa = (new Whatsapp())
- ->set_access_token($wab["access-token"])
- ->msg_from($wab["phone-id"])
- ->msg_to($msgTo)
- ->template($template);
- foreach ($nu['parametros'] as $param) {
- $wa->add_body_param($param['text'], $param['type']);
- }
- $resultado = $wa->send_template_message();
-
- if($resultado === null) {
- $this->stdout("Ocurrió un error al enviar el mensaje\n");
- return;
- }
- $nu->detalle = $resultado;
- $nu->save();
- // $this->stdout("Proceso terminado\n");
- } catch (\Exception $e) {
- $this->stdout("Error al enviar el mensaje: {$e->getMessage()}\n");
- }
- }
- $notificacion->estatus = Notificacion::ESTATUS_TERMINADO;
- $notificacion->save();
- } catch(\Exception $e) {
- $notificacion->estatus = Notificacion::ESTATUS_ERROR;
- $notificacion->detalle = "Ocurrió un error en el servidor al generar la notificación {$e->getMessage()} {$e->getCode()}";
- $notificacion->save();
- $this->stdout($e->getMessage() . "\n");
- }
- }
- public function actionFecha() {
- $fecha = \Yii::$app->db->createCommand("select now()")
- ->queryAll();
- var_dump($fecha);
- }
- }
|