/// <summary> /// Send UptimeRobot uptime notification to Telegram /// </summary> /// <param name="uptimeRobotAlert"></param> /// <returns></returns> public async Task <bool> TrySendUptimeRobotAlert(UptimeRobotAlert uptimeRobotAlert) { try { var message = ""; // Down if (uptimeRobotAlert.AlertType == UptimeRobotAlert.AlertTypes.Down) { message = $"<b>{uptimeRobotAlert.FriendlyName}</b> has gone <strong>{uptimeRobotAlert.AlertTypeFriendlyName.ToUpper()}</strong>\n"; } // Up else if (uptimeRobotAlert.AlertType == UptimeRobotAlert.AlertTypes.Up) { message = $"<b>{uptimeRobotAlert.FriendlyName}</b> is now <strong>{uptimeRobotAlert.AlertTypeFriendlyName.ToUpper()}</strong>\nDown for: <b>{uptimeRobotAlert.AlertDuration} seconds</b>\n"; } message += (!string.IsNullOrEmpty(uptimeRobotAlert.Url)) ? $"Target: {uptimeRobotAlert.Url}\n" : ""; message += (!string.IsNullOrEmpty(uptimeRobotAlert.AlertDetails)) ? $"Details: {uptimeRobotAlert.AlertDetails}\n" : ""; var telegramRequest = new TelegramRequest(_telegramSettings.TelegramChannel, "HTML", message); var telegramResponse = await _httpClient.PostAsync(new Uri($"{_telegramSettings.TelegramUrl}/bot{_telegramSettings.Token}/sendMessage"), new StringContent(JsonConvert.SerializeObject(telegramRequest), Encoding.UTF8, "application/json")); return(telegramResponse.IsSuccessStatusCode); } catch (Exception e) { _logger.Error(e, "Got an exception while sending UptimeRobot alert to Twitter"); return(false); } }
/// <summary> /// Send UptimeRobot uptime notification to Twitter /// </summary> /// <param name="uptimeRobotAlert"></param> /// <returns></returns> public Task <bool> TrySendUptimeRobotAlert(UptimeRobotAlert uptimeRobotAlert) { try { Auth.SetUserCredentials(_twitterSettings.ConsumerKey, _twitterSettings.ConsumerSecret, _twitterSettings.AccessToken, _twitterSettings.AccessTokenSecret); var tweet = ""; // Down if (uptimeRobotAlert.AlertType == UptimeRobotAlert.AlertTypes.Down) { tweet = $"{uptimeRobotAlert.FriendlyName} has gone {uptimeRobotAlert.AlertTypeFriendlyName.ToUpper()}\n"; } // Up else if (uptimeRobotAlert.AlertType == UptimeRobotAlert.AlertTypes.Up) { tweet = $"{uptimeRobotAlert.FriendlyName} is now {uptimeRobotAlert.AlertTypeFriendlyName.ToUpper()}\nDown for: {uptimeRobotAlert.AlertDuration} seconds\n"; } tweet += (!string.IsNullOrEmpty(uptimeRobotAlert.Url)) ? $"Target: {uptimeRobotAlert.Url}\n" : ""; tweet += (!string.IsNullOrEmpty(uptimeRobotAlert.AlertDetails)) ? $"Details: {uptimeRobotAlert.AlertDetails}\n" : ""; var publishedTweet = Tweet.PublishTweet(tweet); return(Task.FromResult(publishedTweet?.Id != null && publishedTweet?.Id != default)); } catch (Exception e) { _logger.Error(e, "Got an exception while sending UptimeRobot alert to Twitter"); return(Task.FromResult(false)); } }