casa-bot/src/CasaBot/CasaBotApp/Program.cs

79 lines
2.3 KiB
C#
Raw Normal View History

2025-02-13 19:10:25 -03:00
using AutoScan;
using AutoScan.Options;
2025-02-13 19:10:25 -03:00
using CasaBotApp;
2025-03-26 15:41:39 -03:00
using CasaBotApp.Extensions;
2025-02-12 19:15:20 -03:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2025-02-12 19:15:20 -03:00
using Microsoft.Extensions.Logging;
using Telegram.Bots;
using Telegram.Bots.Extensions.Polling;
2025-03-26 15:41:39 -03:00
using Telegram.Bots.Http;
2025-02-12 19:15:20 -03:00
var environment = Environment.GetEnvironmentVariable("CASABOT_ENVIRONMENT");
2025-02-12 19:15:20 -03:00
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environment}.json", true, true)
.AddEnvironmentVariables()
.Build();
var hostBuilder = new HostBuilder();
hostBuilder.ConfigureServices((_, services) =>
2025-02-12 19:15:20 -03:00
{
services.AddSingleton(configuration);
2025-03-26 15:41:39 -03:00
services.AddLogging(configuration);
services.Configure<TelegramOptions>(configuration.GetSection("Telegram"));
services.Configure<AutoScanOptions>(configuration.GetSection("AutoScan"));
services.Configure<ShinobiOptions>(configuration.GetSection("Shinobi"));
services.AddSingleton<BotHandler>();
var token = configuration["Telegram:BotToken"] ?? "";
services.AddBotClient(token);
services.AddPolling<BotHandler>();
services.AddSingleton<IUpdateHandler>(sp => sp.GetService<BotHandler>()!);
2025-03-26 15:41:39 -03:00
// To get notifications when a retry is performed
services.AddAutoScan();
2025-03-26 15:41:39 -03:00
services.AddPolicyRegistry().Add("RetryPolicy", RetryPolicyExtension.GetRetryPolicy());
services.AddHttpClient<IBotClient, BotClient>().AddPolicyHandler(RetryPolicyExtension.GetRetryPolicy());
services.Configure<HostOptions>(hostOptions =>
{
hostOptions.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
});
});
2025-02-12 19:15:20 -03:00
var host = hostBuilder.Build();
2025-02-12 19:15:20 -03:00
var logger = host.Services.GetService<ILogger<Program>>()!;
var botHandler = host.Services.GetService<BotHandler>()!;
var autoScanApp = host.Services.GetService<AutoScanApp>()!;
2025-02-12 19:15:20 -03:00
2025-03-26 15:41:39 -03:00
CommandRegister.RegisterCommands(botHandler, autoScanApp);
2025-02-12 19:15:20 -03:00
using var cts = new CancellationTokenSource();
2025-02-17 22:20:51 -03:00
_ = autoScanApp.Run(cts.Token);
botHandler.Start(cts.Token);
2025-02-12 19:15:20 -03:00
2025-03-26 15:41:39 -03:00
CommandRegister.UpdateOnScanCompleted(botHandler, autoScanApp, logger);
2025-02-12 19:15:20 -03:00
2025-02-13 19:10:25 -03:00
logger.LogInformation("Bot started");
2025-03-26 15:41:39 -03:00
await host.RunAsync(cts.Token);
2025-03-26 15:41:39 -03:00
await cts.CancelAsync(); // stop the bot
2025-02-12 19:15:20 -03:00
2025-02-12 19:22:18 -03:00
2025-02-12 19:15:20 -03:00