using FrameworkDAL.DTO.MessageHub.MessageHubGenerator; using GB5Shared.DTO.Framework.Login; using Microsoft.Extensions.Options; using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; namespace FrameworkBLL.MessageHub.MessageHubPlatforms { public class SlackPlatform : IMessageHubPlatform.ISlackPlatform { private readonly IHttpClientFactory _httpClientFactory; private readonly SlackSettings _settings; public SlackPlatform( IHttpClientFactory httpClientFactory, IOptions settings) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); _settings = settings?.Value?.Slack ?? throw new ArgumentNullException(nameof(settings)); } // ========================================================== // ✅ TEMPLATE VERSION - Slack MessageHubEngine // ========================================================== public async Task SendSlackTemplate( MessageHubDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) return Fail("Template DTO cannot be null"); if (login == null) return Fail("Login info missing"); if (string.IsNullOrWhiteSpace(_settings?.WebhookUrl)) return Fail("Slack WebhookUrl not configured"); var payload = new { channel = string.IsNullOrWhiteSpace(dto.Recipient) ? _settings.DefaultChannel : dto.Recipient, username = string.IsNullOrWhiteSpace(_settings.DefaultUserName) ? "MessageHub" : _settings.DefaultUserName, icon_emoji = string.IsNullOrWhiteSpace(_settings.DefaultIconEmoji) ? ":robot_face:" : _settings.DefaultIconEmoji, text = $"*📢 TEMPLATE MESSAGE*\n\n{dto.MessageText}\n\n_Sent by: {login.UserName}_" }; try { var client = _httpClientFactory.CreateClient(); var response = await client.PostAsync( _settings.WebhookUrl, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"), ct); var responseContent = await response.Content.ReadAsStringAsync(ct); return new MessageHubResponseDTO { Success = response.IsSuccessStatusCode, Message = response.IsSuccessStatusCode ? "Slack Template message sent successfully" : "Slack Template message failed", ResponseData = responseContent }; } catch (Exception ex) { return Fail(ex.Message); } } // ========================================================== // ✅ CONVERSATION VERSION - Slack MessageHubConversation // ========================================================== public async Task SendSlackConversation( MessageHubDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) return Fail("Conversation DTO cannot be null"); if (login == null) return Fail("Login info missing"); if (string.IsNullOrWhiteSpace(_settings?.WebhookUrl)) return Fail("Slack WebhookUrl not configured"); var payload = new { channel = string.IsNullOrWhiteSpace(dto.Recipient) ? _settings.DefaultChannel : dto.Recipient, username = string.IsNullOrWhiteSpace(_settings.DefaultUserName) ? "MessageHub" : _settings.DefaultUserName, icon_emoji = string.IsNullOrWhiteSpace(_settings.DefaultIconEmoji) ? ":robot_face:" : _settings.DefaultIconEmoji, text = dto.MessageText // Pure conversation text }; try { var client = _httpClientFactory.CreateClient(); var response = await client.PostAsync( _settings.WebhookUrl, new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"), ct); var responseContent = await response.Content.ReadAsStringAsync(ct); return new MessageHubResponseDTO { Success = response.IsSuccessStatusCode, Message = response.IsSuccessStatusCode ? "Slack Conversation message sent successfully" : "Slack Conversation message failed", ResponseData = responseContent }; } catch (Exception ex) { return Fail(ex.Message); } } // ========================================================== // PRIVATE HELPER // ========================================================== private static MessageHubResponseDTO Fail(string reason) => new() { Success = false, Message = reason }; } }