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:
parent
2c567419ec
commit
707ffd492f
@ -41,7 +41,7 @@ namespace TelegramBotBase
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start
|
/// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<BotCommand> BotCommands { get; set; }
|
public Dictionary<BotCommandScope, List<BotCommand>> BotCommandScopes { get; set; } = new Dictionary<BotCommandScope, List<BotCommand>>();
|
||||||
|
|
||||||
|
|
||||||
#region "Events"
|
#region "Events"
|
||||||
@ -93,7 +93,7 @@ namespace TelegramBotBase
|
|||||||
SetSetting(eSettings.SkipAllMessages, false);
|
SetSetting(eSettings.SkipAllMessages, false);
|
||||||
SetSetting(eSettings.SaveSessionsOnConsoleExit, false);
|
SetSetting(eSettings.SaveSessionsOnConsoleExit, false);
|
||||||
|
|
||||||
this.BotCommands = new List<BotCommand>();
|
this.BotCommandScopes = new Dictionary<BotCommandScope, List<BotCommand>>();
|
||||||
|
|
||||||
this.Sessions = new SessionBase();
|
this.Sessions = new SessionBase();
|
||||||
this.Sessions.BotBase = this;
|
this.Sessions.BotBase = this;
|
||||||
@ -247,7 +247,34 @@ namespace TelegramBotBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task UploadBotCommands()
|
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>
|
/// <summary>
|
||||||
|
|||||||
@ -324,7 +324,6 @@ namespace TelegramBotBase.Builder
|
|||||||
bb.Sessions.Client = bb.Client;
|
bb.Sessions.Client = bb.Client;
|
||||||
|
|
||||||
bb.BotCommandScopes = _BotCommandScopes;
|
bb.BotCommandScopes = _BotCommandScopes;
|
||||||
//bb.BotCommands = _botcommands;
|
|
||||||
|
|
||||||
bb.StateMachine = _statemachine;
|
bb.StateMachine = _statemachine;
|
||||||
|
|
||||||
|
|||||||
@ -14,9 +14,9 @@ namespace TelegramBotBase.Commands
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cmds"></param>
|
/// <param name="cmds"></param>
|
||||||
/// <param name="description"></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>
|
/// <summary>
|
||||||
@ -25,9 +25,9 @@ namespace TelegramBotBase.Commands
|
|||||||
/// <param name="cmds"></param>
|
/// <param name="cmds"></param>
|
||||||
/// <param name="description"></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>
|
/// <summary>
|
||||||
@ -37,9 +37,9 @@ namespace TelegramBotBase.Commands
|
|||||||
/// <param name="description"></param>
|
/// <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)
|
if (scope == null)
|
||||||
{
|
{
|
||||||
scope = new BotCommandScopeDefault();
|
scope = BotCommandScope.Default();
|
||||||
}
|
}
|
||||||
|
|
||||||
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
|
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
|
||||||
@ -78,7 +78,7 @@ namespace TelegramBotBase.Commands
|
|||||||
{
|
{
|
||||||
if (scope == null)
|
if (scope == null)
|
||||||
{
|
{
|
||||||
scope = new BotCommandScopeDefault();
|
scope = BotCommandScope.Default();
|
||||||
}
|
}
|
||||||
|
|
||||||
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
|
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
|
||||||
@ -91,8 +91,36 @@ namespace TelegramBotBase.Commands
|
|||||||
{
|
{
|
||||||
cmds[scope] = null;
|
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>
|
/// <summary>
|
||||||
|
|||||||
@ -37,7 +37,7 @@ namespace TelegramBotBase.Factories.MessageLoops
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Is this a bot command ?
|
//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);
|
var sce = new BotCommandEventArgs(mr.BotCommand, mr.BotCommandParameters, mr.Message, session.DeviceId, session);
|
||||||
await Bot.OnBotCommand(sce);
|
await Bot.OnBotCommand(sce);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user