Udara/Udara/Common/Feature/Telegram/TelegramSettingsProvider.cs
2026-03-19 11:53:15 +08:00

39 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Connections;
using Microsoft.EntityFrameworkCore;
using Udara.Database.App;
namespace Udara.Common.Feature.Telegram;
public sealed class TelegramSettingsProvider : ITelegramSettingsProvider
{
private readonly IServiceScopeFactory _scopeFactory;
public TelegramSettingsProvider(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task<TelegramApiSettingsModel> GetSettingsAsync(CancellationToken token = default)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
var telegramConfigDict = (await db.AppConfigs
.Where(x => x.Category == "Telegram")
.ToListAsync(token))
.ToDictionary(x => x.ConfigKey, x => x.ConfigValue);
var model = new TelegramApiSettingsModel
{
Protocol = telegramConfigDict["TelegramApiProtocol"],
Host = telegramConfigDict["TelegramApiHost"],
Mode = telegramConfigDict["TelegramApiBotIdentifier"],
Method = telegramConfigDict["TelegramApiSendMessageMethod"],
Token = telegramConfigDict["TelegramApiBotToken"],
TargetChatId = telegramConfigDict["TelegramApiTargetChatId"]
};
return model;
}
}