// POST: api/MessageResponse public void Post([FromBody] TwillioMessageResponse message) { var logger = LogManager.GetLogger("MessageResponseController"); try { logger.Info("getting reply from " + message.From); var twilioSerializer = new TwilioXmlSerializer(); var filePath = Path.Combine(TwilioSettings.TwilioFilePath, DateTime.Now.Ticks + ".xml"); twilioSerializer.SerializeandSave(filePath, message); } catch (Exception exception) { logger.Error("some error occurred"); logger.Error("Message: " + exception.Message); logger.Error("Stack Trace: " + exception.StackTrace); } }
public bool TwillioResponse(TwillioMessageResponse message) { _notificationService.SmsReceivedNotification(message); return(true); }
public void SmsReceivedNotification(TwillioMessageResponse message) { if (message != null) { var phoneMobile = _phoneNumberFactory.CreatePhoneNumber(message.From, PhoneNumberType.Mobile); if (phoneMobile == null) { return; } var mobilePhonNumber = phoneMobile.AreaCode + phoneMobile.Number; var customers = _customerRepository.GetCustomersByPhoneNumber(mobilePhonNumber, PhoneNumberType.Mobile); var smsReceivedTime = DateTime.Now; var smsReceived = new SmsReceived { DateCreated = smsReceivedTime, Message = message.Body, PhoneNumber = mobilePhonNumber }; smsReceived = _smsReceivedRepository.SaveSmsReceived(smsReceived); if (customers.IsNullOrEmpty()) { _logger.Info("No customer found with matching Phone Number"); return; } if (!string.IsNullOrEmpty(message.Body) && SmsOptOutKeyWords.OptOutKey.Contains(message.Body.ToLower())) { var unsubscriptModel = new CustomerUnsubscribedSmsNotification { DateCreated = smsReceivedTime, SmsReceivedId = smsReceived.Id, StatusId = (long)SmsNotificationSubscriptionStatus.Unsubscribe }; foreach (var customer in customers) { unsubscriptModel.CustomerId = customer.CustomerId; _customerUnsubscribedSmsNotificationRepository.SaveUnsubscribedSmsStatus(unsubscriptModel); customer.IsSubscribed = false; _customerService.SaveCustomerOnly(customer, customer.CustomerId); } } else if (!string.IsNullOrEmpty(message.Body) && SmsOptInKeyWords.OptInKey.Contains(message.Body.ToLower())) { var unsubscriptModel = new CustomerUnsubscribedSmsNotification { DateCreated = smsReceivedTime, SmsReceivedId = smsReceived.Id, StatusId = (long)SmsNotificationSubscriptionStatus.Subscribe }; foreach (var customer in customers) { unsubscriptModel.CustomerId = customer.CustomerId; _customerUnsubscribedSmsNotificationRepository.SaveUnsubscribedSmsStatus(unsubscriptModel); customer.IsSubscribed = true; _customerService.SaveCustomerOnly(customer, customer.CustomerId); } } else { if (!customers.IsNullOrEmpty()) { Customer customer = null; if (customers.Count() > 1) { var lastNotificationSentTo = _notificationRepository.GetLatestNotificationByPhone(mobilePhonNumber); customer = customers.FirstOrDefault(s => s.Id == lastNotificationSentTo.UserId) ?? customers.First(); } else { customer = customers.First(); } if (customer != null) { var screeningReminderSmsNotification = _phoneNotificationModelsFactory.GetDummyWrongSmsResponseNotificationViewModel(); var notification = _notifier.NotifyViaSms(NotificationTypeAlias.WrongSmsReponse, EmailTemplateAlias.WrongSmsReponse, screeningReminderSmsNotification, customer.Id, customer.CustomerId, "Wrong Sms Response"); } } } } }