java.lang.Object
es.kukenan.smartfi.microservice.communication.service.provider.impl.slack.SlackClient
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean

@Component public class SlackClient extends Object implements org.springframework.beans.factory.InitializingBean
Slack client.
  • Field Details

    • DIRECT_PM

      public static final String DIRECT_PM
      Direct PM token for channel identifier property.
      See Also:
    • token

      @Value("${slack.notifications.token:xoxp-3370302806935-3384932117347-3402739053605-4462cabbb5820a543b079cad16d0f586}") private String token
      Slack application token.
    • channel

      @Value("${notifications.slack.channel}") private String channel
      Slack delivery channel. Accepts direct-pm in order to send the message directly to a user or a fixed channel identifier, for example 013XNDSF7G.
    • lookupByEmailApi

      @Value("${slack.notifications.api.users.lookupByEmail:https://slack.com/api/users.lookupByEmail}") private String lookupByEmailApi
      Lookup by email API URL.
    • postMessageApi

      @Value("${slack.notifications.api.chat.postMessage:https://slack.com/api/chat.postMessage}") private String postMessageApi
      Post a new message API URL.
    • restTemplateFactory

      @Autowired private es.kukenan.smartfi.common.utils.RestTemplateFactory restTemplateFactory
      Rest template factory (adds supports for several extensions, for example, proxy).
    • userIdMap

      private Map<String,String> userIdMap
      Email - UserId map, to reduce the number of API calls.
    • restTemplate

      private org.springframework.web.client.RestTemplate restTemplate
      Rest template.
  • Constructor Details

    • SlackClient

      public SlackClient()
  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Slack client initializetion.
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • sendMessage

      public SlackClient sendMessage(String address, String message)
      Sends the given message to the user with the given email.
      Parameters:
      address - user address (email in direct PM, name in fixed channel).
      message - message to send.
      Returns:
      this object to allow fluent coding.
    • findAndCacheUserIdByEmail

      private String findAndCacheUserIdByEmail(String email)
      Locate user identifier (required in postMessage) by the given email. Uses userIdMap as cache to reduce the number of API calls.
      Parameters:
      email - user email.
      Returns:
      User identifier.
    • findUserIdByEmail

      private String findUserIdByEmail(String email)
      Locate user identifier (required in postMessage) by the given email. A RuntimeException is thrown if the API call fails.
      Parameters:
      email - user email.
      Returns:
      User identifier.
    • sendMessageToChannel

      private void sendMessageToChannel(String channelId, String message)
      Sends the given message to the user with the given identifier. A RuntimeException is thrown if the API call fails.
      Parameters:
      channelId - user slack identifier.
      message - message to send.
    • bodyAsStringSafe

      private String bodyAsStringSafe(org.springframework.http.ResponseEntity<? extends Object> responseEntity)
      Tries to extract the body from the given response entity as a string. If anything fails, an empty string is returned.
      Parameters:
      responseEntity - response entity.
      Returns:
      the body from the given response entity as a string.