private void SendEmail(SMSMessage sms, ExchangeData callerDetails)
        {
            if (sms==null) sms = new SMSMessage();
            if (callerDetails==null) callerDetails = new ExchangeData();

            var fromAddress = new MailAddress(Properties.Settings.Default.EmailTo, "Incoming SMS");
            var toAddress = new MailAddress(Properties.Settings.Default.EmailTo, "SMS Email Queue");
            var ccAddress = new MailAddress(Properties.Settings.Default.EmailCC);

            var subject = string.Format("Incoming SMS: {0}", sms.From ?? "Unknown");
            var getBody = CreateHtmlBody(sms, callerDetails);

            var smtp = new SmtpClient();

            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = getBody,
                IsBodyHtml = true

            })
            {
                message.Bcc.Add(ccAddress);
                smtp.Send(message);
            }
        }
 public void Queued()
 {
     var twilioResponse = new SMSMessage { Status = "queued", Sid = "sidReceipt" };
     var response = TwilioWrapper.ProcessResponse(twilioResponse);
     Assert.That(response, Is.TypeOf(typeof(SmsQueued)));
     Assert.That(response.Sid, Is.EqualTo(twilioResponse.Sid));
 }
        public void Success()
        {
            var twilioResponse = new SMSMessage { Status = "sent", Sid = "sidReceipt", DateSent = DateTime.Now, Price = 3 };
            var response = TwilioWrapper.ProcessResponse(twilioResponse);

            Assert.That(response, Is.TypeOf(typeof(SmsSent)));
            Assert.That(response.Sid, Is.EqualTo(response.Sid));
            var smsSent = response as SmsSent;
            Assert.That(smsSent.SmsConfirmationData.Receipt, Is.EqualTo(twilioResponse.Sid));
            Assert.That(smsSent.SmsConfirmationData.SentAtUtc, Is.EqualTo(twilioResponse.DateSent));
            Assert.That(smsSent.SmsConfirmationData.Price, Is.EqualTo(twilioResponse.Price));
        }
 private static string CreateHtmlBody(SMSMessage callDetails, ExchangeData callerDetails)
 {
     var sb = new StringBuilder();
     sb.AppendFormat("<h2>SMS From: {0}</h2>", callDetails.From);
     sb.AppendFormat("<h3>Call Details</h3><table>");
     sb.AppendFormat("<tr><td><b>{0}</b></td><td><h3>{1}</h3></td></tr>", "Message", callDetails.Body);
     sb.AppendFormat("<tr><td><b>{0}</b></td><td>{1}, {2}</td></tr>", "Location", callerDetails.City, callerDetails.State );
     sb.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "Recieved Time", callDetails.DateSent );
     sb.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td></tr>", "Sent To Account", callDetails.To);
     sb.AppendFormat("</table>");
     return sb.ToString();
 }
        public void FailedRestException()
        {
            var twilioResponse = new SMSMessage { RestException = new RestException { Code = "code", Message = "message", MoreInfo = "moreInfo", Status = "status" } };
            var response = TwilioWrapper.ProcessResponse(twilioResponse);

            Assert.That(response, Is.TypeOf(typeof(SmsFailed)));
            Assert.That(response.Sid, Is.EqualTo(twilioResponse.Sid));
            var smsFailed = response as SmsFailed;
            Assert.That(smsFailed.Status, Is.EqualTo(twilioResponse.RestException.Status));
            Assert.That(smsFailed.Code, Is.EqualTo(twilioResponse.RestException.Code));
            Assert.That(smsFailed.Message, Is.EqualTo(twilioResponse.RestException.Message));
            Assert.That(smsFailed.MoreInfo, Is.EqualTo(twilioResponse.RestException.MoreInfo));
        }
        public void SmsServiceSending()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper };

            var smsMessageSending = new SMSMessage { Status = "sending", Sid = "sidReceipt" };
            twilioWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof (SmsSending)));
            Assert.That(response.Sid, Is.EqualTo(smsMessageSending.Sid));
            twilioWrapper.VerifyAllExpectations();
        }
