mirror of
https://github.com/appinfosapienza/so-un-bot.git
synced 2025-05-06 00:52:34 +02:00
Fix bot haning when sending alerts
This commit is contained in:
parent
0f90b3525e
commit
95e02d9a64
29 changed files with 217 additions and 80 deletions
90
Bot/AccessControl/AccessManager.cs
Normal file
90
Bot/AccessControl/AccessManager.cs
Normal file
|
@ -0,0 +1,90 @@
|
|||
using Newtonsoft.Json;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace SoUnBot.AccessControl
|
||||
{
|
||||
public class AccessManager
|
||||
{
|
||||
private string _acl_path;
|
||||
private Dictionary<long, HashSet<string>> _acl;
|
||||
public long AdminId { get; private set; }
|
||||
|
||||
public AccessManager(string aclPath, long adminId)
|
||||
{
|
||||
_acl_path = aclPath;
|
||||
AdminId = adminId;
|
||||
if (!System.IO.File.Exists(aclPath + "/acl.json"))
|
||||
{
|
||||
_acl = new Dictionary<long, HashSet<string>>();
|
||||
return;
|
||||
}
|
||||
var json = System.IO.File.ReadAllText(aclPath + "/acl.json");
|
||||
_acl = JsonConvert.DeserializeObject<Dictionary<long, HashSet<string>>>(json) ?? new Dictionary<long, HashSet<string>>();;
|
||||
}
|
||||
|
||||
public long[] Users()
|
||||
{
|
||||
return _acl.Keys.ToArray();
|
||||
}
|
||||
|
||||
private void SaveToJson()
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(_acl);
|
||||
System.IO.File.WriteAllText(_acl_path + "/acl.json", json);
|
||||
}
|
||||
|
||||
public void GrantPermission(long uid, string perm)
|
||||
{
|
||||
if (_acl.ContainsKey(uid)) _acl[uid].Add(perm);
|
||||
else _acl.Add(uid, new HashSet<string> { perm });
|
||||
SaveToJson();
|
||||
}
|
||||
|
||||
public bool RevokePermission(long uid, string perm)
|
||||
{
|
||||
if (_acl.ContainsKey(uid)) _acl[uid].Remove(perm);
|
||||
else return false;
|
||||
SaveToJson();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CheckPermission(long uid, string perm)
|
||||
{
|
||||
return _acl.ContainsKey(uid) ? _acl[uid].Contains(perm) : false;
|
||||
}
|
||||
|
||||
public bool CheckPermission(User user, string perm, ITelegramBotClient client)
|
||||
{
|
||||
var uid = user.Id;
|
||||
var hasPerm = _acl.ContainsKey(uid) ? _acl[uid].Contains(perm) : false;
|
||||
if (hasPerm) return true;
|
||||
client.SendTextMessageAsync(
|
||||
chatId: uid,
|
||||
text: $"ACM\nNon hai l'accesso a `{ perm }`. Puoi richiederlo ad un amministratore usando il pulsante qui sotto",
|
||||
replyMarkup: new InlineKeyboardMarkup(InlineKeyboardButton.WithCallbackData("🔆 Richiedi accesso"))
|
||||
);
|
||||
return false;
|
||||
}
|
||||
public void AskPermission(User user, string perm, ITelegramBotClient client)
|
||||
{
|
||||
/*InlineKeyboardButton[][] ik = new InlineKeyboardButton[][]
|
||||
{
|
||||
new InlineKeyboardButton[]
|
||||
{
|
||||
new InlineKeyboardButton("✅ Grant")
|
||||
},
|
||||
new InlineKeyboardButton[]
|
||||
{
|
||||
new InlineKeyboardButton("❌ Deny")
|
||||
}
|
||||
};*/
|
||||
client.SendTextMessageAsync(
|
||||
chatId: AdminId,
|
||||
text: $"ACM: { user.Id }\nL'utente { user.FirstName } { user.LastName } @{ user.Username }\nHa richiesto l'accesso a: { perm }",
|
||||
replyMarkup: new InlineKeyboardMarkup(InlineKeyboardButton.WithCallbackData("✅ Grant"))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue