From f33b9fa10efc0317be460a02f7ccaacfe4399362 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Sun, 22 May 2016 14:10:57 +0200 Subject: [PATCH] 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); }