From f39b1d11beb549b2dfc7629f04295a7cc948d975 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sun, 9 Oct 2022 17:12:52 +0200 Subject: [PATCH 1/2] Integration of changes Integrate some changes and cleanups Co-Authored-By: Alexey <110727638+ZavaruKitsu@users.noreply.github.com> --- TelegramBotBase/Base/MessageResult.cs | 2 +- TelegramBotBase/BotBase.cs | 112 ++++++++---------- TelegramBotBase/Builder/BotBaseBuilder.cs | 2 - .../Datasources/ButtonFormDataSource.cs | 2 +- TelegramBotBase/Localizations/English.cs | 4 +- .../{SessionBase.cs => SessionManager.cs} | 69 ++++------- 6 files changed, 79 insertions(+), 112 deletions(-) rename TelegramBotBase/{SessionBase.cs => SessionManager.cs} (83%) diff --git a/TelegramBotBase/Base/MessageResult.cs b/TelegramBotBase/Base/MessageResult.cs index 2e6148b..ed5d4d2 100644 --- a/TelegramBotBase/Base/MessageResult.cs +++ b/TelegramBotBase/Base/MessageResult.cs @@ -164,7 +164,7 @@ namespace TelegramBotBase.Base } /// - /// Confirm incomming action (i.e. Button click) + /// Confirm incoming action (i.e. Button click) /// /// /// diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index b5ebd84..903505c 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -24,7 +24,7 @@ namespace TelegramBotBase /// Bot base class for full Device/Context and Messagehandling /// /// - public class BotBase + public sealed class BotBase { public MessageClient Client { get; set; } @@ -36,7 +36,7 @@ namespace TelegramBotBase /// /// List of all running/active sessions /// - public SessionBase Sessions { get; set; } + public SessionManager Sessions { get; set; } /// /// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start @@ -46,7 +46,7 @@ namespace TelegramBotBase #region "Events" - private EventHandlerList __Events = new EventHandlerList(); + private EventHandlerList __events = new EventHandlerList(); private static object __evSessionBegins = new object(); @@ -83,9 +83,9 @@ namespace TelegramBotBase /// public Dictionary SystemSettings { get; private set; } - public BotBase() + internal BotBase() { - this.SystemSettings = new Dictionary(); + SystemSettings = new Dictionary(); SetSetting(eSettings.MaxNumberOfRetries, 5); SetSetting(eSettings.NavigationMaximum, 10); @@ -93,10 +93,9 @@ namespace TelegramBotBase SetSetting(eSettings.SkipAllMessages, false); SetSetting(eSettings.SaveSessionsOnConsoleExit, false); - this.BotCommandScopes = new Dictionary>(); + BotCommandScopes = new Dictionary>(); - this.Sessions = new SessionBase(); - this.Sessions.BotBase = this; + Sessions = new SessionManager(this); } @@ -104,31 +103,22 @@ namespace TelegramBotBase /// /// Start your Bot /// - public void Start() + public async Task Start() { - if (this.Client == null) - return; - - this.Client.MessageLoop += Client_MessageLoop; + Client.MessageLoop += Client_MessageLoop; - if (this.StateMachine != null) - { - this.Sessions.LoadSessionStates(this.StateMachine); - } + if (StateMachine != null) await Sessions.LoadSessionStates(StateMachine); + //Enable auto session saving - if (this.GetSetting(eSettings.SaveSessionsOnConsoleExit, false)) - { - TelegramBotBase.Tools.Console.SetHandler(() => - { - this.Sessions.SaveSessionStates(); - }); - } + if (GetSetting(eSettings.SaveSessionsOnConsoleExit, false)) + TelegramBotBase.Tools.Console.SetHandler(() => { Task.Run(Sessions.SaveSessionStates); }); + - DeviceSession.MaxNumberOfRetries = this.GetSetting(eSettings.MaxNumberOfRetries, 5); + DeviceSession.MaxNumberOfRetries = GetSetting(eSettings.MaxNumberOfRetries, 5); - this.Client.StartReceiving(); + Client.StartReceiving(); } @@ -137,7 +127,7 @@ namespace TelegramBotBase DeviceSession ds = this.Sessions.GetSession(e.DeviceId); if (ds == null) { - ds = this.Sessions.StartSession(e.DeviceId).GetAwaiter().GetResult(); + ds = Sessions.StartSession(e.DeviceId).GetAwaiter().GetResult(); e.Device = ds; ds.LastMessage = e.RawData.Message; @@ -160,24 +150,24 @@ namespace TelegramBotBase mr.IsFirstHandler = false; - } while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10)); + } while (ds.FormSwitched && i < GetSetting(eSettings.NavigationMaximum, 10)); } /// /// Stop your Bot /// - public void Stop() + public async Task Stop() { - if (this.Client == null) + if (Client == null) return; - this.Client.MessageLoop -= Client_MessageLoop; + Client.MessageLoop -= Client_MessageLoop; - this.Client.StopReceiving(); + Client.StopReceiving(); - this.Sessions.SaveSessionStates(); + await Sessions.SaveSessionStates(); } /// @@ -187,12 +177,12 @@ namespace TelegramBotBase /// public async Task SentToAll(String message) { - if (this.Client == null) + if (Client == null) return; - foreach (var s in this.Sessions.SessionList) + foreach (var s in Sessions.SessionList) { - await this.Client.TelegramClient.SendTextMessageAsync(s.Key, message); + await Client.TelegramClient.SendTextMessageAsync(s.Key, message); } } @@ -252,17 +242,17 @@ namespace TelegramBotBase /// public async Task UploadBotCommands() { - foreach (var bs in this.BotCommandScopes) + foreach (var bs in BotCommandScopes) { - if(bs.Value !=null) + if (bs.Value != null) { - await this.Client.SetBotCommands(bs.Value, bs.Key); + await Client.SetBotCommands(bs.Value, bs.Key); } else { - await this.Client.DeleteBotCommands(bs.Key); + await Client.DeleteBotCommands(bs.Key); } - + } } @@ -273,7 +263,7 @@ namespace TelegramBotBase /// public bool IsKnownBotCommand(String command) { - foreach (var scope in this.BotCommandScopes) + foreach (var scope in BotCommandScopes) { if (scope.Value.Any(a => "/" + a.Command == command)) return true; @@ -289,7 +279,7 @@ namespace TelegramBotBase /// public void SetSetting(eSettings set, uint Value) { - this.SystemSettings[set] = Value; + SystemSettings[set] = Value; } /// @@ -299,7 +289,7 @@ namespace TelegramBotBase /// public void SetSetting(eSettings set, bool Value) { - this.SystemSettings[set] = (Value ? 1u : 0u); + SystemSettings[set] = (Value ? 1u : 0u); } /// @@ -310,10 +300,10 @@ namespace TelegramBotBase /// public uint GetSetting(eSettings set, uint defaultValue) { - if (!this.SystemSettings.ContainsKey(set)) + if (!SystemSettings.ContainsKey(set)) return defaultValue; - return this.SystemSettings[set]; + return SystemSettings[set]; } /// @@ -324,10 +314,10 @@ namespace TelegramBotBase /// public bool GetSetting(eSettings set, bool defaultValue) { - if (!this.SystemSettings.ContainsKey(set)) + if (!SystemSettings.ContainsKey(set)) return defaultValue; - return this.SystemSettings[set] == 0u ? false : true; + return SystemSettings[set] == 0u ? false : true; } #region "Events" @@ -340,17 +330,17 @@ namespace TelegramBotBase { add { - this.__Events.AddHandler(__evSessionBegins, value); + __events.AddHandler(__evSessionBegins, value); } remove { - this.__Events.RemoveHandler(__evSessionBegins, value); + __events.RemoveHandler(__evSessionBegins, value); } } public void OnSessionBegins(SessionBeginEventArgs e) { - (this.__Events[__evSessionBegins] as EventHandler)?.Invoke(this, e); + (__events[__evSessionBegins] as EventHandler)?.Invoke(this, e); } @@ -361,17 +351,17 @@ namespace TelegramBotBase { add { - this.__Events.AddHandler(__evMessage, value); + __events.AddHandler(__evMessage, value); } remove { - this.__Events.RemoveHandler(__evMessage, value); + __events.RemoveHandler(__evMessage, value); } } public void OnMessage(MessageIncomeEventArgs e) { - (this.__Events[__evMessage] as EventHandler)?.Invoke(this, e); + (__events[__evMessage] as EventHandler)?.Invoke(this, e); } @@ -383,7 +373,7 @@ namespace TelegramBotBase public async Task OnBotCommand(BotCommandEventArgs e) { - if (this.BotCommand != null) + if (BotCommand != null) await BotCommand(this, e); } @@ -394,17 +384,17 @@ namespace TelegramBotBase { add { - this.__Events.AddHandler(__evException, value); + __events.AddHandler(__evException, value); } remove { - this.__Events.RemoveHandler(__evException, value); + __events.RemoveHandler(__evException, value); } } public void OnException(SystemExceptionEventArgs e) { - (this.__Events[__evException] as EventHandler)?.Invoke(this, e); + (__events[__evException] as EventHandler)?.Invoke(this, e); } @@ -415,17 +405,17 @@ namespace TelegramBotBase { add { - this.__Events.AddHandler(__evUnhandledCall, value); + __events.AddHandler(__evUnhandledCall, value); } remove { - this.__Events.RemoveHandler(__evUnhandledCall, value); + __events.RemoveHandler(__evUnhandledCall, value); } } public void OnUnhandledCall(UnhandledCallEventArgs e) { - (this.__Events[__evUnhandledCall] as EventHandler)?.Invoke(this, e); + (__events[__evUnhandledCall] as EventHandler)?.Invoke(this, e); } diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 6ad24ee..63b3658 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -335,8 +335,6 @@ namespace TelegramBotBase.Builder bb.Client = _client; - bb.Sessions.Client = bb.Client; - bb.BotCommandScopes = _BotCommandScopes; bb.StateMachine = _statemachine; diff --git a/TelegramBotBase/Datasources/ButtonFormDataSource.cs b/TelegramBotBase/Datasources/ButtonFormDataSource.cs index e8fbfd4..31ef4f6 100644 --- a/TelegramBotBase/Datasources/ButtonFormDataSource.cs +++ b/TelegramBotBase/Datasources/ButtonFormDataSource.cs @@ -35,7 +35,7 @@ namespace TelegramBotBase.Datasources /// - /// Returns the amount of rows exisiting. + /// Returns the amount of rows existing. /// /// public virtual int Count => ButtonForm.Count; diff --git a/TelegramBotBase/Localizations/English.cs b/TelegramBotBase/Localizations/English.cs index d340291..35ac031 100644 --- a/TelegramBotBase/Localizations/English.cs +++ b/TelegramBotBase/Localizations/English.cs @@ -28,10 +28,10 @@ namespace TelegramBotBase.Localizations Values["ToggleButton_OnIcon"] = "⚫"; Values["ToggleButton_OffIcon"] = "⚪"; Values["ToggleButton_Title"] = "Toggle"; - Values["ToggleButton_Changed"] = "Choosen"; + Values["ToggleButton_Changed"] = "Chosen"; Values["MultiToggleButton_SelectedIcon"] = "✅"; Values["MultiToggleButton_Title"] = "Multi-Toggle"; - Values["MultiToggleButton_Changed"] = "Choosen"; + Values["MultiToggleButton_Changed"] = "Chosen"; Values["PromptDialog_Back"] = "Back"; Values["ToggleButton_Changed"] = "Setting changed"; } diff --git a/TelegramBotBase/SessionBase.cs b/TelegramBotBase/SessionManager.cs similarity index 83% rename from TelegramBotBase/SessionBase.cs rename to TelegramBotBase/SessionManager.cs index b94f59a..0cceeda 100644 --- a/TelegramBotBase/SessionBase.cs +++ b/TelegramBotBase/SessionManager.cs @@ -14,14 +14,14 @@ using TelegramBotBase.Sessions; namespace TelegramBotBase { /// - /// Base class for managing all active sessions + /// Class for managing all active sessions /// - public class SessionBase + public sealed class SessionManager { /// /// The Basic message client. /// - public MessageClient Client { get; set; } + public MessageClient Client => BotBase.Client; /// /// A list of all active sessions. @@ -32,29 +32,13 @@ namespace TelegramBotBase /// /// Reference to the Main BotBase instance for later use. /// - public BotBase BotBase { get; set; } + public BotBase BotBase { get; } - public SessionBase() + public SessionManager(BotBase botBase) { - this.SessionList = new Dictionary(); - } - - /// - /// Get device session from Device/ChatId - /// - /// - /// - public DeviceSession this[long key] - { - get - { - return this.SessionList[key]; - } - set - { - this.SessionList[key] = value; - } + BotBase = botBase; + SessionList = new Dictionary(); } /// @@ -64,7 +48,7 @@ namespace TelegramBotBase /// public DeviceSession GetSession(long deviceId) { - DeviceSession ds = this.SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null; + var ds = SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null; return ds; } @@ -77,7 +61,6 @@ namespace TelegramBotBase public async Task StartSession(long deviceId) { var start = BotBase.StartFormFactory.CreateForm(); - //T start = typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { }) as T; start.Client = this.Client; @@ -88,7 +71,7 @@ namespace TelegramBotBase await start.OnOpened(new EventArgs()); - this[deviceId] = ds; + SessionList[deviceId] = ds; return ds; } @@ -98,12 +81,8 @@ namespace TelegramBotBase /// public void EndSession(long deviceId) { - var d = this[deviceId]; - if (d != null) - { - this.SessionList.Remove(deviceId); - - } + var d = SessionList[deviceId]; + if (d != null) SessionList.Remove(deviceId); } /// @@ -112,7 +91,7 @@ namespace TelegramBotBase /// public List GetUserSessions() { - return this.SessionList.Where(a => a.Key > 0).Select(a => a.Value).ToList(); + return SessionList.Where(a => a.Key > 0).Select(a => a.Value).ToList(); } /// @@ -121,27 +100,27 @@ namespace TelegramBotBase /// public List GetGroupSessions() { - return this.SessionList.Where(a => a.Key < 0).Select(a => a.Value).ToList(); + return SessionList.Where(a => a.Key < 0).Select(a => a.Value).ToList(); } /// /// Loads the previously saved states from the machine. /// - public async void LoadSessionStates() + public async Task LoadSessionStates() { if (BotBase.StateMachine == null) { return; } - LoadSessionStates(BotBase.StateMachine); + await LoadSessionStates(BotBase.StateMachine); } /// /// Loads the previously saved states from the machine. /// - public async void LoadSessionStates(IStateMachine statemachine) + public async Task LoadSessionStates(IStateMachine statemachine) { if (statemachine == null) { @@ -159,7 +138,7 @@ namespace TelegramBotBase } //Key already existing - if (this.SessionList.ContainsKey(s.DeviceId)) + if (SessionList.ContainsKey(s.DeviceId)) continue; var form = t.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase; @@ -222,7 +201,7 @@ namespace TelegramBotBase device.ChatTitle = s.ChatTitle; - this.SessionList.Add(s.DeviceId, device); + SessionList.Add(s.DeviceId, device); //Is Subclass of IStateForm var iform = form as IStateForm; @@ -242,7 +221,7 @@ namespace TelegramBotBase catch { //Skip on exception - this.SessionList.Remove(s.DeviceId); + SessionList.Remove(s.DeviceId); } } @@ -254,7 +233,7 @@ namespace TelegramBotBase /// /// Saves all open states into the machine. /// - public void SaveSessionStates(IStateMachine statemachine) + public async Task SaveSessionStates(IStateMachine statemachine) { if (statemachine == null) { @@ -263,7 +242,7 @@ namespace TelegramBotBase var states = new List(); - foreach (var s in this.SessionList) + foreach (var s in SessionList) { if (s.Value == null) { @@ -333,13 +312,13 @@ namespace TelegramBotBase /// /// Saves all open states into the machine. /// - public void SaveSessionStates() + public async Task SaveSessionStates() { - if (this.BotBase.StateMachine == null) + if (BotBase.StateMachine == null) return; - this.SaveSessionStates(this.BotBase.StateMachine); + await SaveSessionStates(BotBase.StateMachine); } } } From dded0ae49eb34b91cbae75f1e746cb48aca5e4a3 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sun, 9 Oct 2022 17:15:06 +0200 Subject: [PATCH 2/2] Update Testproject Co-Authored-By: Alexey <110727638+ZavaruKitsu@users.noreply.github.com> --- TelegramBotBase.Test/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TelegramBotBase.Test/Program.cs b/TelegramBotBase.Test/Program.cs index 8b76b97..6e3eb4c 100644 --- a/TelegramBotBase.Test/Program.cs +++ b/TelegramBotBase.Test/Program.cs @@ -14,7 +14,7 @@ namespace TelegramBotBaseTest { class Program { - static void Main(string[] args) + static async void Main(string[] args) { String APIKey = ""; @@ -43,7 +43,7 @@ namespace TelegramBotBaseTest bb.BotCommand += Bb_BotCommand; //Update Bot commands to botfather - bb.UploadBotCommands().Wait(); + await bb.UploadBotCommands(); bb.SetSetting(TelegramBotBase.Enums.eSettings.LogAllMessages, true);