remove AbsSender Interface from BotCommand and add reference for sending messages

This commit is contained in:
Timo Schulz 2016-05-20 19:23:44 +02:00
parent 265915daf6
commit 2bff35b8b9
3 changed files with 28 additions and 10 deletions

View File

@ -8,7 +8,7 @@ import org.telegram.telegrambots.bots.AbsSender;
* *
* @author tschulz * @author tschulz
*/ */
public abstract class BotCommand extends AbsSender { public abstract class BotCommand {
public final static String COMMAND_INIT_CHARACTER = "/"; public final static String COMMAND_INIT_CHARACTER = "/";
public final static String COMMAND_PARAMETER_SEPARATOR = " "; public final static String COMMAND_PARAMETER_SEPARATOR = " ";
@ -16,7 +16,7 @@ public abstract class BotCommand extends AbsSender {
private final String commandIdentifier; private final String commandIdentifier;
private final String description; private final String description;
private final String botToken; private AbsSender absSender;
/** /**
* construct a command * 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 commandIdentifier the unique identifier of this command (e.g. the command string to enter into chat)
* @param description the description of this command * @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()) { if (commandIdentifier == null || commandIdentifier.isEmpty()) {
throw new IllegalArgumentException("commandIdentifier for command cannot be null or empty"); 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.commandIdentifier = commandIdentifier.toLowerCase();
this.description = description; this.description = description;
this.botToken = botToken;
} }
/** /**
@ -61,9 +60,20 @@ public abstract class BotCommand extends AbsSender {
return description; return description;
} }
@Override /**
public final String getBotToken() { * Setter
return botToken; * @param absSender set AbsSender
*/
public final void setAbsSender(AbsSender absSender) {
this.absSender = absSender;
}
/**
* Getter
* @return the absSender
*/
protected final AbsSender getAbsSender() {
return absSender;
} }
/** /**

View File

@ -16,7 +16,7 @@ public class HelpBotCommand extends BotCommand {
private final ICommandRegistry commandRegistry; private final ICommandRegistry commandRegistry;
public HelpBotCommand(ICommandRegistry commandRegistry, String botToken) { 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; this.commandRegistry = commandRegistry;
} }
@ -25,12 +25,12 @@ public class HelpBotCommand extends BotCommand {
for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) { for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) {
SendMessage sendMessage = new SendMessage(); SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chat.getId()); sendMessage.setChatId(chat.getId().toString());
sendMessage.enableHtml(true); sendMessage.enableHtml(true);
sendMessage.setText("<b>" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "</b>\n" + registeredBotCommand.getDescription()); sendMessage.setText("<b>" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "</b>\n" + registeredBotCommand.getDescription());
try { try {
sendMessage(sendMessage); getAbsSender().sendMessage(sendMessage);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
BotLogger.error("Failed to send HelpMessage", LOGTAG, e); BotLogger.error("Failed to send HelpMessage", LOGTAG, e);
} }

View File

@ -50,21 +50,29 @@ public abstract class TelegramLongPollingBot extends AbsSender implements ITeleg
@Override @Override
public final boolean register(BotCommand botCommand) { public final boolean register(BotCommand botCommand) {
botCommand.setAbsSender(this);
return commandRegistry.register(botCommand); return commandRegistry.register(botCommand);
} }
@Override @Override
public final Map<BotCommand, Boolean> registerAll(BotCommand... botCommands) { public final Map<BotCommand, Boolean> registerAll(BotCommand... botCommands) {
for (BotCommand botCommand : botCommands) {
botCommand.setAbsSender(this);
}
return commandRegistry.registerAll(botCommands); return commandRegistry.registerAll(botCommands);
} }
@Override @Override
public final boolean deregister(BotCommand botCommand) { public final boolean deregister(BotCommand botCommand) {
botCommand.setAbsSender(null);
return commandRegistry.deregister(botCommand); return commandRegistry.deregister(botCommand);
} }
@Override @Override
public final Map<BotCommand, Boolean> deregisterAll(BotCommand... botCommands) { public final Map<BotCommand, Boolean> deregisterAll(BotCommand... botCommands) {
for (BotCommand botCommand : botCommands) {
botCommand.setAbsSender(null);
}
return commandRegistry.deregisterAll(botCommands); return commandRegistry.deregisterAll(botCommands);
} }