diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java index c408580d..5e2e30f0 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java @@ -9,6 +9,7 @@ import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.commandbot.commands.BotCommand; import org.telegram.telegrambots.bots.commandbot.commands.CommandRegistry; +import org.telegram.telegrambots.bots.commandbot.commands.IBotCommand; import org.telegram.telegrambots.bots.commandbot.commands.ICommandRegistry; import java.util.Collection; @@ -105,27 +106,27 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB } @Override - public final boolean register(BotCommand botCommand) { + public final boolean register(IBotCommand botCommand) { return commandRegistry.register(botCommand); } @Override - public final Map registerAll(BotCommand... botCommands) { + public final Map registerAll(IBotCommand... botCommands) { return commandRegistry.registerAll(botCommands); } @Override - public final boolean deregister(BotCommand botCommand) { + public final boolean deregister(IBotCommand botCommand) { return commandRegistry.deregister(botCommand); } @Override - public final Map deregisterAll(BotCommand... botCommands) { + public final Map deregisterAll(IBotCommand... botCommands) { return commandRegistry.deregisterAll(botCommands); } @Override - public final Collection getRegisteredCommands() { + public final Collection getRegisteredCommands() { return commandRegistry.getRegisteredCommands(); } @@ -135,7 +136,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB } @Override - public final BotCommand getRegisteredCommand(String commandIdentifier) { + public final IBotCommand getRegisteredCommand(String commandIdentifier) { return commandRegistry.getRegisteredCommand(commandIdentifier); } diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/BotCommand.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/BotCommand.java index 6d6c3b63..3ef941ec 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/BotCommand.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/BotCommand.java @@ -10,7 +10,7 @@ import org.telegram.telegrambots.bots.AbsSender; * * @author Timo Schulz (Mit0x2) */ -public abstract class BotCommand { +public abstract class BotCommand implements IBotCommand { public final static String COMMAND_INIT_CHARACTER = "/"; public static final String COMMAND_PARAMETER_SEPARATOR_REGEXP = "\\s+"; private final static int COMMAND_MAX_LENGTH = 32; @@ -74,7 +74,7 @@ public abstract class BotCommand { * @param message the message to process * @param arguments passed arguments */ - void processMessage(AbsSender absSender, Message message, String[] arguments) { + public void processMessage(AbsSender absSender, Message message, String[] arguments) { execute(absSender, message.getFrom(), message.getChat(), arguments); } diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/CommandRegistry.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/CommandRegistry.java index 7ca5c0cc..2f44eada 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/CommandRegistry.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/CommandRegistry.java @@ -16,7 +16,7 @@ import java.util.function.BiConsumer; */ public final class CommandRegistry implements ICommandRegistry { - private final Map commandRegistryMap = new HashMap<>(); + private final Map commandRegistryMap = new HashMap<>(); private final boolean allowCommandsWithUsername; private final String botUsername; private BiConsumer defaultConsumer; @@ -37,7 +37,7 @@ public final class CommandRegistry implements ICommandRegistry { } @Override - public final boolean register(BotCommand botCommand) { + public final boolean register(IBotCommand botCommand) { if (commandRegistryMap.containsKey(botCommand.getCommandIdentifier())) { return false; } @@ -46,16 +46,16 @@ public final class CommandRegistry implements ICommandRegistry { } @Override - public final Map registerAll(BotCommand... botCommands) { - Map resultMap = new HashMap<>(botCommands.length); - for (BotCommand botCommand : botCommands) { + public final Map registerAll(IBotCommand... botCommands) { + Map resultMap = new HashMap<>(botCommands.length); + for (IBotCommand botCommand : botCommands) { resultMap.put(botCommand, register(botCommand)); } return resultMap; } @Override - public final boolean deregister(BotCommand botCommand) { + public final boolean deregister(IBotCommand botCommand) { if (commandRegistryMap.containsKey(botCommand.getCommandIdentifier())) { commandRegistryMap.remove(botCommand.getCommandIdentifier()); return true; @@ -64,21 +64,21 @@ public final class CommandRegistry implements ICommandRegistry { } @Override - public final Map deregisterAll(BotCommand... botCommands) { - Map resultMap = new HashMap<>(botCommands.length); - for (BotCommand botCommand : botCommands) { + public final Map deregisterAll(IBotCommand... botCommands) { + Map resultMap = new HashMap<>(botCommands.length); + for (IBotCommand botCommand : botCommands) { resultMap.put(botCommand, deregister(botCommand)); } return resultMap; } @Override - public final Collection getRegisteredCommands() { + public final Collection getRegisteredCommands() { return commandRegistryMap.values(); } @Override - public final BotCommand getRegisteredCommand(String commandIdentifier) { + public final IBotCommand getRegisteredCommand(String commandIdentifier) { return commandRegistryMap.get(commandIdentifier); } diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/DefaultBotCommand.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/DefaultBotCommand.java index a4124150..e4cb2d2d 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/DefaultBotCommand.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/DefaultBotCommand.java @@ -31,7 +31,7 @@ public abstract class DefaultBotCommand extends BotCommand { * @param arguments passed arguments */ @Override - void processMessage(AbsSender absSender, Message message, String[] arguments) { + public void processMessage(AbsSender absSender, Message message, String[] arguments) { execute(absSender, message.getFrom(), message.getChat(), message.getMessageId(), arguments); } diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/IBotCommand.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/IBotCommand.java new file mode 100644 index 00000000..618e9936 --- /dev/null +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/IBotCommand.java @@ -0,0 +1,37 @@ +package org.telegram.telegrambots.bots.commandbot.commands; + +import org.telegram.telegrambots.api.objects.Message; +import org.telegram.telegrambots.bots.AbsSender; + +import java.util.Collection; +import java.util.Map; +import java.util.function.BiConsumer; + +/** + * This Interface represents the a Command that can be executed + * + * @author Timo Schulz (Mit0x2) + */ +public interface IBotCommand { + /** + * Get the identifier of this command + * + * @return the identifier + */ + String getCommandIdentifier(); + + /** + * Get the description of this command + * + * @return the description as String + */ + String getDescription(); + + /** + * Process the message and execute the command + * + * @param absSender absSender to send messages over + * @param message the message to process + */ + void processMessage(AbsSender absSender, Message message, String[] arguments); +} \ No newline at end of file diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/ICommandRegistry.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/ICommandRegistry.java index 1f2a4d5f..a6a4905c 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/ICommandRegistry.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/ICommandRegistry.java @@ -29,7 +29,7 @@ public interface ICommandRegistry { * @param botCommand the command to register * @return whether the command could be registered, was not already registered */ - boolean register(BotCommand botCommand); + boolean register(IBotCommand botCommand); /** * register multiple commands @@ -37,7 +37,7 @@ public interface ICommandRegistry { * @param botCommands commands to register * @return map with results of the command register per command */ - Map registerAll(BotCommand... botCommands); + Map registerAll(IBotCommand... botCommands); /** * deregister a command @@ -45,7 +45,7 @@ public interface ICommandRegistry { * @param botCommand the command to deregister * @return whether the command could be deregistered, was registered */ - boolean deregister(BotCommand botCommand); + boolean deregister(IBotCommand botCommand); /** * deregister multiple commands @@ -53,19 +53,19 @@ public interface ICommandRegistry { * @param botCommands commands to deregister * @return map with results of the command deregistered per command */ - Map deregisterAll(BotCommand... botCommands); + Map deregisterAll(IBotCommand... botCommands); /** * get a collection of all registered commands * * @return a collection of registered commands */ - Collection getRegisteredCommands(); + Collection getRegisteredCommands(); /** * get registered command * * @return registered command if exists or null if not */ - BotCommand getRegisteredCommand(String commandIdentifier); + IBotCommand getRegisteredCommand(String commandIdentifier); } \ No newline at end of file diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/helpCommand/HelpCommand.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/helpCommand/HelpCommand.java index 2926b0e6..bd108a19 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/helpCommand/HelpCommand.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/helpCommand/HelpCommand.java @@ -1,16 +1,15 @@ package org.telegram.telegrambots.bots.commandbot.commands.helpCommand; -import java.util.Collection; - import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.objects.Chat; import org.telegram.telegrambots.api.objects.User; import org.telegram.telegrambots.bots.AbsSender; -import org.telegram.telegrambots.bots.commandbot.TelegramLongPollingCommandBot; -import org.telegram.telegrambots.bots.commandbot.commands.BotCommand; +import org.telegram.telegrambots.bots.commandbot.commands.IBotCommand; import org.telegram.telegrambots.bots.commandbot.commands.ICommandRegistry; import org.telegram.telegrambots.exceptions.TelegramApiException; +import java.util.Collection; + /** * A special bot command used for printing help messages similiar to the Linux man command. * The commands printed by this command should implement the {@link IManCommand} interface to provide an extended description. @@ -29,9 +28,9 @@ public class HelpCommand extends ManCommand { * @param botCommands the Commands that should be included in the String * @return a formatted String containing command and description for all supplied commands */ - public static String getHelpText(BotCommand...botCommands) { + public static String getHelpText(IBotCommand...botCommands) { StringBuilder reply = new StringBuilder(); - for (BotCommand com : botCommands) { + for (IBotCommand com : botCommands) { reply.append(com.toString()).append(System.lineSeparator()).append(System.lineSeparator()); } return reply.toString(); @@ -42,8 +41,8 @@ public class HelpCommand extends ManCommand { * @param botCommands a collection of commands that should be included in the String * @return a formatted String containing command and description for all supplied commands */ - public static String getHelpText(Collection botCommands) { - return getHelpText(botCommands.toArray(new BotCommand[botCommands.size()])); + public static String getHelpText(Collection botCommands) { + return getHelpText(botCommands.toArray(new IBotCommand[botCommands.size()])); } /** @@ -60,7 +59,7 @@ public class HelpCommand extends ManCommand { * @param command a command the extended Descriptions is read from * @return the extended Description or the toString() if IManCommand is not implemented */ - public static String getManText(BotCommand command) { + public static String getManText(IBotCommand command) { return IManCommand.class.isInstance(command) ? getManText((IManCommand) command) : command.toString(); } @@ -96,7 +95,7 @@ public class HelpCommand extends ManCommand { ICommandRegistry registry = (ICommandRegistry) absSender; if (arguments.length > 0) { - BotCommand command = registry.getRegisteredCommand(arguments[0]); + IBotCommand command = registry.getRegisteredCommand(arguments[0]); String reply = getManText(command); try { absSender.execute(new SendMessage(chat.getId(), reply).setParseMode("HTML"));