Files
taxbaik/src/TaxBaik.Web/Services/TelegramNotificationService.cs
T
kjh2064 ea447495d3
TaxBaik CI/CD / build-and-deploy (push) Successful in 2m7s
refactor: move buildable .NET source into src/, update CI/doc paths
Groups the repo root into src (buildable source), docs (already existed),
and everything else (db/, scripts/, tests/, deploy/ - deployment/ops/test
assets that aren't compiled, already organized as their own folders). CI
now only needs src/ to build: dotnet restore/build/test/publish all point
at src/TaxBaik.sln, src/TaxBaik.Web/, src/TaxBaik.Proxy/.

- git mv every project (Domain, Infrastructure, Application,
  Application.Tests, Web, Web.Client, Proxy) and TaxBaik.sln into src/ as a
  unit, so relative ProjectReference/.sln paths stay valid unchanged.
- .gitea/workflows/deploy.yml: 6 dotnet restore/clean/build/test/publish
  invocations now point at src/. db/migrations and scripts/ stay at root
  (deploy_gb.sh and browser-e2e.yml only touch published output and the
  deployed URL, not source paths - verified, no changes needed there).
- scripts/validate_admin_render.sh: admin render-mode file paths now
  src/TaxBaik.Web.Client/...
- scripts/validate_kst_timestamps.sh: dropped deploy.sh from its target
  list - that script was removed in the prior cleanup commit (dead, no
  CI workflow referenced it) but this validator still expected it to exist.
- CLAUDE.md, docs/ENGINEERING_HARNESS.md, docs/ADMIN_PATTERN_CRITIQUE_WBS.md:
  updated project-structure diagram, dotnet run/build commands, and grep
  targets to the new src/ paths (also fixed a pre-existing stale path in
  ADMIN_PATTERN_CRITIQUE_WBS.md that still said TaxBaik.Web/Components/Admin
  from before that ever moved to TaxBaik.Web.Client).
- Added a Repo Root harness rule + Architecture Guardrail entries: new files
  belong under src/docs/tests/scripts/db/deploy, not loose at root; temp
  work stays outside the repo (or under a gitignored .scratch/) and is
  never committed.

Verified locally: dotnet build/test src/TaxBaik.sln (26/26 tests), and all
three scripts/validate_*.sh pass against the new layout.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-03 10:37:37 +09:00

131 lines
4.9 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);
Task SendInquiryNotificationAsync(string message, CancellationToken ct = default);
Task SendSystemNotificationAsync(string message, CancellationToken ct = default);
Task SendReportAsync(string reportTitle, string reportContent, CancellationToken ct = default);
}
public class TelegramNotificationService : ITelegramNotificationService
{
private readonly HttpClient _httpClient;
private readonly ILogger<TelegramNotificationService> _logger;
private readonly string _botToken;
private readonly string _defaultChatId;
private readonly string _inquiryChatId;
private readonly string _systemChatId;
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"] ?? "";
_defaultChatId = config["Telegram:ChatId"] ?? "-5434691215";
_inquiryChatId = config["Telegram:InquiryChatId"] ?? _defaultChatId;
_systemChatId = config["Telegram:SystemChatId"] ?? "-5585148480";
}
public async Task SendMessageAsync(string message, CancellationToken ct = default)
{
if (string.IsNullOrEmpty(_botToken) || string.IsNullOrEmpty(_defaultChatId))
{
_logger.LogWarning("Telegram credentials not configured");
return;
}
if (!TelegramAlertGate.ShouldSend("telegram:default", message, TimeSpan.FromMinutes(5)))
return;
await SendToChat(_defaultChatId, message, ct);
}
public async Task SendInquiryNotificationAsync(string message, CancellationToken ct = default)
{
var text = $"<b>📋 문의 사항</b>\n\n{message}";
if (!TelegramAlertGate.ShouldSend("telegram:inquiry", text, TimeSpan.FromMinutes(10)))
return;
await SendToChat(_inquiryChatId, text, ct);
}
public async Task SendSystemNotificationAsync(string message, CancellationToken ct = default)
{
var text = $"<b>🔧 시스템 알림</b>\n\n{message}";
if (!TelegramAlertGate.ShouldSend("telegram:system", text, TimeSpan.FromMinutes(10)))
return;
await SendToChat(_systemChatId, text, ct);
}
private async Task SendToChat(string chatId, string message, CancellationToken ct)
{
if (string.IsNullOrEmpty(_botToken) || string.IsNullOrEmpty(chatId))
{
_logger.LogWarning("Telegram credentials not configured for chatId {ChatId}", chatId);
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 to {ChatId}: {StatusCode}", chatId, response.StatusCode);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending Telegram message to {ChatId}", chatId);
}
}
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>";
if (!TelegramAlertGate.ShouldSend("telegram:error", message, TimeSpan.FromMinutes(15)))
return;
await SendToChat(_systemChatId, 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>";
if (!TelegramAlertGate.ShouldSend("telegram:info", text, TimeSpan.FromMinutes(30)))
return;
await SendToChat(_defaultChatId, text, ct);
}
public async Task SendReportAsync(string reportTitle, string reportContent, CancellationToken ct = default)
{
var text = $"<b>📊 {reportTitle}</b>\n\n{reportContent}\n\n<i>{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</i>";
if (!TelegramAlertGate.ShouldSend("telegram:report", text, TimeSpan.FromHours(20)))
return;
await SendToChat(_systemChatId, text, ct);
}
}