From c6e467beedec36edd01d39c871bab053331df4c0 Mon Sep 17 00:00:00 2001 From: shayaantx <5449086+shayaantx@users.noreply.github.com> Date: Sun, 9 Jan 2022 14:41:13 -0500 Subject: [PATCH] Add support for telegram groups + minor fixes (#49) --- docker/entrypoint.sh | 1 + pom.xml | 2 +- sample.properties | 2 ++ src/main/java/com/botdarr/Config.java | 5 +++++ .../java/com/botdarr/clients/ChatClientType.java | 16 ++++++++++++---- .../clients/telegram/TelegramChatClient.java | 11 ++++++++++- .../telegram/TelegramResponseBuilder.java | 4 ++-- src/main/resources/version.txt | 2 +- src/test/java/com/botdarr/ConfigTests.java | 12 ++++++++++++ 9 files changed, 46 insertions(+), 9 deletions(-) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index aab39a2..f5ff9e0 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -19,6 +19,7 @@ if [ ! -e "$propertiesFile" ]; then [[ ! -z "${TELEGRAM_TOKEN}" ]] && addConfiguration "telegram-token" "${TELEGRAM_TOKEN}" "${propertiesFile}" [[ ! -z "${TELEGRAM_PRIVATE_CHANNELS}" ]] && addConfiguration "telegram-private-channels" "${TELEGRAM_PRIVATE_CHANNELS}" "${propertiesFile}" + [[ ! -z "${TELEGRAM_PRIVATE_GROUPS}" ]] && addConfiguration "telegram-private-groups" "${TELEGRAM_PRIVATE_GROUPS}" "${propertiesFile}" [[ ! -z "${SLACK_BOT_TOKEN}" ]] && addConfiguration "slack-bot-token" "${SLACK_BOT_TOKEN}" "${propertiesFile}" [[ ! -z "${SLACK_USER_TOKEN}" ]] && addConfiguration "slack-user-token" "${SLACK_USER_TOKEN}" "${propertiesFile}" diff --git a/pom.xml b/pom.xml index b714f59..af09a83 100644 --- a/pom.xml +++ b/pom.xml @@ -76,7 +76,7 @@ com.github.pengrad java-telegram-bot-api - 4.6.0 + 5.5.0 com.j2html diff --git a/sample.properties b/sample.properties index 27b7f76..32c6c3f 100644 --- a/sample.properties +++ b/sample.properties @@ -19,6 +19,8 @@ # the id is between c// # example: plex-channel1:id1,plex-channel2:id2 #telegram-private-channels= +# Same as above except for groups +#telegram-private-groups= # Your matrix bot user #matrix-username= diff --git a/src/main/java/com/botdarr/Config.java b/src/main/java/com/botdarr/Config.java index cb5ae59..9072e67 100644 --- a/src/main/java/com/botdarr/Config.java +++ b/src/main/java/com/botdarr/Config.java @@ -215,6 +215,11 @@ public static final class Constants { */ public static final String TELEGRAM_PRIVATE_CHANNELS = "telegram-private-channels"; + /** + * The telegram private group(s) to send notifications to + */ + public static final String TELEGRAM_PRIVATE_GROUPS = "telegram-private-groups"; + /** * The discord bot token */ diff --git a/src/main/java/com/botdarr/clients/ChatClientType.java b/src/main/java/com/botdarr/clients/ChatClientType.java index cdbebb3..5d30d06 100644 --- a/src/main/java/com/botdarr/clients/ChatClientType.java +++ b/src/main/java/com/botdarr/clients/ChatClientType.java @@ -89,6 +89,10 @@ public String getReadableName() { } }, TELEGRAM() { + private boolean isUsingChannels() { + String telegramGroups = Config.getProperty(Config.Constants.TELEGRAM_PRIVATE_GROUPS); + return telegramGroups == null || telegramGroups.isEmpty(); + } @Override public void init() throws Exception { ChatClientResponseBuilder responseChatClientResponseBuilder = new TelegramResponseBuilder(); @@ -99,7 +103,7 @@ public void init() throws Exception { telegramChatClient.addUpdateListener(list -> { try { for (Update update : list) { - com.pengrad.telegrambot.model.Message message = update.channelPost(); + com.pengrad.telegrambot.model.Message message = isUsingChannels() ? update.channelPost() : update.message(); if (message != null) { String text = message.text(); //TODO: the telegram api doesn't seem return "from" field in channel posts for some reason @@ -124,9 +128,13 @@ public void init() throws Exception { @Override public boolean isConfigured(Properties properties) { - return - !Strings.isBlank(properties.getProperty(Config.Constants.TELEGRAM_TOKEN)) && - !Strings.isBlank(properties.getProperty(Config.Constants.TELEGRAM_PRIVATE_CHANNELS)); + boolean isTelegramConfigured = !Strings.isBlank(properties.getProperty(Config.Constants.TELEGRAM_TOKEN)); + boolean telegramPrivateChannelsExist = !Strings.isBlank(properties.getProperty(Config.Constants.TELEGRAM_PRIVATE_CHANNELS)); + boolean telegramPrivateGroupsExist = !Strings.isBlank(properties.getProperty(Config.Constants.TELEGRAM_PRIVATE_GROUPS)); + if (isTelegramConfigured && telegramPrivateChannelsExist && telegramPrivateGroupsExist) { + throw new RuntimeException("Cannot configure telegram for private channels and groups, you must pick one or the other"); + } + return isTelegramConfigured && (telegramPrivateChannelsExist || telegramPrivateGroupsExist); } @Override diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java b/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java index a5eb396..97493c1 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java @@ -14,6 +14,7 @@ import com.pengrad.telegrambot.response.SendResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.util.Strings; import java.util.HashMap; import java.util.List; @@ -53,6 +54,14 @@ public void sendMessage(CommandResponse commandResponse, Chat } } + private String getMessageEndpoint() { + String channels = Config.getProperty(Config.Constants.TELEGRAM_PRIVATE_CHANNELS); + if (Strings.isBlank(channels)) { + return Config.getProperty(Config.Constants.TELEGRAM_PRIVATE_GROUPS); + } + return channels; + } + private void sendTelegramMessage(long id, String html) { SendResponse sendResponse = bot.execute(new SendMessage(id, html).parseMode(ParseMode.HTML)); if (LOGGER.isDebugEnabled() && sendResponse.errorCode() == 0) { @@ -64,7 +73,7 @@ private void sendTelegramMessage(long id, String html) { } private void sendMessages(MessageSender messageSender, Chat chat) { - Set configTelegramChannels = Sets.newHashSet(Splitter.on(',').trimResults().split(Config.getProperty(Config.Constants.TELEGRAM_PRIVATE_CHANNELS))); + Set configTelegramChannels = Sets.newHashSet(Splitter.on(',').trimResults().split(getMessageEndpoint())); Map supportedTelegramChannels = new HashMap<>(); for (String channel : configTelegramChannels) { String[] fields = channel.split(":"); diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java b/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java index 1e02055..4de4935 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java @@ -128,7 +128,7 @@ public TelegramResponse getMovieDownloadResponses(RadarrQueue radarrQueue) { StringBuilder details = new StringBuilder(); details.append("Quality - " + radarrQueue.getQuality().getQuality().getName() + "\n"); details.append("Status - " + radarrQueue.getStatus() + "\n"); - details.append("Time Left - " + radarrQueue.getTimeleft() == null ? "unknown" : radarrQueue.getTimeleft() + "\n"); + details.append("Time Left - " + (radarrQueue.getTimeleft() == null ? "unknown" : radarrQueue.getTimeleft() + "\n")); if (radarrQueue.getStatusMessages() != null) { for (RadarrQueueStatusMessages statusMessage : radarrQueue.getStatusMessages()) { for (String message : statusMessage.getMessages()) { @@ -146,7 +146,7 @@ public TelegramResponse getArtistDownloadResponses(LidarrQueueRecord lidarrQueue domContents.add(b(lidarrQueueRecord.getTitle())); StringBuilder details = new StringBuilder(); details.append("Status - " + lidarrQueueRecord.getStatus() + "\n"); - details.append("Time Left - " + lidarrQueueRecord.getTimeleft() == null ? "unknown" : lidarrQueueRecord.getTimeleft() + "\n"); + details.append("Time Left - " + (lidarrQueueRecord.getTimeleft() == null ? "unknown" : lidarrQueueRecord.getTimeleft() + "\n")); if (lidarrQueueRecord.getStatusMessages() != null) { //limit messages to 5, since lidarr can really throw a ton of status messages for (LidarrQueueStatusMessage statusMessage : ListUtils.subList(lidarrQueueRecord.getStatusMessages(), 5)) { diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index b3fc696..0c5d745 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -5.1.12 \ No newline at end of file +5.1.13 \ No newline at end of file diff --git a/src/test/java/com/botdarr/ConfigTests.java b/src/test/java/com/botdarr/ConfigTests.java index bf0d757..8963a0c 100644 --- a/src/test/java/com/botdarr/ConfigTests.java +++ b/src/test/java/com/botdarr/ConfigTests.java @@ -78,6 +78,18 @@ public void getConfig_moreThanOneChatClientsConfigured() throws Exception { Config.getProperty(""); } + @Test + public void getConfig_telegramGroupsAndChannelsConfigured() throws Exception { + Properties properties = new Properties(); + properties.put("telegram-token", "%H$$54j45i"); + properties.put("telegram-private-channels", "channel1:459349"); + properties.put("telegram-private-groups", "group1:459349"); + writeFakePropertiesFile(properties); + expectedException.expect(RuntimeException.class); + expectedException.expectMessage("Cannot configure telegram for private channels and groups, you must pick one or the other"); + Config.getProperty(""); + } + private void writeFakePropertiesFile(Properties properties) throws Exception { File propertiesFile = new File(temporaryFolder.getRoot(), "properties"); Deencapsulation.setField(Config.class, "propertiesPath", propertiesFile.getPath());