2025-02-13 19:10:25 -03:00
|
|
|
|
using AutoScan;
|
2025-02-15 15:55:29 -03:00
|
|
|
|
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;
|
2025-02-18 13:06:49 -03:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-02-12 19:15:20 -03:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-18 13:06:49 -03:00
|
|
|
|
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
|
|
|
|
|
2025-02-18 13:06:49 -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();
|
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
var hostBuilder = new HostBuilder();
|
|
|
|
|
hostBuilder.ConfigureServices((_, services) =>
|
2025-02-12 19:15:20 -03:00
|
|
|
|
{
|
2025-02-18 13:06:49 -03:00
|
|
|
|
services.AddSingleton(configuration);
|
2025-03-26 15:41:39 -03:00
|
|
|
|
|
|
|
|
|
services.AddLogging(configuration);
|
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
services.Configure<TelegramOptions>(configuration.GetSection("Telegram"));
|
|
|
|
|
services.Configure<AutoScanOptions>(configuration.GetSection("AutoScan"));
|
|
|
|
|
services.Configure<ShinobiOptions>(configuration.GetSection("Shinobi"));
|
2025-02-14 20:32:04 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
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
|
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
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-18 13:06:49 -03:00
|
|
|
|
});
|
2025-02-12 19:15:20 -03:00
|
|
|
|
|
2025-02-14 20:32:04 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
var host = hostBuilder.Build();
|
2025-02-12 19:15:20 -03:00
|
|
|
|
|
|
|
|
|
|
2025-02-18 13:06:49 -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-02-14 20:32:04 -03:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|