mirror of
https://github.com/appinfosapienza/so-un-bot.git
synced 2025-05-06 00:32:35 +02:00
Refactor repo structure
This commit is contained in:
parent
36ac339086
commit
8fc89fbc03
1732 changed files with 3812 additions and 67 deletions
493
Bot/Modules/OttoLinux/BotGame.cs
Normal file
493
Bot/Modules/OttoLinux/BotGame.cs
Normal file
|
@ -0,0 +1,493 @@
|
|||
using HomeBot.ACL;
|
||||
using HomeBot.ModuleLoader;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace HomeBot.Modules.OttoLinux
|
||||
{
|
||||
public class BotGame : IModule
|
||||
{
|
||||
private ACM _acm;
|
||||
private List<Question> _questions;
|
||||
private string _questionsPath;
|
||||
private string _name;
|
||||
private bool _lock;
|
||||
|
||||
private Dictionary<long, OttoScore> _scores;
|
||||
private Dictionary<long, Question> _playingQuestions;
|
||||
private Dictionary<long, List<int>> _playedQuestions;
|
||||
private Dictionary<Question, OttoScore> _questionStats;
|
||||
|
||||
private static Random _rng = new Random();
|
||||
|
||||
public BotGame(ACM acm, string name, string path, bool locke, int version = 1)
|
||||
{
|
||||
_acm = acm;
|
||||
_questionsPath = path;
|
||||
_name = name;
|
||||
_lock = locke;
|
||||
|
||||
_questions = new List<Question>();
|
||||
_scores = new Dictionary<long, OttoScore>();
|
||||
_playingQuestions = new Dictionary<long, Question>();
|
||||
_questionStats = new Dictionary<Question, OttoScore>();
|
||||
_playedQuestions = new Dictionary<long, List<int>>();
|
||||
|
||||
if (version == 2) LoadQuestionsV2();
|
||||
else LoadQuestions();
|
||||
}
|
||||
public BotGame(ACM acm)
|
||||
{
|
||||
_acm = acm;
|
||||
_questions = new List<Question>();
|
||||
_scores = new Dictionary<long, OttoScore>();
|
||||
_playingQuestions = new Dictionary<long, Question>();
|
||||
_questionStats = new Dictionary<Question, OttoScore>();
|
||||
_playedQuestions = new Dictionary<long, List<int>>();
|
||||
|
||||
LoadQuestions();
|
||||
}
|
||||
private void LoadQuestions()
|
||||
{
|
||||
var lines = System.IO.File.ReadAllLines(_questionsPath);
|
||||
Question cur = null;
|
||||
var preEmpty = true;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (line.Equals("")) preEmpty = true;
|
||||
else preEmpty = false;
|
||||
if (line.StartsWith(">")) cur.AddAnswer(line.Substring(2), false);
|
||||
else if (line.StartsWith("v")) cur.AddAnswer(line.Substring(2), true);
|
||||
else
|
||||
{
|
||||
if (!preEmpty && cur != null)
|
||||
{
|
||||
cur.Append("\n" + line);
|
||||
continue;
|
||||
}
|
||||
if (cur != null) _questions.Add(cur);
|
||||
cur = new Question(line);
|
||||
}
|
||||
}
|
||||
_questions.Add(cur);
|
||||
}
|
||||
|
||||
private void LoadQuestionsV2()
|
||||
{
|
||||
var questions = System.IO.Directory.GetFileSystemEntries(_questionsPath);
|
||||
foreach (var questPath in questions)
|
||||
{
|
||||
var quest = System.IO.File.ReadAllText(questPath + "/quest.txt");
|
||||
|
||||
if (quest.StartsWith("img=")) quest = quest.Insert(quest.IndexOf("\n") + 1, questPath.Split(new char[] { '/', '\\' }).Last() + ". ");
|
||||
else quest = questPath.Split(new char[] { '/', '\\' }).Last() + ". " + quest;
|
||||
|
||||
Question cur = new Question(quest);
|
||||
|
||||
var shuffledAnsPaths = Directory.GetFiles(questPath).OrderBy(a => _rng.Next()).ToArray();
|
||||
|
||||
foreach (var ansPath in shuffledAnsPaths)
|
||||
{
|
||||
if (ansPath.EndsWith("quest.txt")) continue;
|
||||
if (ansPath.EndsWith(".png")) continue;
|
||||
|
||||
var ans = System.IO.File.ReadAllText(ansPath);
|
||||
if (ansPath.EndsWith("correct.txt")) cur.AddAnswer(ans, true);
|
||||
else cur.AddAnswer(ans, false);
|
||||
}
|
||||
_questions.Add(cur);
|
||||
}
|
||||
}
|
||||
|
||||
public Question PickRandomQuestion(long player, ITelegramBotClient botClient)
|
||||
{
|
||||
//WebRequest w = WebRequest.Create($"https://www.random.org/integers/?num=1&min=1&max={_questions.Count - 1 }&col=1&base=10&format=plain&rnd=new");
|
||||
//w.Method = "GET";
|
||||
|
||||
//var number = int.Parse(new StreamReader(w.GetResponse().GetResponseStream()).ReadToEnd());
|
||||
var number = _rng.Next(1, _questions.Count - 1);
|
||||
|
||||
|
||||
if (!_playedQuestions.ContainsKey(player)) _playedQuestions.Add(player, new List<int>());
|
||||
|
||||
if (_playedQuestions[player].Count >= _questions.Count)
|
||||
{
|
||||
_playedQuestions[player].Clear();
|
||||
botClient.SendTextMessageAsync(
|
||||
chatId: player,
|
||||
text: $"🥳🥳🥳 Congratulazioni! Hai risposto a tutte le {_questions.Count} domande!"
|
||||
);
|
||||
}
|
||||
|
||||
while (_playedQuestions[player].Contains(number))
|
||||
{
|
||||
if (number < _questions.Count - 1) number++;
|
||||
else number = 0;
|
||||
}
|
||||
_playedQuestions[player].Add(number);
|
||||
|
||||
return _questions[number];
|
||||
}
|
||||
|
||||
public string Cmd()
|
||||
{
|
||||
return GetName();
|
||||
}
|
||||
|
||||
async void IModule.ProcessUpdate(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
var uid = update.Message.From.Id;
|
||||
|
||||
if (_lock)
|
||||
{
|
||||
if (!_acm.CheckPermission(update.Message.From, Cmd(), botClient)) return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_acm.CheckPermission(uid, Cmd()))
|
||||
{
|
||||
_acm.GrantPermission(uid, Cmd());
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: _acm.AdminId,
|
||||
text: $"ACM: { update.Message.From.Id }\nL'utente { update.Message.From.FirstName } { update.Message.From.LastName } @{ update.Message.From.Username }\nHa iniziato a usare il bot."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//if (!scores.Keys.Contains(uid))
|
||||
//{
|
||||
// await botClient.SendTextMessageAsync(
|
||||
// chatId: _acm.AdminId,
|
||||
// text: $"ACM: { update.Message.From.Id }\nL'utente { update.Message.From.FirstName } { update.Message.From.LastName } @{ update.Message.From.Username }\nHa iniziato a usare il bot."
|
||||
// );
|
||||
//}
|
||||
|
||||
if (update.Type != UpdateType.Message)
|
||||
return;
|
||||
if (update.Message!.Type != MessageType.Text)
|
||||
return;
|
||||
|
||||
if (update.Message.Text.StartsWith("/qsc"))
|
||||
{
|
||||
int number = 1;
|
||||
if (update.Message.Text.Length > 4) int.TryParse(update.Message.Text.Substring(5), out number);
|
||||
|
||||
var mostCorrect = _questionStats.GroupBy(e => e.Value.Correct).ToDictionary(e => e.Key, t => t.Select(r => r.Key).ToArray());
|
||||
|
||||
var c = 0;
|
||||
foreach (var item in mostCorrect.Keys.OrderByDescending(i => i))
|
||||
{
|
||||
if (c == number) break;
|
||||
var msg = mostCorrect[item].Select(q => q.Quest.Substring(0, 30)).Aggregate((a, b) => a + "\n\n" + b);
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"✅ Risposte indovinate {item} volte:\n{msg}",
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
c++;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (update.Message.Text.StartsWith("/qsw"))
|
||||
{
|
||||
int number = 1;
|
||||
if (update.Message.Text.Length > 4) int.TryParse(update.Message.Text.Substring(5), out number);
|
||||
|
||||
var mostWrong = _questionStats.GroupBy(e => e.Value.Wrong).ToDictionary(e => e.Key, t => t.Select(r => r.Key).ToArray());
|
||||
|
||||
var c = 0;
|
||||
foreach (var item in mostWrong.Keys.OrderByDescending(i => i))
|
||||
{
|
||||
if (c == number) break;
|
||||
var msg = mostWrong[item].Select(q => q.Quest.Substring(0, 30)).Aggregate((a, b) => a + "\n\n" + b);
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"❌ Risposte sbagliate {item} volte:\n{msg}",
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
c++;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (update.Message.Text.StartsWith("/qsb"))
|
||||
{
|
||||
int number = 1;
|
||||
if (update.Message.Text.Length > 4) int.TryParse(update.Message.Text.Substring(5), out number);
|
||||
|
||||
var mostBlank = _questionStats.GroupBy(e => e.Value.Blank).ToDictionary(e => e.Key, t => t.Select(r => r.Key).ToArray());
|
||||
|
||||
var c = 0;
|
||||
foreach (var item in mostBlank.Keys.OrderByDescending(i => i))
|
||||
{
|
||||
if (c == number) break;
|
||||
var msg = mostBlank[item].Select(q => q.Quest.Substring(0, 30)).Aggregate((a, b) => a + "\n\n" + b);
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"🟡 Risposte non date {item} volte:\n{msg}",
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
c++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (update.Message.Text.Equals("/rsp"))
|
||||
{
|
||||
if (!_playedQuestions.ContainsKey(uid))
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "❌ Non c'è niente da eliminare!",
|
||||
cancellationToken: cancellationToken);
|
||||
return;
|
||||
}
|
||||
_playedQuestions[uid].Clear();
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "✅ Memoria eliminata!",
|
||||
cancellationToken: cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((!_playingQuestions.ContainsKey(uid)) || update.Message.Text == GetName().ToLower() || update.Message.Text == "/" + GetName().ToLower() || update.Message.Text == "/reset" || update.Message.Text == "/restart")
|
||||
{
|
||||
if (_scores.ContainsKey(update.Message.From.Id)) _scores[update.Message.From.Id] = new OttoScore();
|
||||
else _scores.Add(update.Message.From.Id, new OttoScore());
|
||||
|
||||
SendRandomQuestion(update.Message.From.Id, botClient, cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
var cur = _playingQuestions[uid];
|
||||
var wrongMsg = "🟡 La risposta corretta era la " + (cur.Correct + 1) + " ☹️";
|
||||
|
||||
if (update.Message.Text == "n" || update.Message.Text == "Passa")
|
||||
{
|
||||
_scores[uid].Blank += 1;
|
||||
_questionStats[cur].Blank += 1;
|
||||
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: wrongMsg,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
SendRandomQuestion(uid, botClient, cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
//todo try parse
|
||||
|
||||
var pick = -1;
|
||||
if (!int.TryParse(update.Message.Text, out pick))
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "❓ scusa, non ho capito 😭",
|
||||
cancellationToken: cancellationToken);
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "⭕️ per uscire da 8linux, scrivi /leave",
|
||||
cancellationToken: cancellationToken);
|
||||
return;
|
||||
}
|
||||
pick -= 1;
|
||||
|
||||
|
||||
if (pick == _playingQuestions[uid].Correct)
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "✅ Risposta esatta!",
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
_scores[uid].Correct += 1;
|
||||
_questionStats[cur].Correct += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "❌ Risposta errata!",
|
||||
cancellationToken: cancellationToken);
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: wrongMsg,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
_scores[uid].Wrong += 1;
|
||||
_questionStats[cur].Wrong += 1;
|
||||
}
|
||||
SendStats(uid, botClient, cancellationToken);
|
||||
await Task.Delay(400);
|
||||
SendRandomQuestion(uid, botClient, cancellationToken);
|
||||
}
|
||||
|
||||
private async void SendRandomQuestion(long uid, ITelegramBotClient botClient, CancellationToken cancellationToken)
|
||||
{
|
||||
var qst = PickRandomQuestion(uid, botClient);
|
||||
|
||||
if (qst.Quest.StartsWith("img=")) Console.WriteLine("Sto inviando la domanda " + qst.Quest.Substring(qst.Quest.IndexOf("\n"), 7) + " a " + uid);
|
||||
else Console.WriteLine("Sto inviando la domanda " + qst.Quest.Substring(0, 7) + " a " + uid);
|
||||
|
||||
while (qst.Answers.Count == 0)
|
||||
{
|
||||
qst = PickRandomQuestion(uid, botClient);
|
||||
botClient.SendTextMessageAsync(
|
||||
chatId: _acm.AdminId,
|
||||
text: $"DOMANDA SENZA RISPOSTE -> {qst.Quest}"
|
||||
);
|
||||
}
|
||||
|
||||
if (!_questionStats.ContainsKey(qst)) _questionStats.Add(qst, new OttoScore());
|
||||
|
||||
if (_playingQuestions.ContainsKey(uid)) _playingQuestions[uid] = qst;
|
||||
else _playingQuestions.Add(uid, qst);
|
||||
|
||||
string answers = "";
|
||||
List<KeyboardButton> kbs = new List<KeyboardButton>();
|
||||
|
||||
bool splitAns = false;
|
||||
foreach (var ans in qst.Answers)
|
||||
if ((ans.Contains("\n") && ans.Substring(ans.IndexOf("\n")).Length > 1 ) || ans.Contains("img=")) splitAns = true;
|
||||
|
||||
var corry = qst.Answers[qst.Correct];
|
||||
|
||||
for (int i = 0; i < qst.Answers.Count; i++)
|
||||
{
|
||||
if (!splitAns) answers += (i + 1) + ". " + qst.Answers[i] + "\n\n";
|
||||
kbs.Add((i + 1).ToString());
|
||||
}
|
||||
|
||||
KeyboardButton[] fr = kbs.ToArray();
|
||||
|
||||
ReplyKeyboardMarkup replyKeyboardMarkup = new(new[]
|
||||
{
|
||||
fr,
|
||||
new KeyboardButton[] { "Passa" },
|
||||
})
|
||||
{
|
||||
ResizeKeyboard = true
|
||||
};
|
||||
|
||||
string quest = qst.Quest;
|
||||
if (qst.Quest.StartsWith("img="))
|
||||
{
|
||||
try
|
||||
{
|
||||
await botClient.SendPhotoAsync(
|
||||
chatId: uid,
|
||||
photo: quest.Substring(4).Split('\n')[0]);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CatchParsingError(botClient, quest, uid, e);
|
||||
}
|
||||
|
||||
quest = quest.Substring(quest.IndexOf('\n') + 1);
|
||||
}
|
||||
try
|
||||
{
|
||||
Message sentMessage = await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "📎 " + PrepareHtml(quest) + "\n\n" + PrepareHtml(answers),
|
||||
replyMarkup: replyKeyboardMarkup,
|
||||
parseMode: ParseMode.Html
|
||||
);
|
||||
}
|
||||
catch (Exception e) { CatchParsingError(botClient, quest, uid, e); }
|
||||
|
||||
if (splitAns)
|
||||
{
|
||||
for (int i = 0; i < qst.Answers.Count; i++)
|
||||
{
|
||||
await Task.Delay(350);
|
||||
if (qst.Answers[i].StartsWith("img="))
|
||||
{
|
||||
try
|
||||
{
|
||||
await botClient.SendPhotoAsync(
|
||||
chatId: uid,
|
||||
photo: qst.Answers[i].Split('\n')[0].Substring(4),
|
||||
caption: "✏️ Risposta " + (i + 1) + ": " + qst.Answers[i].Substring(qst.Answers[i].Split('\n')[0].Length)
|
||||
);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CatchParsingError(botClient, "[R] " + qst.Answers[i].Substring(0, 20) + " -> " + quest, uid, e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Message sentMessage = await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "✏️ Risposta " + PrepareHtml((i + 1) + ":\n" + qst.Answers[i]),
|
||||
replyMarkup: replyKeyboardMarkup,
|
||||
parseMode: ParseMode.Html
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async void CatchParsingError(ITelegramBotClient botClient, string quest, long uid, Exception e)
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: "❌ Cercavo di inviarti una domanda ma si è verificato un errore anomalo nel parsing. Per favore, invia /restart per resettarmi.\nL'errore verrà automaticamente segnalato (visto che tecnologia avanzatissima?)"
|
||||
);
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"❌ La domanda {quest.Substring(0, 60)} è rotta.\nSi è verificato {e.Message}"
|
||||
);
|
||||
}
|
||||
|
||||
private static string PrepareHtml(string s)
|
||||
{
|
||||
return s.Replace("<", "<")
|
||||
.Replace(">", ">")
|
||||
.Replace("<code>", "<code>")
|
||||
.Replace("</code>", "</code>")
|
||||
.Replace("<pre>", "<pre>")
|
||||
.Replace("</pre>", "</pre>")
|
||||
.Replace("<b>", "<b>")
|
||||
.Replace("</b>", "</b>");
|
||||
}
|
||||
|
||||
private async void SendStats(long uid, ITelegramBotClient botClient, CancellationToken cancellationToken)
|
||||
{
|
||||
var stats = _scores[uid];
|
||||
|
||||
var total = stats.Correct + stats.Wrong + stats.Blank;
|
||||
|
||||
Message sentMessage = await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: stats.Correct + " corrette (" + ((float)stats.Correct / (float)total) * 100f + "%)\n" + stats.Wrong + " errate (" + ((float)stats.Wrong / (float)total) * 100f + "%)\n" + stats.Blank + " non date (" + ((float)stats.Blank / (float)total) * 100f + "%)\n",
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
public void ProcessUpdate(ITelegramBotClient botClient, global::Telegram.Bot.Types.Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<Question> GetQuestions()
|
||||
{
|
||||
return _questions;
|
||||
}
|
||||
}
|
||||
}
|
197
Bot/Modules/OttoLinux/OttoReverse.cs
Normal file
197
Bot/Modules/OttoLinux/OttoReverse.cs
Normal file
|
@ -0,0 +1,197 @@
|
|||
// This feature is not maintained anymore
|
||||
// it has been commented as it depends on the old project structure
|
||||
|
||||
/** using HomeBot.ACL;
|
||||
using HomeBot.ModuleLoader;
|
||||
using System.Net;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace HomeBot.Modules.OttoLinux
|
||||
{
|
||||
public class OttoReverse : IModule
|
||||
{
|
||||
private List<Question> _questions;
|
||||
private string NAME = "8reverse";
|
||||
private bool LOCK = false;
|
||||
|
||||
public OttoReverse(string name, List<Question> questions, bool lck)
|
||||
{
|
||||
_questions = questions;
|
||||
LOCK = lck;
|
||||
NAME = name;
|
||||
|
||||
Thread thread = new Thread(() => new WebReverse(GetMatch));
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
public string Cmd()
|
||||
{
|
||||
return GetName();
|
||||
}
|
||||
|
||||
public List<Question> GetMatch(string qst)
|
||||
{
|
||||
var m = qst
|
||||
.Replace("\n", "")
|
||||
.Replace("\r", "")
|
||||
.Replace("<pre>", "")
|
||||
.Replace("<code>", "")
|
||||
.Replace("</pre>", "")
|
||||
.Replace("</code>", "")
|
||||
.Replace("'", "")
|
||||
.Replace("à", "a")
|
||||
.Replace("è", "e")
|
||||
.Replace("é", "e")
|
||||
.Replace("ì", "i")
|
||||
.Replace("ò", "o")
|
||||
.Replace("ù", "u")
|
||||
.Replace(" ", "")
|
||||
.Replace(" ", "")
|
||||
.ToLower();
|
||||
|
||||
var filtered = _questions.FindAll((Question q) =>
|
||||
{
|
||||
return q.Quest
|
||||
.Replace("\n", "")
|
||||
.Replace("\r", "")
|
||||
.Replace("<pre>", "")
|
||||
.Replace("<code>", "")
|
||||
.Replace("</pre>", "")
|
||||
.Replace("</code>", "")
|
||||
.Replace("'", "")
|
||||
.Replace("à", "a")
|
||||
.Replace("è", "e")
|
||||
.Replace("é", "e")
|
||||
.Replace("ì", "i")
|
||||
.Replace("ò", "o")
|
||||
.Replace("ù", "u")
|
||||
.Replace(" ", "")
|
||||
.Replace(" ", "")
|
||||
.ToLower()
|
||||
.Contains(m);
|
||||
});
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
async void IModule.ProcessUpdate(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
var uid = update.Message.Chat.Id;
|
||||
|
||||
if (LOCK)
|
||||
{
|
||||
if (!ACM.CheckPermission(update.Message.From, Cmd(), botClient)) return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ACM.CheckPermission(uid, Cmd()))
|
||||
{
|
||||
ACM.GrantPermission(uid, Cmd());
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: _acm.AdminId,
|
||||
text: $"ACM: {update.Message.From.Id}\nL'utente {update.Message.From.FirstName} {update.Message.From.LastName} @{update.Message.From.Username}\nHa iniziato a usare il bot."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (update.Type != UpdateType.Message)
|
||||
return;
|
||||
if (update.Message!.Type != MessageType.Text)
|
||||
return;
|
||||
|
||||
if (update.Message.Text.Equals("/" + NAME))
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"🤫 Hai appena scoperto una funzione nascosta! 🤐🥶"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
var m = update.Message.Text
|
||||
.Replace("\n", "")
|
||||
.Replace("\r", "")
|
||||
.Replace("<pre>", "")
|
||||
.Replace("<code>", "")
|
||||
.Replace("</pre>", "")
|
||||
.Replace("</code>", "")
|
||||
.Replace("'", "")
|
||||
.Replace("à", "a")
|
||||
.Replace("è", "e")
|
||||
.Replace("é", "e")
|
||||
.Replace("ì", "i")
|
||||
.Replace("ò", "o")
|
||||
.Replace("ù", "u")
|
||||
.Replace(" ", "")
|
||||
.Replace(" ", "")
|
||||
.ToLower();
|
||||
|
||||
if (string.IsNullOrEmpty(m)) return;
|
||||
|
||||
var filtered = _questions.FindAll((Question q) =>
|
||||
{
|
||||
return q.Quest
|
||||
.Replace("\n", "")
|
||||
.Replace("\r", "")
|
||||
.Replace("<pre>", "")
|
||||
.Replace("<code>", "")
|
||||
.Replace("</pre>", "")
|
||||
.Replace("</code>", "")
|
||||
.Replace("'", "")
|
||||
.Replace("à", "a")
|
||||
.Replace("è", "e")
|
||||
.Replace("é", "e")
|
||||
.Replace("ì", "i")
|
||||
.Replace("ò", "o")
|
||||
.Replace("ù", "u")
|
||||
.Replace(" ", "")
|
||||
.Replace(" ", "")
|
||||
.ToLower()
|
||||
.Contains(m);
|
||||
});
|
||||
|
||||
if (!filtered.Any())
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"❌ No match :("
|
||||
);
|
||||
else
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"✅ Found {filtered.Count} matches!"
|
||||
);
|
||||
|
||||
foreach (var q in filtered)
|
||||
{
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"❓ Possible match:\n\n{q.Quest}"
|
||||
);
|
||||
await Task.Delay(250);
|
||||
await botClient.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"✅ Correct answare for the match:\n\n{q.Answers[q.Correct]}"
|
||||
);
|
||||
await Task.Delay(500);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetName()
|
||||
{
|
||||
return NAME;
|
||||
}
|
||||
|
||||
public void ProcessUpdate(ITelegramBotClient botClient, global::Telegram.Bot.Types.Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<Question> GetQuestions()
|
||||
{
|
||||
return _questions;
|
||||
}
|
||||
}
|
||||
} **/
|
16
Bot/Modules/OttoLinux/OttoScore.cs
Normal file
16
Bot/Modules/OttoLinux/OttoScore.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HomeBot.Modules.OttoLinux
|
||||
{
|
||||
public class OttoScore
|
||||
{
|
||||
public int Score { get; set; }
|
||||
public int Correct { get; set; }
|
||||
public int Wrong { get; set; }
|
||||
public int Blank { get; set; }
|
||||
}
|
||||
}
|
33
Bot/Modules/OttoLinux/Question.cs
Normal file
33
Bot/Modules/OttoLinux/Question.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HomeBot.Modules.OttoLinux
|
||||
{
|
||||
public class Question
|
||||
{
|
||||
public String Quest { get; set; }
|
||||
public List<string> Answers { get; }
|
||||
public int Correct { get; private set; }
|
||||
|
||||
public Question(String quest)
|
||||
{
|
||||
Quest = quest;
|
||||
Answers = new List<string>();
|
||||
Correct = 0;
|
||||
}
|
||||
|
||||
public void AddAnswer(String answer, bool correct)
|
||||
{
|
||||
Answers.Add(answer);
|
||||
Correct = correct ? Answers.Count - 1 : Correct;
|
||||
}
|
||||
|
||||
public void Append(String quest)
|
||||
{
|
||||
Quest += quest;
|
||||
}
|
||||
}
|
||||
}
|
57
Bot/Modules/OttoLinux/WebReverse.cs
Normal file
57
Bot/Modules/OttoLinux/WebReverse.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace HomeBot.Modules.OttoLinux
|
||||
{
|
||||
internal class WebReverse
|
||||
{
|
||||
public WebReverse(Func<string, List<Question>> match)
|
||||
{
|
||||
using var listener = new HttpListener();
|
||||
listener.Prefixes.Add("http://+:8001/");
|
||||
|
||||
listener.Start();
|
||||
|
||||
Console.WriteLine("Listening on port 8001...");
|
||||
|
||||
string pre = "";
|
||||
|
||||
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", "POST");
|
||||
resp.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var body = new StreamReader(ctx.Request.InputStream).ReadToEnd();
|
||||
//if (body.Equals("")) body = pre;
|
||||
pre = body;
|
||||
|
||||
string data = JsonConvert.SerializeObject(match(pre));
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(data);
|
||||
resp.ContentLength64 = buffer.Length;
|
||||
|
||||
using Stream ros = resp.OutputStream;
|
||||
try
|
||||
{
|
||||
ros.Write(buffer, 0, buffer.Length);
|
||||
} catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
**/
|
Loading…
Add table
Add a link
Reference in a new issue