Merge pull request #330 from vadimgoroshevsky/dev

Add DefaultBotCommand, with message ID in execute method
This commit is contained in:
Ruben Bermudez 2017-11-15 18:30:50 +01:00 committed by GitHub
commit 135602b5b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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);
}