From 992065d289d2a275d025b733e418193717a96339 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Tue, 21 Apr 2020 21:43:25 +0200 Subject: [PATCH] Update BotBase.cs - improving system settings for bot --- TelegramBotBase/BotBase.cs | 110 ++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 33 deletions(-) diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 249910b..02030cb 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -9,6 +9,7 @@ using Telegram.Bot.Types; using TelegramBotBase.Args; using TelegramBotBase.Attributes; using TelegramBotBase.Base; +using TelegramBotBase.Enums; using TelegramBotBase.Form; using TelegramBotBase.Interfaces; using TelegramBotBase.Sessions; @@ -59,47 +60,42 @@ namespace TelegramBotBase #endregion - /// - /// Skips all messages during running (good for big delay updates) - /// - public bool SkipAllMessages { get; set; } = false; - - /// - /// Loggs all messages and sent them to the event handler - /// - public bool LogAllMessages { get; set; } = false; - /// /// Enable the SessionState (you need to implement on call forms the IStateForm interface) /// public IStateMachine StateMachine { get; set; } - /// - /// How often could a form navigate to another (within one user action/call/message) - /// - private const int NavigationMaximum = 10; + + public Dictionary SystemSettings { get; private set; } + + private BotBase() + { + this.SystemSettings = new Dictionary(); + + SetSetting(eSettings.NavigationMaximum, 10); + SetSetting(eSettings.LogAllMessages, false); + SetSetting(eSettings.SkipAllMessages, false); + + this.BotCommands = new List(); + + this.Sessions = new SessionBase(); + } /// /// Simple start of your Bot with the APIKey /// /// - public BotBase(String apiKey, bool initClient = true) + public BotBase(String apiKey, bool initClient = true) : this() { this.APIKey = apiKey; - this.BotCommands = new List(); + if (!initClient) + return; - this.Sessions = new SessionBase(); + this.Client = new Base.MessageClient(this.APIKey); + this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0); - if (initClient) - { - this.Client = new Base.MessageClient(this.APIKey); - this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0); - - this.Sessions.Client = this.Client; - } - - + this.Sessions.Client = this.Client; } /// @@ -211,7 +207,7 @@ namespace TelegramBotBase private async void Client_Message(object sender, MessageResult e) { - if (this.SkipAllMessages) + if (this.GetSetting(eSettings.SkipAllMessages, false)) return; try @@ -219,7 +215,7 @@ namespace TelegramBotBase DeviceSession ds = this.Sessions.GetSession(e.DeviceId); e.Device = ds; - if (LogAllMessages) + if (this.GetSetting(eSettings.LogAllMessages, false)) { OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e)); } @@ -305,14 +301,14 @@ namespace TelegramBotBase e.IsFirstHandler = false; - } while (ds.FormSwitched && i < NavigationMaximum); + } while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10)); } private async void Client_MessageEdit(object sender, MessageResult e) { - if (this.SkipAllMessages) + if (this.GetSetting(eSettings.SkipAllMessages, false)) return; try @@ -320,7 +316,7 @@ namespace TelegramBotBase DeviceSession ds = this.Sessions.GetSession(e.DeviceId); e.Device = ds; - if (LogAllMessages) + if (this.GetSetting(eSettings.LogAllMessages, false)) { OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e)); } @@ -371,7 +367,7 @@ namespace TelegramBotBase DeviceSession ds = this.Sessions.GetSession(e.DeviceId); e.Device = ds; - if (LogAllMessages) + if (this.GetSetting(eSettings.LogAllMessages, false)) { OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e)); } @@ -459,7 +455,7 @@ namespace TelegramBotBase e.IsFirstHandler = false; - } while (ds.FormSwitched && i < NavigationMaximum); + } while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10)); } @@ -471,6 +467,54 @@ namespace TelegramBotBase await this.Client.SetBotCommands(this.BotCommands); } + /// + /// Could set a variety of settings to improve the bot handling. + /// + /// + /// + public void SetSetting(eSettings set, uint Value) + { + this.SystemSettings[set] = Value; + } + + /// + /// Could set a variety of settings to improve the bot handling. + /// + /// + /// + public void SetSetting(eSettings set, bool Value) + { + this.SystemSettings[set] = (Value ? 1u : 0u); + } + + /// + /// Could get the current value of a setting + /// + /// + /// + /// + public uint GetSetting(eSettings set, uint defaultValue) + { + if (!this.SystemSettings.ContainsKey(set)) + return defaultValue; + + return this.SystemSettings[set]; + } + + /// + /// Could get the current value of a setting + /// + /// + /// + /// + public bool GetSetting(eSettings set, bool defaultValue) + { + if (!this.SystemSettings.ContainsKey(set)) + return defaultValue; + + return this.SystemSettings[set] == 0u ? false : true; + } + #region "Events" ///