From fe79de9bb7cc3dc8933f05a1182d9b467550c825 Mon Sep 17 00:00:00 2001 From: Daniil547 <72149318+Daniil547@users.noreply.github.com> Date: Tue, 6 Oct 2020 15:49:49 +0000 Subject: [PATCH] Update AbilityUtils.java --- .../abilitybots/api/util/AbilityUtils.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java index 6b8ca9ce..e639682e 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java @@ -269,4 +269,29 @@ public final class AbilityUtils { public static String escape(String username) { return username.replace("_", "\\_"); } -} \ No newline at end of file + + /** + * Checks if the passed string is a valid bot command according to the requirements of Telegram Bot API: + * "A command must always start with the '/' symbol and may not be longer than 32 characters. + * Commands can use latin letters, numbers and underscores." + * (https://core.telegram.org/bots#commands) + * + * @param command String representation of a command to be checked for validity + * @return whether the command is valid + */ + public static boolean isValidCommand(String command){ + if (command == null || command.length() > 32) return false; + return command.matches("/[A-Za-z_0-9]+"); + } + + /** + * Checks if the passed String is a valid command name. Command name is text of a command without leading '/' + * + * @param commandName the command's name to be checked for validity + * @return whether the command's name is valid + */ + public static boolean isValidCommandName(String commandName){ + if (commandName == null || commandName.length() > 31) return false; + return commandName.matches("[A-Za-z_0-9]+"); + } +}