move command bot functionality to own bot base class, to avoid api change for future releases

This commit is contained in:
Timo Schulz 2016-05-21 14:56:08 +02:00
parent 2bff35b8b9
commit 3dd0b87ca0
2 changed files with 97 additions and 76 deletions

View File

@ -1,85 +1,11 @@
package org.telegram.telegrambots.bots; package org.telegram.telegrambots.bots;
import org.telegram.telegrambots.BotLogger;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.commands.BotCommand;
import org.telegram.telegrambots.api.commands.CommandRegistry;
import org.telegram.telegrambots.api.commands.ICommandRegistry;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import java.util.Collection;
import java.util.Map;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
* @version 1.0 * @version 1.0
* @brief TODO * @brief TODO
* @date 14 of January of 2016 * @date 14 of January of 2016
*/ */
public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry { public abstract class TelegramLongPollingBot extends AbsSender implements ITelegramLongPollingBot {
public static final String LOGTAG = "TelegramLongPollingBot"; }
private final CommandRegistry commandRegistry;
public TelegramLongPollingBot() {
this.commandRegistry = new CommandRegistry(getBotToken());
}
@Override
public final void onUpdateReceived(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
if (message.isCommand()) {
if (!commandRegistry.executeCommand(message)) {
SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(message.getChatId().toString());
sendMessage.setText("The command you provided is not registered for this bot");
try {
sendMessage(sendMessage);
} catch (TelegramApiException e) {
BotLogger.error("Cannot send message", LOGTAG, e);
}
}
return;
}
}
processNonCommandUpdate(update);
}
@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);
}
@Override
public final Collection<BotCommand> getRegisteredCommands() {
return commandRegistry.getRegisteredCommands();
}
public abstract void processNonCommandUpdate(Update update);
}

View File

@ -0,0 +1,95 @@
package org.telegram.telegrambots.bots;
import org.telegram.telegrambots.BotLogger;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.commands.BotCommand;
import org.telegram.telegrambots.api.commands.CommandRegistry;
import org.telegram.telegrambots.api.commands.ICommandRegistry;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import java.util.Collection;
import java.util.Map;
/**
* This class adds command functionality to the TelegramLongPollingBot
*
* @author tschulz
*/
public abstract class TelegramLongPollingCommandBot extends AbsSender implements ITelegramLongPollingBot, ICommandRegistry{
public static final String LOGTAG = "TelegramLongPollingCommandBot";
private final CommandRegistry commandRegistry;
/**
* construct creates CommandRegistry for this bot.
* Use ICommandRegistry's methods on this bot to register commands
*/
public TelegramLongPollingCommandBot() {
this.commandRegistry = new CommandRegistry(getBotToken());
}
@Override
public final void onUpdateReceived(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
if (message.isCommand()) {
if (!commandRegistry.executeCommand(message)) {
SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(message.getChatId().toString());
sendMessage.setText("The command you provided is not registered for this bot");
try {
sendMessage(sendMessage);
} catch (TelegramApiException e) {
BotLogger.error("Cannot send message", LOGTAG, e);
}
}
return;
}
}
processNonCommandUpdate(update);
}
@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);
}
@Override
public final Collection<BotCommand> getRegisteredCommands() {
return commandRegistry.getRegisteredCommands();
}
/**
* Process all updates, that are not commands.
* Attention: commands, that have valid syntax but are not registered on this bot,
* won't be forwarded to this method!
*
* @param update the update
*/
public abstract void processNonCommandUpdate(Update update);
}