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
*/
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;
}
/**

View File

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

View File

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