From 20832d776ecd381af1e938f03ac7b9b1cef21b16 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Thu, 4 Jun 2020 21:14:54 +0200 Subject: [PATCH 1/3] allow access restrictions on chat ids Signed-off-by: Jan N. Klug --- .../org.openhab.binding.telegram/README.md | 15 ++++--- .../binding/telegram/bot/TelegramActions.java | 10 ++--- .../telegram/internal/TelegramHandler.java | 41 ++++++++++++++++--- .../resources/ESH-INF/thing/thing-types.xml | 2 +- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.binding.telegram/README.md b/bundles/org.openhab.binding.telegram/README.md index 6c5b499f3569f..fb05dcd186859 100644 --- a/bundles/org.openhab.binding.telegram/README.md +++ b/bundles/org.openhab.binding.telegram/README.md @@ -62,29 +62,34 @@ In order to send a message, an action must be used instead. | `proxyPort` | None | No | Proxy port for telegram binding. | | `proxyType` | SOCKS5 | No | Type of proxy server for telegram binding (SOCKS5 or HTTP). Default: SOCKS5 | +By default chat ids are bi-directionally, i.e. they can send and receive messages. +They can be prefixed with an access modifier: +- `<` restricts the chat to send only, i.e. this chat id can send messages to openHAB, but will never receive a notification. +- `>` restricts the chat to receive only, i.e. this chat id will receive all notifications, but messages from this chat id will be discarded. +To use the reply function, chat ids need to be bi-directional. telegram.thing (no proxy): ``` -Thing telegram:telegramBot:Telegram_Bot [ chatIds="< ID >", botToken="< TOKEN >" ] +Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID", botToken="TOKEN" ] ``` telegram.thing (multiple chat ids and markdown format): ``` -Thing telegram:telegramBot:Telegram_Bot [ chatIds="< ID1 >","< ID2 >", botToken="< TOKEN >", parseMode ="Markdown" ] +Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID1","ID2", botToken="TOKEN", parseMode ="Markdown" ] ``` telegram.thing (SOCKS5 proxy server is used): ``` -Thing telegram:telegramBot:Telegram_Bot [ chatIds="< ID >", botToken="< TOKEN >", proxyHost="< HOST >", proxyPort="< PORT >", proxyType="< TYPE >" ] +Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID", botToken="TOKEN", proxyHost="HOST", proxyPort="PORT", proxyType="TYPE" ] ``` or HTTP proxy server ``` -Thing telegram:telegramBot:Telegram_Bot [ chatIds="< ID >", botToken="< TOKEN >", proxyHost="localhost", proxyPort="8123", proxyType="HTTP" ] +Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID", botToken="TOKEN", proxyHost="localhost", proxyPort="8123", proxyType="HTTP" ] ``` @@ -97,7 +102,7 @@ Thing telegram:telegramBot:Telegram_Bot [ chatIds="< ID >", botToken="< TOKEN >" | lastMessageDate | DateTime | The date of the last received message (UTC) | | lastMessageName | String | The full name of the sender of the last received message | | lastMessageUsername | String | The username of the sender of the last received message | -| chatId | String | The id of the chat of the last received meesage | +| chatId | String | The id of the chat of the last received message | | replyId | String | The id of the reply which was passed to sendTelegram() as replyId argument. This id can be used to have an unambiguous assignment of the users reply to the message which was sent by the bot | All channels are read-only. diff --git a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/bot/TelegramActions.java b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/bot/TelegramActions.java index 39b3bba0ba0ca..3844a755d8ae2 100644 --- a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/bot/TelegramActions.java +++ b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/bot/TelegramActions.java @@ -151,7 +151,7 @@ public boolean sendTelegramAnswer(@ActionInput(name = "replyId") @Nullable Strin @ActionInput(name = "message") @Nullable String message) { TelegramHandler localHandler = handler; if (localHandler != null) { - for (Long chatId : localHandler.getChatIds()) { + for (Long chatId : localHandler.getReceiverChatIds()) { if (!sendTelegramAnswer(chatId, replyId, message)) { return false; } @@ -170,7 +170,7 @@ public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId, public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message) { TelegramHandler localHandler = handler; if (localHandler != null) { - for (Long chatId : localHandler.getChatIds()) { + for (Long chatId : localHandler.getReceiverChatIds()) { if (!sendTelegram(chatId, message)) { return false; } @@ -193,7 +193,7 @@ public boolean sendTelegramQuery(@ActionInput(name = "message") @Nullable String @ActionInput(name = "buttons") @Nullable String... buttons) { TelegramHandler localHandler = handler; if (localHandler != null) { - for (Long chatId : localHandler.getChatIds()) { + for (Long chatId : localHandler.getReceiverChatIds()) { if (!sendTelegramQuery(chatId, message, replyId, buttons)) { return false; } @@ -264,7 +264,7 @@ public boolean sendTelegram(@ActionInput(name = "message") @Nullable String mess @ActionInput(name = "args") @Nullable Object... args) { TelegramHandler localHandler = handler; if (localHandler != null) { - for (Long chatId : localHandler.getChatIds()) { + for (Long chatId : localHandler.getReceiverChatIds()) { if (!sendTelegram(chatId, message, args)) { return false; } @@ -377,7 +377,7 @@ public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable Strin @ActionInput(name = "password") @Nullable String password) { TelegramHandler localHandler = handler; if (localHandler != null) { - for (Long chatId : localHandler.getChatIds()) { + for (Long chatId : localHandler.getReceiverChatIds()) { if (!sendTelegramPhoto(chatId, photoURL, caption, username, password)) { return false; } diff --git a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java index 0af9687b5daa0..9481b259b5582 100644 --- a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java +++ b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java @@ -105,7 +105,9 @@ public boolean equals(@Nullable Object obj) { } } - private final List chatIds = new ArrayList<>(); + private final List authorizedSenderChatId = new ArrayList<>(); + private final List receiverChatId = new ArrayList<>(); + private final Logger logger = LoggerFactory.getLogger(TelegramHandler.class); private @Nullable ScheduledFuture thingOnlineStatusJob; @@ -138,10 +140,23 @@ public void initialize() { TelegramConfiguration config = getConfigAs(TelegramConfiguration.class); String botToken = config.getBotToken(); - chatIds.clear(); + authorizedSenderChatId.clear(); + receiverChatId.clear(); + for (String chatIdStr : config.getChatIds()) { try { - chatIds.add(Long.valueOf(chatIdStr)); + if (chatIdStr.startsWith("<")) { + // inbound only + authorizedSenderChatId.add(Long.valueOf(chatIdStr.replace("<", ""))); + } else if (chatIdStr.startsWith(">")) { + // outbound only + receiverChatId.add(Long.valueOf(chatIdStr.replace(">", ""))); + } else { + // bi-directional (default) + Long chatId = Long.valueOf(chatIdStr); + authorizedSenderChatId.add(chatId); + receiverChatId.add(chatId); + } } catch (NumberFormatException e) { logger.warn("The chat id {} is not a number and will be ignored", chatIdStr); } @@ -233,7 +248,7 @@ private int handleUpdates(List updates) { if (message != null) { chatId = message.chat().id(); - if (!chatIds.contains(chatId)) { + if (!authorizedSenderChatId.contains(chatId)) { logger.warn( "Ignored message from unknown chat id {}. If you know the sender of that chat, add it to the list of chat ids in the thing configuration to authorize it", chatId); @@ -344,8 +359,22 @@ public Collection> getServices() { return Collections.singleton(TelegramActions.class); } - public List getChatIds() { - return chatIds; + /** + * get the list of all authorized senders + * + * @return list of chatIds + */ + public List getAuthorizedSenderChatIds() { + return authorizedSenderChatId; + } + + /** + * get the list of all receivers + * + * @return list of chatIds + */ + public List getReceiverChatIds() { + return receiverChatId; } public void addMessageId(Long chatId, String replyId, Integer messageId) { diff --git a/bundles/org.openhab.binding.telegram/src/main/resources/ESH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.telegram/src/main/resources/ESH-INF/thing/thing-types.xml index 46d2c510ba233..d017073f6e428 100644 --- a/bundles/org.openhab.binding.telegram/src/main/resources/ESH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.telegram/src/main/resources/ESH-INF/thing/thing-types.xml @@ -25,7 +25,7 @@ - Enter your chat id(s). Only messages from this id(s) will be send to openHAB. + One or more chat id(s). Access modifiers ("<" for inbound only, ">" for outbound only) can be used as prefix (optional). From 4c7f7a87476a1a2508fcfbe6835ce544bcd18655 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Fri, 5 Jun 2020 15:30:47 +0200 Subject: [PATCH 2/3] address review comments Signed-off-by: Jan N. Klug --- bundles/org.openhab.binding.telegram/README.md | 11 +++++++++-- .../binding/telegram/internal/TelegramHandler.java | 8 ++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.telegram/README.md b/bundles/org.openhab.binding.telegram/README.md index fb05dcd186859..53dd371d0b39e 100644 --- a/bundles/org.openhab.binding.telegram/README.md +++ b/bundles/org.openhab.binding.telegram/README.md @@ -74,10 +74,17 @@ telegram.thing (no proxy): Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID", botToken="TOKEN" ] ``` -telegram.thing (multiple chat ids and markdown format): +telegram.thing (multiple chat ids, one bi-directional chat (ID1), one outbound-only (ID2)): ``` -Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID1","ID2", botToken="TOKEN", parseMode ="Markdown" ] +Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID1",">ID2", botToken="TOKEN" ] +``` + + +telegram.thing (markdown format): + +``` +Thing telegram:telegramBot:Telegram_Bot [ chatIds="ID", botToken="TOKEN", parseMode ="Markdown" ] ``` telegram.thing (SOCKS5 proxy server is used): diff --git a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java index 9481b259b5582..4d8a47858d471 100644 --- a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java +++ b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java @@ -145,12 +145,12 @@ public void initialize() { for (String chatIdStr : config.getChatIds()) { try { - if (chatIdStr.startsWith("<")) { + if (chatIdStr.trim().startsWith("<")) { // inbound only - authorizedSenderChatId.add(Long.valueOf(chatIdStr.replace("<", ""))); - } else if (chatIdStr.startsWith(">")) { + authorizedSenderChatId.add(Long.valueOf(chatIdStr.substring(1))); + } else if (chatIdStr.trim().startsWith(">")) { // outbound only - receiverChatId.add(Long.valueOf(chatIdStr.replace(">", ""))); + receiverChatId.add(Long.valueOf(chatIdStr.substring(1))); } else { // bi-directional (default) Long chatId = Long.valueOf(chatIdStr); From 0e53bb61098a3f7b4a419c893280110f86e8341b Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Sat, 6 Jun 2020 10:06:14 +0200 Subject: [PATCH 3/3] address more review comments Signed-off-by: Jan N. Klug --- .../binding/telegram/internal/TelegramHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java index 4d8a47858d471..7104b05b72318 100644 --- a/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java +++ b/bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/TelegramHandler.java @@ -144,16 +144,17 @@ public void initialize() { receiverChatId.clear(); for (String chatIdStr : config.getChatIds()) { + String trimmedChatId = chatIdStr.trim(); try { - if (chatIdStr.trim().startsWith("<")) { + if (trimmedChatId.startsWith("<")) { // inbound only - authorizedSenderChatId.add(Long.valueOf(chatIdStr.substring(1))); - } else if (chatIdStr.trim().startsWith(">")) { + authorizedSenderChatId.add(Long.valueOf(trimmedChatId.substring(1))); + } else if (trimmedChatId.startsWith(">")) { // outbound only - receiverChatId.add(Long.valueOf(chatIdStr.substring(1))); + receiverChatId.add(Long.valueOf(trimmedChatId.substring(1))); } else { // bi-directional (default) - Long chatId = Long.valueOf(chatIdStr); + Long chatId = Long.valueOf(trimmedChatId); authorizedSenderChatId.add(chatId); receiverChatId.add(chatId); }