using AutoScan; using AutoScan.Options; using CasaBotApp; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Telegram.Bots; using Telegram.Bots.Extensions.Polling; 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(builder => { builder.AddConfiguration(configuration.GetSection("Logging")); //add time to logs builder.AddSimpleConsole(options => { options.IncludeScopes = true; options.SingleLine = true; options.TimestampFormat = "[HH:mm:ss] "; }); }); services.Configure(configuration.GetSection("Telegram")); services.Configure(configuration.GetSection("AutoScan")); services.Configure(configuration.GetSection("Shinobi")); services.AddSingleton(); var token = configuration["Telegram:BotToken"] ?? ""; services.AddBotClient(token); services.AddPolling(); services.AddSingleton(sp => sp.GetService()!); services.AddAutoScan(); services.AddHttpClient(); }); var host = hostBuilder.Build(); var logger = host.Services.GetService>()!; var botHandler = host.Services.GetService()!; var autoScanApp = host.Services.GetService()!; using var cts = new CancellationTokenSource(); _ = autoScanApp.Run(cts.Token); botHandler.Start(cts.Token); autoScanApp.OnScanCompleted = async options => { logger.LogInformation("Scan completed at {At}", DateTime.Now); //list all the images in the detection folder if (options.Scanner?.DetectionFolder is null) return; var images = Directory.GetFiles(options.Scanner.DetectionFolder , "*.jpg"); botHandler.UpdatePhotos(images); }; _ = host.RunAsync(cts.Token); logger.LogInformation("Bot started"); logger.LogInformation("Press any key to stop the bot..."); Console.ReadLine(); await cts.CancelAsync(); // stop the bot return;