Add webhook command bot

This commit is contained in:
loolzaaa 2021-04-03 18:31:09 +05:00
parent c8ad62b85a
commit 54575990bc
3 changed files with 173 additions and 32 deletions

View File

@ -0,0 +1,43 @@
package org.telegram.telegrambots.extensions.bots.commandbot;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
/**
* This interface represents common functions for command bots
*
* @author Andrey Korsakov (loolzaaa)
*/
public interface CommandBot {
/**
* Process all updates, that are not commands.
*
* @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>.
*/
void processNonCommandUpdate(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
*
* @param update Received update from Telegram
*/
void processInvalidCommandUpdate(Update 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:
* #
* # return !message.getChat().isGroupChat();
* #
*
* @param message Received message
* @return true if the message must be ignored by the command bot and treated as a non command message,
* false otherwise
* @note Default implementation doesn't filter anything
*/
boolean filter(Message message);
}

View File

@ -19,7 +19,7 @@ import java.util.function.BiConsumer;
*
* @author Timo Schulz (Mit0x2)
*/
public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry {
public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements CommandBot, ICommandRegistry {
private final CommandRegistry commandRegistry;
/**
@ -70,31 +70,13 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
processNonCommandUpdate(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
*
* @param update Received update from Telegram
*/
protected void processInvalidCommandUpdate(Update update) {
@Override
public 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:
* #
* # return !message.getChat().isGroupChat();
* #
*
* @param message Received message
* @return true if the message must be ignored by the command bot and treated as a non command message,
* false otherwise
* @note Default implementation doesn't filter anything
*/
protected boolean filter(Message message) {
@Override
public boolean filter(Message message) {
return false;
}
@ -138,13 +120,4 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
*/
@Override
public abstract String getBotUsername();
/**
* Process all updates, that are not commands.
*
* @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);
}

View File

@ -0,0 +1,125 @@
package org.telegram.telegrambots.extensions.bots.commandbot;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
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;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;
import java.util.Collection;
import java.util.Map;
import java.util.function.BiConsumer;
/**
* This class adds command functionality to the TelegramWebhookBot
*
* @author Andrey Korsakov (loolzaaa)
*/
public abstract class TelegramWebhookCommandBot extends TelegramWebhookBot implements CommandBot, ICommandRegistry {
private final CommandRegistry commandRegistry;
/**
* Creates a TelegramWebhookCommandBot using default options
* Use ICommandRegistry's methods on this bot to register commands
*
*/
public TelegramWebhookCommandBot() {
this(new DefaultBotOptions());
}
/**
* Creates a TelegramWebhookCommandBot with custom options and allowing commands with
* usernames
* Use ICommandRegistry's methods on this bot to register commands
*
* @param options Bot options
*/
public TelegramWebhookCommandBot(DefaultBotOptions options) {
this(options, true);
}
/**
* Creates a TelegramWebhookCommandBot
* 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 TelegramWebhookCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) {
super(options);
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername);
}
@Override
public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
if (message.isCommand() && !filter(message)) {
if (!commandRegistry.executeCommand(this, message)) {
//we have received a not registered command, handle it as invalid
processInvalidCommandUpdate(update);
}
return null;
}
}
processNonCommandUpdate(update);
return null;
}
@Override
public void processInvalidCommandUpdate(Update update) {
processNonCommandUpdate(update);
}
@Override
public boolean filter(Message message) {
return false;
}
@Override
public final boolean register(IBotCommand botCommand) {
return commandRegistry.register(botCommand);
}
@Override
public final Map<IBotCommand, Boolean> registerAll(IBotCommand... botCommands) {
return commandRegistry.registerAll(botCommands);
}
@Override
public final boolean deregister(IBotCommand botCommand) {
return commandRegistry.deregister(botCommand);
}
@Override
public final Map<IBotCommand, Boolean> deregisterAll(IBotCommand... botCommands) {
return commandRegistry.deregisterAll(botCommands);
}
@Override
public final Collection<IBotCommand> getRegisteredCommands() {
return commandRegistry.getRegisteredCommands();
}
@Override
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
commandRegistry.registerDefaultAction(defaultConsumer);
}
@Override
public final IBotCommand getRegisteredCommand(String commandIdentifier) {
return commandRegistry.getRegisteredCommand(commandIdentifier);
}
/**
* @return Bot username
*/
@Override
public abstract String getBotUsername();
}