diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index d28d4947..b3b31cef 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -20,7 +20,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB private final CommandRegistry commandRegistry; /** - * construct creates CommandRegistry for this bot. + * Creates a TelegramLongPollingCommandBot using default options * Use ICommandRegistry's methods on this bot to register commands */ public TelegramLongPollingCommandBot() { @@ -28,12 +28,25 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB } /** - * construct creates CommandRegistry for this bot. + * Creates a TelegramLongPollingCommandBot with custom options and allowing commands with + * usernames * Use ICommandRegistry's methods on this bot to register commands + * @param options Bot options */ public TelegramLongPollingCommandBot(BotOptions options) { + this(options, true); + } + + /** + * Creates a TelegramLongPollingCommandBot + * Use ICommandRegistry's methods on this bot to register commands + * @param options Bot options + * @param allowCommandsWithUsername true to allow commands with parameters (default), + * false otherwise + */ + public TelegramLongPollingCommandBot(BotOptions options, boolean allowCommandsWithUsername) { super(options); - this.commandRegistry = new CommandRegistry(); + this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, getBotUsername()); } @Override diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index bdc75e6d..e43c273b 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -17,8 +17,20 @@ import java.util.function.BiConsumer; public final class CommandRegistry implements ICommandRegistry { private final Map commandRegistryMap = new HashMap<>(); + private final boolean allowCommandsWithUsername; + private final String botUsername; private BiConsumer defaultConsumer; + /** + * Creates a Command registry + * @param allowCommandsWithUsername True to allow commands with username, false otherwise + * @param botUsername Bot username + */ + public CommandRegistry(boolean allowCommandsWithUsername, String botUsername) { + this.allowCommandsWithUsername = allowCommandsWithUsername; + this.botUsername = botUsername; + } + @Override public void registerDefaultAction(BiConsumer defaultConsumer) { this.defaultConsumer = defaultConsumer; @@ -87,7 +99,7 @@ public final class CommandRegistry implements ICommandRegistry { String commandMessage = text.substring(1); String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR); - String command = commandSplit[0]; + String command = removeUsernameFromCommandIfNeeded(commandSplit[0]); if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); @@ -101,4 +113,17 @@ public final class CommandRegistry implements ICommandRegistry { } return false; } + + /** + * if {@link #allowCommandsWithUsername} is enabled, the username of the bot is removed from + * the command + * @param command Command to simplify + * @return Simplified command + */ + private String removeUsernameFromCommandIfNeeded(String command) { + if (allowCommandsWithUsername) { + return command.replace("@" + botUsername, "").trim(); + } + return command; + } } \ No newline at end of file