79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using AutoScan;
|
|
using AutoScan.Options;
|
|
using CasaBotApp;
|
|
using CasaBotApp.Extensions;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Telegram.Bots;
|
|
using Telegram.Bots.Extensions.Polling;
|
|
using Telegram.Bots.Http;
|
|
|
|
var environment = Environment.GetEnvironmentVariable("CASABOT_ENVIRONMENT");
|
|
IConfigurationRoot configuration = new ConfigurationBuilder()
|
|
.AddJsonFile($"appsettings.json", true, true)
|
|
.AddJsonFile($"appsettings.{environment}.json", true, true)
|
|
.AddEnvironmentVariables()
|
|
.Build();
|
|
|
|
var hostBuilder = new HostBuilder();
|
|
hostBuilder.ConfigureServices((_, services) =>
|
|
{
|
|
services.AddSingleton(configuration);
|
|
|
|
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>()!);
|
|
|
|
|
|
// To get notifications when a retry is performed
|
|
|
|
services.AddAutoScan();
|
|
|
|
services.AddPolicyRegistry().Add("RetryPolicy", RetryPolicyExtension.GetRetryPolicy());
|
|
services.AddHttpClient<IBotClient, BotClient>().AddPolicyHandler(RetryPolicyExtension.GetRetryPolicy());
|
|
|
|
|
|
services.Configure<HostOptions>(hostOptions =>
|
|
{
|
|
hostOptions.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
|
|
});
|
|
|
|
});
|
|
|
|
|
|
var host = hostBuilder.Build();
|
|
|
|
|
|
var logger = host.Services.GetService<ILogger<Program>>()!;
|
|
var botHandler = host.Services.GetService<BotHandler>()!;
|
|
var autoScanApp = host.Services.GetService<AutoScanApp>()!;
|
|
|
|
CommandRegister.RegisterCommands(botHandler, autoScanApp);
|
|
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
_ = autoScanApp.Run(cts.Token);
|
|
botHandler.Start(cts.Token);
|
|
|
|
CommandRegister.UpdateOnScanCompleted(botHandler, autoScanApp, logger);
|
|
|
|
logger.LogInformation("Bot started");
|
|
await host.RunAsync(cts.Token);
|
|
|
|
await cts.CancelAsync(); // stop the bot
|
|
|
|
|
|
|
|
|