Beispiel #1
0
        public ActionResult Mensajes(Models.Comunicaciones comunicaciones)
        {
            ViewBag.Rol = Session["Rol"].ToString();

            try
            {
                DATA.Telefonos telefono = new DATA.Telefonos();
                telefono = tel.ListarTelefonos().Where(x => x.Username == Session["Username"].ToString()).FirstOrDefault();
                var dateNow = DateTime.Now;
                comunicaciones.Fecha         = dateNow;
                comunicaciones.Estado        = 4;
                comunicaciones.Numero_Twilio = telefono.Numero;
                DATA.Contactos contacto = new DATA.Contactos();
                contacto = cont.BuscarContactos(comunicaciones.Id_Contacto);

                TwilioClient.Init(telefono.Account_Id, telefono.Authtoken);
                var message = MessageResource.Create(
                    body: comunicaciones.Mensaje,
                    from: new Twilio.Types.PhoneNumber("+" + telefono.Numero),
                    to: new Twilio.Types.PhoneNumber("+" + contacto.Numero)
                    );

                com.InsertarComunicaciones(Mapper.Map <DATA.Comunicaciones>(comunicaciones));


                return(RedirectToAction("Mensajes", new { Id_Contacto = comunicaciones.Id_Contacto }));
            }
            catch (Exception ex)
            {
                ViewBag.Mensaje = ex.Message;
                return(RedirectToAction("Mensajes"));
            }
        }
Beispiel #2
0
        public ActionResult Index()
        {
            try
            {
                string cuerpoDeRespuesta           = Request.Form["Body"];
                string remitente                   = Request.Form["From"];
                string emisor                      = Request.Form["To"];
                var    dateNow                     = DateTime.Now;
                DATA.Comunicaciones comunicaciones = new DATA.Comunicaciones();
                DATA.Contactos      contacto       = new DATA.Contactos();
                contacto = con.ListarContactos().Where(x => x.Numero == remitente.Replace("+", "")).FirstOrDefault();
                comunicaciones.Id_Contacto   = contacto.Id_Contacto;
                comunicaciones.Numero_Twilio = emisor.Replace("+", "");
                comunicaciones.Mensaje       = cuerpoDeRespuesta;
                comunicaciones.Estado        = 6;
                comunicaciones.Fecha         = dateNow;
                com.InsertarComunicaciones(comunicaciones);

                var logMessage = $"\"{cuerpoDeRespuesta}\", \"{remitente}\", \"{emisor}\"";
                Trace.WriteLine(logMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(Content("Handled"));
        }
        public void enviarMensajes(string UserPhone, string accountSid, string authToken, string mensaje, int idLista, DATA.Difusiones difusion, string correoUsuario, string passwordCorreo)
        {
            try
            {
                difusion.Id_Estado = 2;
                dif.ActualizarDifusiones(difusion);

                List <DATA.Lista_Negra> listaNegra = listNegra.ListarListaNegra();
                var lista_Negra = Mapper.Map <List <Models.Lista_Negra> >(listaNegra);

                TwilioClient.Init(accountSid, authToken);

                var contactos = conList.Listar().Where(x => x.Id_Lista == idLista);

                foreach (DATA.Contactos_Por_Listas infoContacto in contactos)
                {
                    var noEnviar = lista_Negra.Where(x => x.Id_Contacto == infoContacto.Id_contacto).FirstOrDefault();
                    if (noEnviar == null)
                    {
                        Console.WriteLine("no esta en lista naegra: " + infoContacto.Id_contacto);
                        DATA.Contactos   contacto  = con.BuscarContactos(infoContacto.Id_contacto);
                        DATA.Historiales historial = new DATA.Historiales();
                        historial.Id_Difusion = difusion.Id_Difusion;
                        historial.Id_Contacto = contacto.Id_Contacto;

                        if (difusion.Id_Tipo_Mensaje == 1)
                        {
                            var message = MessageResource.Create(
                                body: mensaje,
                                from: new Twilio.Types.PhoneNumber("+" + UserPhone),
                                statusCallback: new Uri(link + "/MessageStatus"),
                                to: new Twilio.Types.PhoneNumber("+" + contacto.Numero)
                                );

                            historial.Id_Message = message.Sid;
                            historial.Estado     = 6;
                        }
                        else
                        {
                            if (difusion.Id_Tipo_Mensaje == 2)
                            {
                                Debug.WriteLine("Voz");
                                Guid        id  = Guid.NewGuid();
                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Say voice=\"alice\" language=\"es-ES\">" + mensaje + "</Say></Response>");
                                String rootPath = Server.MapPath("~");
                                rootPath = rootPath + "\\Content\\xml\\" + id + ".xml";
                                doc.Save(rootPath);

                                var call = CallResource.Create(
                                    method: Twilio.Http.HttpMethod.Get,
                                    url: new Uri(link + "/Content/xml/" + id + ".xml"),
                                    from: new Twilio.Types.PhoneNumber("+" + UserPhone),
                                    to: new Twilio.Types.PhoneNumber("+" + contacto.Numero),
                                    statusCallback: new Uri(link + "/LlamadasStatus")
                                    );

                                historial.Id_Message = call.Sid;
                                historial.Estado     = 6;
                            }
                            else
                            {
                                if (difusion.Id_Tipo_Mensaje == 3)
                                {
                                    Debug.WriteLine("Correo");
                                    MailMessage email = new MailMessage();
                                    email.To.Add(new MailAddress(contacto.Correo));
                                    email.From       = new MailAddress("*****@*****.**");
                                    email.Subject    = "Asunto ( " + DateTime.Now.ToString("dd / MMM / yyy hh:mm:ss") + " ) ";
                                    email.Body       = mensaje;
                                    email.IsBodyHtml = true;
                                    email.Priority   = MailPriority.Normal;

                                    SmtpClient smtp = new SmtpClient();
                                    smtp.Host                  = "Smtp.Gmail.com";
                                    smtp.Port                  = 587;
                                    smtp.EnableSsl             = true;
                                    smtp.UseDefaultCredentials = false;
                                    smtp.Credentials           = new NetworkCredential(correoUsuario, passwordCorreo);

                                    //string output = null;
                                    historial.Id_Message = "--";

                                    try
                                    {
                                        smtp.Send(email);
                                        email.Dispose();
                                        //output = "Corre electrónico fue enviado satisfactoriamente.";
                                        historial.Estado = 4;
                                    }
                                    catch (Exception ex)
                                    {
                                        //output = "Error enviando correo electrónico: " + ex.Message;
                                        historial.Estado = 7;
                                    }
                                }
                            }
                        }

                        his.InsertarHistoriales(historial);
                    }
                    else
                    {
                        Console.WriteLine("esta en lista naegra: " + infoContacto.Id_contacto);
                    }
                }

                difusion.Id_Estado = 3;
                dif.ActualizarDifusiones(difusion);
            }
            catch (Exception ex)
            {
                ViewBag.Mensaje = ex.Message;
            }
        }