From 37f987d244f956e8f4c8973b2d9bfe8e214d74b1 Mon Sep 17 00:00:00 2001 From: Guillermo Marcel Date: Wed, 12 Feb 2025 19:15:20 -0300 Subject: [PATCH] feat: initial version of working bot --- src/CasaBot/.dockerignore | 25 ++++ src/CasaBot/CasaBot.sln | 16 +++ src/CasaBot/CasaBotApp/BotConfiguration.cs | 6 + src/CasaBot/CasaBotApp/BotHandler.cs | 142 +++++++++++++++++++++ src/CasaBot/CasaBotApp/CasaBotApp.csproj | 21 +++ src/CasaBot/CasaBotApp/Dockerfile | 21 +++ src/CasaBot/CasaBotApp/Program.cs | 73 +++++++++++ 7 files changed, 304 insertions(+) create mode 100644 src/CasaBot/.dockerignore create mode 100644 src/CasaBot/CasaBot.sln create mode 100644 src/CasaBot/CasaBotApp/BotConfiguration.cs create mode 100644 src/CasaBot/CasaBotApp/BotHandler.cs create mode 100644 src/CasaBot/CasaBotApp/CasaBotApp.csproj create mode 100644 src/CasaBot/CasaBotApp/Dockerfile create mode 100644 src/CasaBot/CasaBotApp/Program.cs diff --git a/src/CasaBot/.dockerignore b/src/CasaBot/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/src/CasaBot/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/src/CasaBot/CasaBot.sln b/src/CasaBot/CasaBot.sln new file mode 100644 index 0000000..b81d86e --- /dev/null +++ b/src/CasaBot/CasaBot.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CasaBotApp", "CasaBotApp\CasaBotApp.csproj", "{FF1AF6E7-88E4-488B-B6FB-BDAC126DD94E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FF1AF6E7-88E4-488B-B6FB-BDAC126DD94E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF1AF6E7-88E4-488B-B6FB-BDAC126DD94E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF1AF6E7-88E4-488B-B6FB-BDAC126DD94E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF1AF6E7-88E4-488B-B6FB-BDAC126DD94E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/src/CasaBot/CasaBotApp/BotConfiguration.cs b/src/CasaBot/CasaBotApp/BotConfiguration.cs new file mode 100644 index 0000000..ae2b2ff --- /dev/null +++ b/src/CasaBot/CasaBotApp/BotConfiguration.cs @@ -0,0 +1,6 @@ +namespace CasaBotApp; + +public class BotConfiguration +{ + public required string Token { get; set; } +} \ No newline at end of file diff --git a/src/CasaBot/CasaBotApp/BotHandler.cs b/src/CasaBot/CasaBotApp/BotHandler.cs new file mode 100644 index 0000000..6b65e30 --- /dev/null +++ b/src/CasaBot/CasaBotApp/BotHandler.cs @@ -0,0 +1,142 @@ +using Microsoft.Extensions.Logging; +using Telegram.Bot; +using Telegram.Bot.Polling; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.ReplyMarkups; + +namespace CasaBotApp; + +public class BotHandler +{ + private readonly ILogger _logger; + private readonly BotConfiguration _botConfiguration; + private readonly List _subscribers = []; + + private TelegramBotClient? _bot; + + + public BotHandler(BotConfiguration botConfiguration, ILogger logger) + { + _logger = logger; + if (string.IsNullOrEmpty(botConfiguration.Token)) + { + _logger.LogError("Bot token is not provided"); + throw new ArgumentException("Bot token is required", nameof(botConfiguration)); + } + _botConfiguration = botConfiguration; + } + + + public void Start(CancellationToken cancellationToken) + { + _logger.LogInformation("Starting bot..."); + _bot = new TelegramBotClient(_botConfiguration.Token, cancellationToken: cancellationToken); + // var me = await _bot.GetMe(); + + _bot.OnError += OnError; + _bot.OnMessage += OnMessage; + _bot.OnUpdate += OnUpdate; + } + + public void Update(string message) + { + if (_bot is null) + { + _logger.LogWarning("Bot is not initialized yet"); + return; + } + + if (_subscribers.Count == 0) + { + _logger.LogWarning("No subscribers to send message to"); + return; + } + + foreach (var subscriber in _subscribers) + { + _bot.SendMessage(subscriber, message); + } + } + + private async Task SendImageTest(long id) + { + if (_bot is null) + { + _logger.LogWarning("Bot is not initialized yet"); + return; + } + + await using var stream = File.OpenRead(@"C:\Users\GuillermoMarcel\Pictures\prueba.jpeg"); + var inputFile = InputFile.FromStream(stream, "gorda.jpeg"); + // var message = await _bot.SendPhotoAsync(new ChatId(id), new InputOnlineFile(stream, "prueba.jpeg")); + // await _bot.SendDocument(new ChatId(id), document: inputFile, caption: "imagen de prueba"); + // await _bot.SendPhoto(new ChatId(id), + // "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/6.png", "Wild charizard"); + + await _bot.SendPhoto(new ChatId(id), inputFile, "mi gorda"); + } + + // method to handle errors in polling or in your OnMessage/OnUpdate code + private Task OnError(Exception exception, HandleErrorSource source) + { + _logger.LogError(exception, "Error in {Source}", source); + return Task.CompletedTask; + } + +// method that handle messages received by the bot: + private async Task OnMessage(Message msg, UpdateType type) + { + if (_bot is null) + { + _logger.LogWarning("Bot is not initialized yet"); + return; + } + + switch (msg.Text) + { + case "/start": + await _bot.SendMessage(msg.Chat, "Welcome! Pick one direction", + replyMarkup: new InlineKeyboardMarkup().AddButtons("Left", "Right")); + return; + + case "/register": + if (_subscribers.Any(s => s.Identifier == msg.Chat.Id)) + { + await _bot.SendMessage(msg.Chat, "You are already registered to receive messages"); + return; + } + + _subscribers.Add(msg.Chat); + _logger.LogInformation("User {User} registered to receive messages", msg.Chat); + await _bot.SendMessage(msg.Chat, "You are registered to receive messages every minute"); + return; + + case "/photo": + await SendImageTest(msg.Chat.Id); + return; + + default: + _logger.LogInformation("Received {Type} '{Text}' in {Chat}", type, msg.Text, msg.Chat); + await _bot.SendMessage(msg.Chat, "Commands: \n/start to start over \n/register to get messages every minute \n/photo to get a photo"); + // await _bot.SendMessage(msg.Chat, "Hola vida te amo mucho ❤️"); + break; + } + } + + // method that handle other types of updates received by the bot: + private async Task OnUpdate(Update update) + { + if (_bot is null) + { + _logger.LogWarning("Bot is not initialized yet"); + return; + } + + if (update is { CallbackQuery: { } query }) // non-null CallbackQuery + { + await _bot.AnswerCallbackQuery(query.Id, $"You picked {query.Data}"); + await _bot.SendMessage(query.Message!.Chat, $"User {query.From} clicked on {query.Data}"); + } + } +} \ No newline at end of file diff --git a/src/CasaBot/CasaBotApp/CasaBotApp.csproj b/src/CasaBot/CasaBotApp/CasaBotApp.csproj new file mode 100644 index 0000000..aac6563 --- /dev/null +++ b/src/CasaBot/CasaBotApp/CasaBotApp.csproj @@ -0,0 +1,21 @@ + + + + Exe + net9.0 + enable + enable + Linux + + + + + .dockerignore + + + + + + + + diff --git a/src/CasaBot/CasaBotApp/Dockerfile b/src/CasaBot/CasaBotApp/Dockerfile new file mode 100644 index 0000000..f1fd035 --- /dev/null +++ b/src/CasaBot/CasaBotApp/Dockerfile @@ -0,0 +1,21 @@ +FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base +USER $APP_UID +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["CasaBotApp/CasaBotApp.csproj", "CasaBotApp/"] +RUN dotnet restore "CasaBotApp/CasaBotApp.csproj" +COPY . . +WORKDIR "/src/CasaBotApp" +RUN dotnet build "CasaBotApp.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "CasaBotApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "CasaBotApp.dll"] diff --git a/src/CasaBot/CasaBotApp/Program.cs b/src/CasaBot/CasaBotApp/Program.cs new file mode 100644 index 0000000..cc95872 --- /dev/null +++ b/src/CasaBot/CasaBotApp/Program.cs @@ -0,0 +1,73 @@ +using CasaBotApp; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +// See https://aka.ms/new-console-template for more information + +var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); +IConfigurationRoot configuration = new ConfigurationBuilder() + .AddJsonFile($"appsettings.json", true, true) + .AddJsonFile($"appsettings.{environment}.json", true, true) + .AddEnvironmentVariables() + .Build(); + +var serviceCollection = new ServiceCollection(); + +serviceCollection.AddLogging(builder => +{ + builder.AddConsole(); +}); +serviceCollection.AddSingleton(new BotConfiguration() +{ + Token = configuration.GetValue("TelegramToken") ?? "" +}); +serviceCollection.AddSingleton(); + +var serviceProvider = serviceCollection.BuildServiceProvider(); + + + +// var url = "https://api.telegram.org/bot/getMe"; + +// var token = ; + +var botHandler = serviceProvider.GetService(); +if (botHandler is null) +{ + Console.WriteLine("Bot is not initialized"); + return; +} + +using var cts = new CancellationTokenSource(); + +botHandler.Start(cts.Token); + +// call the bot handler to send a message every minute to the subscribers +_ = SendMessageToSubscribers(cts.Token); + +// var bot = new TelegramBotClient(token, cancellationToken: cts.Token); +// var me = await bot.GetMe(); +// +// bot.OnError += OnError; +// bot.OnMessage += OnMessage; +// bot.OnUpdate += OnUpdate; +// + +Console.WriteLine($"bot is running... Press Enter to terminate"); +Console.ReadLine(); +cts.Cancel(); // stop the bot +return; + +//async method to send a message to the subscribers every two minutes with the current time +async Task SendMessageToSubscribers(CancellationToken cancellationToken) +{ + while (!cancellationToken.IsCancellationRequested) + { + await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken); + botHandler.Update("Hello from CasaBot! at " + DateTime.Now); + } +} + + +