mirror of
https://github.com/appinfosapienza/so-un-bot.git
synced 2025-03-14 08:46:14 +01:00
Add local images support ...finally!
This commit is contained in:
parent
276ae8af2e
commit
e4998d433d
5 changed files with 95 additions and 9 deletions
|
@ -16,6 +16,7 @@ namespace SoUnBot.Modules.OttoLinux
|
||||||
private string _questionsPath;
|
private string _questionsPath;
|
||||||
private string _name;
|
private string _name;
|
||||||
private bool _lock;
|
private bool _lock;
|
||||||
|
private string _webBaseURL;
|
||||||
|
|
||||||
private Dictionary<long, OttoScore> _scores;
|
private Dictionary<long, OttoScore> _scores;
|
||||||
private Dictionary<long, Question> _playingQuestions;
|
private Dictionary<long, Question> _playingQuestions;
|
||||||
|
@ -24,12 +25,13 @@ namespace SoUnBot.Modules.OttoLinux
|
||||||
|
|
||||||
private static Random _rng = new Random();
|
private static Random _rng = new Random();
|
||||||
|
|
||||||
public BotGame(AccessManager accessManager, string name, string path, bool locke, int version = 1)
|
public BotGame(AccessManager accessManager, string name, string path, bool locke, string webBaseUrl, int version = 1)
|
||||||
{
|
{
|
||||||
_accessManager = accessManager;
|
_accessManager = accessManager;
|
||||||
_questionsPath = path;
|
_questionsPath = path;
|
||||||
_name = name;
|
_name = name;
|
||||||
_lock = locke;
|
_lock = locke;
|
||||||
|
_webBaseURL = webBaseUrl;
|
||||||
|
|
||||||
_questions = new List<Question>();
|
_questions = new List<Question>();
|
||||||
_scores = new Dictionary<long, OttoScore>();
|
_scores = new Dictionary<long, OttoScore>();
|
||||||
|
@ -416,13 +418,26 @@ namespace SoUnBot.Modules.OttoLinux
|
||||||
};
|
};
|
||||||
|
|
||||||
string quest = qst.Quest;
|
string quest = qst.Quest;
|
||||||
|
if (qst.Image != "")
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await botClient.SendPhotoAsync(
|
||||||
|
chatId: uid,
|
||||||
|
photo: qst.Image.Contains("http") ? qst.Image : _webBaseURL + "/" + qst.Image);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
CatchParsingError(botClient, quest, uid, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (qst.Quest.StartsWith("img="))
|
if (qst.Quest.StartsWith("img="))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await botClient.SendPhotoAsync(
|
await botClient.SendPhotoAsync(
|
||||||
chatId: uid,
|
chatId: uid,
|
||||||
photo: quest.Substring(4).Split('\n')[0]);
|
photo: quest.Substring(4).Split('\n')[0].Contains("http") ? quest.Substring(4).Split('\n')[0] : _webBaseURL + "/" + quest.Substring(4).Split('\n')[0]);
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
|
@ -453,7 +468,7 @@ namespace SoUnBot.Modules.OttoLinux
|
||||||
{
|
{
|
||||||
await botClient.SendPhotoAsync(
|
await botClient.SendPhotoAsync(
|
||||||
chatId: uid,
|
chatId: uid,
|
||||||
photo: qst.Answers[i].Split('\n')[0].Substring(4),
|
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)
|
caption: "✏️ Risposta " + (i + 1) + ": " + qst.Answers[i].Substring(qst.Answers[i].Split('\n')[0].Length)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
66
Bot/Modules/OttoLinux/PhotoServer.cs
Normal file
66
Bot/Modules/OttoLinux/PhotoServer.cs
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
// This feature is not maintained anymore
|
||||||
|
// it was exposing a webserver returning a json with all the questions and answers.
|
||||||
|
// It was originally intended for a web version of the bot, but was never completed.
|
||||||
|
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace SoUnBot.Modules.OttoLinux
|
||||||
|
{
|
||||||
|
public class PhotoServer
|
||||||
|
{
|
||||||
|
public PhotoServer(string baseDir)
|
||||||
|
{
|
||||||
|
using var listener = new HttpListener();
|
||||||
|
listener.Prefixes.Add("http://+:8001/");
|
||||||
|
listener.Start();
|
||||||
|
Console.WriteLine("PhotoServer is listening on port 8001...");
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
HttpListenerContext ctx = listener.GetContext();
|
||||||
|
using HttpListenerResponse resp = ctx.Response;
|
||||||
|
|
||||||
|
resp.StatusCode = (int)HttpStatusCode.OK;
|
||||||
|
resp.StatusDescription = "Status OK";
|
||||||
|
resp.AddHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
resp.AddHeader("Access-Control-Allow-Headers", "*");
|
||||||
|
resp.AddHeader("Access-Control-Allow-Methods", "GET");
|
||||||
|
|
||||||
|
if (ctx.Request.Url == null)
|
||||||
|
{
|
||||||
|
resp.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!File.Exists(baseDir + "/" + ctx.Request.Url.AbsolutePath))
|
||||||
|
{
|
||||||
|
resp.StatusCode = (int)HttpStatusCode.NotFound;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.Request.Url.AbsolutePath.EndsWith("png"))
|
||||||
|
{
|
||||||
|
resp.AddHeader("Content-Type", "image/png");
|
||||||
|
}
|
||||||
|
else if (ctx.Request.Url.AbsolutePath.EndsWith("jpg"))
|
||||||
|
{
|
||||||
|
resp.AddHeader("Content-Type", "image/jpeg");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
resp.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] buffer = File.ReadAllBytes(baseDir + "/" + ctx.Request.Url.AbsolutePath);
|
||||||
|
resp.ContentLength64 = buffer.Length;
|
||||||
|
|
||||||
|
using Stream ros = resp.OutputStream;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ros.Write(buffer, 0, buffer.Length);
|
||||||
|
} catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,6 +15,8 @@ namespace SoUnBot.Modules.OttoLinux
|
||||||
public List<string> Answers { get; }
|
public List<string> Answers { get; }
|
||||||
[JsonProperty("correct")]
|
[JsonProperty("correct")]
|
||||||
public int Correct { get; private set; }
|
public int Correct { get; private set; }
|
||||||
|
[JsonProperty("image")]
|
||||||
|
public string Image { get; private set; }
|
||||||
|
|
||||||
public Question(String quest)
|
public Question(String quest)
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,9 +5,10 @@ using SoUnBot.Telegram;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
string dataPath = Environment.GetEnvironmentVariable("DATA_PATH") ?? "BotData";
|
string dataPath = Environment.GetEnvironmentVariable("DATA_PATH") ?? "BotData";
|
||||||
string aclPath = Environment.GetEnvironmentVariable("ACL_PATH") ?? "BotData";
|
string aclPath = Environment.GetEnvironmentVariable("ACL_PATH") ?? "BotData/ACL";
|
||||||
string tgToken = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN") ?? "this-string-is-not-a-token";
|
string tgToken = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN") ?? "this-string-is-not-a-token";
|
||||||
string tgAdminId = Environment.GetEnvironmentVariable("TELEGRAM_ADMIN_ID") ?? "000000";
|
string tgAdminId = Environment.GetEnvironmentVariable("TELEGRAM_ADMIN_ID") ?? "000000";
|
||||||
|
string webBaseURL = Environment.GetEnvironmentVariable("WEB_BASE_URL") ?? "http://localhost:8001";
|
||||||
|
|
||||||
Console.WriteLine("Welcome to SO un bot!");
|
Console.WriteLine("Welcome to SO un bot!");
|
||||||
|
|
||||||
|
@ -28,12 +29,12 @@ try
|
||||||
if (f.EndsWith("txt"))
|
if (f.EndsWith("txt"))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
|
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
|
||||||
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false));
|
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, webBaseURL));
|
||||||
}
|
}
|
||||||
else if (f.EndsWith("json"))
|
else if (f.EndsWith("json"))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
|
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
|
||||||
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, 3));
|
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, webBaseURL, 3));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -43,7 +44,7 @@ try
|
||||||
foreach (string d in Directory.GetDirectories(dataPath + "/Questions"))
|
foreach (string d in Directory.GetDirectories(dataPath + "/Questions"))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Loading module " + Path.GetFileName(d));
|
Console.WriteLine("Loading module " + Path.GetFileName(d));
|
||||||
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileName(d), d, false, 2));
|
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileName(d), d, false, webBaseURL, 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (System.Exception ex)
|
catch (System.Exception ex)
|
||||||
|
@ -55,6 +56,8 @@ catch (System.Exception ex)
|
||||||
Console.WriteLine("Starting Telegram bot listener...");
|
Console.WriteLine("Starting Telegram bot listener...");
|
||||||
new TelegramBot(tgToken, acl, dataPath + "/motd.txt", moduleLoader.Modules);
|
new TelegramBot(tgToken, acl, dataPath + "/motd.txt", moduleLoader.Modules);
|
||||||
|
|
||||||
|
new PhotoServer(dataPath + "/Images");
|
||||||
|
|
||||||
// worst way ever to keep the main thread running, I know
|
// worst way ever to keep the main thread running, I know
|
||||||
while (true)
|
while (true)
|
||||||
Thread.Sleep(10000);
|
Thread.Sleep(10000);
|
|
@ -13,7 +13,7 @@ namespace SoUnBot.Telegram
|
||||||
{
|
{
|
||||||
private AccessManager _accessManager;
|
private AccessManager _accessManager;
|
||||||
private string _moth_path;
|
private string _moth_path;
|
||||||
|
|
||||||
private Dictionary<string, IModule> _modules;
|
private Dictionary<string, IModule> _modules;
|
||||||
public TelegramBotClient BotClient { get; private set; }
|
public TelegramBotClient BotClient { get; private set; }
|
||||||
private Dictionary<long, IModule> _usersContext;
|
private Dictionary<long, IModule> _usersContext;
|
||||||
|
|
Loading…
Reference in a new issue