Merge pull request #814 from Daniil547/master

Change Ability name check according to Telegram bot API requirements
This commit is contained in:
Ruben Bermudez 2020-10-26 01:15:40 +00:00 committed by GitHub
commit 5d058d41c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -50,9 +50,8 @@ public final class Ability {
@SafeVarargs @SafeVarargs
private Ability(String name, String info, Locality locality, Privacy privacy, int argNum, boolean statsEnabled, Consumer<MessageContext> action, Consumer<MessageContext> postAction, List<Reply> replies, Predicate<Update>... flags) { private Ability(String name, String info, Locality locality, Privacy privacy, int argNum, boolean statsEnabled, Consumer<MessageContext> action, Consumer<MessageContext> postAction, List<Reply> replies, Predicate<Update>... flags) {
checkArgument(!isEmpty(name), "Method name cannot be empty"); checkArgument(isValidCommandName(name), "Method name can only contain alpha-numeric characters and underscores," +
checkArgument(!containsWhitespace(name), "Method name cannot contain spaces"); " cannot be longer than 31 characters, empty or null", name);
checkArgument(isAlphanumeric(name), "Method name can only be alpha-numeric", name);
this.name = name; this.name = name;
this.info = info; this.info = info;

View File

@ -269,4 +269,29 @@ public final class AbilityUtils {
public static String escape(String username) { public static String escape(String username) {
return username.replace("_", "\\_"); return username.replace("_", "\\_");
} }
/**
* 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 name to be checked for validity
* @return whether the command 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]+");
}
} }