This commit is contained in:
Rubenlagus 2016-08-24 23:08:56 +02:00
parent 6b9fdec891
commit add72983a3
2 changed files with 42 additions and 4 deletions

View File

@ -20,7 +20,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
private final CommandRegistry commandRegistry; 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 * Use ICommandRegistry's methods on this bot to register commands
*/ */
public TelegramLongPollingCommandBot() { 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 * Use ICommandRegistry's methods on this bot to register commands
* @param options Bot options
*/ */
public TelegramLongPollingCommandBot(BotOptions 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); super(options);
this.commandRegistry = new CommandRegistry(); this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, getBotUsername());
} }
@Override @Override

View File

@ -17,8 +17,20 @@ import java.util.function.BiConsumer;
public final class CommandRegistry implements ICommandRegistry { public final class CommandRegistry implements ICommandRegistry {
private final Map<String, BotCommand> commandRegistryMap = new HashMap<>(); private final Map<String, BotCommand> commandRegistryMap = new HashMap<>();
private final boolean allowCommandsWithUsername;
private final String botUsername;
private BiConsumer<AbsSender, Message> defaultConsumer; private BiConsumer<AbsSender, Message> 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 @Override
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) { public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
this.defaultConsumer = defaultConsumer; this.defaultConsumer = defaultConsumer;
@ -87,7 +99,7 @@ public final class CommandRegistry implements ICommandRegistry {
String commandMessage = text.substring(1); String commandMessage = text.substring(1);
String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR); String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR);
String command = commandSplit[0]; String command = removeUsernameFromCommandIfNeeded(commandSplit[0]);
if (commandRegistryMap.containsKey(command)) { if (commandRegistryMap.containsKey(command)) {
String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
@ -101,4 +113,17 @@ public final class CommandRegistry implements ICommandRegistry {
} }
return false; 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;
}
} }