From 707ffd492f21ce5f3b9a71420eb044c8f247272a Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sun, 15 May 2022 23:20:16 +0200 Subject: [PATCH] Upgrading BotCommands to new BotCommandScope format - adding IsKnownBotCommand method to BotBase - updated UploadBotCommand method to new format - adding new Commandformat to BotBaseBuilder - adding AddChatCommand and ClearChatCommand for custom Chat commands - integrating new checks into FormBaseMessageLoop --- TelegramBotBase/BotBase.cs | 33 ++++++++++++-- TelegramBotBase/Builder/BotBaseBuilder.cs | 1 - TelegramBotBase/Commands/Extensions.cs | 44 +++++++++++++++---- .../MessageLoops/FormBaseMessageLoop.cs | 2 +- 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index c853c30..38977d6 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -41,7 +41,7 @@ namespace TelegramBotBase /// /// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start /// - public List BotCommands { get; set; } + public Dictionary> BotCommandScopes { get; set; } = new Dictionary>(); #region "Events" @@ -93,7 +93,7 @@ namespace TelegramBotBase SetSetting(eSettings.SkipAllMessages, false); SetSetting(eSettings.SaveSessionsOnConsoleExit, false); - this.BotCommands = new List(); + this.BotCommandScopes = new Dictionary>(); this.Sessions = new SessionBase(); this.Sessions.BotBase = this; @@ -247,7 +247,34 @@ namespace TelegramBotBase /// public async Task UploadBotCommands() { - await this.Client.SetBotCommands(this.BotCommands); + foreach (var bs in this.BotCommandScopes) + { + if(bs.Value !=null) + { + await this.Client.SetBotCommands(bs.Value, bs.Key); + } + else + { + await this.Client.DeleteBotCommands(bs.Key); + } + + } + } + + /// + /// Searching if parameter is a known command in all configured BotCommandScopes. + /// + /// + /// + public bool IsKnownBotCommand(String command) + { + foreach (var scope in this.BotCommandScopes) + { + if (scope.Value.Any(a => "/" + a.Command == command)) + return true; + } + + return false; } /// diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 69be91a..403e1a5 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -324,7 +324,6 @@ namespace TelegramBotBase.Builder bb.Sessions.Client = bb.Client; bb.BotCommandScopes = _BotCommandScopes; - //bb.BotCommands = _botcommands; bb.StateMachine = _statemachine; diff --git a/TelegramBotBase/Commands/Extensions.cs b/TelegramBotBase/Commands/Extensions.cs index 55dcf83..c98903c 100644 --- a/TelegramBotBase/Commands/Extensions.cs +++ b/TelegramBotBase/Commands/Extensions.cs @@ -14,9 +14,9 @@ namespace TelegramBotBase.Commands /// /// /// - public static void Start(this Dictionary> cmds, String description, BotCommandScope scope = null) + public static void Start(this Dictionary> cmds, String description) { - Add(cmds, "start", description, scope); + Add(cmds, "start", description, null); } /// @@ -25,9 +25,9 @@ namespace TelegramBotBase.Commands /// /// - public static void Help(this Dictionary> cmds, String description, BotCommandScope scope = null) + public static void Help(this Dictionary> cmds, String description) { - Add(cmds, "help", description, scope); + Add(cmds, "help", description, null); } /// @@ -37,9 +37,9 @@ namespace TelegramBotBase.Commands /// - public static void Settings(this Dictionary> cmds, String description, BotCommandScope scope = null) + public static void Settings(this Dictionary> cmds, String description) { - Add(cmds, "settings", description, scope); + Add(cmds, "settings", description, null); } @@ -53,7 +53,7 @@ namespace TelegramBotBase.Commands { if (scope == null) { - scope = new BotCommandScopeDefault(); + scope = BotCommandScope.Default(); } var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type); @@ -78,7 +78,7 @@ namespace TelegramBotBase.Commands { if (scope == null) { - scope = new BotCommandScopeDefault(); + scope = BotCommandScope.Default(); } var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type); @@ -91,8 +91,36 @@ namespace TelegramBotBase.Commands { cmds[scope] = null; } + } + + /// + /// Clears all default commands. + /// + /// + public static void ClearDefaultCommands(this Dictionary> cmds) + { + Clear(cmds, null); + } + /// + /// Clears all commands of a specific device. + /// + /// + public static void ClearChatCommands(this Dictionary> cmds, long DeviceId) + { + Clear(cmds, new BotCommandScopeChat() { ChatId = DeviceId }); + } + + /// + /// Adding a chat command with a description. + /// + /// + /// + /// + public static void AddChatCommand(this Dictionary> cmds, long DeviceId, String command, String description) + { + Add(cmds, command, description, new BotCommandScopeChat() { ChatId = DeviceId }); } /// diff --git a/TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs b/TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs index 6568e19..3dcc641 100644 --- a/TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs +++ b/TelegramBotBase/Factories/MessageLoops/FormBaseMessageLoop.cs @@ -37,7 +37,7 @@ namespace TelegramBotBase.Factories.MessageLoops } //Is this a bot command ? - if (mr.IsFirstHandler && mr.IsBotCommand && Bot.BotCommands.Count(a => "/" + a.Command == mr.BotCommand) > 0) + if (mr.IsFirstHandler && mr.IsBotCommand && Bot.IsKnownBotCommand(mr.BotCommand)) { var sce = new BotCommandEventArgs(mr.BotCommand, mr.BotCommandParameters, mr.Message, session.DeviceId, session); await Bot.OnBotCommand(sce);