Skip to content

Commit

Permalink
Merge pull request #1385 from innovationacademy-kr/be/dev/feat_admin_…
Browse files Browse the repository at this point in the history
…prod/#1384

[BE] Discord Webhook Messenger 추가
  • Loading branch information
Ssuamje authored Sep 15, 2023
2 parents 1caa56a + 5b44edc commit a80e070
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
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

0 comments on commit a80e070

Please sign in to comment.