public IHttpActionResult SendLinkUserPhone(string phone) { var userdevice = UserManager.FindByName(User.Identity.Name); var user = _db.Users.First(node => node.Id == userdevice.UserId); if (!string.IsNullOrEmpty(user.Sms)) { return(BadRequest("User has phone number linked already.")); } user.ConfirmKey = GetUniqueKeyNumber(6); user.ConfirmTimestamp = DateTime.UtcNow.AddMinutes(15); // Create an instance of the Twilio client. var client = new TwilioRestClient(AccountSid, AuthToken); // Send an SMS message. Twilio.Message result = client.SendMessage( FromNumber, phone, "Your confirmation number is " + user.ConfirmKey); if (result.RestException != null) { //an exception occurred making the REST call string message = result.RestException.Message; } _db.Users.AddOrUpdate(user); _db.SaveChanges(); return(Ok()); }
public static void Main(string[] args) { try { using (var db = new TransafeRxEntities()) { db.Database.CommandTimeout = 0; var messages = db.GetMessagesToSend_30().ToList();//db.GetMedAdhMsgs().ToList(); if (messages.Any()) { var twilioClient = new TwilioRestClient(ConfigurationManager.AppSettings["twilio_sid"], ConfigurationManager.AppSettings["twilio_authToken"]); foreach (var message in messages) { try { Twilio.Message tMessage = twilioClient.SendMessage(ConfigurationManager.AppSettings["twilioNbr"], message.MobPhone, message.MsgText); db.UpdateMsgSendQueue(message.mqID); //db.InsertPatientMessageSent(message.UserId, message.MsgText, tMessage.DateCreated, message.MsgID, tMessage.Sid, tMessage.Status, tMessage.ErrorMessage); //db.AddPersonalizedSurveyMessageHistory(message.UserId, message.MsgID); } catch (Exception e) { using (var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["mail_host"])) { string im = (e.InnerException != null) ? e.InnerException.Message : ""; string body = e.Message + ';' + im; var mMsg = new MailMessage("*****@*****.**", ConfigurationManager.AppSettings["SupportEmail"]); mMsg.Subject = "TransafeRx Personalized Msgs In Loop Error Report"; mMsg.Body = body; mMsg.IsBodyHtml = true; smtpClient.Send(mMsg); } //throw; } } } } } catch (Exception ex) { using (var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["mail_host"])) { string im = (ex.InnerException != null) ? ex.InnerException.Message : ""; string body = ex.Message + ';' + im; var mMsg = new MailMessage("*****@*****.**", ConfigurationManager.AppSettings["SupportEmail"]); mMsg.Subject = "TransafeRx Personalized Msgs Report"; mMsg.Body = body; mMsg.IsBodyHtml = true; smtpClient.Send(mMsg); } throw; } }
public IHttpActionResult CreateUserPhone(string phone) { if (string.IsNullOrEmpty(phone)) { return(BadRequest("Phone not present")); } // If user exists, add them to the database. Else update their codes. var user = _db.Users.FirstOrDefault(node => node.Sms == phone); var userExists = user != null; if (!userExists) { user = new User { Sms = phone, NickName = phone, Joined = DateTime.UtcNow }; } user.ConfirmKey = GetUniqueKeyNumber(6); user.ConfirmTimestamp = DateTime.UtcNow.AddMinutes(15); user.AvatarId = 1; _db.Users.AddOrUpdate(user); _db.SaveChanges(); // Create an instance of the Twilio client. var client = new TwilioRestClient(AccountSid, AuthToken); // Send an SMS message. Twilio.Message result = client.SendMessage( FromNumber, phone, "Your confirmation number is " + user.ConfirmKey); if (result.RestException != null) { return(BadRequest(result.RestException.Message)); } return(Ok()); }
private static void SendThroughSecondaryApi(string phoneNumber, string activationCode) { if (_twilioAccountSid == null) { _twilioAccountSid = ConfigurationManager.AppSettings[NeeoConstants.TwilioAccountSid].ToString(); } if (_twilioAuthToken == null) { _twilioAuthToken = ConfigurationManager.AppSettings[NeeoConstants.TwilioAuthToken].ToString(); } if (_twilioPhoneNumber == null) { _twilioPhoneNumber = ConfigurationManager.AppSettings[NeeoConstants.TwilioPhoneNumber].ToString(); } TwilioRestClient twilioClient = new TwilioRestClient(_twilioAccountSid, _twilioAuthToken); Twilio.Message message = twilioClient.SendMessage(_twilioPhoneNumber, phoneNumber, _smsBody.Replace(_activationCodeMask, activationCode)); if (message.RestException == null) { if (message.Status == "failed") { LogManager.CurrentInstance.ErrorLogger.LogError( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Twilio - Phone # : \"" + phoneNumber + "\" is invalid."); throw new ApplicationException(CustomHttpStatusCode.InvalidNumber.ToString("D")); } } else { LogManager.CurrentInstance.ErrorLogger.LogError( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Twilio - " + message.RestException.Message); throw new ApplicationException(CustomHttpStatusCode.SmsApiException.ToString("D")); } }
private CommunicationRecipientStatus GetCommunicationRecipientStatus(Message response) { if (response.ErrorCode != null || response.RestException != null || (response.Status != null && response.Status.ToLower() == "failed")) { return CommunicationRecipientStatus.Failed; } return CommunicationRecipientStatus.Delivered; }