Beispiel #7
0
 // Methods
 public Task SendSMSAsync(string phoneNumber, string message)
 {
     return Task.Run(() =>
         {
             try
             {
                 TwilioRestClient twilio =
                     new TwilioRestClient(authentication.AccountSid, authentication.AccountSid);
                 result = twilio.SendSmsMessage(authentication.TwilioPhoneNumber, phoneNumber, message);
             }
             catch
             {
                 string errorMessage = result.RestException.Message;
             }
         }
     );
 }
        public HttpResponseMessage Incoming(SMSMessage sms)
        {
            var cnam = TelcoData.TelcoData.GetExchange(sms.From);

            var message = String.Format("Hello {0}, how is {1}, {2} today?", sms.Status, cnam.City,
                                        cnam.State);
            var response = new TwilioResponse();
            try
            {
                SendEmail(sms, cnam);
            }
            catch (Exception ex)
            {
                response.Sms(ex.Message);
            }

            return Request.CreateResponse(HttpStatusCode.OK, response.Element);
        }
        public void SmsServiceRestException()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message") };
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper };

            var smsMessageSending = new SMSMessage { RestException = new RestException {Code = "code", Message = "message", MoreInfo = "moreInfo", Status = "status"}};
            twilioWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessageSending);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof(SmsFailed)));
            Assert.That(response.Sid, Is.EqualTo(smsMessageSending.Sid));
            var smsFailed = response as SmsFailed;
            Assert.That(smsFailed.Status, Is.EqualTo(smsMessageSending.RestException.Status));
            Assert.That(smsFailed.Code, Is.EqualTo(smsMessageSending.RestException.Code));
            Assert.That(smsFailed.Message, Is.EqualTo(smsMessageSending.RestException.Message));
            Assert.That(smsFailed.MoreInfo, Is.EqualTo(smsMessageSending.RestException.MoreInfo));
            twilioWrapper.VerifyAllExpectations();
        }
        private SmsStatus ProcessSms(SMSMessage createdSmsMessage)
        {
            if ((string.IsNullOrWhiteSpace(createdSmsMessage.Status) && createdSmsMessage.RestException != null)
                || createdSmsMessage.Status.Equals("failed", StringComparison.CurrentCultureIgnoreCase))
            {
                var e = createdSmsMessage.RestException;
                return new SmsFailed(createdSmsMessage.Sid, e.Code, e.Message, e.MoreInfo, e.Status);
            }

            if (createdSmsMessage.Status.Equals("sent", StringComparison.CurrentCultureIgnoreCase))
                return new SmsSent(new SmsConfirmationData(createdSmsMessage.Sid, createdSmsMessage.DateSent, createdSmsMessage.Price));

            if (createdSmsMessage.Status.Equals("sending", StringComparison.CurrentCultureIgnoreCase))
            {
                return new SmsSending(createdSmsMessage.Sid);
            }

            if (createdSmsMessage.Status.Equals("queued", StringComparison.CurrentCultureIgnoreCase))
                return new SmsQueued(createdSmsMessage.Sid);

            return null;
        }
        public static SmsStatus ProcessResponse(SMSMessage twilioResponse)
        {
            if ((string.IsNullOrWhiteSpace(twilioResponse.Status) && twilioResponse.RestException != null)
                || twilioResponse.Status.Equals("failed", StringComparison.CurrentCultureIgnoreCase))
            {
                var e = twilioResponse.RestException;
                return new SmsFailed(twilioResponse.Sid, e.Code, e.Message, e.MoreInfo, e.Status);
            }

            if (twilioResponse.Status.Equals("sent", StringComparison.CurrentCultureIgnoreCase))
                return new SmsSent(new SmsConfirmationData(twilioResponse.Sid, twilioResponse.DateSent, twilioResponse.Price));

            if (twilioResponse.Status.Equals("sending", StringComparison.CurrentCultureIgnoreCase))
            {
                return new SmsSending(twilioResponse.Sid);
            }

            if (twilioResponse.Status.Equals("queued", StringComparison.CurrentCultureIgnoreCase))
                return new SmsQueued(twilioResponse.Sid);

            return null;
        }
        public void SmsServiceSuccess()
        {
            var messageToSend = new SendOneMessageNow { SmsData = new SmsData("mobile", "message")};
            var twilioWrapper = MockRepository.GenerateMock<ITwilioWrapper>();
            var smsService = new SmsService { TwilioWrapper = twilioWrapper };

            var smsMessage = new SMSMessage { Status = "sent", Sid = "sidReceipt", DateSent = DateTime.Now, Price = 3 };
            twilioWrapper
                .Expect(t => t.SendSmsMessage(messageToSend.SmsData.Mobile, messageToSend.SmsData.Message))
                .Return(smsMessage);

            var response = smsService.Send(messageToSend);

            Assert.That(response, Is.TypeOf(typeof(SmsSent)));
            Assert.That(response.Sid, Is.EqualTo(smsMessage.Sid));
            var smsSent = response as SmsSent;
            Assert.That(smsSent.SmsConfirmationData.Receipt, Is.EqualTo(smsMessage.Sid));
            Assert.That(smsSent.SmsConfirmationData.SentAtUtc, Is.EqualTo(smsMessage.DateSent));
            Assert.That(smsSent.SmsConfirmationData.Price, Is.EqualTo(smsMessage.Price));
            twilioWrapper.VerifyAllExpectations();
        }
