Class TelegramLongPollingCommandBot
- java.lang.Object
-
- org.telegram.telegrambots.meta.bots.AbsSender
-
- org.telegram.telegrambots.bots.DefaultAbsSender
-
- org.telegram.telegrambots.bots.TelegramLongPollingBot
-
- org.telegram.telegrambots.extensions.bots.commandbot.TelegramLongPollingCommandBot
-
- All Implemented Interfaces:
ICommandRegistry
,org.telegram.telegrambots.meta.generics.LongPollingBot
,org.telegram.telegrambots.meta.generics.TelegramBot
public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry
This class adds command functionality to the TelegramLongPollingBot- Author:
- Timo Schulz (Mit0x2)
-
-
Field Summary
-
Fields inherited from class org.telegram.telegrambots.bots.DefaultAbsSender
exe
-
-
Constructor Summary
Constructors Constructor Description TelegramLongPollingCommandBot()
Creates a TelegramLongPollingCommandBot using default options Use ICommandRegistry's methods on this bot to register commandsTelegramLongPollingCommandBot(DefaultBotOptions options)
Creates a TelegramLongPollingCommandBot with custom options and allowing commands with usernames Use ICommandRegistry's methods on this bot to register commandsTelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername)
Creates a TelegramLongPollingCommandBot Use ICommandRegistry's methods on this bot to register commands
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
deregister(IBotCommand botCommand)
deregister a commandMap<IBotCommand,Boolean>
deregisterAll(IBotCommand... botCommands)
deregister multiple commandsprotected boolean
filter(org.telegram.telegrambots.meta.api.objects.Message message)
Override this function in your bot implementation to filter messages with commandsabstract String
getBotUsername()
IBotCommand
getRegisteredCommand(String commandIdentifier)
get registered commandCollection<IBotCommand>
getRegisteredCommands()
get a collection of all registered commandsvoid
onUpdateReceived(org.telegram.telegrambots.meta.api.objects.Update update)
protected void
processInvalidCommandUpdate(org.telegram.telegrambots.meta.api.objects.Update update)
This method is called when user sends a not registered command.abstract void
processNonCommandUpdate(org.telegram.telegrambots.meta.api.objects.Update update)
Process all updates, that are not commands.boolean
register(IBotCommand botCommand)
register a commandMap<IBotCommand,Boolean>
registerAll(IBotCommand... botCommands)
register multiple commandsvoid
registerDefaultAction(BiConsumer<org.telegram.telegrambots.meta.bots.AbsSender,org.telegram.telegrambots.meta.api.objects.Message> defaultConsumer)
Register a default action when there is no command register that match the message sent-
Methods inherited from class org.telegram.telegrambots.bots.TelegramLongPollingBot
clearWebhook, onClosing
-
Methods inherited from class org.telegram.telegrambots.bots.DefaultAbsSender
downloadFile, downloadFile, downloadFile, downloadFile, downloadFileAsStream, downloadFileAsStream, downloadFileAsync, downloadFileAsync, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, execute, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, executeAsync, getBaseUrl, getBotToken, getOptions, sendApiMethod, sendApiMethodAsync, sendApiMethodAsync
-
Methods inherited from class org.telegram.telegrambots.meta.bots.AbsSender
execute, executeAsync, executeAsync, getMe, getMeAsync, getMeAsync, getWebhookInfo, getWebhookInfoAsync, getWebhookInfoAsync
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Constructor Detail
-
TelegramLongPollingCommandBot
public TelegramLongPollingCommandBot()
Creates a TelegramLongPollingCommandBot using default options Use ICommandRegistry's methods on this bot to register commands
-
TelegramLongPollingCommandBot
public TelegramLongPollingCommandBot(DefaultBotOptions options)
Creates a TelegramLongPollingCommandBot with custom options and allowing commands with usernames Use ICommandRegistry's methods on this bot to register commands- Parameters:
options
- Bot options
-
TelegramLongPollingCommandBot
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername)
Creates a TelegramLongPollingCommandBot Use ICommandRegistry's methods on this bot to register commands- Parameters:
options
- Bot optionsallowCommandsWithUsername
- true to allow commands with parameters (default), false otherwise
-
-
Method Detail
-
onUpdateReceived
public final void onUpdateReceived(org.telegram.telegrambots.meta.api.objects.Update update)
- Specified by:
onUpdateReceived
in interfaceorg.telegram.telegrambots.meta.generics.LongPollingBot
-
processInvalidCommandUpdate
protected void processInvalidCommandUpdate(org.telegram.telegrambots.meta.api.objects.Update update)
This method is called when user sends a not registered command. By default it will just call processNonCommandUpdate(), override it in your implementation if you want your bot to do other things, such as sending an error message- Parameters:
update
- Received update from Telegram
-
filter
protected boolean filter(org.telegram.telegrambots.meta.api.objects.Message message)
Override this function in your bot implementation to filter messages with commandsFor example, if you want to prevent commands execution incoming from group chat: # # return !message.getChat().isGroupChat(); #
- Parameters:
message
- Received message- Returns:
- true if the message must be ignored by the command bot and treated as a non command message, false otherwise
-
register
public final boolean register(IBotCommand botCommand)
Description copied from interface:ICommandRegistry
register a command- Specified by:
register
in interfaceICommandRegistry
- Parameters:
botCommand
- the command to register- Returns:
- whether the command could be registered, was not already registered
-
registerAll
public final Map<IBotCommand,Boolean> registerAll(IBotCommand... botCommands)
Description copied from interface:ICommandRegistry
register multiple commands- Specified by:
registerAll
in interfaceICommandRegistry
- Parameters:
botCommands
- commands to register- Returns:
- map with results of the command register per command
-
deregister
public final boolean deregister(IBotCommand botCommand)
Description copied from interface:ICommandRegistry
deregister a command- Specified by:
deregister
in interfaceICommandRegistry
- Parameters:
botCommand
- the command to deregister- Returns:
- whether the command could be deregistered, was registered
-
deregisterAll
public final Map<IBotCommand,Boolean> deregisterAll(IBotCommand... botCommands)
Description copied from interface:ICommandRegistry
deregister multiple commands- Specified by:
deregisterAll
in interfaceICommandRegistry
- Parameters:
botCommands
- commands to deregister- Returns:
- map with results of the command deregistered per command
-
getRegisteredCommands
public final Collection<IBotCommand> getRegisteredCommands()
Description copied from interface:ICommandRegistry
get a collection of all registered commands- Specified by:
getRegisteredCommands
in interfaceICommandRegistry
- Returns:
- a collection of registered commands
-
registerDefaultAction
public void registerDefaultAction(BiConsumer<org.telegram.telegrambots.meta.bots.AbsSender,org.telegram.telegrambots.meta.api.objects.Message> defaultConsumer)
Description copied from interface:ICommandRegistry
Register a default action when there is no command register that match the message sent- Specified by:
registerDefaultAction
in interfaceICommandRegistry
- Parameters:
defaultConsumer
- Consumer to evaluate the message
-
getRegisteredCommand
public final IBotCommand getRegisteredCommand(String commandIdentifier)
Description copied from interface:ICommandRegistry
get registered command- Specified by:
getRegisteredCommand
in interfaceICommandRegistry
- Returns:
- registered command if exists or null if not
-
getBotUsername
public abstract String getBotUsername()
- Specified by:
getBotUsername
in interfaceorg.telegram.telegrambots.meta.generics.TelegramBot
- Returns:
- Bot username
-
processNonCommandUpdate
public abstract void processNonCommandUpdate(org.telegram.telegrambots.meta.api.objects.Update update)
Process all updates, that are not commands.- Parameters:
update
- the update
-
-