/// <summary> /// Send a notification email to someone /// </summary> /// <param name="n">The notification details from the database</param> private void SendEmail(PersonNotification n) { try { // send email System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(new MailAddress(n.PersonEmail, n.Name)); message.From = new MailAddress("*****@*****.**", "GxAlert"); message.Subject = this.FillPlaceholders(n.EmailSubject, n, false); message.Body = this.FillPlaceholders(n.EmailBody, n, false); SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["smtpServer"], 587); smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPassword"]); smtp.Send(message); // store in log DB.InsertNotificationLog(n, message.Subject, message.Body, true, false, false); } catch (Exception e) { Logger.Log("Sending email to \"" + n.PersonEmail + "\" error:" + e.Message, LogLevel.Error); } return; }
/// <summary> /// Call a person according to the passed notification /// </summary> /// <param name="n">The notification details from the database</param> private void InitiateCall(PersonNotification n) { try { // call phone string phone = this.FillPlaceholders(n.PhoneBody, n, false); var twilio = new TwilioRestClient(ConfigurationManager.AppSettings["twilioAccountSid"], ConfigurationManager.AppSettings["twilioAuthToken"]); var call = twilio.InitiateOutboundCall(ConfigurationManager.AppSettings["twilioFromNumber"], n.PersonPhone, "http://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3ENew+MDR-TB+case+in+Nigeria%21%3C%2FSay%3E%3C%2FResponse%3E"); // store in log DB.InsertNotificationLog(n, phone, false, false, true); } catch (Exception e) { Logger.Log("Calling \"" + n.PersonPhone + "\" error:" + e.Message, LogLevel.Error); } return; }
/// <summary> /// Send SMS to a person /// </summary> /// <param name="n">The notification details from the database</param> private void SendSms(PersonNotification n) { try { // send sms string sms = this.FillPlaceholders(n.SmsBody, n, true); var twilio = new TwilioRestClient(ConfigurationManager.AppSettings["twilioAccountSid"], ConfigurationManager.AppSettings["twilioAuthToken"]); var msg = twilio.SendSmsMessage(ConfigurationManager.AppSettings["twilioFromNumber"], n.PersonCell, sms); // store in log DB.InsertNotificationLog(n, sms, false, true, false); } catch (Exception e) { Logger.Log("Sending SMS to \"" + n.PersonCell + "\" error:" + e.Message, LogLevel.Error); } return; }
/// <summary> /// Inserts a new entry in the notification log /// </summary> /// <param name="n">Person Notification object that the notification is based on</param> /// <param name="subject">Subject of the notification (for emails)</param> /// <param name="body">Body text of the notification</param> /// <param name="email">Was the notification sent as an email?</param> /// <param name="sms">Was the notification sent as an SMS?</param> /// <param name="phone">Was the notification sent as a phone call?</param> internal static void InsertNotificationLog(PersonNotification n, string subject, string body, bool email, bool sms, bool phone) { using (GxAlertEntities bpe = new GxAlertEntities()) { notificationlog nl = new notificationlog(); nl.Body = body; nl.Email = email; nl.NotificationId = n.NotificationId; nl.NotificationName = n.NotificationName; nl.PersonId = n.PersonId; nl.PersonName = n.Name; nl.Phone = phone; nl.SentBy = ConfigurationManager.AppSettings["appName"]; nl.SentOn = DateTime.Now; nl.Sms = sms; nl.Subject = subject; bpe.notificationlogs.Add(nl); bpe.SaveChanges(); } }
/// <summary> /// Fills the placeholders in the given string with details from a test /// </summary> /// <param name="message">Message with placeholders to replace</param> /// <param name="n">Person Notification object that we use to fill placeholders</param> /// <param name="isSms">Indicate whether this is an SMS (results in slightly different formatting)</param> /// <returns>Message with all placeholders replaced</returns> private string FillPlaceholders(string message, PersonNotification n, bool isSms) { CultureInfo ci = new CultureInfo(n.PersonCulture); message = message.Replace("[DeploymentDescription]", n.DeploymentDescription ?? n.DeploymentHostId) .Replace("[DeploymentCountry]", n.DeploymentCountry) .Replace("[MessageSentOn]", n.MessageSentOn.ToString("d", ci) + " " + n.MessageSentOn.ToString("t", ci)) .Replace("[TestEndedOn]", n.TestEndedOn.ToString("d", ci) + " " + n.TestEndedOn.ToString("t", ci)) .Replace("[DeploymentHostId]", n.DeploymentHostId); if (!isSms) { message = message.Replace("[Results]", n.ResultText.Replace("|", ", ").Trim(new char[] { ' ', ',' })); } else { message = message.Replace("[Results]", n.ResultText.Replace("|", System.Environment.NewLine).Trim(new char[] { ' ', '\\', 'n' })); } return(message); }
/// <summary> /// Inserts a new entry in the notification log /// </summary> /// <param name="n">Person Notification object that the notification is based on</param> /// <param name="body">Body text of the notification</param> /// <param name="email">Was the notification sent as an email?</param> /// <param name="sms">Was the notification sent as an SMS?</param> /// <param name="phone">Was the notification sent as a phone call?</param> internal static void InsertNotificationLog(PersonNotification n, string body, bool email, bool sms, bool phone) { InsertNotificationLog(n, null, body, email, sms, phone); }