コード例 #1
0
ファイル: TwitterAlertClient.cs プロジェクト: NoExitTV/pi-dns
        /// <summary>
        /// Send hetrix tools uptime notification to Twitter
        /// </summary>
        /// <param name="hetrixToolsAlert"></param>
        /// <returns></returns>
        public Task <bool> TrySendHetrixToolsAlert(HetrixToolsAlert hetrixToolsAlert)
        {
            try
            {
                Auth.SetUserCredentials(_twitterSettings.ConsumerKey, _twitterSettings.ConsumerSecret, _twitterSettings.AccessToken, _twitterSettings.AccessTokenSecret);
                var tweet = "";

                // Down
                if (hetrixToolsAlert.Monitor_status == HetrixToolsAlert.MonitorStatus.Offline)
                {
                    tweet = $"{hetrixToolsAlert.Monitor_name} has gone {hetrixToolsAlert.Monitor_status.ToUpper()}\nTarget: {hetrixToolsAlert.Monitor_target}\nMonitor Type: {hetrixToolsAlert.Monitor_type}\n";
                }
                // Up
                else if (hetrixToolsAlert.Monitor_status == HetrixToolsAlert.MonitorStatus.Online)
                {
                    tweet = $"{hetrixToolsAlert.Monitor_name} is now {hetrixToolsAlert.Monitor_status.ToUpper()}\nTarget: {hetrixToolsAlert.Monitor_target}\nType: {hetrixToolsAlert.Monitor_type}";
                }

                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 hetrix tools alert to Twitter");
                return(Task.FromResult(false));
            }
        }
コード例 #2
0
        /// <summary>
        /// Send HetrixTools uptime notification to Telegram
        /// </summary>
        /// <param name="hetrixToolsAlert"></param>
        /// <returns></returns>
        public async Task <bool> TrySendHetrixToolsAlert(HetrixToolsAlert hetrixToolsAlert)
        {
            try
            {
                var message = "";

                // Down
                if (hetrixToolsAlert.Monitor_status == HetrixToolsAlert.MonitorStatus.Offline)
                {
                    message = $"<b>{hetrixToolsAlert.Monitor_name}</b> has gone <strong>{hetrixToolsAlert.Monitor_status.ToUpper()}</strong>\nTarget: <b>{hetrixToolsAlert.Monitor_target}</b>\nMonitor Type: {hetrixToolsAlert.Monitor_type}\nMonitor Reports: \n";

                    foreach (var obj in hetrixToolsAlert.Monitor_errors.GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(p => !p.GetIndexParameters().Any())
                             .Where(p => p.CanRead && p.CanWrite))
                    {
                        var value = obj.GetValue(hetrixToolsAlert.Monitor_errors, null);
                        if (value != null)
                        {
                            message += $"\t-{obj.Name}: {value}\n";
                        }
                    }
                }
                // Up
                else if (hetrixToolsAlert.Monitor_status == HetrixToolsAlert.MonitorStatus.Online)
                {
                    message = $"<b>{hetrixToolsAlert.Monitor_name}</b> is now <strong>{hetrixToolsAlert.Monitor_status.ToUpper()}</strong>\nTarget: <b>{hetrixToolsAlert.Monitor_target}</b>\nType: {hetrixToolsAlert.Monitor_type}";
                }

                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 hetrix tools alert to Telegram");
                return(false);
            }
        }