refactor: restructure handlebot and add docs

This commit is contained in:
Guillermo Marcel 2025-03-29 11:07:23 -03:00
parent 13d9bef207
commit 35b42cc819
2 changed files with 63 additions and 16 deletions

View File

@ -73,7 +73,11 @@ public class BotHandler : IUpdateHandler
_logger.LogInformation("User {Id} subscribed to receive messages", id);
}
public async Task Update(string message)
/// <summary>
/// Send text message to all subscribers
/// </summary>
/// <param name="message"></param>
public async Task UpdateText(string message)
{
if (_subscribers.Count == 0)
{
@ -99,7 +103,10 @@ public class BotHandler : IUpdateHandler
}
/// <summary>
/// Send photo to all subscribers
/// </summary>
/// <param name="path"></param>
public async Task UpdatePhoto(string path)
{
@ -116,20 +123,12 @@ public class BotHandler : IUpdateHandler
}
}
public async Task SendVideo(long chatId, string path)
{
_logger.LogInformation("Sending video to {ChatId}", chatId);
await using var stream = File.OpenRead(path);
var send = new SendVideoFile(chatId.ToString(), stream);
var response = await _bot.HandleAsync(send);
if (!response.Ok)
{
_logger.LogError("Error sending video.");
}
_logger.LogInformation("Video sent to {ChatId}", chatId);
stream.Close();
}
/// <summary>
/// Send photos to all subscribers
/// </summary>
/// <param name="paths"></param>
public async Task UpdatePhotos(string[] paths)
{
if (_subscribers.Count == 0)
@ -205,6 +204,19 @@ public class BotHandler : IUpdateHandler
/// <param name="chatId"></param>
/// <param name="paths"></param>
public async Task SendPhotos(long chatId, string[] paths)
{
var groups = paths.Select((x, i) => (x, i))
.GroupBy(x => x.i / 10)
.Select(x => x.Select(y => y.x).ToArray())
.ToArray();
foreach(var group in groups)
{
await SendPhotosInt(chatId, group);
}
}
private async Task SendPhotosInt(long chatId, string[] paths)
{
var streams = paths.Select(File.OpenRead).ToArray();
var photos = streams.Select(stream => new PhotoFile(stream)
@ -226,6 +238,38 @@ public class BotHandler : IUpdateHandler
}
}
/// <summary>
/// Send Text Message to a specific chat
/// </summary>
/// <param name="chatId"></param>
/// <param name="textMessage"></param>
public async Task SendText(long chatId, string textMessage)
{
var response = await SndTxt(chatId, textMessage);
if (!response.Ok)
{
_logger.LogError("Error sending message to {ChatId}", chatId);
}
}
/// <summary>
/// Send single video to a specific chat
/// </summary>
/// <param name="chatId"></param>
/// <param name="path"></param>
public async Task SendVideo(long chatId, string path)
{
_logger.LogInformation("Sending video to {ChatId}", chatId);
await using var stream = File.OpenRead(path);
var send = new SendVideoFile(chatId.ToString(), stream);
var response = await _bot.HandleAsync(send);
if (!response.Ok)
{
_logger.LogError("Error sending video.");
}
_logger.LogInformation("Video sent to {ChatId}", chatId);
stream.Close();
}
private async Task OnMessage(TextMessage msg)
@ -306,4 +350,5 @@ public class BotHandler : IUpdateHandler
}
}
}

View File

@ -33,5 +33,7 @@
"DetectionFolder": "./media/detections/",
"RunDry": false
}
}
},
"RemoveOriginalFiles": true,
"RemoveDetectionFiles": false
}