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

This commit is contained in:
Timo Schulz 2016-05-22 14:10:57 +02:00
parent e51d6eaa4b
commit f33b9fa10e
4 changed files with 10 additions and 32 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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("<b>" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "</b>\n" + registeredBotCommand.getDescription());
try {
getAbsSender().sendMessage(sendMessage);
absSender.sendMessage(sendMessage);
} catch (TelegramApiException e) {
BotLogger.error("Failed to send HelpMessage", LOGTAG, e);
}

View File

@ -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<BotCommand, Boolean> 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<BotCommand, Boolean> deregisterAll(BotCommand... botCommands) {
for (BotCommand botCommand : botCommands) {
botCommand.setAbsSender(null);
}
return commandRegistry.deregisterAll(botCommands);
}