using AutoScan; using AutoScan.Options; using CasaBotApp; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; 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); _ = SendMessageToSubscribers(cts.Token); _ = 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; async Task SendMessageToSubscribers(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken); logger.LogInformation("Sending message to subscribers"); await botHandler.Update($"Hello from CasaBot! at {DateTime.Now}"); } }