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
This commit is contained in:
FlorianDahn 2022-05-15 23:20:16 +02:00
parent 2c567419ec
commit 707ffd492f
4 changed files with 67 additions and 13 deletions

View File

@ -41,7 +41,7 @@ namespace TelegramBotBase
/// <summary>
/// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start
/// </summary>
public List<BotCommand> BotCommands { get; set; }
public Dictionary<BotCommandScope, List<BotCommand>> BotCommandScopes { get; set; } = new Dictionary<BotCommandScope, List<BotCommand>>();
#region "Events"
@ -93,7 +93,7 @@ namespace TelegramBotBase
SetSetting(eSettings.SkipAllMessages, false);
SetSetting(eSettings.SaveSessionsOnConsoleExit, false);
this.BotCommands = new List<BotCommand>();
this.BotCommandScopes = new Dictionary<BotCommandScope, List<BotCommand>>();
this.Sessions = new SessionBase();
this.Sessions.BotBase = this;
@ -247,7 +247,34 @@ namespace TelegramBotBase
/// </summary>
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);
}
}
}
/// <summary>
/// Searching if parameter is a known command in all configured BotCommandScopes.
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public bool IsKnownBotCommand(String command)
{
foreach (var scope in this.BotCommandScopes)
{
if (scope.Value.Any(a => "/" + a.Command == command))
return true;
}
return false;
}
/// <summary>

View File

@ -324,7 +324,6 @@ namespace TelegramBotBase.Builder
bb.Sessions.Client = bb.Client;
bb.BotCommandScopes = _BotCommandScopes;
//bb.BotCommands = _botcommands;
bb.StateMachine = _statemachine;

View File

@ -14,9 +14,9 @@ namespace TelegramBotBase.Commands
/// </summary>
/// <param name="cmds"></param>
/// <param name="description"></param>
public static void Start(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description, BotCommandScope scope = null)
public static void Start(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description)
{
Add(cmds, "start", description, scope);
Add(cmds, "start", description, null);
}
/// <summary>
@ -25,9 +25,9 @@ namespace TelegramBotBase.Commands
/// <param name="cmds"></param>
/// <param name="description"></param>
public static void Help(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description, BotCommandScope scope = null)
public static void Help(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description)
{
Add(cmds, "help", description, scope);
Add(cmds, "help", description, null);
}
/// <summary>
@ -37,9 +37,9 @@ namespace TelegramBotBase.Commands
/// <param name="description"></param>
public static void Settings(this Dictionary<BotCommandScope, List<BotCommand>> cmds, String description, BotCommandScope scope = null)
public static void Settings(this Dictionary<BotCommandScope, List<BotCommand>> 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;
}
}
/// <summary>
/// Clears all default commands.
/// </summary>
/// <param name="cmds"></param>
public static void ClearDefaultCommands(this Dictionary<BotCommandScope, List<BotCommand>> cmds)
{
Clear(cmds, null);
}
/// <summary>
/// Clears all commands of a specific device.
/// </summary>
/// <param name="cmds"></param>
public static void ClearChatCommands(this Dictionary<BotCommandScope, List<BotCommand>> cmds, long DeviceId)
{
Clear(cmds, new BotCommandScopeChat() { ChatId = DeviceId });
}
/// <summary>
/// Adding a chat command with a description.
/// </summary>
/// <param name="cmds"></param>
/// <param name="command"></param>
/// <param name="description"></param>
public static void AddChatCommand(this Dictionary<BotCommandScope, List<BotCommand>> cmds, long DeviceId, String command, String description)
{
Add(cmds, command, description, new BotCommandScopeChat() { ChatId = DeviceId });
}
/// <summary>

View File

@ -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);