2025-02-13 19:10:25 -03:00
|
|
|
|
using AutoScan;
|
2025-02-15 15:55:29 -03:00
|
|
|
|
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;
|
2025-02-18 13:06:49 -03:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-02-12 19:15:20 -03:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-18 19:21:19 -03:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2025-02-18 13:06:49 -03:00
|
|
|
|
using Telegram.Bots;
|
|
|
|
|
using Telegram.Bots.Extensions.Polling;
|
2025-02-12 19:15:20 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -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();
|
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
var hostBuilder = new HostBuilder();
|
|
|
|
|
hostBuilder.ConfigureServices((_, services) =>
|
2025-02-12 19:15:20 -03:00
|
|
|
|
{
|
2025-02-18 13:06:49 -03:00
|
|
|
|
services.AddSingleton(configuration);
|
|
|
|
|
services.AddLogging(builder =>
|
2025-02-14 20:32:04 -03:00
|
|
|
|
{
|
2025-02-18 13:06:49 -03:00
|
|
|
|
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-14 20:32:04 -03:00
|
|
|
|
});
|
2025-02-13 19:10:25 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
services.Configure<TelegramOptions>(configuration.GetSection("Telegram"));
|
|
|
|
|
services.Configure<AutoScanOptions>(configuration.GetSection("AutoScan"));
|
|
|
|
|
services.Configure<ShinobiOptions>(configuration.GetSection("Shinobi"));
|
2025-02-14 20:32:04 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
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
|
|
|
|
|
2025-02-14 20:32:04 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -03:00
|
|
|
|
var host = hostBuilder.Build();
|
2025-02-12 19:15:20 -03:00
|
|
|
|
|
|
|
|
|
|
2025-02-18 13:06:49 -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
|
|
|
|
|
2025-02-18 19:21:19 -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-18 19:21:19 -03:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-12 19:15:20 -03:00
|
|
|
|
|
2025-02-18 13:06:49 -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();
|
2025-02-18 13:06:49 -03:00
|
|
|
|
await cts.CancelAsync(); // stop the bot
|
2025-02-14 20:32:04 -03:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|