Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[telegram] allow access restrictions on chat ids #7857

Merged
merged 3 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions bundles/org.openhab.binding.telegram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,41 @@ 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):
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):

```
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" ]
```


Expand All @@ -97,7 +109,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public boolean equals(@Nullable Object obj) {
}
}

private final List<Long> chatIds = new ArrayList<>();
private final List<Long> authorizedSenderChatId = new ArrayList<>();
private final List<Long> receiverChatId = new ArrayList<>();

private final Logger logger = LoggerFactory.getLogger(TelegramHandler.class);
private @Nullable ScheduledFuture<?> thingOnlineStatusJob;

Expand Down Expand Up @@ -138,10 +140,24 @@ public void initialize() {
TelegramConfiguration config = getConfigAs(TelegramConfiguration.class);

String botToken = config.getBotToken();
chatIds.clear();
authorizedSenderChatId.clear();
receiverChatId.clear();

for (String chatIdStr : config.getChatIds()) {
String trimmedChatId = chatIdStr.trim();
try {
chatIds.add(Long.valueOf(chatIdStr));
if (trimmedChatId.startsWith("<")) {
// inbound only
authorizedSenderChatId.add(Long.valueOf(trimmedChatId.substring(1)));
} else if (trimmedChatId.startsWith(">")) {
// outbound only
receiverChatId.add(Long.valueOf(trimmedChatId.substring(1)));
} else {
// bi-directional (default)
Long chatId = Long.valueOf(trimmedChatId);
authorizedSenderChatId.add(chatId);
receiverChatId.add(chatId);
}
} catch (NumberFormatException e) {
logger.warn("The chat id {} is not a number and will be ignored", chatIdStr);
}
Expand Down Expand Up @@ -233,7 +249,7 @@ private int handleUpdates(List<Update> 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);
Expand Down Expand Up @@ -344,8 +360,22 @@ public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(TelegramActions.class);
}

public List<Long> getChatIds() {
return chatIds;
/**
* get the list of all authorized senders
*
* @return list of chatIds
*/
public List<Long> getAuthorizedSenderChatIds() {
return authorizedSenderChatId;
}

/**
* get the list of all receivers
*
* @return list of chatIds
*/
public List<Long> getReceiverChatIds() {
return receiverChatId;
}

public void addMessageId(Long chatId, String replyId, Integer messageId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</parameter>
<parameter name="chatIds" type="text" required="true" multiple="true">
<label>Chat Id(s)</label>
<description>Enter your chat id(s). Only messages from this id(s) will be send to openHAB.</description>
<description>One or more chat id(s). Access modifiers ("&lt;" for inbound only, "&gt;" for outbound only) can be used as prefix (optional).</description>
</parameter>
<parameter name="parseMode" type="text" required="false">
<label>Parse Mode</label>
Expand Down