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; package systems.brn.gotifymc;
import java.util.List;
public class ConfigObject { public class ConfigObject {
public String instanceURL = "https://gotify.example.com/message"; public String instanceURL = "https://gotify.example.com/message";
public String token = "YOUR-TOKEN-HERE"; public List<String> tokens = List.of("YOUR-TOKEN-HERE");
} }

@ -12,32 +12,35 @@ import static systems.brn.gotifymc.GotifyMC.*;
public class GotifyAPI { public class GotifyAPI {
public static void sendNotification(String title, String message, int priority) { 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 { try {
URL instanceURL = URI.create(configObject.instanceURL).toURL(); URL instanceURL = URI.create(configObject.instanceURL).toURL();
HttpURLConnection connection = getHttpURLConnection(instanceURL); for (String token : configObject.tokens) {
connection.connect(); HttpURLConnection connection = getHttpURLConnection(instanceURL, token);
GotifyMessage gotifyMessage = new GotifyMessage(title, message, priority); connection.connect();
mapper.writeValue(connection.getOutputStream(), gotifyMessage); GotifyMessage gotifyMessage = new GotifyMessage(title, message, priority);
// Get the response code mapper.writeValue(connection.getOutputStream(), gotifyMessage);
int responseCode = connection.getResponseCode(); // Get the response code
if (responseCode != 200) { int responseCode = connection.getResponseCode();
modLogger.warn("Failed to send notification. Response code: {}", responseCode); if (responseCode != 200) {
modLogger.warn("Failed to send notification. Response code: {}", responseCode);
}
} }
} catch (IOException e) { } catch (IOException e) {
modLogger.error("Failed to create instance URL", 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(); HttpURLConnection connection = (HttpURLConnection) instanceURL.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8"); connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-Gotify-Key", configObject.token); connection.setRequestProperty("X-Gotify-Key", token);
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setDoInput(true); connection.setDoInput(true);
connection.setUseCaches(false); connection.setUseCaches(false);