Compare commits
2 Commits
0fd63ce192
...
ba1021c5e6
Author | SHA1 | Date | |
---|---|---|---|
ba1021c5e6 | |||
2b33a596d6 |
12
src/CasaBot/CasaBotApp/BotCommand.cs
Normal file
12
src/CasaBot/CasaBotApp/BotCommand.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.Net.Mime;
|
||||||
|
using Telegram.Bots.Types;
|
||||||
|
|
||||||
|
namespace CasaBotApp;
|
||||||
|
|
||||||
|
public class BotCommand
|
||||||
|
{
|
||||||
|
public string Command { get; set; } = "/example";
|
||||||
|
public string Description { get; set; } = "Start the bot";
|
||||||
|
public Func<Message, string, Task<Response<TextMessage>>> Responder { get; set; } = (_, _) => Task.FromResult<Response<TextMessage>>(null);
|
||||||
|
public Func<TextMessage, BotCommand, Task>? Action { get; set; }
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using System.Text;
|
||||||
using System.Xml.Schema;
|
using System.Xml.Schema;
|
||||||
using Telegram.Bots;
|
using Telegram.Bots;
|
||||||
using Telegram.Bots.Extensions.Polling;
|
using Telegram.Bots.Extensions.Polling;
|
||||||
@ -14,6 +15,7 @@ public class BotHandler : IUpdateHandler
|
|||||||
private readonly ILogger<BotHandler> _logger;
|
private readonly ILogger<BotHandler> _logger;
|
||||||
private readonly TelegramOptions _telegramOptions;
|
private readonly TelegramOptions _telegramOptions;
|
||||||
private readonly List<Chat> _subscribers = [];
|
private readonly List<Chat> _subscribers = [];
|
||||||
|
private readonly Dictionary<string, BotCommand> _commands;
|
||||||
|
|
||||||
private readonly IBotClient _bot;
|
private readonly IBotClient _bot;
|
||||||
|
|
||||||
@ -22,6 +24,27 @@ public class BotHandler : IUpdateHandler
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
_telegramOptions = telegramConfiguration.Value;
|
_telegramOptions = telegramConfiguration.Value;
|
||||||
_bot = bot;
|
_bot = bot;
|
||||||
|
_commands = [];
|
||||||
|
RegisterCommand(new()
|
||||||
|
{
|
||||||
|
Command = "/register",
|
||||||
|
Description = "Register to receive messages on every update",
|
||||||
|
Action = RegisterUser,
|
||||||
|
Responder = Respond
|
||||||
|
});
|
||||||
|
RegisterCommand(new()
|
||||||
|
{
|
||||||
|
Command = "/photo",
|
||||||
|
Description = "Get a photo",
|
||||||
|
Action = SendImageTest,
|
||||||
|
Responder = Respond
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterCommand(BotCommand command)
|
||||||
|
{
|
||||||
|
command.Responder = Respond;
|
||||||
|
_commands[command.Command] = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -149,11 +172,11 @@ public class BotHandler : IUpdateHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendImageTest(long id)
|
private async Task SendImageTest(TextMessage msg, BotCommand _)
|
||||||
{
|
{
|
||||||
await using var stream = File.OpenRead(@"C:\Users\GuillermoMarcel\Pictures\prueba.jpeg");
|
await using var stream = File.OpenRead(@"C:\Users\GuillermoMarcel\Pictures\prueba.jpeg");
|
||||||
|
|
||||||
var send = new SendPhotoFile(id.ToString(), stream);
|
var send = new SendPhotoFile(msg.Chat.Id.ToString(), stream);
|
||||||
await _bot.HandleAsync(send);
|
await _bot.HandleAsync(send);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,38 +184,43 @@ public class BotHandler : IUpdateHandler
|
|||||||
|
|
||||||
private async Task OnMessage(TextMessage msg)
|
private async Task OnMessage(TextMessage msg)
|
||||||
{
|
{
|
||||||
|
if(!_commands.TryGetValue(msg.Text, out var command))
|
||||||
switch (msg.Text)
|
|
||||||
{
|
{
|
||||||
case "/register":
|
await SendHelp(msg);
|
||||||
if (_subscribers.Any(c => c.Id == msg.Chat.Id))
|
return;
|
||||||
{
|
|
||||||
await Respond(msg, "You are already registered to receive messages");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_subscribers.Add(msg.Chat);
|
|
||||||
_logger.LogInformation("User {User} ({id}) registered to receive messages", msg.Chat.FirstName, msg.Chat.Id);
|
|
||||||
await Respond(msg, "You are registered to receive messages every minute");
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
case "/photo":
|
|
||||||
await SendImageTest(msg.Chat.Id);
|
|
||||||
return;
|
|
||||||
|
|
||||||
case "/soyandre":
|
|
||||||
await Respond(msg, "Hola vida, te amo mucho ❤️");
|
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
|
||||||
_logger.LogInformation("Received '{Text}' in {Chat}", msg.Text, msg.Chat);
|
|
||||||
|
|
||||||
const string commands =
|
|
||||||
"Commands: \n/help to show the commands \n/register to get messages every minute \n/photo to get a photo \n/soyandre por si sos andre";
|
|
||||||
await Respond(msg, commands);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (command?.Action is null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Command {Command} has no action", msg.Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
await command!.Action!(msg, command);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SendHelp(TextMessage msg)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Received '{Text}' in {Chat}", msg.Text, msg.Chat);
|
||||||
|
var message = new StringBuilder();
|
||||||
|
message.AppendLine("Commands:");
|
||||||
|
foreach (var command in _commands.Values)
|
||||||
|
{
|
||||||
|
message.AppendLine($"{command.Command} - {command.Description}");
|
||||||
|
}
|
||||||
|
await Respond(msg, message.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task RegisterUser(TextMessage msg, BotCommand _)
|
||||||
|
{
|
||||||
|
if (_subscribers.Any(c => c.Id == msg.Chat.Id))
|
||||||
|
{
|
||||||
|
await Respond(msg, "You are already registered to receive messages");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_subscribers.Add(msg.Chat);
|
||||||
|
_logger.LogInformation("User {User} ({id}) registered to receive messages", msg.Chat.FirstName, msg.Chat.Id);
|
||||||
|
await Respond(msg, "You are registered to receive messages every minute");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
using AutoScan;
|
using AutoScan;
|
||||||
using AutoScan.Options;
|
using AutoScan.Options;
|
||||||
using CasaBotApp;
|
using CasaBotApp;
|
||||||
|
using CasaBotApp.Extensions;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Telegram.Bots;
|
using Telegram.Bots;
|
||||||
using Telegram.Bots.Extensions.Polling;
|
using Telegram.Bots.Extensions.Polling;
|
||||||
|
using Telegram.Bots.Http;
|
||||||
|
|
||||||
var environment = Environment.GetEnvironmentVariable("CASABOT_ENVIRONMENT");
|
var environment = Environment.GetEnvironmentVariable("CASABOT_ENVIRONMENT");
|
||||||
IConfigurationRoot configuration = new ConfigurationBuilder()
|
IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||||
@ -20,18 +21,9 @@ var hostBuilder = new HostBuilder();
|
|||||||
hostBuilder.ConfigureServices((_, services) =>
|
hostBuilder.ConfigureServices((_, services) =>
|
||||||
{
|
{
|
||||||
services.AddSingleton(configuration);
|
services.AddSingleton(configuration);
|
||||||
services.AddLogging(builder =>
|
|
||||||
{
|
services.AddLogging(configuration);
|
||||||
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<TelegramOptions>(configuration.GetSection("Telegram"));
|
services.Configure<TelegramOptions>(configuration.GetSection("Telegram"));
|
||||||
services.Configure<AutoScanOptions>(configuration.GetSection("AutoScan"));
|
services.Configure<AutoScanOptions>(configuration.GetSection("AutoScan"));
|
||||||
services.Configure<ShinobiOptions>(configuration.GetSection("Shinobi"));
|
services.Configure<ShinobiOptions>(configuration.GetSection("Shinobi"));
|
||||||
@ -43,9 +35,20 @@ hostBuilder.ConfigureServices((_, services) =>
|
|||||||
services.AddPolling<BotHandler>();
|
services.AddPolling<BotHandler>();
|
||||||
services.AddSingleton<IUpdateHandler>(sp => sp.GetService<BotHandler>()!);
|
services.AddSingleton<IUpdateHandler>(sp => sp.GetService<BotHandler>()!);
|
||||||
|
|
||||||
|
|
||||||
|
// To get notifications when a retry is performed
|
||||||
|
|
||||||
services.AddAutoScan();
|
services.AddAutoScan();
|
||||||
services.AddHttpClient();
|
|
||||||
|
services.AddPolicyRegistry().Add("RetryPolicy", RetryPolicyExtension.GetRetryPolicy());
|
||||||
|
services.AddHttpClient<IBotClient, BotClient>().AddPolicyHandler(RetryPolicyExtension.GetRetryPolicy());
|
||||||
|
|
||||||
|
|
||||||
|
services.Configure<HostOptions>(hostOptions =>
|
||||||
|
{
|
||||||
|
hostOptions.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -56,39 +59,20 @@ var logger = host.Services.GetService<ILogger<Program>>()!;
|
|||||||
var botHandler = host.Services.GetService<BotHandler>()!;
|
var botHandler = host.Services.GetService<BotHandler>()!;
|
||||||
var autoScanApp = host.Services.GetService<AutoScanApp>()!;
|
var autoScanApp = host.Services.GetService<AutoScanApp>()!;
|
||||||
|
|
||||||
|
CommandRegister.RegisterCommands(botHandler, autoScanApp);
|
||||||
|
|
||||||
using var cts = new CancellationTokenSource();
|
using var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
_ = autoScanApp.Run(cts.Token);
|
_ = autoScanApp.Run(cts.Token);
|
||||||
botHandler.Start(cts.Token);
|
botHandler.Start(cts.Token);
|
||||||
|
|
||||||
autoScanApp.OnScanCompleted = async options =>
|
CommandRegister.UpdateOnScanCompleted(botHandler, autoScanApp, logger);
|
||||||
{
|
|
||||||
logger.LogInformation("Scan completed at {At}", DateTime.Now);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
_ = host.RunAsync(cts.Token);
|
|
||||||
|
|
||||||
logger.LogInformation("Bot started");
|
logger.LogInformation("Bot started");
|
||||||
logger.LogInformation("Press any key to stop the bot...");
|
await host.RunAsync(cts.Token);
|
||||||
Console.ReadLine();
|
|
||||||
await cts.CancelAsync(); // stop the bot
|
await cts.CancelAsync(); // stop the bot
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
"RunDry": false,
|
"RunDry": false,
|
||||||
"At": "07:00",
|
"At": "07:00",
|
||||||
"FromDayBefore": false,
|
"FromDayBefore": false,
|
||||||
"From": "01:00",
|
"From": "03:00",
|
||||||
"To": "05:00",
|
"To": "05:00",
|
||||||
"MaxAmount": 0,
|
"MaxAmount": 0,
|
||||||
"MediaFolder": "./media/originals/",
|
"MediaFolder": "./media/originals/",
|
||||||
@ -33,6 +33,8 @@
|
|||||||
"ConfigFile": "./dvr-scanner/dvr-scan.cfg",
|
"ConfigFile": "./dvr-scanner/dvr-scan.cfg",
|
||||||
"DetectionFolder": "./media/detections/",
|
"DetectionFolder": "./media/detections/",
|
||||||
"RunDry": false
|
"RunDry": false
|
||||||
}
|
},
|
||||||
|
"RemoveOriginalFiles": false,
|
||||||
|
"RemoveDetectionFiles": false
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user