Switch from webserver to telegram inputfile

This commit is contained in:
Marco Realacci 2024-01-31 12:49:28 +01:00
parent ea3ff8aba8
commit a38357d7e7
3 changed files with 46 additions and 32 deletions

View file

@ -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<long, OttoScore> _scores;
private Dictionary<long, Question> _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<Question>();
_scores = new Dictionary<long, OttoScore>();
@ -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)
{

View file

@ -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);

View file

@ -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
- ${ACL_DIR}:/acl