Skip to content
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

[BE] Discord Webhook Messenger 추가 #1385

Merged
merged 12 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.ftclub.cabinet.alarm;

import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Builder
@Getter
public class DiscordAlarmMessage {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final String subject;
private final String packageName;
private final String httpMethod;
private final String methodName;
private final String parameters;
private final String returnValue;

@Override
public String toString() {
return "```java\n" +
"Subject: \"" + subject + "\"\n" +
"Issued at: \"" + LocalDateTime.now().format(formatter) + "\"\n" +
"Package: \"" + packageName + "\"\n" +
"HTTP: \"" + httpMethod + "\"\n" +
"Method: \"" + methodName + "\"\n" +
"Parameters: \"" + parameters + "\"\n" +
"Return: " + returnValue + "\n" +
"```";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.ftclub.cabinet.alarm;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.HashMap;
import java.util.Map;

@Component
public class DiscordWebHookMessenger {
private final static String DISCORD_WEBHOOK_MESSAGE_KEY = "content";
private final String discordWebHookUrl;

DiscordWebHookMessenger(@Value("${webhook.discord-admin}") String discordWebHookUrl) {
this.discordWebHookUrl = discordWebHookUrl;
}

public void sendMessage(DiscordAlarmMessage message) {
Map<String, String> body = new HashMap<>();
body.put(DISCORD_WEBHOOK_MESSAGE_KEY, message.toString());

WebClient.create().post()
.uri(discordWebHookUrl)
.bodyValue(body)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
21 changes: 21 additions & 0 deletions backend/src/main/java/org/ftclub/cabinet/log/LogParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.ftclub.cabinet.log;

import org.ftclub.cabinet.alarm.DiscordAlarmMessage;
import org.springframework.stereotype.Component;

@Component
public class LogParser {
private static final String delimiter = "#";

public DiscordAlarmMessage parseToDiscordAlarmMessage(String log) {
String[] tokens = log.split(delimiter);
return DiscordAlarmMessage.builder()
.subject(tokens[0])
.packageName(tokens[1])
.httpMethod(tokens[2])
.methodName(tokens[3])
.parameters(tokens[4])
.returnValue(tokens[5])
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.ftclub.cabinet.log;

import lombok.RequiredArgsConstructor;
import org.ftclub.cabinet.alarm.DiscordAlarmMessage;
import org.ftclub.cabinet.alarm.DiscordWebHookMessenger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class WebHookTestController {
private final DiscordWebHookMessenger discordWebHookMessenger;
private final LogParser logParser;

@PostMapping("/test/discord")
public void post(
@RequestBody String log
) {
DiscordAlarmMessage discordAlarmMessage = logParser.parseToDiscordAlarmMessage(log);
discordWebHookMessenger.sendMessage(discordAlarmMessage);
}
}
2 changes: 2 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ spring:
starttls:
enable: true

webhook:
discord-admin: https://discord.com/api/webhooks/for-test
2 changes: 1 addition & 1 deletion config