Adding DiscordNotificationAgent

This commit is contained in:
Jason House
2020-09-18 10:51:04 +09:00
parent 432880f2ac
commit 63ec6381fa
8 changed files with 67 additions and 67 deletions
@@ -44,6 +44,8 @@ public final class PlexProperties {
@NotNull
private PushOverProperties pushOverProperties;
@NotNull
private DiscordProperties discordProperties;
@NotNull
private Schedule schedule;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -54,6 +56,7 @@ public final class PlexProperties {
@JsonProperty(value = "gotifyProperties") @Nullable GotifyProperties gotifyProperties,
@JsonProperty(value = "slackProperties") @Nullable SlackProperties slackProperties,
@JsonProperty(value = "pushOverProperties") @Nullable PushOverProperties pushOverProperties,
@JsonProperty(value = "discordProperties") @Nullable DiscordProperties discordProperties,
@JsonProperty(value = "movieDbApiKey") @Nullable String movieDbApiKey,
@JsonProperty(value = "password") @Nullable String password,
@JsonProperty(value = "schedule") @Nullable Schedule schedule) {
@@ -64,6 +67,7 @@ public final class PlexProperties {
this.gotifyProperties = gotifyProperties == null ? GotifyProperties.getDefault() : gotifyProperties;
this.slackProperties = slackProperties == null ? SlackProperties.getDefault() : slackProperties;
this.pushOverProperties = pushOverProperties == null ? PushOverProperties.getDefault() : pushOverProperties;
this.discordProperties = discordProperties == null ? DiscordProperties.getDefault() : discordProperties;
this.movieDbApiKey = movieDbApiKey == null ? "" : movieDbApiKey;
this.password = password == null ? "" : password;
this.schedule = schedule == null ? Schedule.EVERY_MONDAY : schedule;
@@ -77,6 +81,7 @@ public final class PlexProperties {
this.gotifyProperties = GotifyProperties.getDefault();
this.slackProperties = SlackProperties.getDefault();
this.pushOverProperties = PushOverProperties.getDefault();
this.discordProperties = DiscordProperties.getDefault();
this.movieDbApiKey = "";
this.password = "";
this.schedule = Schedule.EVERY_MONDAY;
@@ -172,20 +177,28 @@ public final class PlexProperties {
this.schedule = schedule;
}
public @NotNull DiscordProperties getDiscordProperties() {
return discordProperties;
}
public void setDiscordProperties(@NotNull DiscordProperties discordProperties) {
this.discordProperties = discordProperties;
}
@Override
public String toString() {
return "PlexProperties{" +
"plexServers=" + plexServers +
", movieDbApiKey='" + movieDbApiKey + '\'' +
", password='" + password + '\'' +
", telegramProperties=" + telegramProperties +
", pushBulletProperties=" + pushBulletProperties +
", emailProperties=" + emailProperties +
", gotifyProperties=" + gotifyProperties +
", slackProperties=" + slackProperties +
", pushOverProperties=" + pushOverProperties +
", movieDbApiKey='" + movieDbApiKey + '\'' +
", password='" + password + '\'' +
", discordProperties=" + discordProperties +
", schedule=" + schedule +
'}';
}
}
@@ -10,6 +10,7 @@
package com.jasonhhouse.gaps;
import com.jasonhhouse.gaps.notifications.DiscordNotificationAgent;
import com.jasonhhouse.gaps.notifications.EmailNotificationAgent;
import com.jasonhhouse.gaps.notifications.GotifyNotificationAgent;
import com.jasonhhouse.gaps.notifications.PushBulletNotificationAgent;
@@ -58,4 +59,9 @@ public class NotificationConfiguration {
public PushOverNotificationAgent getPushOverElement() {
return new PushOverNotificationAgent(fileIoService);
}
@Bean
public DiscordNotificationAgent getDiscordElement() {
return new DiscordNotificationAgent(fileIoService);
}
}
@@ -10,11 +10,10 @@
package com.jasonhhouse.gaps.notifications;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jasonhhouse.gaps.NotificationType;
import com.jasonhhouse.gaps.properties.SlackProperties;
import com.jasonhhouse.gaps.properties.DiscordProperties;
import com.jasonhhouse.gaps.service.FileIoService;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@@ -31,7 +30,7 @@ import org.slf4j.LoggerFactory;
import static com.jasonhhouse.gaps.notifications.NotificationStatus.TIMEOUT;
public class DiscordNotificationAgent extends AbstractNotificationAgent<SlackProperties> {
public final class DiscordNotificationAgent extends AbstractNotificationAgent<DiscordProperties> {
private static final Logger LOGGER = LoggerFactory.getLogger(DiscordNotificationAgent.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
@@ -49,12 +48,12 @@ public class DiscordNotificationAgent extends AbstractNotificationAgent<SlackPro
@Override
public int getId() {
return 2;
return 6;
}
@Override
public String getName() {
return "Slack Notification Agent";
return "Discord Notification Agent";
}
@Override
@@ -71,18 +70,18 @@ public class DiscordNotificationAgent extends AbstractNotificationAgent<SlackPro
.add("Content-Type", org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
.build();
Slack slack = new Slack(String.format("*%s*%n%s", title, message));
Discord discord = new Discord(title, message);
String slackMessage = "";
String discordMessage = "";
try {
slackMessage = objectMapper.writeValueAsString(slack);
discordMessage = objectMapper.writeValueAsString(discord);
} catch (JsonProcessingException e) {
LOGGER.error("Failed to turn Slack message into JSON", e);
LOGGER.error("Failed to turn Discord message into JSON", e);
return false;
}
LOGGER.info("slackMessage {}", slackMessage);
RequestBody body = RequestBody.create(slackMessage, MediaType.get(org.springframework.http.MediaType.APPLICATION_JSON_VALUE));
LOGGER.info("discordMessage {}", discordMessage);
RequestBody body = RequestBody.create(discordMessage, MediaType.get(org.springframework.http.MediaType.APPLICATION_JSON_VALUE));
Request request = new Request.Builder()
.headers(headers)
@@ -92,72 +91,54 @@ public class DiscordNotificationAgent extends AbstractNotificationAgent<SlackPro
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
LOGGER.info("Slack message sent via {}", url);
LOGGER.info("Discord message sent via {}", url);
return true;
} else {
LOGGER.error("Error with Slack Url: {} Body returned {}", url, response.body());
LOGGER.error("Error with Discord Url: {} Body returned {}", url, response.body());
return false;
}
} catch (IOException e) {
LOGGER.error(String.format("Error with Slack Url: %s", url), e);
LOGGER.error(String.format("Error with Discord Url: %s", url), e);
return false;
}
}
@Nullable
@Override
public SlackProperties getNotificationProperties() {
return fileIoService.readProperties().getSlackProperties();
public DiscordProperties getNotificationProperties() {
return fileIoService.readProperties().getDiscordProperties();
}
private static final class Slack {
private final Block[] blocks;
private static final class Discord {
private final Embeds embeds;
private Slack(String message) {
blocks = new Block[1];
blocks[0] = new DiscordNotificationAgent.Block(new Text(message));
private Discord(String title, String message) {
embeds = new Embeds(title, message);
}
public Block[] getBlocks() {
return blocks;
public Embeds getEmbeds() {
return embeds;
}
}
private static final class Embeds {
private final String title;
private final String message;
private Embeds(String title, String message) {
this.title = title;
this.message = message;
}
public String getTitle() {
return title;
}
public String getMessage() {
return message;
}
}
private static final class Block {
private final String type;
private final Text text;
private Block(Text text) {
this.type = "section";
this.text = text;
}
public String getType() {
return type;
}
public Text getText() {
return text;
}
}
private static final class Text {
private final String type;
private final String value;
private Text(String value) {
this.type = "mrkdwn";
this.value = value;
}
public String getType() {
return type;
}
@JsonProperty("text")
public String getValue() {
return value;
}
}
}
@@ -23,7 +23,7 @@ import org.springframework.mail.javamail.JavaMailSenderImpl;
import static com.jasonhhouse.gaps.notifications.NotificationStatus.SEND_MESSAGE;
public class EmailNotificationAgent extends AbstractNotificationAgent<EmailProperties> {
public final class EmailNotificationAgent extends AbstractNotificationAgent<EmailProperties> {
private static final Logger LOGGER = LoggerFactory.getLogger(EmailNotificationAgent.class);
@@ -31,7 +31,7 @@ import static com.jasonhhouse.gaps.notifications.NotificationStatus.FAILED_TO_PA
import static com.jasonhhouse.gaps.notifications.NotificationStatus.SEND_MESSAGE;
import static com.jasonhhouse.gaps.notifications.NotificationStatus.TIMEOUT;
public class GotifyNotificationAgent extends AbstractNotificationAgent<GotifyProperties> {
public final class GotifyNotificationAgent extends AbstractNotificationAgent<GotifyProperties> {
private static final Logger LOGGER = LoggerFactory.getLogger(GotifyNotificationAgent.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
@@ -32,7 +32,7 @@ import static com.jasonhhouse.gaps.notifications.NotificationStatus.FAILED_TO_PA
import static com.jasonhhouse.gaps.notifications.NotificationStatus.SEND_MESSAGE;
import static com.jasonhhouse.gaps.notifications.NotificationStatus.TIMEOUT;
public class PushBulletNotificationAgent extends AbstractNotificationAgent<PushBulletProperties> {
public final class PushBulletNotificationAgent extends AbstractNotificationAgent<PushBulletProperties> {
private static final Logger LOGGER = LoggerFactory.getLogger(PushBulletNotificationAgent.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
@@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
import static com.jasonhhouse.gaps.notifications.NotificationStatus.TIMEOUT;
public class SlackNotificationAgent extends AbstractNotificationAgent<SlackProperties> {
public final class SlackNotificationAgent extends AbstractNotificationAgent<SlackProperties> {
private static final Logger LOGGER = LoggerFactory.getLogger(SlackNotificationAgent.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
@@ -30,7 +30,7 @@ import static com.jasonhhouse.gaps.notifications.NotificationStatus.FAILED_TO_PA
import static com.jasonhhouse.gaps.notifications.NotificationStatus.SEND_MESSAGE;
import static com.jasonhhouse.gaps.notifications.NotificationStatus.TIMEOUT;
public class TelegramNotificationAgent extends AbstractNotificationAgent<TelegramProperties> {
public final class TelegramNotificationAgent extends AbstractNotificationAgent<TelegramProperties> {
private static final Logger LOGGER = LoggerFactory.getLogger(TelegramNotificationAgent.class);
private static final ObjectMapper objectMapper = new ObjectMapper();