public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.EnterPhoneNumber, container, false);

            var titleTextView = view.FindViewById <TextView>(Resource.Id.TitleTextField);

            titleTextView.Text = "Add Phone Number";

            PhoneNumberTextField           = view.FindViewById <EditText>(Resource.Id.PhoneNumberTextField);
            PhoneNumberTextField.Hint      = Strings.Basic.phone_placeholder;
            PhoneNumberTextField.InputType = Android.Text.InputTypes.ClassPhone;

            var progressButton = view.FindViewById <Button>(Resource.Id.ProgressButton);

            progressButton.Click += async(sender, e) =>
            {
                string phoneNumber = StringUtils.RemoveNonIntegers(PhoneNumberTextField.Text.Trim());
                phoneNumber = StringUtils.RemoveLeadingSymbol(phoneNumber, "1");

                bool allFieldsValid = ValidateAllFields();
                if (!allFieldsValid)
                {
                    return;
                }

                ShowHud(Strings.Hud.please_wait);

                try
                {
                    var result = await TwilioServices.StartPhoneVerificationAsync(phoneNumber);

                    if (result != null && result.success && result.is_cellphone)
                    {
                        var frag = new EnterPhoneNumberVerificaionCodeFragment();
                        frag.PhoneNumber = phoneNumber;
                        ((BaseActivity)Activity).AddFragmentOver(frag);
                    }
                    else
                    {
                        PhoneNumberTextField.SetInvalid(Resources);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }

                HideHud();
            };


            return(view);
        }
        async partial void NextButtonClicked(Foundation.NSObject sender)
        {
            View.EndEditing(true);

            string code = StringUtils.RemoveNonIntegers(VerificationCodeTextField.Text.Trim());

            bool allFieldsValid = ValidateAllFields();

            if (!allFieldsValid)
            {
                return;
            }

            ShowHud(Strings.Hud.please_wait);

            try
            {
                var result = await TwilioServices.VerifyPhoneAsync(PhoneNumber, code);

                if (result != null && result.success)
                {
                    var outlet = new Outlet();
                    outlet.Handle = PhoneNumber;
                    outlet.Type   = Outlet.outlet_type_phone;
                    outlet.Name   = PhoneNumber;

                    var outletSaved = RealmServices.SaveOutlet(outlet);
                    if (outletSaved)
                    {
                        var popToViewController = NavigationController.ViewControllers.Where(c => c.GetType() == typeof(MyOutletsViewController)).First();
                        NavigationController.PopToViewController(popToViewController, true);
                    }
                    else
                    {
                        ShowDuplicateEntryAlert();
                    }
                }
                else
                {
                    VerificationCodeTextField.SetInvalid();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            HideHud();
        }
Esempio n. 3
0
        private void EnviarSMS(Agenda agenda, ConfiguracaoNotificacaoAgenda configuracao, String mensagemSMS)
        {
            if (ConfigurationManager.AppSettings["ENVIAR_SMS"] != null)
            {
                String numeroTelefone = String.Format("{0}{1}", CodigoE164, agenda.MedicoExecucaoAgenda.Celular);

                if (Convert.ToBoolean(ConfigurationManager.AppSettings["ENVIAR_SMS"]))
                {
                    String MessageId = TwilioServices.SendSMS(numeroTelefone, mensagemSMS);
                    ConsoleLogMessage("SMS enviado", ConsoleMessageType.SUCCESS);

                    LogSmsAgenda logSmsAgenda = new LogSmsAgenda();
                    logSmsAgenda.Agenda               = agenda;
                    logSmsAgenda.SMSEnviado           = true;
                    logSmsAgenda.SMSDataProcessamento = DateTime.Now;
                    logSmsAgenda.SMSMessageID         = MessageId;
                    logSmsAgenda.Observacao           =
                        String.Format
                        (
                            "Origem SERVIÇO: mensagem destinado ao nº {0} como lembrete ({1} {2} antes), enviada com sucesso ao servidor de serviço SMS."
                            , numeroTelefone
                            , configuracao.Tempo
                            , configuracao.UnidadeTempoAgenda.Unidade
                        );
                    new LogSmsAgendaRepository().Create(logSmsAgenda);
                    ConsoleLogMessage("Log gravado", ConsoleMessageType.SUCCESS);
                }
                else
                {
                    ConsoleLogMessage("SMS não foi enviado.", ConsoleMessageType.WARNING);
                    LogSmsAgenda logSmsAgenda = new LogSmsAgenda();
                    logSmsAgenda.Agenda               = agenda;
                    logSmsAgenda.SMSEnviado           = false;
                    logSmsAgenda.SMSDataProcessamento = DateTime.Now;
                    logSmsAgenda.SMSMessageID         = null;
                    logSmsAgenda.Observacao           =
                        String.Format
                        (
                            "Origem SERVIÇO: mensagem destinado ao nº {0} como lembrete ({1} {2} antes) não foi enviada, o serviço de envio de SMS está desabilitado. Utilize a chave ENVIAR_SMS do app.config da aplicação."
                            , numeroTelefone
                            , configuracao.Tempo
                            , configuracao.UnidadeTempoAgenda.Unidade
                        );
                    new LogSmsAgendaRepository().Create(logSmsAgenda);
                    ConsoleLogMessage("Log gravado", ConsoleMessageType.SUCCESS);
                }
            }
        }
 private void SendSMSByTwilio()
 {
     try
     {
         TwilioServices ts        = new TwilioServices();
         String         returnSid = ts.SendSMS(this.txtDestiyPhone.Text, this.txtMessage.Text);
         MessageBox.Show(this, String.Format("SMS sended. SID: {0}", returnSid));
     }
     catch (Exception e)
     {
         MessageBox.Show
         (
             this
             , "Error on the sms send process.\r\nDetails: " + e.Message
             , "Error"
             , MessageBoxButtons.OK
             , MessageBoxIcon.Error
         );
     }
 }
        async partial void ValidateButtonClicked(Foundation.NSObject sender)
        {
            View.EndEditing(true);

            string phoneNumber = StringUtils.RemoveNonIntegers(PhoneNumberTextField.Text.Trim());

            phoneNumber = StringUtils.RemoveLeadingSymbol(phoneNumber, "1");

            bool allFieldsValid = ValidateAllFields();

            if (!allFieldsValid)
            {
                return;
            }

            ShowHud(Strings.Hud.please_wait);

            try
            {
                var result = await TwilioServices.StartPhoneVerificationAsync(phoneNumber);

                if (result != null && result.success && result.is_cellphone)
                {
                    var vc = new EnterPhoneNumberVerificationCodeViewController();
                    vc.PhoneNumber = phoneNumber;
                    NavigationController.PushViewController(vc, true);
                }
                else
                {
                    PhoneNumberTextField.SetInvalid();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            HideHud();
        }
Esempio n. 6
0
 public TwilioController()
 {
     service = new TwilioServices();
 }
Esempio n. 7
0
        private void Salvar(HttpContext context)
        {
            Model.Entities.Agenda agenda = new Model.Entities.Agenda();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ReturnJSON <Model.Entities.Agenda> returnJSON = new ReturnJSON <Model.Entities.Agenda>();
            String retorno = null;

            try
            {
                String json = new StreamReader(context.Request.InputStream).ReadToEnd();
                agenda = (Model.Entities.Agenda)serializer.Deserialize <Model.Entities.Agenda>(json);

                // Recupera novamente o EstadoAgenda, pois no client/browser só foi possível enviar o seu ID.
                agenda.EstadoAgenda =
                    new EstadoAgendaRepository()
                    .Details(new EstadoAgenda()
                {
                    EstadoAgendaID = agenda.EstadoAgenda.EstadoAgendaID
                });

                // Recupera novamente o MedicoExecucaoAgenda, pois no client/browser só foi possível enviar o seu ID.
                agenda.MedicoExecucaoAgenda =
                    new MedicoExecucaoAgendaRepository()
                    .Details(new MedicoExecucaoAgenda()
                {
                    MedicoExecucaoAgendaID = agenda.MedicoExecucaoAgenda.MedicoExecucaoAgendaID
                });

                if (!agenda.DataHoraEhNoiteOuFimSemana(agenda.DataHoraEvento.Value))
                {
                    returnJSON.Message    = "O agendamento da congelação <b>não</b> pode ser realizado após as 19:00 e aos finais de semana.";
                    returnJSON.ReturnCode = Enum.GetName(typeof(ReturnType), ReturnType.WARNING);
                    retorno = serializer.Serialize(returnJSON);
                    context.Response.ContentType     = "text/json";
                    context.Response.ContentEncoding = Encoding.UTF8;
                    context.Response.Write(retorno);
                    return;
                }

                // É novo registro? Verifica se não está sendo gravado uma nova agenda com data retroativa.
                if (agenda.AgendaID.HasValue && agenda.AgendaID.Value == 0)
                {
                    if (agenda.DataHoraEhRetroativa(agenda.DataHoraEvento.Value))
                    {
                        returnJSON.Message    = "Não é permitido agendar uma congelação com data/hora retroativa.";
                        returnJSON.ReturnCode = Enum.GetName(typeof(ReturnType), ReturnType.WARNING);
                        retorno = serializer.Serialize(returnJSON);
                        context.Response.ContentType     = "text/json";
                        context.Response.ContentEncoding = Encoding.UTF8;
                        context.Response.Write(retorno);
                        return;
                    }
                }

                String assuntoSMS = String.Empty;

                // Registro em edição?
                if (agenda.AgendaID.HasValue && agenda.AgendaID.Value > 0)
                {
                    assuntoSMS = "Atualização de agendamento de congelação";

                    if (agenda.EstadoAgenda.Estado.Equals("Confirmado"))
                    {
                        // Consulta a existência de lembrestes configurados para a agenda.
                        List <NotificacaoAgenda> notificacoesExistentes = new NotificacaoAgendaRepository()
                                                                          .Retreave(new NotificacaoAgenda()
                        {
                            Agenda = agenda
                        });

                        // Se não houver nenhum lembrete, cria com base nas configurações existentes (recorrência de notificação).
                        if (notificacoesExistentes.Count == 0)
                        {
                            List <ConfiguracaoNotificacaoAgenda> configuracaoNotificacaoAgendas
                                = new ConfiguracaoNotificacaoAgendaRepository()
                                  .Retreave(new ConfiguracaoNotificacaoAgenda());

                            foreach (ConfiguracaoNotificacaoAgenda configuracao in configuracaoNotificacaoAgendas)
                            {
                                DateTime inicioNotificacao = agenda.DataHoraEvento.Value;

                                // Verifica a unidade de tempo da configuração para determinar a data/hora inicial da notificação.
                                switch (configuracao.UnidadeTempoAgenda.Unidade)
                                {
                                case "Minutos":
                                    inicioNotificacao = inicioNotificacao.AddMinutes(-Convert.ToDouble(configuracao.Tempo));
                                    break;

                                case "Horas":
                                    inicioNotificacao = inicioNotificacao.AddHours(-Convert.ToDouble(configuracao.Tempo));
                                    break;

                                case "Dias":
                                    inicioNotificacao = inicioNotificacao.AddDays(-Convert.ToDouble(configuracao.Tempo));
                                    break;

                                case "Semanas":
                                    inicioNotificacao = inicioNotificacao.AddDays(-Convert.ToDouble(configuracao.Tempo) * 7);
                                    break;

                                default:
                                    break;
                                }

                                /*
                                 * Verifica se a data/hora do momento está dentro do intervalo:
                                 * data/hora de início da notificação e a data/hora evento (fim) da agenda.
                                 * Como é uma nova agenda ou alteração de agenda, se a data/hora evento estiver
                                 * dentro do tempo de intervalo de notificação, a notificãção não será criada.
                                 */
                                if (DateTime.Now >= inicioNotificacao && DateTime.Now <= agenda.DataHoraEvento.Value)
                                {
                                    // Evita criar o registro no banco.
                                    continue;
                                }

                                NotificacaoAgenda notificacaoAgenda = new NotificacaoAgenda();
                                notificacaoAgenda.Agenda = agenda;
                                notificacaoAgenda.ConfiguracaoNotificacaoAgenda = configuracao;
                                new NotificacaoAgendaRepository().Create(notificacaoAgenda);
                            }
                        }
                        // Se não, se já existir o lembrete (por que a mesma agenda foi cancelada e confirmada posteriormente)
                        //, mantém ativado.
                        else
                        {
                            NotificacaoAgenda notificacaoAgenda = new NotificacaoAgenda();
                            notificacaoAgenda.Agenda    = agenda;
                            notificacaoAgenda.Utilizado = false;
                            notificacaoAgenda.Ativo     = true;
                            new NotificacaoAgendaRepository().ReativarNotificacao(notificacaoAgenda);
                        }
                    }
                    else if (agenda.EstadoAgenda.Estado.Equals("Cancelado") ||
                             agenda.EstadoAgenda.Estado.Equals("Agendado") ||
                             agenda.EstadoAgenda.Estado.Equals("Finalizado")
                             )
                    {
                        NotificacaoAgenda notificacaoAgenda = new NotificacaoAgenda();
                        notificacaoAgenda.Agenda = agenda;
                        new NotificacaoAgendaRepository().Delete(notificacaoAgenda);
                    }

                    new AgendaRepository().Update(agenda);
                }
                // Ou é novo registro?
                else
                {
                    agenda.AgendaID = new AgendaRepository().CreateWithReturnID(agenda);
                    assuntoSMS      = "Novo agendamento de congelação";

                    // Se a nova agenda é para o mesmo dia, evidencia no assunto do SMS.
                    if (agenda.DataHoraEvento.Value.Day == DateTime.Now.Day &&
                        agenda.DataHoraEvento.Value.Month == DateTime.Now.Month &&
                        agenda.DataHoraEvento.Value.Year == DateTime.Now.Year)
                    {
                        assuntoSMS = "*** Atenção *** Uma nova congelação foi marcada hoje.";
                    }
                }

                String mensagemSMS = Model.SMS.FormataMensagemSMS(agenda, assuntoSMS);

                if (ConfigurationManager.AppSettings["ENVIAR_SMS"] != null)
                {
                    String numeroTelefone = String.Format("{0}{1}", CodigoE164, agenda.MedicoExecucaoAgenda.Celular);

                    if (Convert.ToBoolean(ConfigurationManager.AppSettings["ENVIAR_SMS"]))
                    {
                        String MessageId = TwilioServices.SendSMS(numeroTelefone, mensagemSMS);

                        LogSmsAgenda logSmsAgenda = new LogSmsAgenda();
                        logSmsAgenda.Agenda               = agenda;
                        logSmsAgenda.SMSEnviado           = true;
                        logSmsAgenda.SMSDataProcessamento = DateTime.Now;
                        logSmsAgenda.SMSMessageID         = MessageId;
                        logSmsAgenda.Observacao           =
                            String.Format
                            (
                                "Origem SITE: mensagem destinado ao nº {0} enviada com sucesso ao servidor de serviço SMS."
                                , numeroTelefone
                            );
                        new LogSmsAgendaRepository().Create(logSmsAgenda);
                    }
                    else
                    {
                        LogSmsAgenda logSmsAgenda = new LogSmsAgenda();
                        logSmsAgenda.Agenda               = agenda;
                        logSmsAgenda.SMSEnviado           = false;
                        logSmsAgenda.SMSDataProcessamento = DateTime.Now;
                        logSmsAgenda.SMSMessageID         = null;
                        logSmsAgenda.Observacao           =
                            String.Format
                            (
                                "Origem SITE: mensagem destinado ao nº {0} não foi enviada, o serviço de envio de SMS está desabilitado. Utilize a chave ENVIAR_SMS do web.config da aplicação."
                                , numeroTelefone
                            );
                        new LogSmsAgendaRepository().Create(logSmsAgenda);
                    }
                }

                returnJSON.Message          = "Registro salvo com sucesso.";
                returnJSON.ReturnCode       = Enum.GetName(typeof(ReturnType), ReturnType.SUCCESS);
                context.Response.StatusCode = 201;
            }
            catch (SqlException e)
            {
                Log.Create(e);
                Email.Send("Agendamento de congelação - falha na aplicação", e);
                returnJSON.Message          = "Não foi possível salvar o registro.";
                returnJSON.ReturnCode       = Enum.GetName(typeof(ReturnType), ReturnType.ERROR);
                context.Response.StatusCode = 500;
            }
            catch (Exception e)
            {
                LogSmsAgenda logSmsAgenda = new LogSmsAgenda();
                logSmsAgenda.Agenda               = agenda;
                logSmsAgenda.SMSEnviado           = false;
                logSmsAgenda.SMSDataProcessamento = DateTime.Now;
                logSmsAgenda.Observacao           =
                    String.Format
                    (
                        "Ocorreu uma falha ao enviar o SMS para o nº {0}. Detalhes: {1}"
                        , String.Format("{0}{1}", CodigoE164, agenda.MedicoExecucaoAgenda.Celular)
                        , e.Message
                    );
                new LogSmsAgendaRepository().Create(logSmsAgenda);

                Log.Create(e);
                Email.Send("Agendamento de congelação - falha na aplicação", e);

                returnJSON.Message          = "Não foi possível enviar o SMS.";
                returnJSON.ReturnCode       = Enum.GetName(typeof(ReturnType), ReturnType.ERROR);
                context.Response.StatusCode = 500;
            }

            retorno = serializer.Serialize(returnJSON);
            context.Response.ContentType     = "text/json";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.Write(retorno);
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.EnterPhoneNumberVerificationCode, container, false);

            PhoneNumberTextField           = view.FindViewById <EditText>(Resource.Id.PhoneNumberTextField);
            PhoneNumberTextField.InputType = Android.Text.InputTypes.ClassNumber;

            var titleTextView = view.FindViewById <TextView>(Resource.Id.TitleTextView);

            titleTextView.Text = "Verify Phone Number";

            var messageTextView = view.FindViewById <TextView>(Resource.Id.MessageTextView);

            messageTextView.Text = "We've sent you an text!";

            var progressButton = view.FindViewById <Button>(Resource.Id.ProgressButton);

            progressButton.Click += async(sender, e) =>
            {
                string code = StringUtils.RemoveNonIntegers(PhoneNumberTextField.Text.Trim());

                bool allFieldsValid = ValidateAllFields();
                if (!allFieldsValid)
                {
                    return;
                }

                ShowHud(Strings.Hud.please_wait);

                try
                {
                    var result = await TwilioServices.VerifyPhoneAsync(PhoneNumber, code);

                    if (result != null && result.success)
                    {
                        var outlet = new Outlet();
                        outlet.Handle = PhoneNumber;
                        outlet.Type   = Outlet.outlet_type_phone;
                        outlet.Name   = PhoneNumber;

                        var outletSaved = RealmServices.SaveOutlet(outlet);
                        if (outletSaved)
                        {
                            var activity = Activity as BaseActivity;
                            activity?.PopFragmentOverUntil(typeof(MyOutletsRecyclerViewFragment));
                        }
                        else
                        {
                            ShowDuplicateEntryAlert();
                        }
                    }
                    else
                    {
                        PhoneNumberTextField.SetInvalid(Resources);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }

                HideHud();
            };


            return(view);
        }