From fb7b624ad8402973a917d1d049b5a5c08be2b1a6 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Sat, 11 Apr 2020 19:12:34 +0200 Subject: [PATCH] BotCommands and refactoring - refactoring SystemCalls to BotCommand (due new Telegram Bot update) - Adding UploadBotCommands method to BotBase - refactoring and simplifying BotBase constructors - adding getBotCommands and SetBotCommands to MessageClient --- ...allEventArgs.cs => BotCommandEventArgs.cs} | 6 +- TelegramBotBase/Base/MessageClient.cs | 25 +++++- TelegramBotBase/Base/MessageResult.cs | 14 ++-- TelegramBotBase/BotBase.cs | 81 ++++++++++--------- 4 files changed, 76 insertions(+), 50 deletions(-) rename TelegramBotBase/Args/{SystemCallEventArgs.cs => BotCommandEventArgs.cs} (85%) diff --git a/TelegramBotBase/Args/SystemCallEventArgs.cs b/TelegramBotBase/Args/BotCommandEventArgs.cs similarity index 85% rename from TelegramBotBase/Args/SystemCallEventArgs.cs rename to TelegramBotBase/Args/BotCommandEventArgs.cs index 354bd90..f07909b 100644 --- a/TelegramBotBase/Args/SystemCallEventArgs.cs +++ b/TelegramBotBase/Args/BotCommandEventArgs.cs @@ -11,7 +11,7 @@ namespace TelegramBotBase.Args /// /// Base class for given system call results /// - public class SystemCallEventArgs : EventArgs + public class BotCommandEventArgs : EventArgs { public String Command { get; set; } @@ -26,13 +26,13 @@ namespace TelegramBotBase.Args public Message OriginalMessage { get; set; } - public SystemCallEventArgs() + public BotCommandEventArgs() { } - public SystemCallEventArgs(String Command, List Parameters, Message Message, long DeviceId, DeviceSession Device) + public BotCommandEventArgs(String Command, List Parameters, Message Message, long DeviceId, DeviceSession Device) { this.Command = Command; this.Parameters = Parameters; diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index d57d480..22a71fe 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -6,6 +6,7 @@ using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Telegram.Bot.Types; namespace TelegramBotBase.Base { @@ -15,6 +16,7 @@ namespace TelegramBotBase.Base public class MessageClient { + public String APIKey { get; set; } public Telegram.Bot.TelegramBotClient TelegramClient { get; set; } @@ -40,7 +42,7 @@ namespace TelegramBotBase.Base { this.APIKey = APIKey; this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy); - + Prepare(); } @@ -142,6 +144,27 @@ namespace TelegramBotBase.Base } } + /// + /// This will return the current list of bot commands. + /// + /// + public async Task GetBotCommands() + { + return await this.TelegramClient.GetMyCommandsAsync(); + } + + /// + /// This will set your bot commands to the given list. + /// + /// + /// + public async Task SetBotCommands(List botcommands) + { + await this.TelegramClient.SetMyCommandsAsync(botcommands); + } + + + #region "Events" diff --git a/TelegramBotBase/Base/MessageResult.cs b/TelegramBotBase/Base/MessageResult.cs index da1851f..e1cc460 100644 --- a/TelegramBotBase/Base/MessageResult.cs +++ b/TelegramBotBase/Base/MessageResult.cs @@ -74,9 +74,9 @@ namespace TelegramBotBase.Base } /// - /// Is this a system call ? Starts with a slash '/' and a command + /// Is this a command ? Starts with a slash '/' and a command /// - public bool IsSystemCall + public bool IsBotCommand { get { @@ -87,11 +87,11 @@ namespace TelegramBotBase.Base /// /// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => 123,456,789) /// - public List SystemCallParameters + public List BotCommandParameters { get { - if (!IsSystemCall) + if (!IsBotCommand) return new List(); //Split by empty space and skip first entry (command itself), return as list @@ -100,13 +100,13 @@ namespace TelegramBotBase.Base } /// - /// Returns just the System call command (i.e. /start 1 2 3 => /start) + /// Returns just the command (i.e. /start 1 2 3 => /start) /// - public String SystemCommand + public String BotCommand { get { - if (!IsSystemCall) + if (!IsBotCommand) return null; return this.MessageText.Split(' ')[0]; diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 4b01345..9224cdf 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Telegram.Bot; +using Telegram.Bot.Types; using TelegramBotBase.Args; using TelegramBotBase.Attributes; using TelegramBotBase.Base; @@ -36,7 +37,10 @@ namespace TelegramBotBase /// /// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start /// - public List SystemCalls { get; set; } + public List BotCommands { get; set; } + + + #region "Events" private EventHandlerList __Events = new EventHandlerList(); @@ -46,12 +50,14 @@ namespace TelegramBotBase private static object __evSystemCall = new object(); - public delegate Task SystemCallEventHandler(object sender, SystemCallEventArgs e); + public delegate Task BotCommandEventHandler(object sender, BotCommandEventArgs e); private static object __evException = new object(); private static object __evUnhandledCall = new object(); + #endregion + /// /// Skips all messages during running (good for big delay updates) @@ -77,17 +83,21 @@ namespace TelegramBotBase /// Simple start of your Bot with the APIKey /// /// - public BotBase(String apiKey) + public BotBase(String apiKey, bool initClient = true) { this.APIKey = apiKey; - this.Client = new Base.MessageClient(this.APIKey); - this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0); + if (initClient) + { + this.Client = new Base.MessageClient(this.APIKey); + this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0); - this.SystemCalls = new List(); + this.Sessions.Client = this.Client; + } + + this.BotCommands = new List(); this.Sessions = new SessionBase(); - this.Sessions.Client = this.Client; } /// @@ -95,15 +105,10 @@ namespace TelegramBotBase /// /// /// i.e. https://127.0.0.1:10000 - public BotBase(String apiKey, System.Net.Http.HttpClient proxy) + public BotBase(String apiKey, System.Net.Http.HttpClient proxy) : this(apiKey, false) { - this.APIKey = apiKey; - this.Client = new Base.MessageClient(this.APIKey, proxy); - this.SystemCalls = new List(); - - this.Sessions = new SessionBase(); this.Sessions.Client = this.Client; } @@ -112,15 +117,10 @@ namespace TelegramBotBase /// /// /// - public BotBase(String apiKey, TelegramBotClient client) + public BotBase(String apiKey, TelegramBotClient client) : this(apiKey, false) { - this.APIKey = apiKey; - this.Client = new Base.MessageClient(this.APIKey, client); - this.SystemCalls = new List(); - - this.Sessions = new SessionBase(); this.Sessions.Client = this.Client; } @@ -129,17 +129,12 @@ namespace TelegramBotBase /// /// /// i.e. https://127.0.0.1:10000 - public BotBase(String apiKey, String proxyBaseAddress) + public BotBase(String apiKey, String proxyBaseAddress) : this(apiKey, false) { - this.APIKey = apiKey; - var url = new Uri(proxyBaseAddress); this.Client = new Base.MessageClient(this.APIKey, url); - this.SystemCalls = new List(); - - this.Sessions = new SessionBase(); this.Sessions.Client = this.Client; } @@ -149,15 +144,10 @@ namespace TelegramBotBase /// /// i.e. 127.0.0.1 /// i.e. 10000 - public BotBase(String apiKey, String proxyHost, int proxyPort) + public BotBase(String apiKey, String proxyHost, int proxyPort) : this(apiKey, false) { - this.APIKey = apiKey; - this.Client = new Base.MessageClient(this.APIKey, proxyHost, proxyPort); - this.SystemCalls = new List(); - - this.Sessions = new SessionBase(); this.Sessions.Client = this.Client; } @@ -265,10 +255,10 @@ namespace TelegramBotBase ds.LastMessage = e.Message; //Is this a systemcall ? - if (e.IsSystemCall && this.SystemCalls.Contains(e.SystemCommand)) + if (e.IsBotCommand && this.BotCommands.Count(a => a.Command == e.BotCommand) > 0) { - var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, e.Message, ds.DeviceId, ds); - await OnSystemCall(sce); + var sce = new BotCommandEventArgs(e.BotCommand, e.BotCommandParameters, e.Message, ds.DeviceId, ds); + await OnBotCommand(sce); if (sce.Handled) return; @@ -474,6 +464,16 @@ namespace TelegramBotBase } + /// + /// This method will update all local created bot commands to the botfather. + /// + public async Task UploadBotCommands() + { + await this.Client.SetBotCommands(this.BotCommands); + } + + #region "Events" + /// /// Will be called if a session/context gets started /// @@ -518,15 +518,15 @@ namespace TelegramBotBase } /// - /// Will be called if a system call gets raised + /// Will be called if a bot command gets raised /// - public event SystemCallEventHandler SystemCall; + public event BotCommandEventHandler BotCommand; - public async Task OnSystemCall(SystemCallEventArgs e) + public async Task OnBotCommand(BotCommandEventArgs e) { - if (this.SystemCall != null) - await SystemCall(this, e); + if (this.BotCommand != null) + await BotCommand(this, e); } /// @@ -570,5 +570,8 @@ namespace TelegramBotBase (this.__Events[__evUnhandledCall] as EventHandler)?.Invoke(this, e); } + + #endregion + } }