private async Task <SlackSettings.Channel> GetChannelAsync(string type, string sender) { var channel = _settings.Channels.FirstOrDefault(x => x.Type == type); if (!string.IsNullOrWhiteSpace(channel?.WebHookUrl)) { return(channel); } channel = _settings.Channels.FirstOrDefault(x => x.Type == _warning); if (string.IsNullOrWhiteSpace(channel?.WebHookUrl)) { channel = _settings.Channels.FirstOrDefault(x => x.Type.StartsWith(_warning)); } if (!string.IsNullOrWhiteSpace(channel?.WebHookUrl)) { if (string.IsNullOrWhiteSpace(sender)) { sender = _unknownSender; } var env = !string.IsNullOrEmpty(_settings.Env) ? _settings.Env : _unknownEnv; await HttpRequestClient.PostRequest(new { text = $"{env}: Couldn't find webhook for {type} from {sender}" }.ToJson(), channel.WebHookUrl); } return(null); }
public async Task SendNotificationAsync(string type, string message, string sender = null) { var channel = await GetChannelAsync(type, sender); if (channel == null) { return; } var strBuilder = new StringBuilder(); if (_environment != null) { strBuilder.AppendLine(_environment); } var processedMessage = await ProcessMessageAsync(sender, message, channel); strBuilder.AppendLine(sender != null ? $"{sender} : {processedMessage}" : processedMessage); await HttpRequestClient.PostRequest(new { text = strBuilder.ToString() }.ToJson(), channel.WebHookUrl); if (channel.Opsgenie) { await PostRequest(sender, processedMessage); } }