2017-06-18 05:34:13 +02:00
|
|
|
package org.telegram.telegrambots.bots.commandbot.commands;
|
2016-05-20 11:12:22 +02:00
|
|
|
|
|
|
|
import org.telegram.telegrambots.api.objects.Message;
|
2016-05-22 14:10:57 +02:00
|
|
|
import org.telegram.telegrambots.bots.AbsSender;
|
2016-05-20 11:12:22 +02:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2016-05-27 03:19:17 +02:00
|
|
|
import java.util.function.BiConsumer;
|
2016-05-20 11:12:22 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-31 10:54:40 +02:00
|
|
|
* This class manages all the commands for a bot. You can register and deregister commands on demand
|
|
|
|
*
|
2016-05-31 20:01:50 +02:00
|
|
|
* @author Timo Schulz (Mit0x2)
|
2016-05-20 11:12:22 +02:00
|
|
|
*/
|
2016-05-20 11:16:39 +02:00
|
|
|
public final class CommandRegistry implements ICommandRegistry {
|
2016-05-20 11:12:22 +02:00
|
|
|
|
2016-05-20 11:56:15 +02:00
|
|
|
private final Map<String, BotCommand> commandRegistryMap = new HashMap<>();
|
2016-08-24 23:08:56 +02:00
|
|
|
private final boolean allowCommandsWithUsername;
|
|
|
|
private final String botUsername;
|
2016-05-27 03:19:17 +02:00
|
|
|
private BiConsumer<AbsSender, Message> defaultConsumer;
|
2016-05-20 11:12:22 +02:00
|
|
|
|
2016-08-24 23:08:56 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2016-05-27 03:19:17 +02:00
|
|
|
@Override
|
|
|
|
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
|
|
|
|
this.defaultConsumer = defaultConsumer;
|
2016-05-20 11:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 11:56:15 +02:00
|
|
|
public final boolean register(BotCommand botCommand) {
|
|
|
|
if (commandRegistryMap.containsKey(botCommand.getCommandIdentifier())) {
|
2016-05-20 11:12:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-20 11:56:15 +02:00
|
|
|
commandRegistryMap.put(botCommand.getCommandIdentifier(), botCommand);
|
2016-05-20 11:12:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 11:56:15 +02:00
|
|
|
public final Map<BotCommand, Boolean> registerAll(BotCommand... botCommands) {
|
|
|
|
Map<BotCommand, Boolean> resultMap = new HashMap<>(botCommands.length);
|
|
|
|
for (BotCommand botCommand : botCommands) {
|
|
|
|
resultMap.put(botCommand, register(botCommand));
|
2016-05-20 11:12:22 +02:00
|
|
|
}
|
|
|
|
return resultMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 11:56:15 +02:00
|
|
|
public final boolean deregister(BotCommand botCommand) {
|
|
|
|
if (commandRegistryMap.containsKey(botCommand.getCommandIdentifier())) {
|
|
|
|
commandRegistryMap.remove(botCommand.getCommandIdentifier());
|
2016-05-20 11:12:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 11:56:15 +02:00
|
|
|
public final Map<BotCommand, Boolean> deregisterAll(BotCommand... botCommands) {
|
|
|
|
Map<BotCommand, Boolean> resultMap = new HashMap<>(botCommands.length);
|
|
|
|
for (BotCommand botCommand : botCommands) {
|
|
|
|
resultMap.put(botCommand, deregister(botCommand));
|
2016-05-20 11:12:22 +02:00
|
|
|
}
|
|
|
|
return resultMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 11:56:15 +02:00
|
|
|
public final Collection<BotCommand> getRegisteredCommands() {
|
2016-05-20 11:12:22 +02:00
|
|
|
return commandRegistryMap.values();
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:49:06 +02:00
|
|
|
@Override
|
|
|
|
public final BotCommand getRegisteredCommand(String commandIdentifier) {
|
|
|
|
return commandRegistryMap.get(commandIdentifier);
|
|
|
|
}
|
|
|
|
|
2016-05-20 11:12:22 +02:00
|
|
|
/**
|
2016-05-27 03:19:17 +02:00
|
|
|
* Executes a command action if the command is registered.
|
2016-05-20 11:12:22 +02:00
|
|
|
*
|
2016-05-27 03:19:17 +02:00
|
|
|
* @note If the command is not registered and there is a default consumer,
|
|
|
|
* that action will be performed
|
|
|
|
*
|
|
|
|
* @param absSender absSender
|
2016-05-20 11:12:22 +02:00
|
|
|
* @param message input message
|
2016-05-27 03:19:17 +02:00
|
|
|
* @return True if a command or default action is executed, false otherwise
|
2016-05-20 11:12:22 +02:00
|
|
|
*/
|
2016-05-22 14:10:57 +02:00
|
|
|
public final boolean executeCommand(AbsSender absSender, Message message) {
|
2016-05-20 11:12:22 +02:00
|
|
|
if (message.hasText()) {
|
|
|
|
String text = message.getText();
|
2016-05-27 03:19:17 +02:00
|
|
|
if (text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
|
2016-05-20 11:12:22 +02:00
|
|
|
String commandMessage = text.substring(1);
|
2017-09-17 00:10:38 +02:00
|
|
|
String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR);
|
2016-05-20 11:12:22 +02:00
|
|
|
|
2016-08-24 23:08:56 +02:00
|
|
|
String command = removeUsernameFromCommandIfNeeded(commandSplit[0]);
|
2016-05-20 11:12:22 +02:00
|
|
|
|
|
|
|
if (commandRegistryMap.containsKey(command)) {
|
|
|
|
String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
|
2016-05-31 19:57:54 +02:00
|
|
|
commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters);
|
2016-05-20 11:12:22 +02:00
|
|
|
return true;
|
2016-05-27 03:19:17 +02:00
|
|
|
} else if (defaultConsumer != null) {
|
|
|
|
defaultConsumer.accept(absSender, message);
|
|
|
|
return true;
|
2016-05-20 11:12:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-24 23:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2017-09-17 00:10:38 +02:00
|
|
|
}
|