Skip to content

Commit 6a7fa95

Browse files
committed
Give option to only send message to webhooks
1 parent d51ea8a commit 6a7fa95

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/main/java/com/shweit/serverapi/webhooks/RegisterWebHooks.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ private static void sendWebHook(final String url, final JSONObject jsonObject) {
168168
try {
169169
URI uri = URI.create(url);
170170
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
171-
.uri(uri)
172-
.header("Content-Type", "application/json");
171+
.uri(uri);
173172

174173
// Check for Basic Authentication in URL
175174
String userInfo = uri.getUserInfo();
@@ -190,8 +189,22 @@ private static void sendWebHook(final String url, final JSONObject jsonObject) {
190189
requestBuilder.uri(uri);
191190
}
192191

192+
// Check if only message should be sent
193+
boolean onlyMessage = MinecraftServerAPI.config.getBoolean("webhooks.onlyMessage", false);
194+
String bodyContent;
195+
196+
if (onlyMessage && jsonObject.has("message")) {
197+
// Send only the message content as plain text
198+
bodyContent = jsonObject.getString("message");
199+
requestBuilder.header("Content-Type", "text/plain; charset=UTF-8");
200+
} else {
201+
// Send full JSON object
202+
bodyContent = jsonObject.toString();
203+
requestBuilder.header("Content-Type", "application/json");
204+
}
205+
193206
HttpRequest request = requestBuilder
194-
.POST(HttpRequest.BodyPublishers.ofString(jsonObject.toString()))
207+
.POST(HttpRequest.BodyPublishers.ofString(bodyContent))
195208
.build();
196209

197210
CompletableFuture<HttpResponse<String>> response = HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString());

src/main/resources/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ webhooks:
2525
urls:
2626
- "https://example.com/webhook1"
2727
- "https://example.com/webhook2"
28+
29+
# If set to true, only the message content will be sent to the webhook endpoints
30+
# instead of the full JSON object with event, load_type, and message fields.
31+
# Example with onlyMessage: false -> {"load_type":"STARTUP","event":"server_start","message":"Server has started"}
32+
# Example with onlyMessage: true -> "Server has started"
33+
onlyMessage: false
2834

2935
# Here is a list of all available Webhooks.
3036
# You can enable or disable them here.

0 commit comments

Comments
 (0)