public SentSmsMessageLogEntry Send(string phoneNumber, string message) { SentSmsMessageLogEntry result = new SentSmsMessageLogEntry { Date = DateTime.UtcNow, Message = message, Destination = phoneNumber, }; SMSMessage msg = Client.SendSmsMessage(_Configuration.FromNumber, phoneNumber, message); if (msg.Status == "failed") { result.Status = (int)MessageSendStatus.Error; if (msg.RestException != null) { result.Details = string.Format(CultureInfo.InvariantCulture, "Code: {0} : Status: {1}", msg.RestException.Code, msg.RestException.Status); } else { result.Details = "Unknown error (Twilio did not report a RestException)"; } } else { result.MessageId = msg.Sid; result.Status = (int)MessageSendStatus.Success; } return result; }
void MessageSent(SentSmsMessageLogEntry result, OutgoingSmsMessage message) { IDataStore store = DataStore.GetInstance(); if (result.Status == (int)MessageSendStatus.Success) { // delete copy local to this store store.Remove(store.OutgoingMessages.Where(m => m.Id == message.Id).FirstOrDefault()); } else { ProcessSendFailure(message); } }
void IDataStore.Save(SentSmsMessageLogEntry entry) { SentSmsMessageLogEntries.Add(entry); SaveChanges(); }
public void Save(SentSmsMessageLogEntry entry) { lock (_lock) { if (entry.Id == 0) { entry.Id = NextId(); } _sentSmsMessageLogEntries[entry.Id] = entry; } }
private void ProcessSendFailure(OutgoingSmsMessage msg) { IDataStore store = DataStore.GetInstance(); // reload from the local store msg = store.OutgoingMessages.Where(m => m.Id == msg.Id).First(); if (msg != null) { if (msg.Attempts < 10) { msg.Attempts++; DateTime next; if (msg.Attempts < 10) { next = DateTime.UtcNow.AddMinutes(msg.Attempts * 10); } else { next = DateTime.UtcNow.AddHours(18); } msg.NextAttempt = next; store.Save(msg); } else { SentSmsMessageLogEntry entry = new SentSmsMessageLogEntry { Date = DateTime.UtcNow, Destination = msg.Destination, Message = msg.Message, Status = (int)MessageSendStatus.Abandoned, Details = string.Format(CultureInfo.InvariantCulture, "Fatal SMS Send error - giving up on sending message {0} - it was attempted {1} times.", msg.Id, msg.Attempts), }; store.Save(entry); store.Remove(msg); } } }