feat: notify telegram on new inquiries
This commit is contained in:
@@ -9,6 +9,7 @@ using Microsoft.AspNetCore.ResponseCompression;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using MudBlazor.Services;
|
||||
using TaxBaik.Application;
|
||||
using TaxBaik.Application.Services;
|
||||
using TaxBaik.Infrastructure;
|
||||
using TaxBaik.Web.Services;
|
||||
|
||||
@@ -23,6 +24,10 @@ builder.Services.AddHealthChecks();
|
||||
// Razor Pages + Blazor Server 통합
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
|
||||
builder.Services.Configure<Microsoft.AspNetCore.Components.Server.CircuitOptions>(options =>
|
||||
{
|
||||
options.DetailedErrors = true;
|
||||
});
|
||||
|
||||
// JWT 인증
|
||||
var connectionString = builder.Configuration.GetConnectionString("Default")
|
||||
@@ -70,6 +75,7 @@ builder.Services.AddMemoryCache();
|
||||
builder.Services.AddResponseCompression(opts => {
|
||||
opts.Providers.Add<GzipCompressionProvider>();
|
||||
});
|
||||
builder.Services.AddScoped<IInquiryNotificationService, TelegramInquiryNotificationService>();
|
||||
|
||||
// 한글 포함 다국어 문자를 유니코드 엔티티로 변환하지 않도록 설정
|
||||
builder.Services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using TaxBaik.Application.Services;
|
||||
|
||||
namespace TaxBaik.Web.Services;
|
||||
|
||||
public class TelegramInquiryNotificationService : IInquiryNotificationService
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<TelegramInquiryNotificationService> _logger;
|
||||
private readonly string _baseUrl;
|
||||
|
||||
public TelegramInquiryNotificationService(IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<TelegramInquiryNotificationService> logger)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_baseUrl = (_configuration["App:PublicBaseUrl"] ?? "http://178.104.200.7/taxbaik").TrimEnd('/');
|
||||
}
|
||||
|
||||
public async Task NotifyCreatedAsync(int inquiryId, string name, string phone, string serviceType, string message, CancellationToken ct = default)
|
||||
{
|
||||
var botToken = _configuration["Telegram:BotToken"];
|
||||
var chatId = _configuration["Telegram:ChatId"];
|
||||
if (string.IsNullOrWhiteSpace(botToken) || string.IsNullOrWhiteSpace(chatId))
|
||||
return;
|
||||
|
||||
var adminLink = $"{_baseUrl}/admin/inquiries/{inquiryId}";
|
||||
var text = new StringBuilder()
|
||||
.AppendLine("새 문의가 접수되었습니다.")
|
||||
.AppendLine()
|
||||
.AppendLine($"제목: {serviceType}")
|
||||
.AppendLine($"이름: {name}")
|
||||
.AppendLine($"연락처: {phone}")
|
||||
.AppendLine()
|
||||
.AppendLine("내용:")
|
||||
.AppendLine(message)
|
||||
.AppendLine()
|
||||
.AppendLine($"답변 링크: {adminLink}")
|
||||
.ToString();
|
||||
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var url = $"https://api.telegram.org/bot{botToken}/sendMessage";
|
||||
var payload = new
|
||||
{
|
||||
chat_id = chatId,
|
||||
text,
|
||||
disable_web_page_preview = false
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.PostAsJsonAsync(url, payload, ct);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
_logger.LogWarning("텔레그램 알림 전송 실패: {StatusCode}", response.StatusCode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "텔레그램 알림 전송 중 오류 발생");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,13 @@
|
||||
"Jwt": {
|
||||
"SecretKey": "dev-secret-key-change-in-production-min-32-chars!"
|
||||
},
|
||||
"App": {
|
||||
"PublicBaseUrl": "http://178.104.200.7/taxbaik"
|
||||
},
|
||||
"Telegram": {
|
||||
"BotToken": "",
|
||||
"ChatId": ""
|
||||
},
|
||||
"SiteSettings": {
|
||||
"PhoneNumber": "010-4122-8268",
|
||||
"EmailAddress": "taxbaik5668@gmail.com",
|
||||
|
||||
Reference in New Issue
Block a user