Added processInvalidCommandUpdate(), to handle invalid command sent by user

This commit is contained in:
malta895 2017-11-15 11:44:16 +01:00
parent 10d0934439
commit ff1ef8586f

View File

@ -38,6 +38,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
* Creates a TelegramLongPollingCommandBot with custom options and allowing commands with * Creates a TelegramLongPollingCommandBot with custom options and allowing commands with
* usernames * usernames
* Use ICommandRegistry's methods on this bot to register commands * Use ICommandRegistry's methods on this bot to register commands
*
* @param options Bot options * @param options Bot options
* @param botUsername Username of the bot * @param botUsername Username of the bot
*/ */
@ -48,6 +49,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
/** /**
* Creates a TelegramLongPollingCommandBot * Creates a TelegramLongPollingCommandBot
* Use ICommandRegistry's methods on this bot to register commands * Use ICommandRegistry's methods on this bot to register commands
*
* @param options Bot options * @param options Bot options
* @param allowCommandsWithUsername true to allow commands with parameters (default), * @param allowCommandsWithUsername true to allow commands with parameters (default),
* false otherwise * false otherwise
@ -64,26 +66,39 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
if (update.hasMessage()) { if (update.hasMessage()) {
Message message = update.getMessage(); Message message = update.getMessage();
if (message.isCommand() && !filter(message)) { if (message.isCommand() && !filter(message)) {
if (commandRegistry.executeCommand(this, message)) { if (!commandRegistry.executeCommand(this, message)) {
return; //we have received a not registered command, handle it as invalid
processInvalidCommandUpdate(update);
} }
return;
} }
} }
processNonCommandUpdate(update); processNonCommandUpdate(update);
} }
/** /**
* Override this function in your bot implementation to filter messages with commands * 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
* *
* @param update Received update from Telegram
*/
protected void processInvalidCommandUpdate(Update update) {
processNonCommandUpdate(update);
}
/**
* Override this function in your bot implementation to filter messages with commands
* <p>
* For example, if you want to prevent commands execution incoming from group chat: * For example, if you want to prevent commands execution incoming from group chat:
* # * #
* # return !message.getChat().isGroupChat(); * # return !message.getChat().isGroupChat();
* # * #
* *
* @note Default implementation doesn't filter anything
* @param message Received message * @param message Received message
* @return true if the message must be ignored by the command bot and treated as a non command message, * @return true if the message must be ignored by the command bot and treated as a non command message,
* false otherwise * false otherwise
* @note Default implementation doesn't filter anything
*/ */
protected boolean filter(Message message) { protected boolean filter(Message message) {
return false; return false;
@ -134,10 +149,10 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
/** /**
* Process all updates, that are not commands. * Process all updates, that are not commands.
* @warning Commands that have valid syntax but are not registered on this bot,
* won't be forwarded to this method <b>if a default action is present</b>.
* *
* @param update the update * @param update the update
* @warning Commands that have valid syntax but are not registered on this bot,
* won't be forwarded to this method <b>if a default action is present</b>.
*/ */
public abstract void processNonCommandUpdate(Update update); public abstract void processNonCommandUpdate(Update update);
} }