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
This commit is contained in:
FlorianDahn 2020-04-11 19:12:34 +02:00
parent 965e6e18b9
commit fb7b624ad8
4 changed files with 76 additions and 50 deletions

View File

@ -11,7 +11,7 @@ namespace TelegramBotBase.Args
/// <summary> /// <summary>
/// Base class for given system call results /// Base class for given system call results
/// </summary> /// </summary>
public class SystemCallEventArgs : EventArgs public class BotCommandEventArgs : EventArgs
{ {
public String Command { get; set; } public String Command { get; set; }
@ -26,13 +26,13 @@ namespace TelegramBotBase.Args
public Message OriginalMessage { get; set; } public Message OriginalMessage { get; set; }
public SystemCallEventArgs() public BotCommandEventArgs()
{ {
} }
public SystemCallEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device) public BotCommandEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
{ {
this.Command = Command; this.Command = Command;
this.Parameters = Parameters; this.Parameters = Parameters;

View File

@ -6,6 +6,7 @@ using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot.Types;
namespace TelegramBotBase.Base namespace TelegramBotBase.Base
{ {
@ -15,6 +16,7 @@ namespace TelegramBotBase.Base
public class MessageClient public class MessageClient
{ {
public String APIKey { get; set; } public String APIKey { get; set; }
public Telegram.Bot.TelegramBotClient TelegramClient { get; set; } public Telegram.Bot.TelegramBotClient TelegramClient { get; set; }
@ -142,6 +144,27 @@ namespace TelegramBotBase.Base
} }
} }
/// <summary>
/// This will return the current list of bot commands.
/// </summary>
/// <returns></returns>
public async Task<BotCommand[]> GetBotCommands()
{
return await this.TelegramClient.GetMyCommandsAsync();
}
/// <summary>
/// This will set your bot commands to the given list.
/// </summary>
/// <param name="botcommands"></param>
/// <returns></returns>
public async Task SetBotCommands(List<BotCommand> botcommands)
{
await this.TelegramClient.SetMyCommandsAsync(botcommands);
}
#region "Events" #region "Events"

View File

@ -74,9 +74,9 @@ namespace TelegramBotBase.Base
} }
/// <summary> /// <summary>
/// Is this a system call ? Starts with a slash '/' and a command /// Is this a command ? Starts with a slash '/' and a command
/// </summary> /// </summary>
public bool IsSystemCall public bool IsBotCommand
{ {
get get
{ {
@ -87,11 +87,11 @@ namespace TelegramBotBase.Base
/// <summary> /// <summary>
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => 123,456,789) /// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => 123,456,789)
/// </summary> /// </summary>
public List<String> SystemCallParameters public List<String> BotCommandParameters
{ {
get get
{ {
if (!IsSystemCall) if (!IsBotCommand)
return new List<string>(); return new List<string>();
//Split by empty space and skip first entry (command itself), return as list //Split by empty space and skip first entry (command itself), return as list
@ -100,13 +100,13 @@ namespace TelegramBotBase.Base
} }
/// <summary> /// <summary>
/// Returns just the System call command (i.e. /start 1 2 3 => /start) /// Returns just the command (i.e. /start 1 2 3 => /start)
/// </summary> /// </summary>
public String SystemCommand public String BotCommand
{ {
get get
{ {
if (!IsSystemCall) if (!IsBotCommand)
return null; return null;
return this.MessageText.Split(' ')[0]; return this.MessageText.Split(' ')[0];

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types;
using TelegramBotBase.Args; using TelegramBotBase.Args;
using TelegramBotBase.Attributes; using TelegramBotBase.Attributes;
using TelegramBotBase.Base; using TelegramBotBase.Base;
@ -36,7 +37,10 @@ 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<String> SystemCalls { get; set; } public List<BotCommand> BotCommands { get; set; }
#region "Events"
private EventHandlerList __Events = new EventHandlerList(); private EventHandlerList __Events = new EventHandlerList();
@ -46,12 +50,14 @@ namespace TelegramBotBase
private static object __evSystemCall = new object(); 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 __evException = new object();
private static object __evUnhandledCall = new object(); private static object __evUnhandledCall = new object();
#endregion
/// <summary> /// <summary>
/// Skips all messages during running (good for big delay updates) /// Skips all messages during running (good for big delay updates)
@ -77,17 +83,21 @@ namespace TelegramBotBase
/// Simple start of your Bot with the APIKey /// Simple start of your Bot with the APIKey
/// </summary> /// </summary>
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
public BotBase(String apiKey) public BotBase(String apiKey, bool initClient = true)
{ {
this.APIKey = apiKey; this.APIKey = apiKey;
this.Client = new Base.MessageClient(this.APIKey); if (initClient)
this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0); {
this.Client = new Base.MessageClient(this.APIKey);
this.Client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
this.SystemCalls = new List<string>(); this.Sessions.Client = this.Client;
}
this.BotCommands = new List<BotCommand>();
this.Sessions = new SessionBase(); this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
} }
/// <summary> /// <summary>
@ -95,15 +105,10 @@ namespace TelegramBotBase
/// </summary> /// </summary>
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
/// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param> /// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param>
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.Client = new Base.MessageClient(this.APIKey, proxy);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client; this.Sessions.Client = this.Client;
} }
@ -112,15 +117,10 @@ namespace TelegramBotBase
/// </summary> /// </summary>
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
/// <param name="client"></param> /// <param name="client"></param>
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.Client = new Base.MessageClient(this.APIKey, client);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client; this.Sessions.Client = this.Client;
} }
@ -129,17 +129,12 @@ namespace TelegramBotBase
/// </summary> /// </summary>
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
/// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param> /// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param>
public BotBase(String apiKey, String proxyBaseAddress) public BotBase(String apiKey, String proxyBaseAddress) : this(apiKey, false)
{ {
this.APIKey = apiKey;
var url = new Uri(proxyBaseAddress); var url = new Uri(proxyBaseAddress);
this.Client = new Base.MessageClient(this.APIKey, url); this.Client = new Base.MessageClient(this.APIKey, url);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client; this.Sessions.Client = this.Client;
} }
@ -149,15 +144,10 @@ namespace TelegramBotBase
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
/// <param name="proxyHost">i.e. 127.0.0.1</param> /// <param name="proxyHost">i.e. 127.0.0.1</param>
/// <param name="proxyPort">i.e. 10000</param> /// <param name="proxyPort">i.e. 10000</param>
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.Client = new Base.MessageClient(this.APIKey, proxyHost, proxyPort);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client; this.Sessions.Client = this.Client;
} }
@ -265,10 +255,10 @@ namespace TelegramBotBase
ds.LastMessage = e.Message; ds.LastMessage = e.Message;
//Is this a systemcall ? //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); var sce = new BotCommandEventArgs(e.BotCommand, e.BotCommandParameters, e.Message, ds.DeviceId, ds);
await OnSystemCall(sce); await OnBotCommand(sce);
if (sce.Handled) if (sce.Handled)
return; return;
@ -474,6 +464,16 @@ namespace TelegramBotBase
} }
/// <summary>
/// This method will update all local created bot commands to the botfather.
/// </summary>
public async Task UploadBotCommands()
{
await this.Client.SetBotCommands(this.BotCommands);
}
#region "Events"
/// <summary> /// <summary>
/// Will be called if a session/context gets started /// Will be called if a session/context gets started
/// </summary> /// </summary>
@ -518,15 +518,15 @@ namespace TelegramBotBase
} }
/// <summary> /// <summary>
/// Will be called if a system call gets raised /// Will be called if a bot command gets raised
/// </summary> /// </summary>
public event SystemCallEventHandler SystemCall; public event BotCommandEventHandler BotCommand;
public async Task OnSystemCall(SystemCallEventArgs e) public async Task OnBotCommand(BotCommandEventArgs e)
{ {
if (this.SystemCall != null) if (this.BotCommand != null)
await SystemCall(this, e); await BotCommand(this, e);
} }
/// <summary> /// <summary>
@ -570,5 +570,8 @@ namespace TelegramBotBase
(this.__Events[__evUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e); (this.__Events[__evUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e);
} }
#endregion
} }
} }