2018-07-08 01:46:26 +02:00
|
|
|
package org.telegram.telegrambots.extensions.bots.commandbot;
|
2016-05-21 14:56:08 +02:00
|
|
|
|
2016-05-25 18:08:10 +02:00
|
|
|
|
2018-07-08 01:41:21 +02:00
|
|
|
import org.telegram.telegrambots.meta.api.objects.Message;
|
|
|
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
|
|
import org.telegram.telegrambots.meta.bots.AbsSender;
|
2017-06-18 05:34:13 +02:00
|
|
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
|
|
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
2018-07-08 01:46:26 +02:00
|
|
|
import org.telegram.telegrambots.extensions.bots.commandbot.commands.CommandRegistry;
|
|
|
|
import org.telegram.telegrambots.extensions.bots.commandbot.commands.IBotCommand;
|
|
|
|
import org.telegram.telegrambots.extensions.bots.commandbot.commands.ICommandRegistry;
|
2016-05-21 14:56:08 +02:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Map;
|
2016-05-27 03:19:17 +02:00
|
|
|
import java.util.function.BiConsumer;
|
2016-05-21 14:56:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class adds command functionality to the TelegramLongPollingBot
|
|
|
|
*
|
2016-05-31 20:01:50 +02:00
|
|
|
* @author Timo Schulz (Mit0x2)
|
2016-05-21 14:56:08 +02:00
|
|
|
*/
|
2021-04-03 15:31:09 +02:00
|
|
|
public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements CommandBot, ICommandRegistry {
|
2016-05-21 14:56:08 +02:00
|
|
|
private final CommandRegistry commandRegistry;
|
|
|
|
|
2017-06-18 14:38:26 +02:00
|
|
|
/**
|
|
|
|
* Creates a TelegramLongPollingCommandBot using default options
|
|
|
|
* Use ICommandRegistry's methods on this bot to register commands
|
|
|
|
*
|
2023-05-30 02:32:15 +02:00
|
|
|
* @deprecated Use {{@link #TelegramLongPollingBot(String)}
|
2017-06-18 14:38:26 +02:00
|
|
|
*/
|
2023-05-30 02:32:15 +02:00
|
|
|
@Deprecated
|
2019-06-27 21:06:08 +02:00
|
|
|
public TelegramLongPollingCommandBot() {
|
2020-11-03 03:57:36 +01:00
|
|
|
this(new DefaultBotOptions());
|
2017-06-18 14:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a TelegramLongPollingCommandBot with custom options and allowing commands with
|
|
|
|
* usernames
|
|
|
|
* Use ICommandRegistry's methods on this bot to register commands
|
2017-11-15 11:44:16 +01:00
|
|
|
*
|
|
|
|
* @param options Bot options
|
2023-05-30 02:32:15 +02:00
|
|
|
*
|
|
|
|
* @deprecated Use {{@link #TelegramLongPollingBot(DefaultBotOptions, String)}
|
2017-06-18 14:38:26 +02:00
|
|
|
*/
|
2023-05-30 02:32:15 +02:00
|
|
|
@Deprecated
|
2019-06-27 21:06:08 +02:00
|
|
|
public TelegramLongPollingCommandBot(DefaultBotOptions options) {
|
|
|
|
this(options, true);
|
2017-06-18 14:38:26 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 10:25:58 +02:00
|
|
|
/**
|
|
|
|
* Creates a TelegramLongPollingCommandBot
|
|
|
|
* Use ICommandRegistry's methods on this bot to register commands
|
2017-11-15 11:44:16 +01:00
|
|
|
*
|
|
|
|
* @param options Bot options
|
2017-06-09 10:25:58 +02:00
|
|
|
* @param allowCommandsWithUsername true to allow commands with parameters (default),
|
|
|
|
* false otherwise
|
2023-05-30 02:32:15 +02:00
|
|
|
*
|
|
|
|
* @deprecated Use {{@link #TelegramLongPollingBot(DefaultBotOptions, boolean, String)}
|
2017-06-09 10:25:58 +02:00
|
|
|
*/
|
2023-05-30 02:32:15 +02:00
|
|
|
@Deprecated
|
2019-06-27 21:06:08 +02:00
|
|
|
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) {
|
2017-06-09 10:25:58 +02:00
|
|
|
super(options);
|
2020-10-20 19:41:34 +02:00
|
|
|
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername);
|
2017-06-09 10:25:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 02:32:15 +02:00
|
|
|
/**
|
|
|
|
* Creates a TelegramLongPollingCommandBot using default options
|
|
|
|
* Use ICommandRegistry's methods on this bot to register commands
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public TelegramLongPollingCommandBot(String botToken) {
|
|
|
|
this(new DefaultBotOptions(), botToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a TelegramLongPollingCommandBot with custom options and allowing commands with
|
|
|
|
* usernames
|
|
|
|
* Use ICommandRegistry's methods on this bot to register commands
|
|
|
|
*
|
|
|
|
* @param options Bot options
|
|
|
|
*/
|
|
|
|
public TelegramLongPollingCommandBot(DefaultBotOptions options, String botToken) {
|
|
|
|
this(options, true, botToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a TelegramLongPollingCommandBot
|
|
|
|
* Use ICommandRegistry's methods on this bot to register commands
|
|
|
|
*
|
|
|
|
* @param options Bot options
|
|
|
|
* @param allowCommandsWithUsername true to allow commands with parameters (default),
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername, String botToken) {
|
|
|
|
super(options, botToken);
|
|
|
|
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername);
|
|
|
|
}
|
|
|
|
|
2016-05-21 14:56:08 +02:00
|
|
|
@Override
|
|
|
|
public final void onUpdateReceived(Update update) {
|
|
|
|
if (update.hasMessage()) {
|
|
|
|
Message message = update.getMessage();
|
2016-07-13 20:28:36 +02:00
|
|
|
if (message.isCommand() && !filter(message)) {
|
2017-11-15 11:44:16 +01:00
|
|
|
if (!commandRegistry.executeCommand(this, message)) {
|
|
|
|
//we have received a not registered command, handle it as invalid
|
|
|
|
processInvalidCommandUpdate(update);
|
2016-05-21 14:56:08 +02:00
|
|
|
}
|
2017-11-15 11:44:16 +01:00
|
|
|
return;
|
2016-05-21 14:56:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
processNonCommandUpdate(update);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 06:37:26 +01:00
|
|
|
public final boolean register(IBotCommand botCommand) {
|
2016-05-21 14:56:08 +02:00
|
|
|
return commandRegistry.register(botCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 06:37:26 +01:00
|
|
|
public final Map<IBotCommand, Boolean> registerAll(IBotCommand... botCommands) {
|
2016-05-21 14:56:08 +02:00
|
|
|
return commandRegistry.registerAll(botCommands);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 06:37:26 +01:00
|
|
|
public final boolean deregister(IBotCommand botCommand) {
|
2016-05-21 14:56:08 +02:00
|
|
|
return commandRegistry.deregister(botCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 06:37:26 +01:00
|
|
|
public final Map<IBotCommand, Boolean> deregisterAll(IBotCommand... botCommands) {
|
2016-05-21 14:56:08 +02:00
|
|
|
return commandRegistry.deregisterAll(botCommands);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-27 06:37:26 +01:00
|
|
|
public final Collection<IBotCommand> getRegisteredCommands() {
|
2016-05-21 14:56:08 +02:00
|
|
|
return commandRegistry.getRegisteredCommands();
|
|
|
|
}
|
|
|
|
|
2016-05-27 03:19:17 +02:00
|
|
|
@Override
|
|
|
|
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
|
|
|
|
commandRegistry.registerDefaultAction(defaultConsumer);
|
|
|
|
}
|
|
|
|
|
2016-07-14 06:49:06 +02:00
|
|
|
@Override
|
2018-02-27 06:37:26 +01:00
|
|
|
public final IBotCommand getRegisteredCommand(String commandIdentifier) {
|
2016-07-14 06:49:06 +02:00
|
|
|
return commandRegistry.getRegisteredCommand(commandIdentifier);
|
|
|
|
}
|
|
|
|
|
2017-06-18 14:38:26 +02:00
|
|
|
/**
|
|
|
|
* @return Bot username
|
|
|
|
*/
|
|
|
|
@Override
|
2020-03-05 17:01:06 +01:00
|
|
|
public abstract String getBotUsername();
|
2016-05-21 14:56:08 +02:00
|
|
|
}
|