diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index 517a8fb..69be91a 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -23,7 +23,12 @@ namespace TelegramBotBase.Builder MessageClient _client = null; - List _botcommands = new List(); + /// + /// Contains different Botcommands for different areas. + /// + Dictionary> _BotCommandScopes { get; set; } = new Dictionary>(); + + //List _botcommands = new List(); IStateMachine _statemachine = null; @@ -116,6 +121,15 @@ namespace TelegramBotBase.Builder return this; } + + public IStartFormSelectionStage MinimalMessageLoop() + { + _messageloopfactory = new Factories.MessageLoops.MinimalMessageLoop(); + + return this; + } + + public IStartFormSelectionStage CustomMessageLoop(IMessageLoopFactory messageLoopClass) { _messageloopfactory = messageLoopClass; @@ -212,7 +226,7 @@ namespace TelegramBotBase.Builder public ISessionSerializationStage OnlyStart() { - _botcommands.Start("Starts the bot"); + _BotCommandScopes.Start("Starts the bot"); return this; @@ -220,15 +234,15 @@ namespace TelegramBotBase.Builder public ISessionSerializationStage DefaultCommands() { - _botcommands.Start("Starts the bot"); - _botcommands.Help("Should show you some help"); - _botcommands.Settings("Should show you some settings"); + _BotCommandScopes.Start("Starts the bot"); + _BotCommandScopes.Help("Should show you some help"); + _BotCommandScopes.Settings("Should show you some settings"); return this; } - public ISessionSerializationStage CustomCommands(Action> action) + public ISessionSerializationStage CustomCommands(Action>> action) { - action?.Invoke(_botcommands); + action?.Invoke(_BotCommandScopes); return this; } @@ -309,7 +323,8 @@ namespace TelegramBotBase.Builder bb.Sessions.Client = bb.Client; - bb.BotCommands = _botcommands; + bb.BotCommandScopes = _BotCommandScopes; + //bb.BotCommands = _botcommands; bb.StateMachine = _statemachine; diff --git a/TelegramBotBase/Builder/Interfaces/IBotCommandsStage.cs b/TelegramBotBase/Builder/Interfaces/IBotCommandsStage.cs index cb1507c..a275090 100644 --- a/TelegramBotBase/Builder/Interfaces/IBotCommandsStage.cs +++ b/TelegramBotBase/Builder/Interfaces/IBotCommandsStage.cs @@ -33,7 +33,9 @@ namespace TelegramBotBase.Builder.Interfaces /// /// /// - ISessionSerializationStage CustomCommands(Action> action); + + ISessionSerializationStage CustomCommands(Action>> action); + } } diff --git a/TelegramBotBase/Commands/Extensions.cs b/TelegramBotBase/Commands/Extensions.cs index a07c031..55dcf83 100644 --- a/TelegramBotBase/Commands/Extensions.cs +++ b/TelegramBotBase/Commands/Extensions.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Data; +using System.Linq; using System.Text; using Telegram.Bot.Types; @@ -12,9 +14,9 @@ namespace TelegramBotBase.Commands /// /// /// - public static void Start(this List cmds, String description) + public static void Start(this Dictionary> cmds, String description, BotCommandScope scope = null) { - cmds.Add(new BotCommand() { Command = "start", Description = description }); + Add(cmds, "start", description, scope); } /// @@ -22,9 +24,10 @@ namespace TelegramBotBase.Commands /// /// /// - public static void Help(this List cmds, String description) + + public static void Help(this Dictionary> cmds, String description, BotCommandScope scope = null) { - cmds.Add(new BotCommand() { Command = "help", Description = description }); + Add(cmds, "help", description, scope); } /// @@ -32,9 +35,37 @@ namespace TelegramBotBase.Commands /// /// /// - public static void Settings(this List cmds, String description) + + + public static void Settings(this Dictionary> cmds, String description, BotCommandScope scope = null) { - cmds.Add(new BotCommand() { Command = "settings", Description = description }); + Add(cmds, "settings", description, scope); + } + + + /// + /// Adding the command with a description. + /// + /// + /// + /// + public static void Add(this Dictionary> cmds, String command, String description, BotCommandScope scope = null) + { + if (scope == null) + { + scope = new BotCommandScopeDefault(); + } + + var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type); + + if (item.Value != null) + { + item.Value.Add(new BotCommand() { Command = command, Description = description }); + } + else + { + cmds.Add(scope, new List { new BotCommand() { Command = command, Description = description } }); + } } /// @@ -43,9 +74,85 @@ namespace TelegramBotBase.Commands /// /// /// - public static void Add(this List cmds, String command, String description) + public static void Clear(this Dictionary> cmds, BotCommandScope scope = null) { - cmds.Add(new BotCommand() { Command = command, Description = description }); + if (scope == null) + { + scope = new BotCommandScopeDefault(); + } + + var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type); + + if (item.Key != null) + { + cmds[item.Key] = null; + } + else + { + cmds[scope] = null; + } + + + } + + /// + /// Adding a group command with a description. + /// + /// + /// + /// + public static void AddGroupCommand(this Dictionary> cmds, String command, String description) + { + Add(cmds, command, description, new BotCommandScopeAllGroupChats()); + } + + /// + /// Clears all group commands. + /// + /// + public static void ClearGroupCommands(this Dictionary> cmds) + { + Clear(cmds, new BotCommandScopeAllGroupChats()); + } + + /// + /// Adding group admin command with a description. + /// + /// + /// + /// + public static void AddGroupAdminCommand(this Dictionary> cmds, String command, String description) + { + Add(cmds, command, description, new BotCommandScopeAllChatAdministrators()); + } + + /// + /// Clears all group admin commands. + /// + /// + public static void ClearGroupAdminCommand(this Dictionary> cmds) + { + Clear(cmds, new BotCommandScopeAllChatAdministrators()); + } + + /// + /// Adding a privat command with a description. + /// + /// + /// + /// + public static void AddPrivateChatCommand(this Dictionary> cmds, String command, String description) + { + Add(cmds, command, description, new BotCommandScopeAllPrivateChats()); + } + + /// + /// Clears all private commands. + /// + /// + public static void ClearPrivateChatCommand(this Dictionary> cmds) + { + Clear(cmds, new BotCommandScopeAllPrivateChats()); } } }