-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[✨ feat/#144] Discord 회원가입 알림 구현 #151
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 additions & 0 deletions
54
src/main/java/org/terning/terningserver/service/WebhookService.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,54 @@ | ||
package org.terning.terningserver.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.terning.terningserver.domain.User; | ||
import org.terning.terningserver.repository.user.UserRepository; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Transactional | ||
public class WebhookService { | ||
|
||
// 디스코드 웹훅 URL 주입 | ||
@Value("${discord.webhook.url}") | ||
private String discordWebhookUrl; | ||
|
||
private final UserRepository userRepository; | ||
|
||
// 알림을 보내는 메서드 | ||
public void sendDiscordNotification(User user) { | ||
|
||
// REST 요청을 처리하기 위한 RestTemplate 객체 생성 | ||
RestTemplate restTemplate = new RestTemplate(); | ||
|
||
// 회원 수를 기존 DB에서 조회하여 총 회원 수 계산 | ||
Long totalMembers = userRepository.count(); | ||
|
||
// 알림 메시지 생성 | ||
String message = String.format("가입자명 : %s\n[%d] 번째 유저가 회원가입했습니다!", user.getName(), totalMembers); | ||
|
||
// HTTP 요청을 위한 헤더 설정 | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
// HTTP 요청 바디에 전송할 메시지 설정 | ||
Map<String, String> body = new HashMap<>(); | ||
body.put("content", message); | ||
|
||
// HTTP 요청 엔터티 생성 | ||
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(body, headers); | ||
|
||
// 디스코드 웹훅 URL로 POST 요청을 보내어 알림 전송 | ||
restTemplate.postForEntity(discordWebhookUrl, requestEntity, String.class); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 yml 파일에 해당 코드 관련해서 설정 추가해야하는거 맞을까요?!