Files
taxbaik/TaxBaik.Web/Services/TelegramNotificationService.cs
T
kjh2064 2bde490e9e
TaxBaik CI/CD / build-and-deploy (push) Successful in 51s
feat: integrate Serilog and Telegram notifications
- Add Serilog for structured logging (Console + File)
- Implement TelegramNotificationService for admin alerts
- Log successful/failed login attempts with Telegram notifications
- Add application startup/shutdown logging
- Log important events to Telegram Chat ID: -5585148480
- Configuration: Telegram:BotToken and Telegram:ChatId in appsettings

Features:
- Automatic daily log rotation
- Structured logging with timestamps
- Environment-aware alerts
- Error and info level Telegram messages

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-06-28 16:19:38 +09:00

77 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace TaxBaik.Web.Services;
using System.Net.Http.Json;
/// <summary>
/// Telegram Bot 알림 서비스
/// 중요 로깅 및 오류를 Telegram으로 전송
/// </summary>
public interface ITelegramNotificationService
{
Task SendMessageAsync(string message, CancellationToken ct = default);
Task SendErrorAsync(string title, string details, CancellationToken ct = default);
Task SendInfoAsync(string title, string message, CancellationToken ct = default);
}
public class TelegramNotificationService : ITelegramNotificationService
{
private readonly HttpClient _httpClient;
private readonly ILogger<TelegramNotificationService> _logger;
private readonly string _botToken;
private readonly string _chatId;
private const string TelegramApiUrl = "https://api.telegram.org";
public TelegramNotificationService(
HttpClient httpClient,
ILogger<TelegramNotificationService> logger,
IConfiguration config)
{
_httpClient = httpClient;
_logger = logger;
_botToken = config["Telegram:BotToken"] ?? "";
_chatId = config["Telegram:ChatId"] ?? "";
}
public async Task SendMessageAsync(string message, CancellationToken ct = default)
{
if (string.IsNullOrEmpty(_botToken) || string.IsNullOrEmpty(_chatId))
{
_logger.LogWarning("Telegram credentials not configured");
return;
}
try
{
var url = $"{TelegramApiUrl}/bot{_botToken}/sendMessage";
var payload = new
{
chat_id = _chatId,
text = message,
parse_mode = "HTML"
};
var response = await _httpClient.PostAsJsonAsync(url, payload, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("Failed to send Telegram message: {StatusCode}", response.StatusCode);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending Telegram message");
}
}
public async Task SendErrorAsync(string title, string details, CancellationToken ct = default)
{
var message = $"<b>❌ {title}</b>\n\n{details}\n\n<i>{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</i>";
await SendMessageAsync(message, ct);
}
public async Task SendInfoAsync(string title, string message, CancellationToken ct = default)
{
var text = $"<b>️ {title}</b>\n\n{message}\n\n<i>{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</i>";
await SendMessageAsync(text, ct);
}
}