Skip to content

Commit

Permalink
Add support for telegram groups + minor fixes (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
shayaantx authored Jan 9, 2022
1 parent 3f862c0 commit c6e467b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
1 change: 1 addition & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>4.6.0</version>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>com.j2html</groupId>
Expand Down
2 changes: 2 additions & 0 deletions sample.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# the id is between c/<id>/<postId>
# 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=
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/botdarr/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/botdarr/clients/ChatClientType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TelegramResponse> responseChatClientResponseBuilder = new TelegramResponseBuilder();
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,6 +54,14 @@ public void sendMessage(CommandResponse<TelegramResponse> 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) {
Expand All @@ -64,7 +73,7 @@ private void sendTelegramMessage(long id, String html) {
}

private void sendMessages(MessageSender messageSender, Chat chat) {
Set<String> configTelegramChannels = Sets.newHashSet(Splitter.on(',').trimResults().split(Config.getProperty(Config.Constants.TELEGRAM_PRIVATE_CHANNELS)));
Set<String> configTelegramChannels = Sets.newHashSet(Splitter.on(',').trimResults().split(getMessageEndpoint()));
Map<String, String> supportedTelegramChannels = new HashMap<>();
for (String channel : configTelegramChannels) {
String[] fields = channel.split(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.1.12
5.1.13
12 changes: 12 additions & 0 deletions src/test/java/com/botdarr/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit c6e467b

Please sign in to comment.