diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java b/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java index 7ae7c03..3945510 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java @@ -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() { @@ -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 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); } } diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index e230c83..7d3cdbf 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -5.3.0 \ No newline at end of file +5.3.1 \ 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 3682e45..280ff80 100644 --- a/src/test/java/com/botdarr/ConfigTests.java +++ b/src/test/java/com/botdarr/ConfigTests.java @@ -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());