From f775d5c00c4e59bf9b0b7231f89edcee42d44321 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:07:14 +0200 Subject: [PATCH] Create and implement basic command --- .../telegrambots/api/commands/Command.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/org/telegram/telegrambots/api/commands/Command.java diff --git a/src/main/java/org/telegram/telegrambots/api/commands/Command.java b/src/main/java/org/telegram/telegrambots/api/commands/Command.java new file mode 100644 index 00000000..87c14abb --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/commands/Command.java @@ -0,0 +1,75 @@ +package org.telegram.telegrambots.api.commands; + +import org.telegram.telegrambots.bots.AbsSender; + +/** + * Representation of a command, which can be executed + * + * @author tschulz + */ +public abstract class Command extends AbsSender { + + public final static String COMMAND_INIT_CHARACTER = "/"; + public final static String COMMAND_PARAMETER_SEPERATOR = " "; + private final static int COMMAND_MAX_LENGTH = 32; + + private final String commandIdentifier; + private final String description; + private final String botToken; + + /** + * construct a command + * + * @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) { + + if (commandIdentifier == null || commandIdentifier.isEmpty()) { + throw new IllegalArgumentException("commandIdentifier for command cannot be null or empty"); + } + + if (commandIdentifier.startsWith(COMMAND_INIT_CHARACTER)) { + commandIdentifier = commandIdentifier.substring(1); + } + + if (commandIdentifier.length() + 1 > COMMAND_MAX_LENGTH) { + throw new IllegalArgumentException("commandIdentifier cannot be longer than " + COMMAND_MAX_LENGTH + " (including " + COMMAND_INIT_CHARACTER + ")"); + } + + this.commandIdentifier = commandIdentifier.toLowerCase(); + this.description = description; + this.botToken = botToken; + } + + /** + * get the identifier of this command + * + * @return the identifier + */ + public final String getCommandIdentifier() { + return commandIdentifier; + } + + /** + * get the description of this command + * + * @return the description as String + */ + public String getDescription() { + return description; + } + + @Override + public String getBotToken() { + return botToken; + } + + /** + * execute the command + * + * @param arguments passed arguments + * @param chatId id of the chat, to be able to send replies + */ + abstract void execute(String[] arguments, long chatId); +} \ No newline at end of file