Class SlackClient
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classGeneric slack response.private static classDTO to hold the minimum data in postMessage API body.private static classUser object slack response. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringSlack delivery channel.static final StringDirect PM token for channel identifier property.private StringLookup by email API URL.private StringPost a new message API URL.private org.springframework.web.client.RestTemplateRest template.private es.kukenan.smartfi.common.utils.RestTemplateFactoryRest template factory (adds supports for several extensions, for example, proxy).private StringSlack application token.Email - UserId map, to reduce the number of API calls. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidSlack client initializetion.private StringbodyAsStringSafe(org.springframework.http.ResponseEntity<? extends Object> responseEntity) Tries to extract the body from the given response entity as a string.private StringfindAndCacheUserIdByEmail(String email) Locate user identifier (required in postMessage) by the given email.private StringfindUserIdByEmail(String email) Locate user identifier (required in postMessage) by the given email.sendMessage(String address, String message) Sends the given message to the user with the given email.private voidsendMessageToChannel(String channelId, String message) Sends the given message to the user with the given identifier.
-
Field Details
-
DIRECT_PM
Direct PM token for channel identifier property.- See Also:
-
token
@Value("${slack.notifications.token:xoxp-3370302806935-3384932117347-3402739053605-4462cabbb5820a543b079cad16d0f586}") private String tokenSlack application token. -
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 lookupByEmailApiLookup by email API URL. -
postMessageApi
@Value("${slack.notifications.api.chat.postMessage:https://slack.com/api/chat.postMessage}") private String postMessageApiPost a new message API URL. -
restTemplateFactory
@Autowired private es.kukenan.smartfi.common.utils.RestTemplateFactory restTemplateFactoryRest template factory (adds supports for several extensions, for example, proxy). -
userIdMap
Email - UserId map, to reduce the number of API calls. -
restTemplate
private org.springframework.web.client.RestTemplate restTemplateRest template.
-
-
Constructor Details
-
SlackClient
public SlackClient()
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()Slack client initializetion.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
sendMessage
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:
thisobject to allow fluent coding.
-
findAndCacheUserIdByEmail
Locate user identifier (required in postMessage) by the given email. UsesuserIdMapas cache to reduce the number of API calls.- Parameters:
email- user email.- Returns:
- User identifier.
-
findUserIdByEmail
Locate user identifier (required in postMessage) by the given email. ARuntimeExceptionis thrown if the API call fails.- Parameters:
email- user email.- Returns:
- User identifier.
-
sendMessageToChannel
Sends the given message to the user with the given identifier. ARuntimeExceptionis 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.
-