All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.symphony.bdk.bot.sdk.event.EventHandler Maven / Gradle / Ivy

package com.symphony.bdk.bot.sdk.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ResolvableType;

import com.symphony.bdk.bot.sdk.event.model.BaseEvent;
import com.symphony.bdk.bot.sdk.feature.FeatureManager;
import com.symphony.bdk.bot.sdk.symphony.MessageClientImpl;
import com.symphony.bdk.bot.sdk.symphony.model.SymphonyMessage;

import lombok.Setter;

/**
 * Base class for Symphony events handling. Provides mechanisms to
 * automatically register child classes to {@link EventDispatcher}.
 *
 * @author Marcus Secato
 *
 */
@Setter
public abstract class EventHandler implements BaseEventHandler {
  private static final Logger LOGGER = LoggerFactory.getLogger(EventHandler.class);

  private EventDispatcher eventDispatcher;

  private MessageClientImpl messageClient;

  private FeatureManager featureManager;

  private void register() {
    init();
    ResolvableType type = ResolvableType.forRawClass(this.getClass());
    eventDispatcher.register(
        type.getSuperType().getGeneric(0).toString(), this);
  }

  /**
   * Initializes the ElementsHandler 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 onEvent(E event) {
    LOGGER.debug("Received event for stream: {}", event.getStreamId());

    final SymphonyMessage eventResponse = new SymphonyMessage();
    try {
      handle(event, eventResponse);

      if (eventResponse.hasContent()
          && featureManager.isCommandFeedbackEnabled()) {
        messageClient._sendMessage(event.getStreamId(), eventResponse);
      }

    } catch (Exception e) {
      LOGGER.error("Error processing event {}", e);
    }
  }

  /**
   * Handles the Symphony event
   *
   * @param event the event triggered in Symphony
   * @param eventResponse the response to be sent to Symphony chat
   */
  public abstract void handle(E event, final SymphonyMessage eventResponse);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy