rename Command class and fix visibility of execute method
This commit is contained in:
parent
9958d677e2
commit
5fe18aefa3
@ -7,10 +7,10 @@ import org.telegram.telegrambots.bots.AbsSender;
|
||||
*
|
||||
* @author tschulz
|
||||
*/
|
||||
public abstract class Command extends AbsSender {
|
||||
public abstract class BotCommand extends AbsSender {
|
||||
|
||||
public final static String COMMAND_INIT_CHARACTER = "/";
|
||||
public final static String COMMAND_PARAMETER_SEPERATOR = " ";
|
||||
public final static String COMMAND_PARAMETER_SEPARATOR = " ";
|
||||
private final static int COMMAND_MAX_LENGTH = 32;
|
||||
|
||||
private final String commandIdentifier;
|
||||
@ -23,7 +23,7 @@ public abstract class Command extends AbsSender {
|
||||
* @param commandIdentifier the unique identifier of this command (e.g. the command string to enter into chat)
|
||||
* @param description the description of this command
|
||||
*/
|
||||
public Command(String commandIdentifier, String description, String botToken) {
|
||||
public BotCommand(String commandIdentifier, String description, String botToken) {
|
||||
|
||||
if (commandIdentifier == null || commandIdentifier.isEmpty()) {
|
||||
throw new IllegalArgumentException("commandIdentifier for command cannot be null or empty");
|
||||
@ -56,12 +56,12 @@ public abstract class Command extends AbsSender {
|
||||
*
|
||||
* @return the description as String
|
||||
*/
|
||||
public String getDescription() {
|
||||
public final String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotToken() {
|
||||
public final String getBotToken() {
|
||||
return botToken;
|
||||
}
|
||||
|
||||
@ -71,5 +71,5 @@ public abstract class Command extends AbsSender {
|
||||
* @param arguments passed arguments
|
||||
* @param chatId id of the chat, to be able to send replies
|
||||
*/
|
||||
abstract void execute(String[] arguments, long chatId);
|
||||
public abstract void execute(String[] arguments, long chatId);
|
||||
}
|
@ -12,50 +12,50 @@ import java.util.Map;
|
||||
*/
|
||||
public final class CommandRegistry implements ICommandRegistry {
|
||||
|
||||
private final Map<String, Command> commandRegistryMap = new HashMap<>();
|
||||
private final Map<String, BotCommand> commandRegistryMap = new HashMap<>();
|
||||
|
||||
public CommandRegistry(String botToken) {
|
||||
register(new HelpCommand(this, botToken));
|
||||
register(new HelpBotCommand(this, botToken));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean register(Command command) {
|
||||
if (commandRegistryMap.containsKey(command.getCommandIdentifier())) {
|
||||
public final boolean register(BotCommand botCommand) {
|
||||
if (commandRegistryMap.containsKey(botCommand.getCommandIdentifier())) {
|
||||
return false;
|
||||
}
|
||||
commandRegistryMap.put(command.getCommandIdentifier(), command);
|
||||
commandRegistryMap.put(botCommand.getCommandIdentifier(), botCommand);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<Command, Boolean> registerAll(Command... commands) {
|
||||
Map<Command, Boolean> resultMap = new HashMap<>(commands.length);
|
||||
for (Command command : commands) {
|
||||
resultMap.put(command, register(command));
|
||||
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));
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean deregister(Command command) {
|
||||
if (commandRegistryMap.containsKey(command.getCommandIdentifier())) {
|
||||
commandRegistryMap.remove(command.getCommandIdentifier());
|
||||
public final boolean deregister(BotCommand botCommand) {
|
||||
if (commandRegistryMap.containsKey(botCommand.getCommandIdentifier())) {
|
||||
commandRegistryMap.remove(botCommand.getCommandIdentifier());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<Command, Boolean> deregisterAll(Command... commands) {
|
||||
Map<Command, Boolean> resultMap = new HashMap<>(commands.length);
|
||||
for (Command command : commands) {
|
||||
resultMap.put(command, deregister(command));
|
||||
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));
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Collection<Command> getRegisteredCommands() {
|
||||
public final Collection<BotCommand> getRegisteredCommands() {
|
||||
return commandRegistryMap.values();
|
||||
}
|
||||
|
||||
@ -68,9 +68,9 @@ public final class CommandRegistry implements ICommandRegistry {
|
||||
public final boolean executeCommand(Message message) {
|
||||
if (message.hasText()) {
|
||||
String text = message.getText();
|
||||
if (!text.isEmpty() && text.startsWith(Command.COMMAND_INIT_CHARACTER)) {
|
||||
if (!text.isEmpty() && text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
|
||||
String commandMessage = text.substring(1);
|
||||
String[] commandSplit = commandMessage.split(Command.COMMAND_PARAMETER_SEPERATOR);
|
||||
String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR);
|
||||
|
||||
String command = commandSplit[0];
|
||||
|
||||
|
@ -9,23 +9,23 @@ import org.telegram.telegrambots.api.methods.send.SendMessage;
|
||||
*
|
||||
* @author tschulz
|
||||
*/
|
||||
public class HelpCommand extends Command {
|
||||
public class HelpBotCommand extends BotCommand {
|
||||
|
||||
private static final String LOGTAG = "HELPCOMMAND";
|
||||
private final ICommandRegistry commandRegistry;
|
||||
|
||||
public HelpCommand(ICommandRegistry commandRegistry, String botToken) {
|
||||
public HelpBotCommand(ICommandRegistry commandRegistry, String botToken) {
|
||||
super("help", "Gives an overview over all Commands registered for this bot", botToken);
|
||||
this.commandRegistry = commandRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
void execute(String[] arguments, long chatId) {
|
||||
for (Command registeredCommand : commandRegistry.getRegisteredCommands()) {
|
||||
public void execute(String[] arguments, long chatId) {
|
||||
for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
sendMessage.setChatId(chatId);
|
||||
sendMessage.enableHtml(true);
|
||||
sendMessage.setText("<b>" + COMMAND_INIT_CHARACTER + registeredCommand.getCommandIdentifier() + "</b>\n" + registeredCommand.getDescription());
|
||||
sendMessage.setText("<b>" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "</b>\n" + registeredBotCommand.getDescription());
|
||||
|
||||
try {
|
||||
sendMessage(sendMessage);
|
@ -11,40 +11,40 @@ public interface ICommandRegistry {
|
||||
/**
|
||||
* register a command
|
||||
*
|
||||
* @param command the command to register
|
||||
* @param botCommand the command to register
|
||||
* @return whether the command could be registered, was not already registered
|
||||
*/
|
||||
boolean register(Command command);
|
||||
boolean register(BotCommand botCommand);
|
||||
|
||||
/**
|
||||
* register multiple commands
|
||||
*
|
||||
* @param commands commands to register
|
||||
* @param botCommands commands to register
|
||||
* @return map with results of the command register per command
|
||||
*/
|
||||
Map<Command, Boolean> registerAll(Command... commands);
|
||||
Map<BotCommand, Boolean> registerAll(BotCommand... botCommands);
|
||||
|
||||
/**
|
||||
* deregister a command
|
||||
*
|
||||
* @param command the command to deregister
|
||||
* @param botCommand the command to deregister
|
||||
* @return whether the command could be deregistered, was registered
|
||||
*/
|
||||
boolean deregister(Command command);
|
||||
boolean deregister(BotCommand botCommand);
|
||||
|
||||
/**
|
||||
* deregister multiple commands
|
||||
*
|
||||
* @param commands commands to deregister
|
||||
* @param botCommands commands to deregister
|
||||
* @return map with results of the command deregistered per command
|
||||
*/
|
||||
Map<Command, Boolean> deregisterAll(Command... commands);
|
||||
Map<BotCommand, Boolean> deregisterAll(BotCommand... botCommands);
|
||||
|
||||
/**
|
||||
* get a collection of all registered commands
|
||||
*
|
||||
* @return a collection of registered commands
|
||||
*/
|
||||
Collection<Command> getRegisteredCommands();
|
||||
Collection<BotCommand> getRegisteredCommands();
|
||||
|
||||
}
|
@ -2,7 +2,7 @@ package org.telegram.telegrambots.bots;
|
||||
|
||||
import org.telegram.telegrambots.BotLogger;
|
||||
import org.telegram.telegrambots.TelegramApiException;
|
||||
import org.telegram.telegrambots.api.commands.Command;
|
||||
import org.telegram.telegrambots.api.commands.BotCommand;
|
||||
import org.telegram.telegrambots.api.commands.CommandRegistry;
|
||||
import org.telegram.telegrambots.api.commands.ICommandRegistry;
|
||||
import org.telegram.telegrambots.api.methods.send.SendMessage;
|
||||
@ -49,27 +49,27 @@ public abstract class TelegramLongPollingBot extends AbsSender implements ITeleg
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean register(Command command) {
|
||||
return commandRegistry.register(command);
|
||||
public final boolean register(BotCommand botCommand) {
|
||||
return commandRegistry.register(botCommand);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<Command, Boolean> registerAll(Command... commands) {
|
||||
return commandRegistry.registerAll(commands);
|
||||
public final Map<BotCommand, Boolean> registerAll(BotCommand... botCommands) {
|
||||
return commandRegistry.registerAll(botCommands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean deregister(Command command) {
|
||||
return commandRegistry.deregister(command);
|
||||
public final boolean deregister(BotCommand botCommand) {
|
||||
return commandRegistry.deregister(botCommand);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Map<Command, Boolean> deregisterAll(Command... commands) {
|
||||
return commandRegistry.deregisterAll(commands);
|
||||
public final Map<BotCommand, Boolean> deregisterAll(BotCommand... botCommands) {
|
||||
return commandRegistry.deregisterAll(botCommands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Collection<Command> getRegisteredCommands() {
|
||||
public final Collection<BotCommand> getRegisteredCommands() {
|
||||
return commandRegistry.getRegisteredCommands();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user