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:
parent
e51d6eaa4b
commit
f33b9fa10e
@ -16,7 +16,6 @@ public abstract class BotCommand {
|
|||||||
|
|
||||||
private final String commandIdentifier;
|
private final String commandIdentifier;
|
||||||
private final String description;
|
private final String description;
|
||||||
private AbsSender absSender;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct a command
|
* construct a command
|
||||||
@ -60,27 +59,12 @@ public abstract class BotCommand {
|
|||||||
return description;
|
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
|
* 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 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);
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package org.telegram.telegrambots.api.commands;
|
package org.telegram.telegrambots.api.commands;
|
||||||
|
|
||||||
import org.telegram.telegrambots.api.objects.Message;
|
import org.telegram.telegrambots.api.objects.Message;
|
||||||
|
import org.telegram.telegrambots.bots.AbsSender;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -65,7 +66,7 @@ public final class CommandRegistry implements ICommandRegistry {
|
|||||||
* @param message input message
|
* @param message input message
|
||||||
* @return true if success or false otherwise
|
* @return true if success or false otherwise
|
||||||
*/
|
*/
|
||||||
public final boolean executeCommand(Message message) {
|
public final boolean executeCommand(AbsSender absSender, Message message) {
|
||||||
if (message.hasText()) {
|
if (message.hasText()) {
|
||||||
String text = message.getText();
|
String text = message.getText();
|
||||||
if (!text.isEmpty() && text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
|
if (!text.isEmpty() && text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
|
||||||
@ -76,7 +77,7 @@ public final class CommandRegistry implements ICommandRegistry {
|
|||||||
|
|
||||||
if (commandRegistryMap.containsKey(command)) {
|
if (commandRegistryMap.containsKey(command)) {
|
||||||
String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import org.telegram.telegrambots.BotLogger;
|
|||||||
import org.telegram.telegrambots.TelegramApiException;
|
import org.telegram.telegrambots.TelegramApiException;
|
||||||
import org.telegram.telegrambots.api.methods.send.SendMessage;
|
import org.telegram.telegrambots.api.methods.send.SendMessage;
|
||||||
import org.telegram.telegrambots.api.objects.Chat;
|
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
|
* 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
|
@Override
|
||||||
public void execute(String[] arguments, Chat chat) {
|
public void execute(AbsSender absSender, Chat chat, String[] arguments) {
|
||||||
|
|
||||||
for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) {
|
for (BotCommand registeredBotCommand : commandRegistry.getRegisteredCommands()) {
|
||||||
SendMessage sendMessage = new SendMessage();
|
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());
|
sendMessage.setText("<b>" + COMMAND_INIT_CHARACTER + registeredBotCommand.getCommandIdentifier() + "</b>\n" + registeredBotCommand.getDescription());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
getAbsSender().sendMessage(sendMessage);
|
absSender.sendMessage(sendMessage);
|
||||||
} catch (TelegramApiException e) {
|
} catch (TelegramApiException e) {
|
||||||
BotLogger.error("Failed to send HelpMessage", LOGTAG, e);
|
BotLogger.error("Failed to send HelpMessage", LOGTAG, e);
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public abstract class TelegramLongPollingCommandBot extends AbsSender implements
|
|||||||
if (update.hasMessage()) {
|
if (update.hasMessage()) {
|
||||||
Message message = update.getMessage();
|
Message message = update.getMessage();
|
||||||
if (message.isCommand()) {
|
if (message.isCommand()) {
|
||||||
if (!commandRegistry.executeCommand(message)) {
|
if (!commandRegistry.executeCommand(this, message)) {
|
||||||
SendMessage sendMessage = new SendMessage();
|
SendMessage sendMessage = new SendMessage();
|
||||||
sendMessage.setChatId(message.getChatId().toString());
|
sendMessage.setChatId(message.getChatId().toString());
|
||||||
sendMessage.setText("The command you provided is not registered for this bot");
|
sendMessage.setText("The command you provided is not registered for this bot");
|
||||||
@ -53,29 +53,21 @@ public abstract class TelegramLongPollingCommandBot extends AbsSender implements
|
|||||||
|
|
||||||
@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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user