-
Notifications
You must be signed in to change notification settings - Fork 2
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 #68 from mju-likelion/feature/slack-bot-#65
Feature/#65 슬랙 연동 작업
- Loading branch information
Showing
9 changed files
with
120 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
src/main/java/org/mjulikelion/bagel/util/slack/SlackService.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,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()); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/mjulikelion/bagel/util/slack/asset/SlackChannel.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,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; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/mjulikelion/bagel/util/slack/asset/message/SlackApplyMessage.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,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)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/mjulikelion/bagel/util/slack/asset/message/SlackApplySaveMessage.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,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())); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/mjulikelion/bagel/util/slack/asset/message/SlackFileMessage.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,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)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/mjulikelion/bagel/util/slack/asset/message/SlackMessage.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,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"); | ||
} | ||
} |