using CasaBotApp; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; // See https://aka.ms/new-console-template for more information var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); IConfigurationRoot configuration = new ConfigurationBuilder() .AddJsonFile($"appsettings.json", true, true) .AddJsonFile($"appsettings.{environment}.json", true, true) .AddEnvironmentVariables() .Build(); var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(builder => { builder.AddConsole(); }); serviceCollection.AddSingleton(new BotConfiguration() { Token = configuration.GetValue("TelegramToken") ?? "" }); serviceCollection.AddSingleton(); var serviceProvider = serviceCollection.BuildServiceProvider(); var botHandler = serviceProvider.GetService(); if (botHandler is null) { Console.WriteLine("Bot is not initialized"); return; } using var cts = new CancellationTokenSource(); botHandler.Start(cts.Token); _ = SendMessageToSubscribers(cts.Token); Console.WriteLine($"bot is running... Press Enter to terminate"); Console.ReadLine(); cts.Cancel(); // stop the bot return; async Task SendMessageToSubscribers(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken); botHandler.Update("Hello from CasaBot! at " + DateTime.Now); } }