Skip to content

Commit

Permalink
Merge pull request #156 from Make-A-Wish-Sopt/feature/slack-alarm
Browse files Browse the repository at this point in the history
[FEAT] 슬랙 알람 연동
  • Loading branch information
wlwpfh authored Dec 19, 2023
2 parents 64e1e55 + 45ca9c7 commit e3f1de3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ repositories {

ext {
set('snippetsDir', file("build/generated-snippets"))
set('springCloudVersion', "2022.0.3")
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.3'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/sopterm/makeawish/config/FeignConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopterm.makeawish.config;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;

@EnableFeignClients(basePackages = "com.sopterm.makeawish.external")
@Configuration
public class FeignConfig {
}
12 changes: 12 additions & 0 deletions src/main/java/com/sopterm/makeawish/external/SlackClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sopterm.makeawish.external;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "slackClient", url = "${slack.url}")
public interface SlackClient {
@PostMapping(value = "", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
void postMessage(@RequestBody String request);
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package com.sopterm.makeawish.service.social;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParser;
import com.sopterm.makeawish.domain.user.InternalTokenManager;
import com.sopterm.makeawish.domain.user.KakaoTokenManager;
import com.sopterm.makeawish.domain.user.User;
import com.sopterm.makeawish.dto.auth.AuthSignInRequestDTO;
import com.sopterm.makeawish.dto.auth.AuthSignInResponseDTO;
import com.sopterm.makeawish.external.SlackClient;
import com.sopterm.makeawish.repository.UserRepository;
import com.sopterm.makeawish.service.SocialLoginService;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

import static com.sopterm.makeawish.common.message.ErrorMessage.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class KakaoLoginService implements SocialLoginService {

@Value("${spring.profiles.active}")
private String activeProfile;
private final UserRepository userRepository;
private final InternalTokenManager tokenManager;
private final KakaoTokenManager kakaoTokenManager;

private final SlackClient slackClient;
private final ObjectMapper jsonMapper = new ObjectMapper();
@Override
public AuthSignInResponseDTO socialLogin(String code, String redirectUri) {
String kakaoAccessToken = null;
Expand All @@ -44,6 +56,47 @@ private User issueAccessToken(AuthSignInRequestDTO request) {
.orElseGet(() -> signup(request));
}
private User signup(AuthSignInRequestDTO request) {

try {
if (Objects.equals(activeProfile, "dev")) {
val slackRequest = createSlackRequest(request.nickname(), request.email(), request.createdAt(), String.valueOf(request.socialType()));
slackClient.postMessage(slackRequest.toString());
}
} catch (RuntimeException ex) {
log.error("슬랙 요청이 실패했습니다 : " + ex.getMessage());
}
return userRepository.save(new User(request));
}

private JsonNode createSlackRequest(String nickname, String email, LocalDateTime createdAt, String socialType) {
val rootNode = jsonMapper.createObjectNode();
rootNode.put("text", "새로운 유저가 가입했어요!");
val blocks = jsonMapper.createArrayNode();

val textField = jsonMapper.createObjectNode();
textField.put("type", "section");
textField.set("text", createTextFieldNode("새로운 유저가 가입했어요!"));

val contentNode = jsonMapper.createObjectNode();
contentNode.put("type", "section");
val fields = jsonMapper.createArrayNode();
fields.add(createTextFieldNode("*이름:*\n" + nickname));
fields.add(createTextFieldNode("*이메알:*\n"+ email));
fields.add(createTextFieldNode("*가입 시간:*\n" + createdAt));
fields.add(createTextFieldNode("*소셜:*\n" + socialType));
contentNode.set("fields", fields);

blocks.add(textField);
blocks.add(contentNode);
rootNode.set("blocks", blocks);
return rootNode;
}

private JsonNode createTextFieldNode (String text) {
val textField = jsonMapper.createObjectNode();
textField.put("type", "mrkdwn");
textField.put("text", text);
return textField;
}

}

0 comments on commit e3f1de3

Please sign in to comment.