From a38357d7e7f3b928776bd4688c802a547a529fb7 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Wed, 31 Jan 2024 12:49:28 +0100 Subject: [PATCH] Switch from webserver to telegram inputfile --- Bot/Modules/OttoLinux/BotGame.cs | 55 ++++++++++++++++++++++++-------- Bot/Program.cs | 9 +++--- docker-compose.yml | 14 +------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Bot/Modules/OttoLinux/BotGame.cs b/Bot/Modules/OttoLinux/BotGame.cs index 92c0939..28da7e3 100644 --- a/Bot/Modules/OttoLinux/BotGame.cs +++ b/Bot/Modules/OttoLinux/BotGame.cs @@ -5,7 +5,9 @@ using SoUnBot.ModuleLoader; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.InputFiles; using Telegram.Bot.Types.ReplyMarkups; +using File = System.IO.File; namespace SoUnBot.Modules.OttoLinux { @@ -16,7 +18,7 @@ namespace SoUnBot.Modules.OttoLinux private string _questionsPath; private string _name; private bool _lock; - private string _webBaseURL; + private string _imgBaseDir; private Dictionary _scores; private Dictionary _playingQuestions; @@ -25,13 +27,13 @@ namespace SoUnBot.Modules.OttoLinux private static Random _rng = new Random(); - public BotGame(AccessManager accessManager, string name, string path, bool locke, string webBaseUrl, int version = 1) + public BotGame(AccessManager accessManager, string name, string path, bool locke, string imgBaseDir, int version = 1) { _accessManager = accessManager; _questionsPath = path; _name = name; _lock = locke; - _webBaseURL = webBaseUrl; + _imgBaseDir = imgBaseDir; _questions = new List(); _scores = new Dictionary(); @@ -422,9 +424,18 @@ namespace SoUnBot.Modules.OttoLinux { try { - await botClient.SendPhotoAsync( - chatId: uid, - photo: qst.Image.Contains("http") ? qst.Image : _webBaseURL + "/" + qst.Image); + if (qst.Image.Contains("http")) + { + await botClient.SendPhotoAsync( + chatId: uid, + photo: qst.Image); + } + else + { + await botClient.SendPhotoAsync( + chatId: uid, + photo: File.OpenRead(_imgBaseDir + "/" + qst.Image)); + } } catch(Exception e) { @@ -435,9 +446,18 @@ namespace SoUnBot.Modules.OttoLinux { try { - await botClient.SendPhotoAsync( - chatId: uid, - photo: quest.Substring(4).Split('\n')[0].Contains("http") ? quest.Substring(4).Split('\n')[0] : _webBaseURL + "/" + quest.Substring(4).Split('\n')[0]); + if (qst.Image.Contains("http")) + { + await botClient.SendPhotoAsync( + chatId: uid, + photo: quest.Substring(4).Split('\n')[0]); + } + else + { + await botClient.SendPhotoAsync( + chatId: uid, + photo: File.OpenRead(_imgBaseDir + "/" + quest.Substring(4).Split('\n')[0])); + } } catch(Exception e) { @@ -466,11 +486,18 @@ namespace SoUnBot.Modules.OttoLinux { try { - await botClient.SendPhotoAsync( - chatId: uid, - photo: qst.Answers[i].Split('\n')[0].Substring(4).Contains("http") ? qst.Answers[i].Split('\n')[0].Substring(4) : _webBaseURL + "/" + qst.Answers[i].Split('\n')[0].Substring(4), - caption: "✏️ Risposta " + (i + 1) + ": " + qst.Answers[i].Substring(qst.Answers[i].Split('\n')[0].Length) - ); + if (qst.Image.Contains("http")) + { + await botClient.SendPhotoAsync( + chatId: uid, + photo: qst.Answers[i].Split('\n')[0].Substring(4)); + } + else + { + await botClient.SendPhotoAsync( + chatId: uid, + photo: File.OpenRead(_imgBaseDir + "/" + qst.Answers[i].Split('\n')[0].Substring(4))); + } } catch(Exception e) { diff --git a/Bot/Program.cs b/Bot/Program.cs index 49810bc..10d817f 100644 --- a/Bot/Program.cs +++ b/Bot/Program.cs @@ -9,6 +9,7 @@ string aclPath = Environment.GetEnvironmentVariable("ACL_PATH") ?? "BotData/ACL" string tgToken = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN") ?? "this-string-is-not-a-token"; string tgAdminId = Environment.GetEnvironmentVariable("TELEGRAM_ADMIN_ID") ?? "000000"; string webBaseURL = Environment.GetEnvironmentVariable("WEB_BASE_URL") ?? "http://localhost:8001"; +string imagesPath = dataPath + "/Images"; Console.WriteLine("Welcome to SO un bot!"); @@ -29,12 +30,12 @@ try if (f.EndsWith("txt")) { Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f)); - moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, webBaseURL)); + moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, imagesPath)); } else if (f.EndsWith("json")) { Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f)); - moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, webBaseURL, 3)); + moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, imagesPath, 3)); } else { @@ -44,7 +45,7 @@ try foreach (string d in Directory.GetDirectories(dataPath + "/Questions")) { Console.WriteLine("Loading module " + Path.GetFileName(d)); - moduleLoader.LoadModule(new BotGame(acl, Path.GetFileName(d), d, false, webBaseURL, 2)); + moduleLoader.LoadModule(new BotGame(acl, Path.GetFileName(d), d, false, imagesPath, 2)); } } catch (System.Exception ex) @@ -56,8 +57,6 @@ catch (System.Exception ex) Console.WriteLine("Starting Telegram bot listener..."); new TelegramBot(tgToken, acl, dataPath + "/motd.txt", moduleLoader.Modules); -new PhotoServer(dataPath + "/Images"); - // worst way ever to keep the main thread running, I know while (true) Thread.Sleep(10000); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 2e648fc..4964cd6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,17 +21,5 @@ services: - TELEGRAM_TOKEN=${TELEGRAM_TOKEN} # User id of the administrator user (will receive logs for errors) - TELEGRAM_ADMIN_ID=${TELEGRAM_ADMIN_ID} - - WEB_BASE_URL=https://so-un-bot.srv.mrlc.cc volumes: - - ${ACL_DIR}:/acl - labels: - - "traefik.enable=true" - - "traefik.http.routers.so-un-bot.entrypoints=web, websecure" - - "traefik.http.routers.so-un-bot.rule=Host(`so-un-bot.srv.mrlc.cc`)" - - "traefik.http.routers.so-un-bot.tls=true" - - "traefik.http.routers.so-un-bot.tls.certresolver=production" - - "traefik.http.services.so-un-bot.loadbalancer.server.port=8001" - -networks: - proxynet: - external: true \ No newline at end of file + - ${ACL_DIR}:/acl \ No newline at end of file