-
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.
Browse files
Browse the repository at this point in the history
feat: 디스코드, 슬랙 봇 추가
- Loading branch information
Showing
22 changed files
with
503 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.Alchive.backend.config; | ||
|
||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.JDABuilder; | ||
import net.dv8tion.jda.api.exceptions.RateLimitedException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.security.auth.login.LoginException; | ||
|
||
@Configuration | ||
public class JDAConfig { | ||
@Value("${DISCORD_BOT_TOKEN}") | ||
private String discordBotToken; | ||
@Bean | ||
public JDA jda() throws LoginException, RateLimitedException { | ||
return JDABuilder.createDefault(discordBotToken) | ||
.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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/Alchive/backend/config/error/exception/sns/InvalidGrantException.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.Alchive.backend.config.error.exception.sns; | ||
|
||
import com.Alchive.backend.config.error.ErrorCode; | ||
import com.Alchive.backend.config.error.exception.BusinessException; | ||
|
||
public class InvalidGrantException extends BusinessException { | ||
public InvalidGrantException() { super(ErrorCode.INVALID_GRANT); } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/Alchive/backend/config/error/exception/sns/NoSuchDiscordUserException.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.Alchive.backend.config.error.exception.sns; | ||
|
||
import com.Alchive.backend.config.error.ErrorCode; | ||
import com.Alchive.backend.config.error.exception.BusinessException; | ||
|
||
public class NoSuchDiscordUserException extends BusinessException { | ||
public NoSuchDiscordUserException() { super(ErrorCode.DISCORD_USER_NOT_FOUND); } | ||
} |
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
68 changes: 68 additions & 0 deletions
68
src/main/java/com/Alchive/backend/controller/DiscordController.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.Alchive.backend.controller; | ||
|
||
import com.Alchive.backend.config.result.ResultResponse; | ||
import com.Alchive.backend.domain.sns.Sns; | ||
import com.Alchive.backend.domain.sns.SnsCategory; | ||
import com.Alchive.backend.domain.user.User; | ||
import com.Alchive.backend.dto.request.SnsCreateRequest; | ||
import com.Alchive.backend.service.SnsService; | ||
import com.Alchive.backend.service.DiscordService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.http.*; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import static com.Alchive.backend.config.result.ResultCode.DISCORD_DM_SEND_SUCCESS; | ||
|
||
@Tag(name = "디스코드", description = "디스코드 관련 api입니다. ") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@Slf4j | ||
@RequestMapping("/api/v2/discord") | ||
public class DiscordController { | ||
private final SnsService snsService; | ||
private final DiscordService discordService; | ||
|
||
@Operation(summary = "디스코드 봇 연결", description = "디스코드 액세스 토큰을 요청하고 DM 채널을 연결하는 api입니다. ") | ||
@GetMapping("/dm/open") | ||
public ResponseEntity<ResultResponse> openDiscordDm(@AuthenticationPrincipal User user, @RequestParam String code) { | ||
// Access Token 요청 | ||
String accessToken = discordService.getAccessToken(code); | ||
log.info("Access Token 반환 완료: " + accessToken); | ||
|
||
// 사용자 DISCORD USER ID 요청 | ||
String discordUserId = discordService.getDiscordUserIdFromAccessToken(accessToken); | ||
log.info("Discord User Id 반환 완료: " + discordUserId); | ||
|
||
// DM 채널 생성 요청 | ||
String channelId = discordService.getDmChannel(discordUserId); | ||
log.info("DM 채널 생성 완료"); | ||
|
||
// Discord SNS 정보 저장 | ||
SnsCreateRequest snsCreateRequest = SnsCreateRequest.builder() | ||
.category(SnsCategory.DISCORD) | ||
.sns_id(discordUserId) // Discord User Id | ||
.channel_id(channelId) // Discord Channel Id | ||
.time("0 0 18 ? * MON") | ||
.build(); | ||
snsService.createSns(user, snsCreateRequest); | ||
log.info("SNS 정보 저장 완료"); | ||
|
||
// DM 전송 요청 | ||
discordService.sendDm(channelId, "안녕하세요! 이제부터 풀지 못한 문제들을 정해진 시간에 알려드릴게요. "); | ||
|
||
return ResponseEntity.ok(ResultResponse.of(DISCORD_DM_SEND_SUCCESS)); | ||
} | ||
|
||
@Operation(summary = "디스코드 DM 전송", description = "디스코드 DM으로 메시지를 전송하는 api입니다. ") | ||
@PostMapping("dm/send") | ||
public ResponseEntity<ResultResponse> sendDiscordDm(@AuthenticationPrincipal User user, @RequestParam String message) { | ||
Sns discordInfo = discordService.getDiscordInfo(user); | ||
discordService.sendDmJda(discordInfo.getSns_id(), message); | ||
return ResponseEntity.ok(ResultResponse.of(DISCORD_DM_SEND_SUCCESS)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/Alchive/backend/controller/SlackController.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.Alchive.backend.controller; | ||
|
||
import com.Alchive.backend.config.result.ResultResponse; | ||
import com.Alchive.backend.domain.sns.Sns; | ||
import com.Alchive.backend.domain.user.User; | ||
import com.Alchive.backend.dto.request.SnsCreateRequest; | ||
import com.Alchive.backend.service.SnsService; | ||
import com.Alchive.backend.service.SlackService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import static com.Alchive.backend.config.result.ResultCode.SLACK_DM_SEND_SUCCESS; | ||
|
||
@Slf4j | ||
@Tag(name = "슬랙", description = "슬랙 관련 API입니다. ") | ||
@RestController | ||
@RequestMapping("/api/v2/slack") | ||
@RequiredArgsConstructor | ||
public class SlackController { | ||
private final SlackService slackService; | ||
private final SnsService snsService; | ||
|
||
@Operation(summary = "슬랙 봇 연결", description = "슬랙 액세스 토큰을 요청하고 DM 채널을 연결하는 api입니다. ") | ||
@GetMapping("/dm/open") | ||
public ResponseEntity<ResultResponse> openSlackDm(@AuthenticationPrincipal User user, @RequestParam String code) { | ||
// Bot Access Token, User Access Token, Slack User Id 요청 | ||
SnsCreateRequest snsCreateRequest = slackService.getSlackInfo(code); | ||
log.info("사용자 slack 정보를 불러왔습니다. "); | ||
|
||
// Slack SNS 정보 저장 | ||
snsService.createSns(user, snsCreateRequest); | ||
log.info("사용자 slack 정보를 저장했습니다. "); | ||
|
||
// DM 전송 요청 | ||
String slackUserId = snsCreateRequest.getSns_id(); | ||
String botAccessToken = snsCreateRequest.getBot_token(); | ||
|
||
slackService.sendDm(slackUserId, botAccessToken, "안녕하세요! 이제부터 풀지 못한 문제들을 정해진 시간에 알려드릴게요. "); | ||
log.info("봇을 사용자 slack 채널에 추가했습니다. "); | ||
return ResponseEntity.ok(ResultResponse.of(SLACK_DM_SEND_SUCCESS)); | ||
} | ||
|
||
@Operation(summary = "슬랙 DM 전송", description = "슬랙 DM으로 메시지를 전송하는 api입니다. ") | ||
@PostMapping("dm/send") | ||
public ResponseEntity<ResultResponse> sendSlackDm(@AuthenticationPrincipal User user, @RequestParam String message) { | ||
Sns sns = slackService.getSlackInfo(user); | ||
slackService.sendDm(sns.getSns_id(), sns.getBot_token(), message); | ||
return ResponseEntity.ok(ResultResponse.of(SLACK_DM_SEND_SUCCESS)); | ||
} | ||
} |
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
Oops, something went wrong.