Skip to content

Commit

Permalink
Fix telegram NPE thrown with invalid channel/group id (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
shayaantx authored Mar 14, 2022
1 parent 9d9a45a commit ba79bb8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import com.botdarr.clients.ChatClientBootstrap;
import com.botdarr.clients.ChatClientResponseBuilder;
import com.botdarr.scheduling.Scheduler;
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update;
import org.apache.logging.log4j.util.Strings;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TelegramBootstrap extends ChatClientBootstrap {
private boolean isUsingChannels() {
Expand Down Expand Up @@ -54,6 +59,23 @@ public boolean isConfigured(Properties properties) {
if (isTelegramConfigured && telegramPrivateChannelsExist && telegramPrivateGroupsExist) {
throw new RuntimeException("Cannot configure telegram for private channels and groups, you must pick one or the other");
}

if (telegramPrivateChannelsExist || telegramPrivateGroupsExist) {
String channels = properties.getProperty(Config.Constants.TELEGRAM_PRIVATE_CHANNELS);
if (Strings.isBlank(channels)) {
channels = properties.getProperty(Config.Constants.TELEGRAM_PRIVATE_GROUPS);
}
Set<String> configTelegramChannels = Sets.newHashSet(Splitter.on(',').trimResults().split(channels));
for (String channel : configTelegramChannels) {
String[] fields = channel.split(":");
if (fields == null || fields.length == 0) {
throw new RuntimeException("Configured telegram channels not in correct format. i.e., CHANNEL_NAME:ID,CHANNEL_NAME2:ID2");
}
if (fields[1].startsWith("-100")) {
throw new RuntimeException("Telegram channel or group contains -100, which is not necessary. We automatically add this to your channel at runtime, you can remove it.");
}
}
}
return isTelegramConfigured && (telegramPrivateChannelsExist || telegramPrivateGroupsExist);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.0
5.3.1
22 changes: 22 additions & 0 deletions src/test/java/com/botdarr/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ public void getConfig_telegramGroupsAndChannelsConfigured() throws Exception {
Config.getProperty("");
}

@Test
public void getConfig_telegramChannelIdsContainNegativeOneHundred() throws Exception {
Properties properties = new Properties();
properties.put("telegram-token", "%H$$54j45i");
properties.put("telegram-private-channels", "channel1:-100459349");
writeFakePropertiesFile(properties);
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Telegram channel or group contains -100, which is not necessary. We automatically add this to your channel at runtime, you can remove it.");
Config.getProperty("");
}

@Test
public void getConfig_telegramGroupIdsContainNegativeOneHundred() throws Exception {
Properties properties = new Properties();
properties.put("telegram-token", "%H$$54j45i");
properties.put("telegram-private-groups", "group1:-100459349");
writeFakePropertiesFile(properties);
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Telegram channel or group contains -100, which is not necessary. We automatically add this to your channel at runtime, you can remove it.");
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 ba79bb8

Please sign in to comment.