Example #1
0
        public String ValidateCreate(MENSAGEM item, USUARIO usuario, Int32?idAss)
        {
            try
            {
                // Verifica existencia prévia
                if (_baseService.CheckExist(item, idAss) != null)
                {
                    return("1");
                }

                // Criticas
                if (item.CONT_CD_ID == null & item.CAMP_CD_ID == null & item.GRUP_CD_ID == null)
                {
                    return("2");
                }
                if (item.TEMP_CD_ID == null & item.MENS_TX_TEXTO == null)
                {
                    return("3");
                }

                // Completa objeto
                item.MENS_IN_ATIVO   = 1;
                item.MENS_DT_DATA    = DateTime.Today.Date;
                item.MENS_DT_ENVIO   = null;
                item.MENS_IN_ENVIADA = 0;
                item.ASSI_CD_ID      = idAss.Value;
                item.USUA_CD_ID      = usuario.USUA_CD_ID;
                if (item.MENS_NM_NOME == null)
                {
                    item.MENS_NM_NOME = "-";
                }

                // Monta Log
                LOG log = new LOG
                {
                    LOG_DT_DATA     = DateTime.Now,
                    ASSI_CD_ID      = idAss.Value,
                    USUA_CD_ID      = usuario.USUA_CD_ID,
                    LOG_NM_OPERACAO = "AddMENS",
                    LOG_IN_ATIVO    = 1,
                    LOG_TX_REGISTRO = Serialization.SerializeJSON <MENSAGEM>(item)
                };

                // Persiste
                Int32 volta = _baseService.Create(item, log, idAss);

                // Monta lista de envio
                List <CONTATO> lista    = new List <CONTATO>();
                String         campanha = null;
                if (item.CONT_CD_ID != null)
                {
                    CONTATO ct = _ctService.GetItemById(item.CONT_CD_ID.Value);
                    lista.Add(ct);
                }
                if (item.GRUP_CD_ID != null)
                {
                    GRUPO gr = _grService.GetItemById(item.GRUP_CD_ID.Value);
                    foreach (var obj in gr.GRUPO_CONTATO)
                    {
                        lista.Add(obj.CONTATO);
                    }
                }
                if (item.CAMP_CD_ID != null)
                {
                    CAMPANHA cp = _cpService.GetItemById(item.CAMP_CD_ID.Value);
                    foreach (var obj in cp.CAMPANHA_CONTATO)
                    {
                        lista.Add(obj.CONTATO);
                    }
                    campanha = cp.CAMP_NM_NOME;
                }
                else
                {
                    if (item.MENS_NM_CAMPANHA != null)
                    {
                        campanha = item.MENS_NM_CAMPANHA;
                    }
                }

                // Monta token
                CONFIGURACAO conf      = _conService.GetItemById(1);
                String       text      = conf.CONF_NM_USER_SMS + ":" + conf.CONF_NM_SENHA_SMS;
                byte[]       textBytes = Encoding.UTF8.GetBytes(text);
                String       token     = Convert.ToBase64String(textBytes);
                String       auth      = "Basic " + token;

                // Monta routing
                String routing = item.MENS_IN_TIPO_SMS.ToString();

                // Monta texto
                String texto = String.Empty;
                if (item.TEMPLATE != null)
                {
                    texto = item.TEMPLATE.TEMP_TX_TEXTO;
                }
                else
                {
                    texto = item.MENS_TX_TEXTO;
                }

                // inicia processo
                List <String> resposta = new List <string>();
                WebRequest    request  = WebRequest.Create("https://api.smsfire.com.br/v1/sms/send");
                request.Headers["Authorization"] = auth;
                request.Method      = "POST";
                request.ContentType = "application/json";

                // Monta destinatarios
                String listaDest = String.Empty;
                if (lista.Count > 0)
                {
                    if (lista.Count <= 200)
                    {
                        foreach (var contato in lista)
                        {
                            if (listaDest.Length == 0)
                            {
                                listaDest += contato.CONT_NR_WHATSAPP + "\"";
                            }
                            else
                            {
                                listaDest += ",\"" + contato.CONT_NR_WHATSAPP + "\"";
                            }
                        }
                    }
                    else
                    {
                        return("5");
                    }
                }
                else
                {
                    return("4");
                }

                // Agendamento
                String agenda = null;
                if (item.MENS_DT_AGENDA != null)
                {
                    agenda = item.MENS_DT_AGENDA.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    //agenda = item.MENS_DT_AGENDA.Value.ToString();
                }

                // Processa lista
                String responseFromServer = null;
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    String json = null;
                    if (agenda == null)
                    {
                        json = "{\"to\":[\"" + listaDest + "]," +
                               "\"from\":\"smsfire\", " +
                               "\"campaignName\":\"" + campanha + "\", " +
                               "\"text\":\"" + texto + "\"} ";
                    }
                    else
                    {
                        json = "{\"to\":[\"" + listaDest + "]," +
                               "\"from\":\"smsfire\", " +
                               "\"campaignName\":\"" + campanha + "\", " +
                               "\"schedule\":\"" + agenda + "\", " +
                               "\"text\":\"" + texto + "\"} ";
                    }

                    streamWriter.Write(json);
                    streamWriter.Close();
                    streamWriter.Dispose();
                }

                WebResponse response = request.GetResponse();
                resposta.Add(response.ToString());

                Stream       dataStream = response.GetResponseStream();
                StreamReader reader     = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
                resposta.Add(responseFromServer);

                // Completa objeto
                item.MENS_DT_ENVIO    = DateTime.Today.Date;
                item.MENS_IN_ENVIADA  = 1;
                item.MENS_TX_RETORNOS = responseFromServer.ToString();
                Int32 volta1 = _baseService.Create(item, log, idAss);

                reader.Close();
                response.Close();
                return(responseFromServer);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #2
0
 public CONFIGURACAO GetItemById(Int32 id)
 {
     return(_baseService.GetItemById(id));
 }