Add DefaultBotCommand, with message ID in execute method

This commit is contained in:
Vadim Goroshevsky 2017-11-02 21:51:44 +02:00
parent 10d0934439
commit 27d6d930cf
No known key found for this signature in database
GPG Key ID: 166A34BEA31EA95F
3 changed files with 66 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package org.telegram.telegrambots.bots.commandbot.commands;
import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.AbsSender;
@ -66,6 +67,17 @@ public abstract class BotCommand {
"</b>\n" + getDescription();
}
/**
* Process the message and execute the command
*
* @param absSender absSender to send messages over
* @param message the message to process
* @param arguments passed arguments
*/
void processMessage(AbsSender absSender, Message message, String[] arguments) {
execute(absSender, message.getFrom(), message.getChat(), arguments);
}
/**
* Execute the command
*

View File

@ -103,7 +103,7 @@ public final class CommandRegistry implements ICommandRegistry {
if (commandRegistryMap.containsKey(command)) {
String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters);
commandRegistryMap.get(command).processMessage(absSender, message, parameters);
return true;
} else if (defaultConsumer != null) {
defaultConsumer.accept(absSender, message);

View File

@ -0,0 +1,53 @@
package org.telegram.telegrambots.bots.commandbot.commands;
import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.AbsSender;
/**
* Bot command with message ID in execute method
*
* @author Vadim Goroshevsky (@vadimgoroshevsky)
*/
public abstract class DefaultBotCommand extends BotCommand {
/**
* Construct a command
*
* @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 DefaultBotCommand(String commandIdentifier, String description) {
super(commandIdentifier, description);
}
/**
* Process the message and execute the command
*
* @param absSender absSender to send messages over
* @param message the message to process
* @param arguments passed arguments
*/
@Override
void processMessage(AbsSender absSender, Message message, String[] arguments) {
execute(absSender, message.getFrom(), message.getChat(), message.getMessageId(), arguments);
}
// We'll override this method here for not repeating it in DefaultBotCommand's children
@Override
public final void execute(AbsSender absSender, User user, Chat chat, String[] arguments) {
}
/**
* Execute the command
*
* @param absSender absSender to send messages over
* @param user the user who sent the command
* @param chat the chat, to be able to send replies
* @param messageId message id for interaction
* @param arguments passed arguments
*/
public abstract void execute(AbsSender absSender, User user, Chat chat, Integer messageId, String[] arguments);
}