/// <summary> /// Send out daily ship fit availability summaries for all accounts. /// <param name="twitterContext">A twitter context for the sending of messages.</param> /// </summary> internal async Task SendDailySummary(LinqToTwitter::TwitterContext twitterContext) { // Fetch all enabled accounts and their notification recipients. IEnumerable <Account> accounts = this.doctrineShipsRepository.GetAccountsForNotifications(); // Are there any enabled accounts? if (accounts != null && accounts.Any() == true) { foreach (var account in accounts) { // Are there any recipients for this account? if (account.NotificationRecipients != null && account.NotificationRecipients.Any() == true) { // Get the summary message for the current account. string summaryMessage = GetDailySummaryMessage(account); // As long as the summary message is not empty, continue. if (summaryMessage != string.Empty) { foreach (var recipient in account.NotificationRecipients) { // If the current recipient is set to receive daily summaries, send a message. if (recipient.ReceivesDailySummary == true) { await this.SendDirectMessage(twitterContext, recipient.TwitterHandle, summaryMessage); } } } } } } }
/// <summary> /// Send a twitter direct message. /// <param name="twitterContext">A twitter context for the sending of messages.</param> /// </summary> internal async Task <bool> SendDirectMessage(LinqToTwitter::TwitterContext twitterContext, string screenName, string message) { try { var result = await twitterContext.NewDirectMessageAsync(screenName, message); if (result != null) { this.logger.LogMessage("Sent A Direct Message To '" + result.RecipientScreenName + "' With Message '" + result.Text + "'.", 2, "Message", MethodBase.GetCurrentMethod().Name); return(true); } else { return(false); } } catch (Exception e) { logger.LogMessage("An error occured while sending a twitter message. Are the twitter api credentials correct?", 0, "Message", MethodBase.GetCurrentMethod().Name); logger.LogMessage(e.ToString(), 0, "Exception", MethodBase.GetCurrentMethod().Name); return(false); } }
/// <summary> /// Send out any ship fit availability alerts for all accounts. /// <param name="twitterContext">A twitter context for the sending of messages.</param> /// </summary> internal async Task SendAvailabilityAlert(LinqToTwitter::TwitterContext twitterContext) { // Fetch all enabled accounts and their notification recipients. IEnumerable <Account> accounts = this.doctrineShipsRepository.GetAccountsForNotifications(); // Are there any enabled accounts? if (accounts != null && accounts.Any() == true) { foreach (var account in accounts) { // Are there any recipients for this account? if (account.NotificationRecipients != null && account.NotificationRecipients.Any() == true) { // Get the alert message for the current account. string alertMessage = GetAvailabilityAlertMessage(account); // As long as the alert message is not empty, continue. if (alertMessage != string.Empty) { foreach (var recipient in account.NotificationRecipients) { // If the current recipient is set to receive alerts and has not received one too recently, send a message. if (recipient.ReceivesAlerts == true && Time.HasElapsed(recipient.LastAlert, TimeSpan.FromHours(recipient.AlertIntervalHours))) { await this.SendDirectMessage(twitterContext, recipient.TwitterHandle, alertMessage); // Update the LastAlert timestamp for the current recipient. recipient.LastAlert = DateTime.UtcNow; this.doctrineShipsRepository.UpdateNotificationRecipient(recipient); this.doctrineShipsRepository.Save(); } } } } } } }