Replacing BotCommands with BotCommandsScope

This commit is contained in:
FlorianDahn 2022-02-08 18:07:59 +01:00
parent 8fa952e68b
commit d7a5c149b1
3 changed files with 141 additions and 17 deletions

View File

@ -23,7 +23,12 @@ namespace TelegramBotBase.Builder
MessageClient _client = null;
List<BotCommand> _botcommands = new List<BotCommand>();
/// <summary>
/// Contains different Botcommands for different areas.
/// </summary>
Dictionary<BotCommandScope, List<BotCommand>> _BotCommandScopes { get; set; } = new Dictionary<BotCommandScope, List<BotCommand>>();
//List<BotCommand> _botcommands = new List<BotCommand>();
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<List<BotCommand>> action)
public ISessionSerializationStage CustomCommands(Action<Dictionary<BotCommandScope, List<BotCommand>>> 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;

View File

@ -33,7 +33,9 @@ namespace TelegramBotBase.Builder.Interfaces
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
ISessionSerializationStage CustomCommands(Action<List<BotCommand>> action);
ISessionSerializationStage CustomCommands(Action<Dictionary<BotCommandScope, List<BotCommand>>> action);
}
}

View File

@ -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
/// </summary>
/// <param name="cmds"></param>
/// <param name="description"></param>
public static void Start(this List<BotCommand> cmds, String description)
public static void Start(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description, BotCommandScope scope = null)
{
cmds.Add(new BotCommand() { Command = "start", Description = description });
Add(cmds, "start", description, scope);
}
/// <summary>
@ -22,9 +24,10 @@ namespace TelegramBotBase.Commands
/// </summary>
/// <param name="cmds"></param>
/// <param name="description"></param>
public static void Help(this List<BotCommand> cmds, String description)
public static void Help(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description, BotCommandScope scope = null)
{
cmds.Add(new BotCommand() { Command = "help", Description = description });
Add(cmds, "help", description, scope);
}
/// <summary>
@ -32,9 +35,37 @@ namespace TelegramBotBase.Commands
/// </summary>
/// <param name="cmds"></param>
/// <param name="description"></param>
public static void Settings(this List<BotCommand> cmds, String description)
public static void Settings(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description, BotCommandScope scope = null)
{
cmds.Add(new BotCommand() { Command = "settings", Description = description });
Add(cmds, "settings", description, scope);
}
/// <summary>
/// Adding the command with a description.
/// </summary>
/// <param name="cmds"></param>
/// <param name="command"></param>
/// <param name="description"></param>
public static void Add(this Dictionary<BotCommandScope, List<BotCommand>> 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<BotCommand> { new BotCommand() { Command = command, Description = description } });
}
}
/// <summary>
@ -43,9 +74,85 @@ namespace TelegramBotBase.Commands
/// <param name="cmds"></param>
/// <param name="command"></param>
/// <param name="description"></param>
public static void Add(this List<BotCommand> cmds, String command, String description)
public static void Clear(this Dictionary<BotCommandScope, List<BotCommand>> 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;
}
}
/// <summary>
/// Adding a group command with a description.
/// </summary>
/// <param name="cmds"></param>
/// <param name="command"></param>
/// <param name="description"></param>
public static void AddGroupCommand(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String command, String description)
{
Add(cmds, command, description, new BotCommandScopeAllGroupChats());
}
/// <summary>
/// Clears all group commands.
/// </summary>
/// <param name="cmds"></param>
public static void ClearGroupCommands(this Dictionary<BotCommandScope, List<BotCommand>> cmds)
{
Clear(cmds, new BotCommandScopeAllGroupChats());
}
/// <summary>
/// Adding group admin command with a description.
/// </summary>
/// <param name="cmds"></param>
/// <param name="command"></param>
/// <param name="description"></param>
public static void AddGroupAdminCommand(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String command, String description)
{
Add(cmds, command, description, new BotCommandScopeAllChatAdministrators());
}
/// <summary>
/// Clears all group admin commands.
/// </summary>
/// <param name="cmds"></param>
public static void ClearGroupAdminCommand(this Dictionary<BotCommandScope, List<BotCommand>> cmds)
{
Clear(cmds, new BotCommandScopeAllChatAdministrators());
}
/// <summary>
/// Adding a privat command with a description.
/// </summary>
/// <param name="cmds"></param>
/// <param name="command"></param>
/// <param name="description"></param>
public static void AddPrivateChatCommand(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String command, String description)
{
Add(cmds, command, description, new BotCommandScopeAllPrivateChats());
}
/// <summary>
/// Clears all private commands.
/// </summary>
/// <param name="cmds"></param>
public static void ClearPrivateChatCommand(this Dictionary<BotCommandScope, List<BotCommand>> cmds)
{
Clear(cmds, new BotCommandScopeAllPrivateChats());
}
}
}