From a7411176e6fa036fa389f6b0419e929b90c8d772 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Wed, 29 May 2024 21:29:14 +0200 Subject: [PATCH] Fixing the prototype solution of group commands detection --- TelegramBotBase/Base/MessageResult.cs | 56 ++++++++++++++------------- TelegramBotBase/BotBase.cs | 9 ++--- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/TelegramBotBase/Base/MessageResult.cs b/TelegramBotBase/Base/MessageResult.cs index 20b2bc6..c4eb955 100644 --- a/TelegramBotBase/Base/MessageResult.cs +++ b/TelegramBotBase/Base/MessageResult.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; @@ -9,16 +10,35 @@ namespace TelegramBotBase.Base; public class MessageResult : ResultBase { - internal MessageResult() - { - } - public MessageResult(Update update) { UpdateData = update; + + init(); } - public Update UpdateData { get; set; } + void init() + { + IsAction = UpdateData.CallbackQuery != null; + + IsBotCommand = UpdateData.Message.Entities.Any(a => a.Type == MessageEntityType.BotCommand); + + if (!IsBotCommand) + return; + + BotCommand = MessageText.Split(' ')[0]; + + IsBotGroupCommand = BotCommand.Contains("@"); + + if (IsBotGroupCommand) + { + BotCommand = BotCommand.Substring(0, BotCommand.LastIndexOf('@')); + } + + + } + + public Update UpdateData { get; private set; } /// /// Returns the Device/ChatId @@ -55,17 +75,17 @@ public class MessageResult : ResultBase /// /// Is this an action ? (i.e. button click) /// - public bool IsAction => UpdateData.CallbackQuery != null; + public bool IsAction { get; private set; } /// /// Is this a command ? Starts with a slash '/' and a command /// - public bool IsBotCommand => MessageText.StartsWith("/"); + public bool IsBotCommand { get; private set; } /// /// Is this a bot command sent from a group via @BotId ? /// - public bool IsBotGroupCommand => IsBotCommand && MessageText.Contains("@"); + public bool IsBotGroupCommand { get; private set; } /// /// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => @@ -88,23 +108,7 @@ public class MessageResult : ResultBase /// /// Returns just the command (i.e. /start 1 2 3 => /start) /// - public string BotCommand - { - get - { - if (IsBotGroupCommand) - { - return MessageText.Substring(0, MessageText.LastIndexOf('@')).Split(' ')[0]; - } - - if (IsBotCommand) - { - return MessageText.Split(' ')[0]; - } - - return null; - } - } + public string BotCommand { get; private set; } /// /// Returns if this message will be used on the first form or not. diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 8332aa2..c7d865a 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -197,13 +197,10 @@ public sealed class BotBase /// Contains the device/chat id of the device to update. public async Task InvokeMessageLoop(long deviceId) { - var mr = new MessageResult + var mr = new MessageResult(new Update { - UpdateData = new Update - { - Message = new Message() - } - }; + Message = new Message() + }); await InvokeMessageLoop(deviceId, mr); }