Skip to content

Commit

Permalink
Merge pull request #68 from mju-likelion/feature/slack-bot-#65
Browse files Browse the repository at this point in the history
Feature/#65 슬랙 연동 작업
  • Loading branch information
Dh3356 authored Feb 20, 2024
2 parents ae7b7b9 + 5f892ef commit ff19281
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'//Redis
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'//AWS
implementation 'javax.xml.bind:jaxb-api:2.3.1'//JAXB
implementation("com.slack.api:bolt:1.18.0")//Slack
implementation("com.slack.api:bolt-servlet:1.18.0")//Slack
implementation("com.slack.api:bolt-jetty:1.18.0")//Slack


testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import org.mjulikelion.bagel.util.converter.ApplicationAgreementConverter;
import org.mjulikelion.bagel.util.converter.ApplicationIntroduceConverter;
import org.mjulikelion.bagel.util.s3.S3Service;
import org.mjulikelion.bagel.util.slack.SlackService;
import org.mjulikelion.bagel.util.slack.asset.message.SlackApplySaveMessage;
import org.mjulikelion.bagel.util.slack.asset.message.SlackFileMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -48,6 +51,7 @@ public class ApplicationCommandServiceImpl implements ApplicationCommandService
private final ApplicationAgreementConverter applicationAgreementConvertor;
private final ApplicationIntroduceConverter applicationIntroduceConvertor;
private final S3Service s3Service;
private final SlackService slackService;

@Override
@Transactional
Expand All @@ -64,13 +68,16 @@ public ResponseEntity<ResponseDto<Void>> saveApplication(ApplicationSaveDto appl

saveApplicationWithDetails(application, agreements, introduces);

this.slackService.sendSlackMessage(new SlackApplySaveMessage(application));

return new ResponseEntity<>(ResponseDto.res(HttpStatus.CREATED, "Created"), HttpStatus.CREATED);
}

@Override
public ResponseEntity<ResponseDto<FileSaveResponseData>> saveFile(MultipartFile file) {
try {
String url = this.s3Service.saveFile(file);
this.slackService.sendSlackMessage(new SlackFileMessage(file.getOriginalFilename(), url));
return new ResponseEntity<>(
ResponseDto.res(HttpStatus.CREATED, "Created", new FileSaveResponseData(url)),
HttpStatus.CREATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.mjulikelion.bagel.model.History;
import org.mjulikelion.bagel.repository.ApplicationRepository;
import org.mjulikelion.bagel.repository.HistoryRepository;
import org.mjulikelion.bagel.util.slack.SlackService;
import org.mjulikelion.bagel.util.slack.asset.message.SlackApplyMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -19,11 +21,11 @@
public class ApplyCommandServiceImpl implements ApplyCommandService {
private final ApplicationRepository applicationRepository;
private final HistoryRepository historyRepository;
private final SlackService slackService;

@Override
@Transactional
public ResponseEntity<ResponseDto<Void>> saveApply(ApplySaveDto applySaveDto) {
//이미 지원서가 있는지 확인
if (this.applicationRepository.existsByStudentId(applySaveDto.getStudentId())) {
throw new ApplicationAlreadyExistException(APPLICATION_ALREADY_EXISTS_ERROR);
}
Expand All @@ -33,6 +35,8 @@ public ResponseEntity<ResponseDto<Void>> saveApply(ApplySaveDto applySaveDto) {
.build();
this.historyRepository.save(history);

this.slackService.sendSlackMessage(new SlackApplyMessage(applySaveDto.getStudentId()));

return new ResponseEntity<>(ResponseDto.res(
HttpStatus.CREATED,
"Created"
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/mjulikelion/bagel/util/slack/SlackService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.mjulikelion.bagel.util.slack;

import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.mjulikelion.bagel.util.slack.asset.message.SlackMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SlackService {
@Value(value = "${slack.token}")
String slackToken;

public void sendSlackMessage(SlackMessage slackMessage) {

String channelAddress = slackMessage.getChannel().getChannelName();

try {
MethodsClient methods = Slack.getInstance().methods(slackToken);

ChatPostMessageRequest request = ChatPostMessageRequest.builder()
.channel(channelAddress)
.text(slackMessage.getMessage())
.build();

methods.chatPostMessage(request);

} catch (SlackApiException | IOException e) {
log.error(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mjulikelion.bagel.util.slack.asset;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum SlackChannel {
FILE("#12기-지원-테스트-파일", "--- 파일이 업로드 되었습니다 ---"),
APPLICATION("#12기-지원-테스트-지원서", "--- 지원서가 업로드 되었습니다 ---"),
APPLY("#12기-지원-테스트-지원", "--- 새 지원 요청이 있습니다 ---");

private final String channelName;
private final String messageTitle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mjulikelion.bagel.util.slack.asset.message;

import org.mjulikelion.bagel.util.slack.asset.SlackChannel;


public class SlackApplyMessage extends SlackMessage {
public SlackApplyMessage(String studentId) {
super(SlackChannel.APPLY, String.format("%s\n\n학번: %s", SlackChannel.APPLY.getMessageTitle(), studentId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mjulikelion.bagel.util.slack.asset.message;

import org.mjulikelion.bagel.model.Application;
import org.mjulikelion.bagel.util.slack.asset.SlackChannel;

public class SlackApplySaveMessage extends SlackMessage {
public SlackApplySaveMessage(Application application) {
super(SlackChannel.APPLICATION, String.format("%s\n\n이름: %s\n학과: %s\n학번: %s\n학년: %s\n전화번호: %s\n이메일: %s\n파트: %s",
SlackChannel.APPLICATION.getMessageTitle(), application.getName(), application.getMajor().getName(),
application.getStudentId(), application.getGrade(), application.getPhoneNumber(),
application.getEmail(), application.getPart()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mjulikelion.bagel.util.slack.asset.message;

import org.mjulikelion.bagel.util.slack.asset.SlackChannel;

public class SlackFileMessage extends SlackMessage {
public SlackFileMessage(String fileName, String fileUrl) {
super(SlackChannel.FILE, String.format("%s\n\n업로드 요청 파일명: %s\n파일 url: %s",
SlackChannel.FILE.getMessageTitle(), fileName, fileUrl));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.mjulikelion.bagel.util.slack.asset.message;

import lombok.Getter;
import org.joda.time.DateTime;
import org.mjulikelion.bagel.util.slack.asset.SlackChannel;

@Getter
public class SlackMessage {
protected SlackChannel channel;
protected String message;

public SlackMessage(SlackChannel channel, String message) {
this.channel = channel;
this.message = message + "\n\n요청 일시: " + buildTimestamp();
}

protected String buildTimestamp() {
return new DateTime().toString("yyyy-MM-dd HH:mm:ss");
}
}

0 comments on commit ff19281

Please sign in to comment.