111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using Managing.Application.Abstractions.Services;
|
|
using Managing.Common;
|
|
using Managing.Domain.Users;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Managing.Application.Shared;
|
|
|
|
public class WebhookService : IWebhookService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<WebhookService> _logger;
|
|
private readonly string _n8nWebhookUrl;
|
|
|
|
public WebhookService(HttpClient httpClient, IConfiguration configuration, ILogger<WebhookService> logger)
|
|
{
|
|
_httpClient = httpClient;
|
|
_configuration = configuration;
|
|
_logger = logger;
|
|
_n8nWebhookUrl = _configuration["N8n:WebhookUrl"] ?? string.Empty;
|
|
|
|
// Configure basic authentication if credentials are provided
|
|
var username = _configuration["N8n:Username"];
|
|
var password = _configuration["N8n:Password"];
|
|
|
|
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
|
|
{
|
|
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
|
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
|
|
}
|
|
}
|
|
|
|
public async Task SendTradeNotification(User user, string message, bool isBadBehavior = false)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(user.TelegramChannel))
|
|
{
|
|
_logger.LogWarning("No telegram channel configured");
|
|
return;
|
|
}
|
|
|
|
// Prepare the payload for n8n webhook
|
|
var payload = new
|
|
{
|
|
message = message,
|
|
isBadBehavior = isBadBehavior,
|
|
timestamp = DateTime.UtcNow,
|
|
type = "trade_notification",
|
|
telegramChannel = Formatings.FormatTelegramChannel(user.TelegramChannel)
|
|
};
|
|
|
|
// Send the webhook notification
|
|
var response = await _httpClient.PostAsJsonAsync(_n8nWebhookUrl, payload);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
_logger.LogInformation($"Successfully sent webhook notification for user {user.Name}");
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Failed to send webhook message. Status: {response.StatusCode}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SentrySdk.CaptureException(ex);
|
|
}
|
|
}
|
|
|
|
public async Task SendMessage(string message, string? telegramChannel = null)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(telegramChannel))
|
|
{
|
|
_logger.LogWarning("No telegram channel configured");
|
|
return;
|
|
}
|
|
|
|
// Prepare the payload for n8n webhook
|
|
var payload = new
|
|
{
|
|
message = message,
|
|
timestamp = DateTime.UtcNow,
|
|
type = "general_message",
|
|
telegramChannel = Formatings.FormatTelegramChannel(telegramChannel)
|
|
};
|
|
|
|
// Send the webhook notification
|
|
var response = await _httpClient.PostAsJsonAsync(_n8nWebhookUrl, payload);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
_logger.LogInformation("Successfully sent webhook message");
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Failed to send webhook message. Status: {response.StatusCode}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SentrySdk.CaptureException(ex);
|
|
}
|
|
}
|
|
} |