com.symphony.bdk.bot.sdk.command.CommandHandler Maven / Gradle / Ivy
package com.symphony.bdk.bot.sdk.command;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.symphony.bdk.bot.sdk.command.model.BotCommand;
import com.symphony.bdk.bot.sdk.feature.FeatureManager;
import com.symphony.bdk.bot.sdk.symphony.MessageClientImpl;
import com.symphony.bdk.bot.sdk.symphony.UsersClient;
import com.symphony.bdk.bot.sdk.symphony.model.SymphonyMessage;
import lombok.Setter;
/**
* Base class for bot command handling. Has it child classes automatically registered to {@link
* CommandDispatcher} and {@link CommandFilter}. Provides mechanism for developers to define a
* response for the command room
*
* @author Marcus Secato
*/
@Setter
public abstract class CommandHandler implements BaseCommandHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandHandler.class);
private CommandDispatcher commandDispatcher;
private CommandFilter commandFilter;
private MessageClientImpl messageClient;
private FeatureManager featureManager;
private UsersClient usersClient;
private void register() {
init();
commandDispatcher.register(getCommandName(), this);
commandFilter.addFilter(getCommandName(), getCommandMatcher());
}
private String getCommandName() {
return this.getClass().getCanonicalName();
}
protected String getBotName() {
return usersClient.getBotDisplayName();
}
/**
* Initializes the CommandHandler dependencies. This method can be overridden by the child classes
* if the developers want to implement initialization logic.
*/
protected void init() {
}
/**
* {@inheritDoc}
*/
@Override
public void onCommand(BotCommand command) {
LOGGER.debug("Received command {}", command.getMessageEvent());
final SymphonyMessage commandResponse = new SymphonyMessage();
try {
handle(command, commandResponse);
if (commandResponse.hasContent() && featureManager.isCommandFeedbackEnabled()) {
messageClient._sendMessage(command.getMessageEvent().getStreamId(), commandResponse);
}
} catch (Exception e) {
LOGGER.error("Error processing command {}\n{}", getCommandName(), e);
if (featureManager.unexpectedErrorResponse() != null) {
messageClient._sendMessage(command.getMessageEvent().getStreamId(),
new SymphonyMessage(featureManager.unexpectedErrorResponse()));
}
}
}
/**
* Returns the pattern used by {@link CommandFilter} to filter out bot commands.
*
* @return the matcher object
*/
protected abstract Predicate getCommandMatcher();
/**
* Handles a command issued to the bot
*
* @param command the command issued in chat room
* @param commandResponse the response to be sent to Symphony chat
*/
public abstract void handle(BotCommand command, final SymphonyMessage commandResponse);
}