Fix bot haning when sending alerts

This commit is contained in:
Marco Realacci 2024-01-19 19:06:53 +01:00
parent 0f90b3525e
commit 95e02d9a64
29 changed files with 217 additions and 80 deletions

View file

@ -1,25 +1,17 @@
using HomeBot.ModuleLoader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using HomeBot.Telegram;
using Newtonsoft.Json;
using Telegram.Bot;
using Telegram.Bot.Types.ReplyMarkups;
using Telegram.Bot.Types;
using File = Telegram.Bot.Types.File;
using Telegram.Bot.Types.ReplyMarkups;
namespace HomeBot.ACL
namespace SoUnBot.AccessControl
{
public class ACM
public class AccessManager
{
private string _acl_path;
private Dictionary<long, HashSet<string>> _acl;
public long AdminId { get; private set; }
public ACM(string aclPath, long adminId)
public AccessManager(string aclPath, long adminId)
{
_acl_path = aclPath;
AdminId = adminId;

View file

@ -1,7 +1,7 @@
using Telegram.Bot;
using Telegram.Bot.Types;
namespace HomeBot.ModuleLoader
namespace SoUnBot.ModuleLoader
{
public interface IModule
{

View file

@ -1,4 +1,4 @@
namespace HomeBot.ModuleLoader
namespace SoUnBot.ModuleLoader
{
public class ModuleLoader
{

View file

@ -1,15 +1,15 @@
using HomeBot.ACL;
using HomeBot.ModuleLoader;
using SoUnBot.AccessControl;
using SoUnBot.ModuleLoader;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
namespace HomeBot.Modules.OttoLinux
namespace SoUnBot.Modules.OttoLinux
{
public class BotGame : IModule
{
private ACM _acm;
private AccessManager _accessManager;
private List<Question> _questions;
private string _questionsPath;
private string _name;
@ -22,9 +22,9 @@ namespace HomeBot.Modules.OttoLinux
private static Random _rng = new Random();
public BotGame(ACM acm, string name, string path, bool locke, int version = 1)
public BotGame(AccessManager accessManager, string name, string path, bool locke, int version = 1)
{
_acm = acm;
_accessManager = accessManager;
_questionsPath = path;
_name = name;
_lock = locke;
@ -38,9 +38,9 @@ namespace HomeBot.Modules.OttoLinux
if (version == 2) LoadQuestionsV2();
else LoadQuestions();
}
public BotGame(ACM acm)
public BotGame(AccessManager accessManager)
{
_acm = acm;
_accessManager = accessManager;
_questions = new List<Question>();
_scores = new Dictionary<long, OttoScore>();
_playingQuestions = new Dictionary<long, Question>();
@ -142,15 +142,15 @@ namespace HomeBot.Modules.OttoLinux
if (_lock)
{
if (!_acm.CheckPermission(update.Message.From, Cmd(), botClient)) return;
if (!_accessManager.CheckPermission(update.Message.From, Cmd(), botClient)) return;
}
else
{
if (!_acm.CheckPermission(uid, Cmd()))
if (!_accessManager.CheckPermission(uid, Cmd()))
{
_acm.GrantPermission(uid, Cmd());
_accessManager.GrantPermission(uid, Cmd());
await botClient.SendTextMessageAsync(
chatId: _acm.AdminId,
chatId: _accessManager.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."
);
}
@ -343,7 +343,7 @@ namespace HomeBot.Modules.OttoLinux
{
qst = PickRandomQuestion(uid, botClient);
botClient.SendTextMessageAsync(
chatId: _acm.AdminId,
chatId: _accessManager.AdminId,
text: $"DOMANDA SENZA RISPOSTE -> {qst.Quest}"
);
}

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeBot.Modules.OttoLinux
namespace SoUnBot.Modules.OttoLinux
{
public class OttoScore
{

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HomeBot.Modules.OttoLinux
namespace SoUnBot.Modules.OttoLinux
{
public class Question
{

View file

@ -10,7 +10,7 @@ using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
namespace HomeBot.Modules.OttoLinux
namespace SoUnBot.Modules.OttoLinux
{
internal class WebReverse
{

View file

@ -1,7 +1,7 @@
using HomeBot.ACL;
using HomeBot.ModuleLoader;
using HomeBot.Modules.OttoLinux;
using HomeBot.Telegram;
using SoUnBot.AccessControl;
using SoUnBot.ModuleLoader;
using SoUnBot.Modules.OttoLinux;
using SoUnBot.Telegram;
using Telegram.Bot.Types;
string dataPath = Environment.GetEnvironmentVariable("DATA_PATH") ?? "BotData";
@ -18,7 +18,7 @@ if (!long.TryParse(tgAdminId, out tgAdminLong))
return;
}
var acl = new ACM(aclPath, tgAdminLong);
var acl = new AccessManager(aclPath, tgAdminLong);
var moduleLoader = new ModuleLoader();
try

View file

@ -1,5 +1,5 @@
using HomeBot.ACL;
using HomeBot.ModuleLoader;
using SoUnBot.AccessControl;
using SoUnBot.ModuleLoader;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;
@ -7,20 +7,20 @@ using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
namespace HomeBot.Telegram
namespace SoUnBot.Telegram
{
internal class TelegramBot
{
private ACM _acm;
private AccessManager _accessManager;
private string _moth_path;
private Dictionary<string, IModule> _modules;
public TelegramBotClient BotClient { get; private set; }
private Dictionary<long, IModule> _usersContext;
public TelegramBot(string token, ACM acm, string motd_path, Dictionary<string, IModule> modules)
public TelegramBot(string token, AccessManager accessManager, string motd_path, Dictionary<string, IModule> modules)
{
_acm = acm;
_accessManager = accessManager;
_moth_path = motd_path;
_modules = modules;
_usersContext = new Dictionary<long, IModule>();
@ -59,7 +59,7 @@ namespace HomeBot.Telegram
{
long uid = int.Parse(update.CallbackQuery.Message.Text.Substring(5).Split('\n')[0]);
string perm = update.CallbackQuery.Message.Text.Split("Ha richiesto l'accesso a: ")[1];
_acm.GrantPermission(uid, perm);
_accessManager.GrantPermission(uid, perm);
await botClient.AnswerCallbackQueryAsync(
update.CallbackQuery.Id,
@ -74,7 +74,7 @@ namespace HomeBot.Telegram
if (update.CallbackQuery.Message.Text.StartsWith("ACM") && update.CallbackQuery.Data.Contains("🔆 Richiedi accesso"))
{
string perm = update.CallbackQuery.Message.Text.Split('`')[1];
_acm.AskPermission(update.CallbackQuery.From, perm, botClient);
_accessManager.AskPermission(update.CallbackQuery.From, perm, botClient);
await botClient.AnswerCallbackQueryAsync(
update.CallbackQuery.Id,
"Richiesta effettuata"
@ -101,32 +101,13 @@ namespace HomeBot.Telegram
if (update.Type == UpdateType.Message && update.Message!.Type == MessageType.Text && update.Message.Text.StartsWith("/spam"))
{
if (!_acm.CheckPermission(update.Message.From, "global.spam", botClient)) return;
if (!_accessManager.CheckPermission(update.Message.From, "global.spam", botClient)) return;
await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Invio annuncio in corso...",
cancellationToken: cancellationToken);
foreach (long user in _acm.Users())
{
try
{
Console.WriteLine("Sto spammando a" + user.ToString());
await botClient.SendTextMessageAsync(
chatId: user,
text: update.Message.Text.Substring(6)
);
await Task.Delay(1000);
} catch
{
Console.WriteLine("Ho fallito");
}
}
await botClient.SendTextMessageAsync(
chatId: chatId,
text: "✅ Annunciato a tutti!",
cancellationToken: cancellationToken);
SendToEveryone(botClient, chatId, update.Message.Text.Substring(6));
return;
}
@ -176,5 +157,27 @@ namespace HomeBot.Telegram
return Task.CompletedTask;
}
private async void SendToEveryone(ITelegramBotClient botClient, long chatId, string text)
{
foreach (long user in _accessManager.Users())
{
try
{
Console.WriteLine("Sto spammando a" + user.ToString());
await botClient.SendTextMessageAsync(
chatId: user,
text: text
);
await Task.Delay(500);
}
catch
{
Console.WriteLine("Ho fallito");
}
}
await botClient.SendTextMessageAsync(
chatId: chatId,
text: "✅ Annunciato a tutti!");
}
}
}

View file

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("HomeBot")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0f90b3525e15d7b555f43c99e89fd846983e65c2")]
[assembly: System.Reflection.AssemblyProductAttribute("HomeBot")]
[assembly: System.Reflection.AssemblyTitleAttribute("HomeBot")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View file

@ -1 +1 @@
72c6f3b361b78aae87749dd6be685e28f0778d89bfd5e20c56f46ab1894d22cf
c8e5760cdee60026fb802757fafaf6f76e6a858fc29bd4ca77cc6c67f07235e4

View file

@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("SoUnBot")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0f90b3525e15d7b555f43c99e89fd846983e65c2")]
[assembly: System.Reflection.AssemblyProductAttribute("SoUnBot")]
[assembly: System.Reflection.AssemblyTitleAttribute("SoUnBot")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generato dalla classe WriteCodeFragment di MSBuild.

View file

@ -0,0 +1 @@
66e8072e473b7388543ce284dd0da693fc6607bd39c78f6abbaaf2843454409e

View file

@ -0,0 +1,13 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = SoUnBot
build_property.ProjectDir = /home/marco/RiderProjects/so-un-bot/Bot/
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =

View file

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Binary file not shown.

View file

@ -65,7 +65,7 @@
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.0, 8.0.0]"
"version": "[8.0.1, 8.0.1]"
}
],
"frameworkReferences": {
@ -73,7 +73,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.100/PortableRuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.101/PortableRuntimeIdentifierGraph.json"
}
}
}

View file

@ -0,0 +1,81 @@
{
"format": 1,
"restore": {
"/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj": {}
},
"projects": {
"/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj",
"projectName": "SoUnBot",
"projectPath": "/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj",
"packagesPath": "/home/marco/.nuget/packages/",
"outputPath": "/home/marco/RiderProjects/so-un-bot/Bot/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/marco/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"dependencies": {
"Newtonsoft.Json": {
"target": "Package",
"version": "[13.0.1, )"
},
"Telegram.Bot": {
"target": "Package",
"version": "[17.0.0, )"
},
"Telegram.Bot.Extensions.Polling": {
"target": "Package",
"version": "[1.0.0, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.1, 8.0.1]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.101/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/marco/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/marco/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.8.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/home/marco/.nuget/packages/" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

View file

@ -207,9 +207,9 @@
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/marco/RiderProjects/so-un-bot/Bot/HomeBot.csproj",
"projectName": "HomeBot",
"projectPath": "/home/marco/RiderProjects/so-un-bot/Bot/HomeBot.csproj",
"projectUniqueName": "/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj",
"projectName": "SoUnBot",
"projectPath": "/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj",
"packagesPath": "/home/marco/.nuget/packages/",
"outputPath": "/home/marco/RiderProjects/so-un-bot/Bot/obj/",
"projectStyle": "PackageReference",
@ -265,7 +265,7 @@
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.0, 8.0.0]"
"version": "[8.0.1, 8.0.1]"
}
],
"frameworkReferences": {
@ -273,7 +273,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.100/PortableRuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.101/PortableRuntimeIdentifierGraph.json"
}
}
}

View file

@ -1,15 +1,15 @@
{
"version": 2,
"dgSpecHash": "LbYDmG/0ruay1tUd+Q4MedQzC9B+Yddw/z1JmGjWjzZy63Je9jlh27Ll1CzBUc8w9TMS7jcU6z56/VlozDSL1w==",
"dgSpecHash": "14ezpUWXpklegX6ueKmBLbaZ7zoadTut58taBGv4o+ffPSwU4PyfKxoECTdyCOu5nefDv+iE1m0OHxSaLmiqDg==",
"success": true,
"projectFilePath": "/home/marco/RiderProjects/so-un-bot/Bot/HomeBot.csproj",
"projectFilePath": "/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj",
"expectedPackageFiles": [
"/home/marco/.nuget/packages/jetbrains.annotations/2021.3.0/jetbrains.annotations.2021.3.0.nupkg.sha512",
"/home/marco/.nuget/packages/newtonsoft.json/13.0.1/newtonsoft.json.13.0.1.nupkg.sha512",
"/home/marco/.nuget/packages/system.threading.channels/6.0.0/system.threading.channels.6.0.0.nupkg.sha512",
"/home/marco/.nuget/packages/telegram.bot/17.0.0/telegram.bot.17.0.0.nupkg.sha512",
"/home/marco/.nuget/packages/telegram.bot.extensions.polling/1.0.0/telegram.bot.extensions.polling.1.0.0.nupkg.sha512",
"/home/marco/.nuget/packages/microsoft.aspnetcore.app.ref/8.0.0/microsoft.aspnetcore.app.ref.8.0.0.nupkg.sha512"
"/home/marco/.nuget/packages/microsoft.aspnetcore.app.ref/8.0.1/microsoft.aspnetcore.app.ref.8.0.1.nupkg.sha512"
],
"logs": []
}

View file

@ -1 +1 @@
"restore":{"projectUniqueName":"/home/marco/RiderProjects/so-un-bot/Bot/HomeBot.csproj","projectName":"HomeBot","projectPath":"/home/marco/RiderProjects/so-un-bot/Bot/HomeBot.csproj","outputPath":"/home/marco/RiderProjects/so-un-bot/Bot/obj/","projectStyle":"PackageReference","originalTargetFrameworks":["net8.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net8.0":{"targetAlias":"net8.0","projectReferences":{}}},"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net8.0":{"targetAlias":"net8.0","dependencies":{"Newtonsoft.Json":{"target":"Package","version":"[13.0.1, )"},"Telegram.Bot":{"target":"Package","version":"[17.0.0, )"},"Telegram.Bot.Extensions.Polling":{"target":"Package","version":"[1.0.0, )"}},"imports":["net461","net462","net47","net471","net472","net48","net481"],"assetTargetFallback":true,"warn":true,"downloadDependencies":[{"name":"Microsoft.AspNetCore.App.Ref","version":"[8.0.0, 8.0.0]"}],"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"/usr/share/dotnet/sdk/8.0.100/PortableRuntimeIdentifierGraph.json"}}
"restore":{"projectUniqueName":"/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj","projectName":"SoUnBot","projectPath":"/home/marco/RiderProjects/so-un-bot/Bot/SoUnBot.csproj","outputPath":"/home/marco/RiderProjects/so-un-bot/Bot/obj/","projectStyle":"PackageReference","originalTargetFrameworks":["net8.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net8.0":{"targetAlias":"net8.0","projectReferences":{}}},"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net8.0":{"targetAlias":"net8.0","dependencies":{"Newtonsoft.Json":{"target":"Package","version":"[13.0.1, )"},"Telegram.Bot":{"target":"Package","version":"[17.0.0, )"},"Telegram.Bot.Extensions.Polling":{"target":"Package","version":"[1.0.0, )"}},"imports":["net461","net462","net47","net471","net472","net48","net481"],"assetTargetFallback":true,"warn":true,"downloadDependencies":[{"name":"Microsoft.AspNetCore.App.Ref","version":"[8.0.1, 8.0.1]"}],"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"/usr/share/dotnet/sdk/8.0.101/PortableRuntimeIdentifierGraph.json"}}

View file

@ -1 +1 @@
17056229120899245
17056866144486474

View file

@ -1 +1 @@
17056276414728889
17056866144486474