Merge pull request #89 from Mit0x2/command_bot_additions

Command bot fixes
This commit is contained in:
Ruben Bermudez 2016-05-31 21:58:04 +02:00
commit 5b1130a788
5 changed files with 12 additions and 9 deletions

View File

@ -21,7 +21,7 @@ In order to use Long Polling mode, just create your own bot extending `org.teleg
If you like to use Webhook, extend `org.telegram.telegrambots.bots.TelegramWebhookBot` If you like to use Webhook, extend `org.telegram.telegrambots.bots.TelegramWebhookBot`
Once done, you just need to creat a `org.telegram.telegrambots.TelegramBotsApi`and register your bots: Once done, you just need to create a `org.telegram.telegrambots.TelegramBotsApi`and register your bots:
```java ```java

View File

@ -14,7 +14,7 @@ import java.util.function.BiConsumer;
/** /**
* This class adds command functionality to the TelegramLongPollingBot * This class adds command functionality to the TelegramLongPollingBot
* *
* @author tschulz * @author Timo Schulz (Mit0x2)
*/ */
public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry { public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry {
private final CommandRegistry commandRegistry; private final CommandRegistry commandRegistry;

View File

@ -1,12 +1,13 @@
package org.telegram.telegrambots.bots.commands; package org.telegram.telegrambots.bots.commands;
import org.telegram.telegrambots.api.objects.Chat; import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.AbsSender; import org.telegram.telegrambots.bots.AbsSender;
/** /**
* Representation of a command, which can be executed * Representation of a command, which can be executed
* *
* @author tschulz * @author Timo Schulz (Mit0x2)
*/ */
public abstract class BotCommand { public abstract class BotCommand {
public final static String COMMAND_INIT_CHARACTER = "/"; public final static String COMMAND_INIT_CHARACTER = "/";
@ -69,8 +70,9 @@ public abstract class BotCommand {
* Execute the command * Execute the command
* *
* @param absSender absSender to send messages over * @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 chat the chat, to be able to send replies
* @param arguments passed arguments * @param arguments passed arguments
*/ */
public abstract void execute(AbsSender absSender, Chat chat, String[] arguments); public abstract void execute(AbsSender absSender, User user, Chat chat, String[] arguments);
} }

View File

@ -10,16 +10,15 @@ import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
/** /**
* @author tschulz * This class manages all the commands for a bot. You can register and deregister commands on demand
*
* @author Timo Schulz (Mit0x2)
*/ */
public final class CommandRegistry implements ICommandRegistry { public final class CommandRegistry implements ICommandRegistry {
private final Map<String, BotCommand> commandRegistryMap = new HashMap<>(); private final Map<String, BotCommand> commandRegistryMap = new HashMap<>();
private BiConsumer<AbsSender, Message> defaultConsumer; private BiConsumer<AbsSender, Message> defaultConsumer;
public CommandRegistry() {
}
@Override @Override
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) { public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
this.defaultConsumer = defaultConsumer; this.defaultConsumer = defaultConsumer;
@ -87,7 +86,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(absSender, message.getChat(), parameters); commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters);
return true; return true;
} else if (defaultConsumer != null) { } else if (defaultConsumer != null) {
defaultConsumer.accept(absSender, message); defaultConsumer.accept(absSender, message);

View File

@ -8,7 +8,9 @@ import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
/** /**
* This Interface represents the gateway for registering and deregistering commands.
* *
* @author Timo Schulz (Mit0x2)
*/ */
public interface ICommandRegistry { public interface ICommandRegistry {