Beispiel #13
0
 private void LogTextResponse(SMSMessage message, Reminder reminder)
 {
     var reminderLog = new ReminderLog(reminder._id, reminder._mobileNumber, reminder._message, reminder._nextScheduledReminder, message.Status, reminder._kind);
     reminderLog.InsertToDb();
 }
Beispiel #14
0
        public HttpResponseMessage ProcessTicketRequest(SMSMessage twilioRequest)
        {
            var invalidRequestMessage = "We could not understand your text.";

            var error = false;
            var message = default(String);
            var responseContext = default(ConversationContext);
            var conversationState = ConvoCookieHelper.Parse<ConversationState>(ConvoCookieHelper.CONVO_COOKIE_NAME_SMS, Request.Headers);

            if (conversationState == null)
                conversationState = new ConversationState() { ConversationStep = 1, ConversationName = twilioRequest.Body };

            // Get Conversation by name
            var conversation = ConversationRegistry.Get(conversationState.ConversationName);

            if (conversation != null)
            {
                try
                {
                    responseContext = conversation.RunStep(conversationState, twilioRequest.Body);
                    message = responseContext.ResponseMessage;
                }
                catch(Exception ex)
                {
                    // Invalid request
                    message = invalidRequestMessage;
                    error = true;
                }
            }
            else
            {
                // Invalid request
                message = invalidRequestMessage;
            }

            var twilioResponse = new TwilioResponse();

            twilioResponse.Sms(message);

            var response = TwilioHelper.WrapTwilioResponseInHttpResponse(this, twilioResponse);

            if (conversation.ConvoEnded(conversationState.ConversationStep))
            {
                // clear cookie
                ConvoCookieHelper.EndConvoCookie(ConvoCookieHelper.CONVO_COOKIE_NAME_SMS, response.Headers);
            }
            else if(!error)
            {
                // update cookie
                var newCookie = ConvoCookieHelper.GetCookieHeaderValue(ConvoCookieHelper.CONVO_COOKIE_NAME_SMS, new ConversationState()
                {
                    Context = responseContext,
                    ConversationName = conversationState.ConversationName,
                    ConversationStep = conversationState.ConversationStep + 1
                });
                
                response.Headers.AddCookies(new CookieHeaderValue[] { newCookie });
            }

            return response;
        }
Beispiel #15
0
 private void LogTextResponse(SMSMessage message, Reminder reminder)
 {
     var reminderLog = new ReminderLog(reminder._id.ToString(), reminder._mobileNumber, reminder._message, reminder._nextScheduledReminder, message.Status, reminder._kind);
 }
Beispiel #16
0
 // This checks for messages on a timer that defaults to every ten seconds, but can be changed by setting the TimerInterval
 void _twilioClient_MessageReceived(object sender, SMSMessage e)
 {
     Console.WriteLine(e.Body);
 }