-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from LikeKNU/Chore/Slack
chore: λΉμ¦λμ€ μμΈ λ°μ μ μ¬λ μλ¦Ό
- Loading branch information
Showing
9 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/ac/knu/likeknu/exception/BusinessExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
package ac.knu.likeknu.exception; | ||
|
||
import ac.knu.likeknu.service.SlackService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class BusinessExceptionHandler { | ||
|
||
private final SlackService slackService; | ||
|
||
public BusinessExceptionHandler(SlackService slackService) { | ||
this.slackService = slackService; | ||
} | ||
|
||
@ExceptionHandler(value = BusinessException.class) | ||
protected ResponseEntity<String> businessExceptionHandler(BusinessException exception) { | ||
String message = exception.getMessage(); | ||
log.info("sd", exception); | ||
slackService.sendMessage(message); | ||
return ResponseEntity.badRequest().body(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ac.knu.likeknu.service; | ||
|
||
import com.github.seratch.jslack.Slack; | ||
import com.github.seratch.jslack.api.model.Attachment; | ||
import com.github.seratch.jslack.api.webhook.Payload; | ||
import com.github.seratch.jslack.api.webhook.WebhookResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
@Slf4j | ||
@Service | ||
public class SlackService { | ||
|
||
private static final String CHANNEL_NAME = "#μλ²-μ€λ₯"; | ||
|
||
@Value("${slack.webhook}") | ||
private String webhook; | ||
|
||
@Async | ||
public void sendMessage(String contents) { | ||
String message = contents + System.lineSeparator(); | ||
Payload payload = buildPayload(message); | ||
|
||
try { | ||
WebhookResponse webhookResponse = Slack.getInstance() | ||
.send(webhook, payload); | ||
if (webhookResponse.getCode() == HttpStatus.OK.value()) { | ||
log.info("Success send to slack!"); | ||
} | ||
log.info(webhookResponse.getMessage()); | ||
} catch (IOException e) { | ||
log.error("Unexpected Error! WebHook:" + webhook); | ||
} | ||
} | ||
|
||
private Payload buildPayload(String message) { | ||
return Payload.builder() | ||
.attachments(Collections.singletonList(Attachment.builder() | ||
.channelName(CHANNEL_NAME) | ||
.build())) | ||
.text(message) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters