casa-bot/src/CasaBot/CasaBotApp/Program.cs

95 lines
2.7 KiB
C#
Raw Normal View History

2025-02-13 19:10:25 -03:00
using AutoScan;
using AutoScan.Options;
2025-02-13 19:10:25 -03:00
using CasaBotApp;
2025-02-12 19:15:20 -03:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2025-02-12 19:15:20 -03:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bots;
using Telegram.Bots.Extensions.Polling;
2025-02-12 19:15:20 -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();
var hostBuilder = new HostBuilder();
hostBuilder.ConfigureServices((_, services) =>
2025-02-12 19:15:20 -03:00
{
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] ";
});
});
2025-02-13 19:10:25 -03:00
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>()!);
services.AddAutoScan();
services.AddHttpClient();
});
2025-02-12 19:15:20 -03:00
var host = hostBuilder.Build();
2025-02-12 19:15:20 -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
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
autoScanApp.OnScanCompleted = async options =>
{
logger.LogInformation("Scan completed at {At}", DateTime.Now);
2025-02-19 12:54:09 -03:00
try
{
//list all the images in the detection folder
if (options.Scanner?.DetectionFolder is null)
return;
var images = Directory.GetFiles(options.Scanner.DetectionFolder , "*.jpg");
botHandler.Update($"Scan completed, found {images.Length} images");
botHandler.UpdatePhotos(images);
}catch(Exception ex)
{
logger.LogError(ex, "Error while sending message");
}
};
2025-02-12 19:15:20 -03:00
_ = host.RunAsync(cts.Token);
2025-02-12 19:15:20 -03:00
2025-02-13 19:10:25 -03:00
logger.LogInformation("Bot started");
logger.LogInformation("Press any key to stop the bot...");
2025-02-12 19:15:20 -03:00
Console.ReadLine();
await cts.CancelAsync(); // stop the bot
2025-02-12 19:15:20 -03:00
return;
2025-02-12 19:22:18 -03:00
2025-02-12 19:15:20 -03:00