This commit is contained in:
Bruno Rybársky 2024-09-09 17:33:53 +02:00
parent 8a37e372b7
commit 3d864e2d06
2 changed files with 17 additions and 12 deletions

@ -1,6 +1,8 @@
package systems.brn.gotifymc;
import java.util.List;
public class ConfigObject {
public String instanceURL = "https://gotify.example.com/message";
public String token = "YOUR-TOKEN-HERE";
public List<String> tokens = List.of("YOUR-TOKEN-HERE");
}

@ -12,10 +12,11 @@ import static systems.brn.gotifymc.GotifyMC.*;
public class GotifyAPI {
public static void sendNotification(String title, String message, int priority) {
if (!Objects.equals(configObject.token, "YOUR-TOKEN-HERE")) {
if (!configObject.tokens.isEmpty() && !Objects.equals(configObject.tokens.getFirst(), "YOUR-TOKEN-HERE")) {
try {
URL instanceURL = URI.create(configObject.instanceURL).toURL();
HttpURLConnection connection = getHttpURLConnection(instanceURL);
for (String token : configObject.tokens) {
HttpURLConnection connection = getHttpURLConnection(instanceURL, token);
connection.connect();
GotifyMessage gotifyMessage = new GotifyMessage(title, message, priority);
mapper.writeValue(connection.getOutputStream(), gotifyMessage);
@ -24,20 +25,22 @@ public class GotifyAPI {
if (responseCode != 200) {
modLogger.warn("Failed to send notification. Response code: {}", responseCode);
}
}
} catch (IOException e) {
modLogger.error("Failed to create instance URL", e);
}
}
}
private static @NotNull HttpURLConnection getHttpURLConnection(URL instanceURL) throws IOException {
private static @NotNull HttpURLConnection getHttpURLConnection(URL instanceURL, String token) throws IOException {
HttpURLConnection connection = (HttpURLConnection) instanceURL.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-Gotify-Key", configObject.token);
connection.setRequestProperty("X-Gotify-Key", token);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);