public SubscriptionModel Subscribe(SubscribeModel model)
        {
            var n            = Environment.NewLine;
            var subscription = _handler.Insert(model.Subscriber, model.Location);
            var title        = _settings.Get(SettingName.Title);
            var subject      = $"{title} subscription to {model.Location} confirmed";
            var protocol     = HttpContext.Request.Host.Value.Contains("localhost") ? "http" : "https";
            var message      = $"{title} thanks you! You will receive one notification in the morning when we will be at {model.Location}.{n}";

            message += $"{n}unsubscribe from this location: {Url.Action("Unsubscribe", "System", new { subscriberLocation = $"{subscription.Subscriber}{subscriptionDelimeter}{subscription.Location}" }, protocol)}";
            message += $"{n}unsubscribe from all locations: {Url.Action("Unsubscribe", "System", new { subscriberLocation = subscription.Subscriber }, protocol)}";

            if (subscription.Type == Subscription.Types.Email)
            {
                var from = _settings.Get(SettingName.ContactEmail);
                EmailSender.Send(_azureSettings.SendgridApiKey, subject, message, subscription.Subscriber, from);
            }
            else if (subscription.Type == Subscription.Types.Text)
            {
                var twilio = new TwilioHelper(_settings.Get(SettingName.TwilioAuthToken), _settings.Get(SettingName.TwilioSid), _settings.Get(SettingName.TwilioPhoneNumber));
                if (!twilio.IsValid)
                {
                    throw new Exception("Twilio is not properly set up");
                }

                ISmsSender smsSender = new TwilioSmsSender(twilio);
                smsSender.Send(twilio.Phone, $"+1{subscription.Subscriber}", message);
            }
            else
            {
                throw new NotImplementedException($"Subscription.Type of {subscription.Type} is not implemented yet");
            }

            return(subscription);
        }
Example #2
0
 private static void NotificationText(AdminAppSettings settings, OrderModel order)
 {
     if (!string.IsNullOrWhiteSpace(order.Phone) && settings.IsTwilioValid)
     {
         ISmsSender smsSender = new TwilioSmsSender(settings.TwilioSid, settings.TwilioAuthToken);
         smsSender.Send(settings.TwilioPhoneNumber, order.Phone, ReadyText(settings));
     }
 }
        private void ConfirmationText(Data.Models.Payment.Order order, SystemModel settings, string name, bool isCustomer = true)
        {
            try
            {
                var twilio = new TwilioHelper(settings.Get(SettingName.TwilioAuthToken), settings.Get(SettingName.TwilioSid), settings.Get(SettingName.TwilioPhoneNumber));

                if (twilio.IsValid)
                {
                    ISmsSender smsSender = new TwilioSmsSender(twilio.Sid, twilio.AuthToken);
                    if (isCustomer && !string.IsNullOrWhiteSpace(order.Phone))
                    {
                        smsSender.Send(twilio.Phone, order.Phone, GetBody(order, name, settings, true));
                    }

                    if (!isCustomer)
                    {
                        var phoneNumbersString = settings.Get(SettingName.OrderNotificationPhoneNumbers);
                        if (!string.IsNullOrWhiteSpace(phoneNumbersString))
                        {
                            var phoneNumbers = phoneNumbersString.Split(new[] { ',', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Length == 10 && x.All(c => char.IsDigit(c)));
                            foreach (var phoneNumber in phoneNumbers)
                            {
                                try { smsSender.Send(twilio.Phone, $"+1{phoneNumber}", GetBody(order, name, settings, false)); }
                                catch (Exception ex) { Log(ex); }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex);
                if (isCustomer)
                {
                    throw new Exception("Order placed successfully but there was an error sending confirmation text - don't worry, your food will be ready!");
                }
            }
        }
Example #4
0
        public IActionResult Notify()
        {
            var json = string.Empty;

            using (var client = new System.Net.WebClient())
            {
                json = client.DownloadString($"{_azureSettings.SiteBaseUrl}api/events");
            }
            var events = JsonConvert.DeserializeObject <IEnumerable <EventModel> >(json).ToList();

            events = events.Where(x => x.Begins.Date == DateTime.Now.Date).ToList();

            var locations = events.Where(x => x.Location != null).Select(x => Subscription.SanitizeLocation(x.Location.Name)).Distinct().ToList();

            var subs = _handler.GetAll(null, locations).Where(x => !string.IsNullOrWhiteSpace(x.Location)).ToList();

            var sentTo = new List <string>();

            foreach (var sub in subs.Where(x => x.Type == Subscription.Types.Email))
            {
                if (!sentTo.Contains(sub.Subscriber))
                {
                    try
                    {
                        var n      = Environment.NewLine;
                        var _event = GetEvent(events, sub.Location);
                        EmailSender.Send(_azureSettings.SendgridApiKey,
                                         $"{_settings.Title} will be at {_event.Location.Name} today!",
                                         SubscriptionMessage(_event, sub),
                                         sub.Subscriber,
                                         _settings.ContactEmail
                                         );
                        sentTo.Add(sub.Subscriber);
                    }
                    catch (Exception ex) { ex.Ship(HttpContext); }
                }
            }

            var twilio = new TwilioHelper(_twilioAuthToken, _twilioSid, _twilioPhone);

            if (!twilio.IsValid)
            {
                new Exception("twilio settings invalid").Ship(HttpContext);
            }
            else
            {
                ISmsSender smsSender = new TwilioSmsSender(twilio);
                foreach (var sub in subs.Where(x => x.Type == Subscription.Types.Text))
                {
                    if (!sentTo.Contains(sub.Subscriber))
                    {
                        try
                        {
                            var _event = GetEvent(events, sub.Location);
                            smsSender.Send(twilio.Phone, $"+1{sub.Subscriber}", SubscriptionMessage(_event, sub));
                            sentTo.Add(sub.Subscriber);
                        }
                        catch (Exception ex) { ex.Ship(HttpContext); }
                    }
                }
            }

            return(Ok());
        }