From 27d6d930cfd0beea2b3572b736012f6fc5fcc381 Mon Sep 17 00:00:00 2001 From: Vadim Goroshevsky Date: Thu, 2 Nov 2017 21:51:44 +0200 Subject: [PATCH] Add DefaultBotCommand, with message ID in execute method --- .../bots/commandbot/commands/BotCommand.java | 12 +++++ .../commandbot/commands/CommandRegistry.java | 2 +- .../commands/DefaultBotCommand.java | 53 +++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/DefaultBotCommand.java 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 00f490db..6d6c3b63 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 @@ -1,6 +1,7 @@ package org.telegram.telegrambots.bots.commandbot.commands; import org.telegram.telegrambots.api.objects.Chat; +import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.User; import org.telegram.telegrambots.bots.AbsSender; @@ -66,6 +67,17 @@ public abstract class BotCommand { "\n" + getDescription(); } + /** + * Process the message and execute the command + * + * @param absSender absSender to send messages over + * @param message the message to process + * @param arguments passed arguments + */ + void processMessage(AbsSender absSender, Message message, String[] arguments) { + execute(absSender, message.getFrom(), message.getChat(), arguments); + } + /** * Execute the command * 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 99858e3f..7ca5c0cc 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 @@ -103,7 +103,7 @@ public final class CommandRegistry implements ICommandRegistry { if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); - commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters); + commandRegistryMap.get(command).processMessage(absSender, message, parameters); return true; } else if (defaultConsumer != null) { defaultConsumer.accept(absSender, message); 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 new file mode 100644 index 00000000..a4124150 --- /dev/null +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/commands/DefaultBotCommand.java @@ -0,0 +1,53 @@ +package org.telegram.telegrambots.bots.commandbot.commands; + +import org.telegram.telegrambots.api.objects.Chat; +import org.telegram.telegrambots.api.objects.Message; +import org.telegram.telegrambots.api.objects.User; +import org.telegram.telegrambots.bots.AbsSender; + +/** + * Bot command with message ID in execute method + * + * @author Vadim Goroshevsky (@vadimgoroshevsky) + */ +public abstract class DefaultBotCommand extends BotCommand { + + /** + * 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 DefaultBotCommand(String commandIdentifier, String description) { + super(commandIdentifier, description); + } + + /** + * Process the message and execute the command + * + * @param absSender absSender to send messages over + * @param message the message to process + * @param arguments passed arguments + */ + @Override + void processMessage(AbsSender absSender, Message message, String[] arguments) { + execute(absSender, message.getFrom(), message.getChat(), message.getMessageId(), arguments); + } + + // We'll override this method here for not repeating it in DefaultBotCommand's children + @Override + public final void execute(AbsSender absSender, User user, Chat chat, String[] arguments) { + } + + /** + * Execute the command + * + * @param absSender absSender to send messages over + * @param user the user who sent the command + * @param chat the chat, to be able to send replies + * @param messageId message id for interaction + * @param arguments passed arguments + */ + public abstract void execute(AbsSender absSender, User user, Chat chat, Integer messageId, String[] arguments); +}