diff --git a/.gitea/workflows/gitea-ci.yml b/.gitea/workflows/gitea-ci.yml old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml old mode 100644 new mode 100755 diff --git a/Models/Post.cs b/Models/Post.cs old mode 100644 new mode 100755 diff --git a/Program.cs b/Program.cs old mode 100644 new mode 100755 index 16c3471..013403b --- a/Program.cs +++ b/Program.cs @@ -1,7 +1,9 @@ -using Microsoft.Extensions.Logging; +using CatsOfMastodonBot.Services; +using Microsoft.Extensions.Logging; using mstdnCats.Services; using Telegram.Bot; using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; public class MastodonBot { @@ -19,7 +21,8 @@ public class MastodonBot }); var logger = loggerFactory.CreateLogger(); - if(!CheckEnv.IsValid()){ + if (!CheckEnv.IsValid()) + { logger.LogCritical("Error reading envinonment variables, either some values are missing or no .env file was found"); throw new Exception("Error reading envinonment variables, either some values are missing or no .env file was found"); } @@ -34,31 +37,41 @@ public class MastodonBot // Setup bot - var bot = new TelegramBotClient(DotNetEnv.Env.GetString("BOT_TOKEN")); + var bot = new TelegramBotClient(DotNetEnv.Env.GetString("BOT_TOKEN")); - var me = await bot.GetMeAsync(); - await bot.DropPendingUpdatesAsync(); - bot.OnUpdate += OnUpdate; + var me = await bot.GetMeAsync(); + await bot.DropPendingUpdatesAsync(); + bot.OnUpdate += OnUpdate; + bot.OnMessage += OnMessage; - logger.LogInformation($"Bot is running as {me.FirstName}."); + logger.LogInformation($"Bot is running as {me.FirstName}."); - - - // Handle bot updates - async Task OnUpdate(Update update) + // Handle bot updates + async Task OnUpdate(Update update) + { + switch (update) { - switch (update) - { - case { CallbackQuery: { } callbackQuery }: await HandlePostAction.HandleCallbackQuery(callbackQuery, db, bot, logger); break; - default: logger.LogInformation($"Received unhandled update {update.Type}"); break; - }; + case { CallbackQuery: { } callbackQuery }: { + if(callbackQuery.Data == "new_random") await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger); + else await HandlePostAction.HandleCallbackQuery(callbackQuery, db, bot, logger); break; + + } + default: logger.LogInformation($"Received unhandled update {update.Type}"); break; + }; + } + + // Handle bot messages + async Task OnMessage(Message message, UpdateType type) + { + if (message.Text == "/start") + { + await HandleStartMessage.HandleStartMessageAsync(message,bot, db, logger); } + } - // Set a timer to fetch and process posts every 15 minutes - _timer = new Timer(async _ => await RunCheck.runAsync(db, bot, DotNetEnv.Env.GetString("TAG"), logger, DotNetEnv.Env.GetString("CUSTOM_INSTANCE")), null, TimeSpan.Zero, TimeSpan.FromMinutes(15)); - Console.ReadLine(); - - + // Set a timer to fetch and process posts every 15 minutes + _timer = new Timer(async _ => await RunCheck.runAsync(db, bot, DotNetEnv.Env.GetString("TAG"), logger, DotNetEnv.Env.GetString("CUSTOM_INSTANCE")), null, TimeSpan.Zero, TimeSpan.FromMinutes(15)); + Console.ReadLine(); } } diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/Services/CheckEnv.cs b/Services/CheckEnv.cs old mode 100644 new mode 100755 diff --git a/Services/DbInitializer.cs b/Services/DbInitializer.cs old mode 100644 new mode 100755 diff --git a/Services/HandlePostAction.cs b/Services/HandlePostAction.cs old mode 100644 new mode 100755 diff --git a/Services/HandleStartMessage.cs b/Services/HandleStartMessage.cs new file mode 100755 index 0000000..cb8b331 --- /dev/null +++ b/Services/HandleStartMessage.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using JsonFlatFileDataStore; +using Microsoft.Extensions.Logging; +using mstdnCats.Models; +using Telegram.Bot; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.ReplyMarkups; + +namespace CatsOfMastodonBot.Services +{ + public class HandleStartMessage + { + public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection _db, ILogger? logger) + { + // choose all media attachments that are approved + var mediaAttachmentsToSelect = _db.AsQueryable().Where(p => p.MediaAttachments.Any(m => m.Approved == true)).ToList(); + // select random approved media attachment + var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)]; + // send media attachment + await _bot.SendPhotoAsync(message.Chat.Id, selectedMediaAttachment.MediaAttachments.FirstOrDefault(m => m.Approved == true).Url, + caption: $"" + $"Here is your cat!" + "
" + "View on Mastodon 🐈" + "
", parseMode: ParseMode.Html + , replyMarkup: new InlineKeyboardMarkup().AddButton(InlineKeyboardButton.WithUrl("Join channel 😺", selectedMediaAttachment.Url)) + .AddNewRow() + .AddButton(InlineKeyboardButton.WithCallbackData("Send me another one!", $"new_random"))); + + + } + } +} \ No newline at end of file diff --git a/Services/PostResolver.cs b/Services/PostResolver.cs old mode 100644 new mode 100755 diff --git a/Services/ProcessPosts.cs b/Services/ProcessPosts.cs old mode 100644 new mode 100755 diff --git a/Services/RunCheck.cs b/Services/RunCheck.cs old mode 100644 new mode 100755 diff --git a/mstdnCats.csproj b/mstdnCats.csproj old mode 100644 new mode 100755