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

196 lines
5.8 KiB
C#
Raw Normal View History

2025-02-12 19:15:20 -03:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bots;
using Telegram.Bots.Extensions.Polling;
using Telegram.Bots.Requests.Usernames;
using Telegram.Bots.Types;
using File = System.IO.File;
2025-02-12 19:15:20 -03:00
namespace CasaBotApp;
public class BotHandler : IUpdateHandler
2025-02-12 19:15:20 -03:00
{
private readonly ILogger<BotHandler> _logger;
private readonly TelegramOptions _telegramOptions;
private readonly List<Chat> _subscribers = [];
2025-02-12 19:15:20 -03:00
private readonly IBotClient _bot;
2025-02-12 19:15:20 -03:00
public BotHandler(IBotClient bot, IOptions<TelegramOptions> telegramConfiguration, ILogger<BotHandler> logger)
2025-02-12 19:15:20 -03:00
{
_logger = logger;
_telegramOptions = telegramConfiguration.Value;
_bot = bot;
2025-02-12 19:15:20 -03:00
}
public void Start(CancellationToken cancellationToken)
{
foreach (var subs in _telegramOptions.SubscribedChatIds)
2025-02-17 22:20:51 -03:00
{
Subscribe(subs);
2025-02-17 22:20:51 -03:00
}
2025-02-12 19:15:20 -03:00
}
public void Subscribe(long id)
2025-02-12 19:15:20 -03:00
{
if (_subscribers.Any(x => x.Id == id))
2025-02-12 19:15:20 -03:00
{
_logger.LogWarning("User {Id} is already subscribed", id);
2025-02-12 19:15:20 -03:00
return;
}
_subscribers.Add(new Chat()
{
Id = id
});
_logger.LogInformation("User {Id} subscribed to receive messages", id);
}
public async Task Update(string message)
{
2025-02-12 19:15:20 -03:00
if (_subscribers.Count == 0)
{
_logger.LogWarning("No subscribers to send message to");
return;
}
var replacement = new List<(Chat from, Chat to)>();
2025-02-12 19:15:20 -03:00
foreach (var subscriber in _subscribers)
{
var response = await SndTxt(subscriber, message);
if (subscriber.FirstName is null)
{
replacement.Add((subscriber, response.Result.Chat));
}
2025-02-12 19:15:20 -03:00
}
foreach (var rep in replacement)
{
_subscribers.Remove(rep.from);
_subscribers.Add(rep.to);
}
}
public async Task UpdatePhoto(string path)
{
if (_subscribers.Count == 0)
{
_logger.LogWarning("No subscribers to send message to");
return;
}
foreach (var subscriber in _subscribers)
{
await using var stream = File.OpenRead(path);
await SndPhoto(subscriber, stream);
}
}
public async Task UpdatePhotos(string[] paths)
{
if (_subscribers.Count == 0)
{
_logger.LogWarning("No subscribers to send message to");
return;
}
var streams = paths.Select(File.OpenRead).ToList();
var photos = streams.Select(stream => new PhotoFile(stream)).Cast<IGroupableMedia>().ToList();
foreach (var subscriber in _subscribers)
{
var request = new SendMediaGroup(subscriber.Id.ToString(), photos);
await _bot.HandleAsync(request);
}
foreach (var stream in streams)
{
stream.Close();
await stream.DisposeAsync();
}
}
2025-02-12 19:15:20 -03:00
private async Task SendImageTest(long id)
{
await using var stream = File.OpenRead(@"C:\Users\GuillermoMarcel\Pictures\prueba.jpeg");
2025-02-12 19:22:18 -03:00
var send = new SendPhotoFile(id.ToString(), stream);
await _bot.HandleAsync(send);
2025-02-12 19:15:20 -03:00
}
2025-02-12 19:22:18 -03:00
private async Task OnMessage(TextMessage msg)
2025-02-12 19:15:20 -03:00
{
2025-02-12 19:15:20 -03:00
switch (msg.Text)
{
case "/register":
if (_subscribers.Any(c => c.Id == msg.Chat.Id))
2025-02-12 19:15:20 -03:00
{
await Respond(msg, "You are already registered to receive messages");
2025-02-12 19:15:20 -03:00
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");
2025-02-12 19:15:20 -03:00
return;
case "/photo":
await SendImageTest(msg.Chat.Id);
return;
case "/soyandre":
await Respond(msg, "Hola vida, te amo mucho ❤️");
return;
2025-02-12 19:15:20 -03:00
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);
2025-02-12 19:15:20 -03:00
break;
}
}
private Task<Response<TextMessage>> SndTxt(long id, string txt) => _bot.HandleAsync(new SendText(id.ToString(), txt));
private Task<Response<TextMessage>> SndTxt(Chat chat, string txt) => SndTxt(chat.Id, txt);
private Task<Response<TextMessage>> Respond(Message msg, string txt) => SndTxt(msg.Chat.Id, txt);
private Task<Response<PhotoMessage>> SndPhoto(Chat chat, FileStream path) =>
_bot.HandleAsync(new SendPhotoFile(chat.Id.ToString(), path));
public Task HandleAsync(IBotClient bot, Update update, CancellationToken cst)
2025-02-12 19:15:20 -03:00
{
try
2025-02-12 19:15:20 -03:00
{
return update switch
{
MessageUpdate u when u.Data is TextMessage message =>
OnMessage(message),
EditedMessageUpdate u when u.Data is TextMessage message =>
bot.HandleAsync(new SendText(message.Chat.Id.ToString(), message.Text)
{
ReplyToMessageId = message.Id
}, cst),
_ => Task.CompletedTask
};
}catch (Exception e)
2025-02-12 19:15:20 -03:00
{
_logger.LogError(e, "Error handling update");
return Task.CompletedTask;
}
2025-02-12 19:15:20 -03:00
}
2025-02-12 19:15:20 -03:00
}