From 88ae4a75169816f6dcd97da9865738bd4c15a29b Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:02:32 +0200 Subject: [PATCH 01/20] Make Chat id a long attribute everywhere --- .../telegrambots/api/methods/send/SendMessage.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java index 722b8f2a..29948c0c 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java @@ -29,7 +29,7 @@ public class SendMessage extends BotApiMethod { private static final String DISABLENOTIFICATION_FIELD = "disable_notification"; private static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; private static final String REPLYMARKUP_FIELD = "reply_markup"; - private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) + private long chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) private String text; ///< Text of the message to be sent private String parseMode; ///< Optional. Send Markdown, if you want Telegram apps to show bold, italic and URL text in your bot's message. private Boolean disableWebPagePreview; ///< Optional. Disables link previews for links in this message @@ -45,11 +45,11 @@ public class SendMessage extends BotApiMethod { super(); } - public String getChatId() { + public long getChatId() { return chatId; } - public SendMessage setChatId(String chatId) { + public SendMessage setChatId(long chatId) { this.chatId = chatId; return this; } @@ -168,7 +168,7 @@ public class SendMessage extends BotApiMethod { public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField(METHOD_FIELD, PATH); - gen.writeStringField(CHATID_FIELD, chatId); + gen.writeStringField(CHATID_FIELD, Long.toHexString(chatId)); gen.writeStringField(TEXT_FIELD, text); if (parseMode != null) { From f775d5c00c4e59bf9b0b7231f89edcee42d44321 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:07:14 +0200 Subject: [PATCH 02/20] 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 From 5d0f508b5a51c6d8180bd3e850b65a7bb386435c Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:08:16 +0200 Subject: [PATCH 03/20] Create interface for the registry of all comannds of a bot --- .../api/commands/ICommandRegistery.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java diff --git a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java new file mode 100644 index 00000000..55cbe647 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java @@ -0,0 +1,42 @@ +package org.telegram.telegrambots.api.commands; + +import java.util.Map; + +/** + * + */ +public interface ICommandRegistery { + + /** + * register a command + * + * @param command the command to register + * @return whether the command could be registered, was not already registered + */ + boolean register(Command command); + + /** + * register multiple commands + * + * @param commands commands to register + * @return map with results of the command register per command + */ + Map registerAll(Command... commands); + + /** + * deregister a command + * + * @param command the command to deregister + * @return whether the command could be deregistered, was registered + */ + boolean deregister(Command command); + + /** + * deregister multiple commands + * + * @param commands commands to deregister + * @return map with results of the command deregistered per command + */ + Map deregisterAll(Command... commands); + +} \ No newline at end of file From 91191ee63b7e6c7487f03740f3cfee763f1289e6 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:10:35 +0200 Subject: [PATCH 04/20] Add method to retrieve all registered commands --- .../telegrambots/api/commands/ICommandRegistery.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java index 55cbe647..58da1080 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java @@ -1,5 +1,6 @@ package org.telegram.telegrambots.api.commands; +import java.util.Collection; import java.util.Map; /** @@ -39,4 +40,11 @@ public interface ICommandRegistery { */ Map deregisterAll(Command... commands); + /** + * get a collection of all registered commands + * + * @return a collection of registered commands + */ + Collection getRegisteredCommands(); + } \ No newline at end of file From 2089dfbe239bf8d70f0cffb5357044b4a8589a3a Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:11:38 +0200 Subject: [PATCH 05/20] Implement basic help command, which provides an overview over all registered commands --- .../api/commands/HelpCommand.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java new file mode 100644 index 00000000..39b3d786 --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java @@ -0,0 +1,37 @@ +package org.telegram.telegrambots.api.commands; + +import org.telegram.telegrambots.BotLogger; +import org.telegram.telegrambots.TelegramApiException; +import org.telegram.telegrambots.api.methods.send.SendMessage; + +/** + * standard help command, which gets registered by default, to supply a list of all available commands + * + * @author tschulz + */ +public class HelpCommand extends Command { + + private static final String LOGTAG = "HELPCOMMAND"; + private final ICommandRegistery commandRegistry; + + public HelpCommand(ICommandRegistery 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()) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + sendMessage.enableHtml(true); + sendMessage.setText("" + registeredCommand.getCommandIdentifier() + "\n" + registeredCommand.getDescription()); + + try { + sendMessage(sendMessage); + } catch (TelegramApiException e) { + BotLogger.error("Failed to send HelpMessage", LOGTAG, e); + } + } + } +} From f2da0a65589748bd29d61e7a94674aff4697b5fe Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:12:22 +0200 Subject: [PATCH 06/20] Implement Command Registry --- .../api/commands/CommandRegistry.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java diff --git a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java new file mode 100644 index 00000000..ab9afd6f --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java @@ -0,0 +1,86 @@ +package org.telegram.telegrambots.api.commands; + +import org.telegram.telegrambots.api.objects.Message; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author tschulz + */ +public final class CommandRegistry implements ICommandRegistery { + + private final Map commandRegistryMap = new HashMap<>(); + + public CommandRegistry(String botToken) { + register(new HelpCommand(this, botToken)); + } + + @Override + public final boolean register(Command command) { + if (commandRegistryMap.containsKey(command.getCommandIdentifier())) { + return false; + } + commandRegistryMap.put(command.getCommandIdentifier(), command); + return true; + } + + @Override + public final Map registerAll(Command... commands) { + Map resultMap = new HashMap<>(commands.length); + for (Command command : commands) { + resultMap.put(command, register(command)); + } + return resultMap; + } + + @Override + public final boolean deregister(Command command) { + if (commandRegistryMap.containsKey(command.getCommandIdentifier())) { + commandRegistryMap.remove(command.getCommandIdentifier()); + return true; + } + return false; + } + + @Override + public final Map deregisterAll(Command... commands) { + Map resultMap = new HashMap<>(commands.length); + for (Command command : commands) { + resultMap.put(command, deregister(command)); + } + return resultMap; + } + + @Override + public final Collection getRegisteredCommands() { + return commandRegistryMap.values(); + } + + /** + * executes a command if present and replies the success + * + * @param message input message + * @return true if success or false otherwise + */ + public final boolean executeCommand(Message message) { + if (message.hasText()) { + String text = message.getText(); + if (!text.isEmpty() && text.startsWith(Command.COMMAND_INIT_CHARACTER)) { + String commandMessage = text.substring(1); + String[] commandSplit = commandMessage.split(Command.COMMAND_PARAMETER_SEPERATOR); + + String command = commandSplit[0]; + + if (commandRegistryMap.containsKey(command)) { + String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); + commandRegistryMap.get(command).execute(parameters, message.getChatId()); + return true; + } + } + } + return false; + } +} \ No newline at end of file From 10d39077eb0c994d3c1a2ef594a47fe67a96f40a Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:12:48 +0200 Subject: [PATCH 07/20] add chat command detection to message (properties field) --- .../telegram/telegrambots/api/objects/Message.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/org/telegram/telegrambots/api/objects/Message.java b/src/main/java/org/telegram/telegrambots/api/objects/Message.java index 126cb67f..034fe3ad 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/Message.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/Message.java @@ -361,6 +361,17 @@ public class Message implements IBotApiObject { return text != null && !text.isEmpty(); } + public boolean isCommand() { + if (entities != null) { + for (MessageEntity entity : entities) { + if (entity != null && "bot_command".equals(entity.getType())) { + return text != null && !text.isEmpty(); + } + } + } + return false; + } + public boolean hasDocument() { return this.document != null; } From 92aa4e218e46eb83bbfe5e3f978069128eea0a29 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:13:29 +0200 Subject: [PATCH 08/20] Add java doc --- src/main/java/org/telegram/telegrambots/TelegramBotsApi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java index 19017ea1..ca8c8ea6 100644 --- a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java +++ b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java @@ -123,7 +123,7 @@ public class TelegramBotsApi { /** * Register a bot. The Bot Session is started immediately, and may be disconnected by calling close. - * @param bot + * @param bot the bot to register */ public BotSession registerBot(TelegramLongPollingBot bot) throws TelegramApiException { setWebhook(bot.getBotToken()); From f49cb8b62f0f856acace6da90adaa786eebf0a2b Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:14:58 +0200 Subject: [PATCH 09/20] Modify TelegramLongPollingBot to support commands --- .../bots/TelegramLongPollingBot.java | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 7afdf9c8..0ebc3092 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -1,11 +1,77 @@ 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.CommandRegistry; +import org.telegram.telegrambots.api.commands.ICommandRegistery; +import org.telegram.telegrambots.api.methods.send.SendMessage; +import org.telegram.telegrambots.api.objects.Message; +import org.telegram.telegrambots.api.objects.Update; + +import java.util.Collection; +import java.util.Map; + /** * @author Ruben Bermudez * @version 1.0 * @brief TODO * @date 14 of January of 2016 */ -public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot { +public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistery { -} + public static final String LOGTAG = "TelegramLongPollingBot"; + private final CommandRegistry commandRegistry; + + public TelegramLongPollingBot() { + this.commandRegistry = new CommandRegistry(getBotToken()); + } + + @Override + public final void onUpdateReceived(Update update) { + if (update.hasMessage()) { + Message message = update.getMessage(); + if (message.isCommand()) { + if (!commandRegistry.executeCommand(message)) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(message.getChatId()); + sendMessage.setText("The command you provided is not registered for this bot"); + try { + sendMessage(sendMessage); + } catch (TelegramApiException e) { + BotLogger.error("Cannot send message", LOGTAG, e); + } + } + return; + } + } + processNonCommandUpdate(update); + } + + @Override + public final boolean register(Command command) { + return commandRegistry.register(command); + } + + @Override + public final Map registerAll(Command... commands) { + return commandRegistry.registerAll(commands); + } + + @Override + public final boolean deregister(Command command) { + return commandRegistry.deregister(command); + } + + @Override + public final Map deregisterAll(Command... commands) { + return commandRegistry.deregisterAll(commands); + } + + @Override + public final Collection getRegisteredCommands() { + return commandRegistry.getRegisteredCommands(); + } + + public abstract void processNonCommandUpdate(Update update); +} \ No newline at end of file From 618fb80333a85c53bbedfddb4c40996f9d0154b4 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:16:39 +0200 Subject: [PATCH 10/20] fix typo --- .../telegram/telegrambots/api/commands/CommandRegistry.java | 2 +- .../org/telegram/telegrambots/api/commands/HelpCommand.java | 4 ++-- .../{ICommandRegistery.java => ICommandRegistry.java} | 2 +- .../telegram/telegrambots/bots/TelegramLongPollingBot.java | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename src/main/java/org/telegram/telegrambots/api/commands/{ICommandRegistery.java => ICommandRegistry.java} (97%) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java index ab9afd6f..3cbb6b83 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java @@ -10,7 +10,7 @@ import java.util.Map; /** * @author tschulz */ -public final class CommandRegistry implements ICommandRegistery { +public final class CommandRegistry implements ICommandRegistry { private final Map commandRegistryMap = new HashMap<>(); diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java index 39b3d786..49f57407 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java @@ -12,9 +12,9 @@ import org.telegram.telegrambots.api.methods.send.SendMessage; public class HelpCommand extends Command { private static final String LOGTAG = "HELPCOMMAND"; - private final ICommandRegistery commandRegistry; + private final ICommandRegistry commandRegistry; - public HelpCommand(ICommandRegistery commandRegistry, String botToken) { + public HelpCommand(ICommandRegistry commandRegistry, String botToken) { super("help", "Gives an overview over all Commands registered for this bot", botToken); this.commandRegistry = commandRegistry; } diff --git a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java similarity index 97% rename from src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java rename to src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java index 58da1080..66db8c8c 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistery.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java @@ -6,7 +6,7 @@ import java.util.Map; /** * */ -public interface ICommandRegistery { +public interface ICommandRegistry { /** * register a command diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 0ebc3092..cd871340 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -4,7 +4,7 @@ import org.telegram.telegrambots.BotLogger; import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.commands.Command; import org.telegram.telegrambots.api.commands.CommandRegistry; -import org.telegram.telegrambots.api.commands.ICommandRegistery; +import org.telegram.telegrambots.api.commands.ICommandRegistry; import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.Update; @@ -18,7 +18,7 @@ import java.util.Map; * @brief TODO * @date 14 of January of 2016 */ -public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistery { +public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry { public static final String LOGTAG = "TelegramLongPollingBot"; private final CommandRegistry commandRegistry; From 9958d677e282bcdb8fef1c975cbd3e286bcfdcb8 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:18:41 +0200 Subject: [PATCH 11/20] add command init character to help command response --- .../org/telegram/telegrambots/api/commands/HelpCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java index 49f57407..29b1ab13 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java @@ -25,7 +25,7 @@ public class HelpCommand extends Command { SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(chatId); sendMessage.enableHtml(true); - sendMessage.setText("" + registeredCommand.getCommandIdentifier() + "\n" + registeredCommand.getDescription()); + sendMessage.setText("" + COMMAND_INIT_CHARACTER + registeredCommand.getCommandIdentifier() + "\n" + registeredCommand.getDescription()); try { sendMessage(sendMessage); From 5fe18aefa34eee400f0a4bcbb98002b632f04072 Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:56:15 +0200 Subject: [PATCH 12/20] rename Command class and fix visibility of execute method --- .../{Command.java => BotCommand.java} | 12 +++--- .../api/commands/CommandRegistry.java | 38 +++++++++---------- .../{HelpCommand.java => HelpBotCommand.java} | 10 ++--- .../api/commands/ICommandRegistry.java | 18 ++++----- .../bots/TelegramLongPollingBot.java | 20 +++++----- 5 files changed, 49 insertions(+), 49 deletions(-) rename src/main/java/org/telegram/telegrambots/api/commands/{Command.java => BotCommand.java} (84%) rename src/main/java/org/telegram/telegrambots/api/commands/{HelpCommand.java => HelpBotCommand.java} (73%) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/Command.java b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java similarity index 84% rename from src/main/java/org/telegram/telegrambots/api/commands/Command.java rename to src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java index 87c14abb..d18c5ae1 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/Command.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java @@ -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); } \ No newline at end of file diff --git a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java index 3cbb6b83..313c06fe 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java @@ -12,50 +12,50 @@ import java.util.Map; */ public final class CommandRegistry implements ICommandRegistry { - private final Map commandRegistryMap = new HashMap<>(); + private final Map 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 registerAll(Command... commands) { - Map resultMap = new HashMap<>(commands.length); - for (Command command : commands) { - resultMap.put(command, register(command)); + public final Map registerAll(BotCommand... botCommands) { + Map 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 deregisterAll(Command... commands) { - Map resultMap = new HashMap<>(commands.length); - for (Command command : commands) { - resultMap.put(command, deregister(command)); + public final Map deregisterAll(BotCommand... botCommands) { + Map resultMap = new HashMap<>(botCommands.length); + for (BotCommand botCommand : botCommands) { + resultMap.put(botCommand, deregister(botCommand)); } return resultMap; } @Override - public final Collection getRegisteredCommands() { + public final Collection 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]; diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java similarity index 73% rename from src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java rename to src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java index 29b1ab13..80e61af2 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java @@ -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("" + COMMAND_INIT_CHARACTER + registeredCommand.getCommandIdentifier() + "\n" + registeredCommand.getDescription()); + sendMessage.setText("" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "\n" + registeredBotCommand.getDescription()); try { sendMessage(sendMessage); diff --git a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java index 66db8c8c..06db1d88 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/ICommandRegistry.java @@ -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 registerAll(Command... commands); + Map 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 deregisterAll(Command... commands); + Map deregisterAll(BotCommand... botCommands); /** * get a collection of all registered commands * * @return a collection of registered commands */ - Collection getRegisteredCommands(); + Collection getRegisteredCommands(); } \ No newline at end of file diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index cd871340..d0750dc4 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -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 registerAll(Command... commands) { - return commandRegistry.registerAll(commands); + public final Map 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 deregisterAll(Command... commands) { - return commandRegistry.deregisterAll(commands); + public final Map deregisterAll(BotCommand... botCommands) { + return commandRegistry.deregisterAll(botCommands); } @Override - public final Collection getRegisteredCommands() { + public final Collection getRegisteredCommands() { return commandRegistry.getRegisteredCommands(); } From 1bb4d129836cd310964b01db3fa783a28e3d1bcf Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 12:03:00 +0200 Subject: [PATCH 13/20] change chat id to chat object to be able to work with username --- .../org/telegram/telegrambots/api/commands/BotCommand.java | 5 +++-- .../telegram/telegrambots/api/commands/CommandRegistry.java | 2 +- .../telegram/telegrambots/api/commands/HelpBotCommand.java | 6 ++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java index d18c5ae1..63ad7ee0 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java @@ -1,5 +1,6 @@ package org.telegram.telegrambots.api.commands; +import org.telegram.telegrambots.api.objects.Chat; import org.telegram.telegrambots.bots.AbsSender; /** @@ -69,7 +70,7 @@ public abstract class BotCommand extends AbsSender { * execute the command * * @param arguments passed arguments - * @param chatId id of the chat, to be able to send replies + * @param chat the chat, to be able to send replies */ - public abstract void execute(String[] arguments, long chatId); + public abstract void execute(String[] arguments, Chat chat); } \ No newline at end of file diff --git a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java index 313c06fe..eb2a9209 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java @@ -76,7 +76,7 @@ public final class CommandRegistry implements ICommandRegistry { if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); - commandRegistryMap.get(command).execute(parameters, message.getChatId()); + commandRegistryMap.get(command).execute(parameters, message.getChat()); return true; } } diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java index 80e61af2..79be1245 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.commands; import org.telegram.telegrambots.BotLogger; import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.methods.send.SendMessage; +import org.telegram.telegrambots.api.objects.Chat; /** * standard help command, which gets registered by default, to supply a list of all available commands @@ -20,10 +21,11 @@ public class HelpBotCommand extends BotCommand { } @Override - public void execute(String[] arguments, long chatId) { + public void execute(String[] arguments, Chat chat) { + for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) { SendMessage sendMessage = new SendMessage(); - sendMessage.setChatId(chatId); + sendMessage.setChatId(chat.getId()); sendMessage.enableHtml(true); sendMessage.setText("" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "\n" + registeredBotCommand.getDescription()); From 265915daf6811d405f1cf3258e9d59499e618f2f Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Fri, 20 May 2016 18:39:06 +0200 Subject: [PATCH 14/20] bring chatId of SendMessage back to String instead of Long --- .../telegrambots/api/methods/send/SendMessage.java | 8 ++++---- .../telegrambots/bots/TelegramLongPollingBot.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java index 29948c0c..722b8f2a 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendMessage.java @@ -29,7 +29,7 @@ public class SendMessage extends BotApiMethod { private static final String DISABLENOTIFICATION_FIELD = "disable_notification"; private static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; private static final String REPLYMARKUP_FIELD = "reply_markup"; - private long chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) + private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) private String text; ///< Text of the message to be sent private String parseMode; ///< Optional. Send Markdown, if you want Telegram apps to show bold, italic and URL text in your bot's message. private Boolean disableWebPagePreview; ///< Optional. Disables link previews for links in this message @@ -45,11 +45,11 @@ public class SendMessage extends BotApiMethod { super(); } - public long getChatId() { + public String getChatId() { return chatId; } - public SendMessage setChatId(long chatId) { + public SendMessage setChatId(String chatId) { this.chatId = chatId; return this; } @@ -168,7 +168,7 @@ public class SendMessage extends BotApiMethod { public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField(METHOD_FIELD, PATH); - gen.writeStringField(CHATID_FIELD, Long.toHexString(chatId)); + gen.writeStringField(CHATID_FIELD, chatId); gen.writeStringField(TEXT_FIELD, text); if (parseMode != null) { diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index d0750dc4..5fae139c 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -34,7 +34,7 @@ public abstract class TelegramLongPollingBot extends AbsSender implements ITeleg if (message.isCommand()) { if (!commandRegistry.executeCommand(message)) { SendMessage sendMessage = new SendMessage(); - sendMessage.setChatId(message.getChatId()); + sendMessage.setChatId(message.getChatId().toString()); sendMessage.setText("The command you provided is not registered for this bot"); try { sendMessage(sendMessage); From 2bff35b8b959dec0efb2d2517d926e6f5252edf4 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Fri, 20 May 2016 19:23:44 +0200 Subject: [PATCH 15/20] remove AbsSender Interface from BotCommand and add reference for sending messages --- .../telegrambots/api/commands/BotCommand.java | 24 +++++++++++++------ .../api/commands/HelpBotCommand.java | 6 ++--- .../bots/TelegramLongPollingBot.java | 8 +++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java index 63ad7ee0..68222543 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java @@ -8,7 +8,7 @@ import org.telegram.telegrambots.bots.AbsSender; * * @author tschulz */ -public abstract class BotCommand extends AbsSender { +public abstract class BotCommand { public final static String COMMAND_INIT_CHARACTER = "/"; public final static String COMMAND_PARAMETER_SEPARATOR = " "; @@ -16,7 +16,7 @@ public abstract class BotCommand extends AbsSender { private final String commandIdentifier; private final String description; - private final String botToken; + private AbsSender absSender; /** * construct a command @@ -24,7 +24,7 @@ public abstract class BotCommand 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 BotCommand(String commandIdentifier, String description, String botToken) { + public BotCommand(String commandIdentifier, String description) { if (commandIdentifier == null || commandIdentifier.isEmpty()) { throw new IllegalArgumentException("commandIdentifier for command cannot be null or empty"); @@ -40,7 +40,6 @@ public abstract class BotCommand extends AbsSender { this.commandIdentifier = commandIdentifier.toLowerCase(); this.description = description; - this.botToken = botToken; } /** @@ -61,9 +60,20 @@ public abstract class BotCommand extends AbsSender { return description; } - @Override - public final String getBotToken() { - return botToken; + /** + * Setter + * @param absSender set AbsSender + */ + public final void setAbsSender(AbsSender absSender) { + this.absSender = absSender; + } + + /** + * Getter + * @return the absSender + */ + protected final AbsSender getAbsSender() { + return absSender; } /** diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java index 79be1245..798ad588 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java @@ -16,7 +16,7 @@ public class HelpBotCommand extends BotCommand { private final ICommandRegistry commandRegistry; public HelpBotCommand(ICommandRegistry commandRegistry, String botToken) { - super("help", "Gives an overview over all Commands registered for this bot", botToken); + super("help", "Gives an overview over all Commands registered for this bot"); this.commandRegistry = commandRegistry; } @@ -25,12 +25,12 @@ public class HelpBotCommand extends BotCommand { for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) { SendMessage sendMessage = new SendMessage(); - sendMessage.setChatId(chat.getId()); + sendMessage.setChatId(chat.getId().toString()); sendMessage.enableHtml(true); sendMessage.setText("" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "\n" + registeredBotCommand.getDescription()); try { - sendMessage(sendMessage); + getAbsSender().sendMessage(sendMessage); } catch (TelegramApiException e) { BotLogger.error("Failed to send HelpMessage", LOGTAG, e); } diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 5fae139c..241259a2 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -50,21 +50,29 @@ public abstract class TelegramLongPollingBot extends AbsSender implements ITeleg @Override public final boolean register(BotCommand botCommand) { + botCommand.setAbsSender(this); return commandRegistry.register(botCommand); } @Override public final Map registerAll(BotCommand... botCommands) { + for (BotCommand botCommand : botCommands) { + botCommand.setAbsSender(this); + } return commandRegistry.registerAll(botCommands); } @Override public final boolean deregister(BotCommand botCommand) { + botCommand.setAbsSender(null); return commandRegistry.deregister(botCommand); } @Override public final Map deregisterAll(BotCommand... botCommands) { + for (BotCommand botCommand : botCommands) { + botCommand.setAbsSender(null); + } return commandRegistry.deregisterAll(botCommands); } From 3dd0b87ca0c5a80f04887f22fd6cda3dbee77ffe Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Sat, 21 May 2016 14:56:08 +0200 Subject: [PATCH 16/20] move command bot functionality to own bot base class, to avoid api change for future releases --- .../bots/TelegramLongPollingBot.java | 78 +-------------- .../bots/TelegramLongPollingCommandBot.java | 95 +++++++++++++++++++ 2 files changed, 97 insertions(+), 76 deletions(-) create mode 100644 src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 241259a2..7afdf9c8 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -1,85 +1,11 @@ package org.telegram.telegrambots.bots; -import org.telegram.telegrambots.BotLogger; -import org.telegram.telegrambots.TelegramApiException; -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; -import org.telegram.telegrambots.api.objects.Message; -import org.telegram.telegrambots.api.objects.Update; - -import java.util.Collection; -import java.util.Map; - /** * @author Ruben Bermudez * @version 1.0 * @brief TODO * @date 14 of January of 2016 */ -public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry { +public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot { - public static final String LOGTAG = "TelegramLongPollingBot"; - private final CommandRegistry commandRegistry; - - public TelegramLongPollingBot() { - this.commandRegistry = new CommandRegistry(getBotToken()); - } - - @Override - public final void onUpdateReceived(Update update) { - if (update.hasMessage()) { - Message message = update.getMessage(); - if (message.isCommand()) { - if (!commandRegistry.executeCommand(message)) { - SendMessage sendMessage = new SendMessage(); - sendMessage.setChatId(message.getChatId().toString()); - sendMessage.setText("The command you provided is not registered for this bot"); - try { - sendMessage(sendMessage); - } catch (TelegramApiException e) { - BotLogger.error("Cannot send message", LOGTAG, e); - } - } - return; - } - } - processNonCommandUpdate(update); - } - - @Override - public final boolean register(BotCommand botCommand) { - botCommand.setAbsSender(this); - return commandRegistry.register(botCommand); - } - - @Override - public final Map registerAll(BotCommand... botCommands) { - for (BotCommand botCommand : botCommands) { - botCommand.setAbsSender(this); - } - return commandRegistry.registerAll(botCommands); - } - - @Override - public final boolean deregister(BotCommand botCommand) { - botCommand.setAbsSender(null); - return commandRegistry.deregister(botCommand); - } - - @Override - public final Map deregisterAll(BotCommand... botCommands) { - for (BotCommand botCommand : botCommands) { - botCommand.setAbsSender(null); - } - return commandRegistry.deregisterAll(botCommands); - } - - @Override - public final Collection getRegisteredCommands() { - return commandRegistry.getRegisteredCommands(); - } - - public abstract void processNonCommandUpdate(Update update); -} \ No newline at end of file +} diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java new file mode 100644 index 00000000..f9a6ab8c --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -0,0 +1,95 @@ +package org.telegram.telegrambots.bots; + +import org.telegram.telegrambots.BotLogger; +import org.telegram.telegrambots.TelegramApiException; +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; +import org.telegram.telegrambots.api.objects.Message; +import org.telegram.telegrambots.api.objects.Update; + +import java.util.Collection; +import java.util.Map; + +/** + * This class adds command functionality to the TelegramLongPollingBot + * + * @author tschulz + */ +public abstract class TelegramLongPollingCommandBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry{ + + public static final String LOGTAG = "TelegramLongPollingCommandBot"; + private final CommandRegistry commandRegistry; + + /** + * construct creates CommandRegistry for this bot. + * Use ICommandRegistry's methods on this bot to register commands + */ + public TelegramLongPollingCommandBot() { + this.commandRegistry = new CommandRegistry(getBotToken()); + } + + @Override + public final void onUpdateReceived(Update update) { + if (update.hasMessage()) { + Message message = update.getMessage(); + if (message.isCommand()) { + if (!commandRegistry.executeCommand(message)) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(message.getChatId().toString()); + sendMessage.setText("The command you provided is not registered for this bot"); + try { + sendMessage(sendMessage); + } catch (TelegramApiException e) { + BotLogger.error("Cannot send message", LOGTAG, e); + } + } + return; + } + } + processNonCommandUpdate(update); + } + + @Override + public final boolean register(BotCommand botCommand) { + botCommand.setAbsSender(this); + return commandRegistry.register(botCommand); + } + + @Override + public final Map registerAll(BotCommand... botCommands) { + for (BotCommand botCommand : botCommands) { + botCommand.setAbsSender(this); + } + return commandRegistry.registerAll(botCommands); + } + + @Override + public final boolean deregister(BotCommand botCommand) { + botCommand.setAbsSender(null); + return commandRegistry.deregister(botCommand); + } + + @Override + public final Map deregisterAll(BotCommand... botCommands) { + for (BotCommand botCommand : botCommands) { + botCommand.setAbsSender(null); + } + return commandRegistry.deregisterAll(botCommands); + } + + @Override + public final Collection getRegisteredCommands() { + return commandRegistry.getRegisteredCommands(); + } + + /** + * Process all updates, that are not commands. + * Attention: commands, that have valid syntax but are not registered on this bot, + * won't be forwarded to this method! + * + * @param update the update + */ + public abstract void processNonCommandUpdate(Update update); +} From e51d6eaa4b08bb8f8031402c6ead729a672a5217 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Sat, 21 May 2016 15:00:40 +0200 Subject: [PATCH 17/20] fix missing blank ;) --- .../telegrambots/bots/TelegramLongPollingCommandBot.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index f9a6ab8c..9f7fbc5f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -17,7 +17,7 @@ import java.util.Map; * * @author tschulz */ -public abstract class TelegramLongPollingCommandBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry{ +public abstract class TelegramLongPollingCommandBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry { public static final String LOGTAG = "TelegramLongPollingCommandBot"; private final CommandRegistry commandRegistry; From f33b9fa10efc0317be460a02f7ccaacfe4399362 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Sun, 22 May 2016 14:10:57 +0200 Subject: [PATCH 18/20] remove AbsSender field for command and instead reach it in on each execute call -> so one command could be reached to multiple bot in parallel --- .../telegrambots/api/commands/BotCommand.java | 22 +++---------------- .../api/commands/CommandRegistry.java | 5 +++-- .../api/commands/HelpBotCommand.java | 5 +++-- .../bots/TelegramLongPollingCommandBot.java | 10 +-------- 4 files changed, 10 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java index 68222543..b22bfde6 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/BotCommand.java @@ -16,7 +16,6 @@ public abstract class BotCommand { private final String commandIdentifier; private final String description; - private AbsSender absSender; /** * construct a command @@ -60,27 +59,12 @@ public abstract class BotCommand { return description; } - /** - * Setter - * @param absSender set AbsSender - */ - public final void setAbsSender(AbsSender absSender) { - this.absSender = absSender; - } - - /** - * Getter - * @return the absSender - */ - protected final AbsSender getAbsSender() { - return absSender; - } - /** * execute the command * - * @param arguments passed arguments + * @param absSender absSender to send messages over * @param chat the chat, to be able to send replies + * @param arguments passed arguments */ - public abstract void execute(String[] arguments, Chat chat); + public abstract void execute(AbsSender absSender, Chat chat, String[] arguments); } \ No newline at end of file diff --git a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java index eb2a9209..8e176508 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java @@ -1,6 +1,7 @@ package org.telegram.telegrambots.api.commands; import org.telegram.telegrambots.api.objects.Message; +import org.telegram.telegrambots.bots.AbsSender; import java.util.Arrays; import java.util.Collection; @@ -65,7 +66,7 @@ public final class CommandRegistry implements ICommandRegistry { * @param message input message * @return true if success or false otherwise */ - public final boolean executeCommand(Message message) { + public final boolean executeCommand(AbsSender absSender, Message message) { if (message.hasText()) { String text = message.getText(); if (!text.isEmpty() && text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) { @@ -76,7 +77,7 @@ public final class CommandRegistry implements ICommandRegistry { if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); - commandRegistryMap.get(command).execute(parameters, message.getChat()); + commandRegistryMap.get(command).execute(absSender, message.getChat(), parameters); return true; } } diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java index 798ad588..a68fc2e1 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java @@ -4,6 +4,7 @@ import org.telegram.telegrambots.BotLogger; import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.objects.Chat; +import org.telegram.telegrambots.bots.AbsSender; /** * standard help command, which gets registered by default, to supply a list of all available commands @@ -21,7 +22,7 @@ public class HelpBotCommand extends BotCommand { } @Override - public void execute(String[] arguments, Chat chat) { + public void execute(AbsSender absSender, Chat chat, String[] arguments) { for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) { SendMessage sendMessage = new SendMessage(); @@ -30,7 +31,7 @@ public class HelpBotCommand extends BotCommand { sendMessage.setText("" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "\n" + registeredBotCommand.getDescription()); try { - getAbsSender().sendMessage(sendMessage); + absSender.sendMessage(sendMessage); } catch (TelegramApiException e) { BotLogger.error("Failed to send HelpMessage", LOGTAG, e); } diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index 9f7fbc5f..a8942fb9 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -35,7 +35,7 @@ public abstract class TelegramLongPollingCommandBot extends AbsSender implements if (update.hasMessage()) { Message message = update.getMessage(); if (message.isCommand()) { - if (!commandRegistry.executeCommand(message)) { + if (!commandRegistry.executeCommand(this, message)) { SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(message.getChatId().toString()); sendMessage.setText("The command you provided is not registered for this bot"); @@ -53,29 +53,21 @@ public abstract class TelegramLongPollingCommandBot extends AbsSender implements @Override public final boolean register(BotCommand botCommand) { - botCommand.setAbsSender(this); return commandRegistry.register(botCommand); } @Override public final Map registerAll(BotCommand... botCommands) { - for (BotCommand botCommand : botCommands) { - botCommand.setAbsSender(this); - } return commandRegistry.registerAll(botCommands); } @Override public final boolean deregister(BotCommand botCommand) { - botCommand.setAbsSender(null); return commandRegistry.deregister(botCommand); } @Override public final Map deregisterAll(BotCommand... botCommands) { - for (BotCommand botCommand : botCommands) { - botCommand.setAbsSender(null); - } return commandRegistry.deregisterAll(botCommands); } From 6daf18f007597d096ca1e7532ecb5ef6c836d8b7 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Wed, 25 May 2016 18:08:10 +0200 Subject: [PATCH 19/20] fix BotLogger import --- .../org/telegram/telegrambots/api/commands/HelpBotCommand.java | 2 +- .../telegrambots/bots/TelegramLongPollingCommandBot.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java index a68fc2e1..2c02af26 100644 --- a/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java +++ b/src/main/java/org/telegram/telegrambots/api/commands/HelpBotCommand.java @@ -1,10 +1,10 @@ package org.telegram.telegrambots.api.commands; -import org.telegram.telegrambots.BotLogger; import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.objects.Chat; import org.telegram.telegrambots.bots.AbsSender; +import org.telegram.telegrambots.logging.BotLogger; /** * standard help command, which gets registered by default, to supply a list of all available commands diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index a8942fb9..e48f95ed 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -1,6 +1,6 @@ package org.telegram.telegrambots.bots; -import org.telegram.telegrambots.BotLogger; + import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.commands.BotCommand; import org.telegram.telegrambots.api.commands.CommandRegistry; @@ -8,6 +8,7 @@ import org.telegram.telegrambots.api.commands.ICommandRegistry; import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.Update; +import org.telegram.telegrambots.logging.BotLogger; import java.util.Collection; import java.util.Map; From 66566e3a652a664569b0b4760b41990e1943827d Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Thu, 26 May 2016 09:24:10 +0200 Subject: [PATCH 20/20] make command bot extend TelegramLongPollingBot --- .../telegrambots/bots/TelegramLongPollingCommandBot.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index e48f95ed..17c0b0a8 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -18,7 +18,7 @@ import java.util.Map; * * @author tschulz */ -public abstract class TelegramLongPollingCommandBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry { +public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry { public static final String LOGTAG = "TelegramLongPollingCommandBot"; private final CommandRegistry commandRegistry;