2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.telegram.internal.action;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.MalformedURLException;
21 import java.nio.charset.StandardCharsets;
22 import java.nio.file.Paths;
23 import java.util.Base64;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
28 import org.apache.commons.io.IOUtils;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.eclipse.jetty.client.HttpClient;
32 import org.eclipse.jetty.client.api.Authentication;
33 import org.eclipse.jetty.client.api.AuthenticationStore;
34 import org.eclipse.jetty.client.api.ContentResponse;
35 import org.eclipse.jetty.client.api.Request;
36 import org.eclipse.jetty.http.HttpHeader;
37 import org.eclipse.jetty.http.HttpMethod;
38 import org.eclipse.jetty.util.B64Code;
39 import org.openhab.binding.telegram.internal.TelegramHandler;
40 import org.openhab.core.automation.annotation.ActionInput;
41 import org.openhab.core.automation.annotation.RuleAction;
42 import org.openhab.core.thing.binding.ThingActions;
43 import org.openhab.core.thing.binding.ThingActionsScope;
44 import org.openhab.core.thing.binding.ThingHandler;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
49 import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
50 import com.pengrad.telegrambot.request.AnswerCallbackQuery;
51 import com.pengrad.telegrambot.request.EditMessageReplyMarkup;
52 import com.pengrad.telegrambot.request.SendMessage;
53 import com.pengrad.telegrambot.request.SendPhoto;
54 import com.pengrad.telegrambot.response.BaseResponse;
55 import com.pengrad.telegrambot.response.SendResponse;
58 * Provides the actions for the Telegram API.
60 * @author Alexander Krasnogolowy - Initial contribution
62 @ThingActionsScope(name = "telegram")
64 public class TelegramActions implements ThingActions {
65 private final Logger logger = LoggerFactory.getLogger(TelegramActions.class);
66 private @Nullable TelegramHandler handler;
68 private boolean evaluateResponse(@Nullable BaseResponse response) {
69 if (response != null && !response.isOk()) {
70 logger.warn("Failed to send telegram message: {}", response.description());
76 private static class BasicResult implements Authentication.Result {
78 private final HttpHeader header;
79 private final URI uri;
80 private final String value;
82 public BasicResult(HttpHeader header, URI uri, String value) {
94 public void apply(@Nullable Request request) {
95 if (request != null) {
96 request.header(this.header, this.value);
101 public String toString() {
102 return String.format("Basic authentication result for %s", this.uri);
106 @RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
107 public boolean sendTelegramAnswer(@ActionInput(name = "chatId") @Nullable Long chatId,
108 @ActionInput(name = "replyId") @Nullable String replyId,
109 @ActionInput(name = "message") @Nullable String message) {
110 if (replyId == null) {
111 logger.warn("ReplyId not defined; action skipped.");
114 if (chatId == null) {
115 logger.warn("chatId not defined; action skipped.");
118 TelegramHandler localHandler = handler;
119 if (localHandler != null) {
120 String callbackId = localHandler.getCallbackId(chatId, replyId);
121 if (callbackId != null) {
122 AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery(
123 localHandler.getCallbackId(chatId, replyId));
124 logger.debug("AnswerCallbackQuery for chatId {} and replyId {} is the callbackId {}", chatId, replyId,
125 localHandler.getCallbackId(chatId, replyId));
126 // we could directly set the text here, but this
127 // doesn't result in a real message only in a
128 // little popup or in an alert, so the only purpose
129 // is to stop the progress bar on client side
130 if (!evaluateResponse(localHandler.execute(answerCallbackQuery))) {
134 Integer messageId = localHandler.removeMessageId(chatId, replyId);
135 logger.debug("remove messageId {} for chatId {} and replyId {}", messageId, chatId, replyId);
137 EditMessageReplyMarkup editReplyMarkup = new EditMessageReplyMarkup(chatId, messageId.intValue())
138 .replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton[0]));// remove reply markup from
140 if (!evaluateResponse(localHandler.execute(editReplyMarkup))) {
143 return message != null ? sendTelegram(chatId, message) : true;
148 @RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
149 public boolean sendTelegramAnswer(@ActionInput(name = "replyId") @Nullable String replyId,
150 @ActionInput(name = "message") @Nullable String message) {
151 TelegramHandler localHandler = handler;
152 if (localHandler != null) {
153 for (Long chatId : localHandler.getReceiverChatIds()) {
154 if (!sendTelegramAnswer(chatId, replyId, message)) {
162 @RuleAction(label = "send a message", description = "Send a Telegram message using the Telegram API.")
163 public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
164 @ActionInput(name = "message") @Nullable String message) {
165 return sendTelegramGeneral(chatId, message, (String) null);
168 @RuleAction(label = "send a message", description = "Send a Telegram message using the Telegram API.")
169 public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message) {
170 TelegramHandler localHandler = handler;
171 if (localHandler != null) {
172 for (Long chatId : localHandler.getReceiverChatIds()) {
173 if (!sendTelegram(chatId, message)) {
181 @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
182 public boolean sendTelegramQuery(@ActionInput(name = "chatId") @Nullable Long chatId,
183 @ActionInput(name = "message") @Nullable String message,
184 @ActionInput(name = "replyId") @Nullable String replyId,
185 @ActionInput(name = "buttons") @Nullable String... buttons) {
186 return sendTelegramGeneral(chatId, message, replyId, buttons);
189 @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
190 public boolean sendTelegramQuery(@ActionInput(name = "message") @Nullable String message,
191 @ActionInput(name = "replyId") @Nullable String replyId,
192 @ActionInput(name = "buttons") @Nullable String... buttons) {
193 TelegramHandler localHandler = handler;
194 if (localHandler != null) {
195 for (Long chatId : localHandler.getReceiverChatIds()) {
196 if (!sendTelegramQuery(chatId, message, replyId, buttons)) {
204 private boolean sendTelegramGeneral(@ActionInput(name = "chatId") @Nullable Long chatId, @Nullable String message,
205 @Nullable String replyId, @Nullable String... buttons) {
206 if (message == null) {
207 logger.warn("Message not defined; action skipped.");
210 if (chatId == null) {
211 logger.warn("chatId not defined; action skipped.");
214 TelegramHandler localHandler = handler;
215 if (localHandler != null) {
216 SendMessage sendMessage = new SendMessage(chatId, message);
217 if (localHandler.getParseMode() != null) {
218 sendMessage.parseMode(localHandler.getParseMode());
220 if (replyId != null) {
221 if (!replyId.contains(" ")) {
222 if (buttons.length > 0) {
223 InlineKeyboardButton[][] keyboard2D = new InlineKeyboardButton[1][];
224 InlineKeyboardButton[] keyboard = new InlineKeyboardButton[buttons.length];
225 keyboard2D[0] = keyboard;
226 for (int i = 0; i < buttons.length; i++) {
227 keyboard[i] = new InlineKeyboardButton(buttons[i]).callbackData(replyId + " " + buttons[i]);
229 InlineKeyboardMarkup keyBoardMarkup = new InlineKeyboardMarkup(keyboard2D);
230 sendMessage.replyMarkup(keyBoardMarkup);
233 "The replyId {} for message {} is given, but no buttons are defined. ReplyMarkup will be ignored.",
237 logger.warn("replyId {} must not contain spaces. ReplyMarkup will be ignored.", replyId);
240 SendResponse retMessage = localHandler.execute(sendMessage);
241 if (!evaluateResponse(retMessage)) {
244 if (replyId != null && retMessage != null) {
245 logger.debug("Adding chatId {}, replyId {} and messageId {}", chatId, replyId,
246 retMessage.message().messageId());
247 localHandler.addMessageId(chatId, replyId, retMessage.message().messageId());
254 @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
255 public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
256 @ActionInput(name = "message") @Nullable String message,
257 @ActionInput(name = "args") @Nullable Object... args) {
258 if (message == null) {
261 return sendTelegram(chatId, String.format(message, args));
264 @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
265 public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message,
266 @ActionInput(name = "args") @Nullable Object... args) {
267 TelegramHandler localHandler = handler;
268 if (localHandler != null) {
269 for (Long chatId : localHandler.getReceiverChatIds()) {
270 if (!sendTelegram(chatId, message, args)) {
278 @RuleAction(label = "send a photo", description = "Send a picture using the Telegram API.")
279 public boolean sendTelegramPhoto(@ActionInput(name = "chatId") @Nullable Long chatId,
280 @ActionInput(name = "photoURL") @Nullable String photoURL,
281 @ActionInput(name = "caption") @Nullable String caption) {
282 return sendTelegramPhoto(chatId, photoURL, caption, null, null);
285 @RuleAction(label = "send a photo", description = "Send a picture using the Telegram API.")
286 public boolean sendTelegramPhoto(@ActionInput(name = "chatId") @Nullable Long chatId,
287 @ActionInput(name = "photoURL") @Nullable String photoURL,
288 @ActionInput(name = "caption") @Nullable String caption,
289 @ActionInput(name = "username") @Nullable String username,
290 @ActionInput(name = "password") @Nullable String password) {
291 if (photoURL == null) {
292 logger.warn("Photo URL not defined; unable to retrieve photo for sending.");
295 if (chatId == null) {
296 logger.warn("chatId not defined; action skipped.");
300 TelegramHandler localHandler = handler;
301 if (localHandler != null) {
302 final SendPhoto sendPhoto;
304 if (photoURL.toLowerCase().startsWith("http")) {
305 // load image from url
306 logger.debug("Photo URL provided.");
307 HttpClient client = localHandler.getClient();
308 if (client == null) {
311 Request request = client.newRequest(photoURL).method(HttpMethod.GET).timeout(30, TimeUnit.SECONDS);
312 if (username != null && password != null) {
313 AuthenticationStore auth = client.getAuthenticationStore();
314 URI uri = URI.create(photoURL);
315 auth.addAuthenticationResult(new BasicResult(HttpHeader.AUTHORIZATION, uri,
316 "Basic " + B64Code.encode(username + ":" + password, StandardCharsets.ISO_8859_1)));
319 ContentResponse contentResponse = request.send();
320 if (contentResponse.getStatus() == 200) {
321 byte[] fileContent = contentResponse.getContent();
322 sendPhoto = new SendPhoto(chatId, fileContent);
324 logger.warn("Download from {} failed with status: {}", photoURL, contentResponse.getStatus());
327 } catch (InterruptedException | TimeoutException | ExecutionException e) {
328 logger.warn("Download from {} failed with exception: {}", photoURL, e.getMessage());
331 } else if (photoURL.toLowerCase().startsWith("file")) {
332 // Load image from local file system
333 logger.debug("Read file from local file system: {}", photoURL);
335 URL url = new URL(photoURL);
336 sendPhoto = new SendPhoto(chatId, Paths.get(url.getPath()).toFile());
337 } catch (MalformedURLException e) {
338 logger.warn("Malformed URL: {}", photoURL);
342 // Load image from provided base64 image
343 logger.debug("Photo base64 provided; converting to binary.");
344 final String photoB64Data;
345 if (photoURL.startsWith("data:")) { // support data URI scheme
346 String[] photoURLParts = photoURL.split(",");
347 if (photoURLParts.length > 1) {
348 photoB64Data = photoURLParts[1];
350 logger.warn("The provided base64 string is not a valid data URI scheme");
354 photoB64Data = photoURL;
356 InputStream is = Base64.getDecoder()
357 .wrap(new ByteArrayInputStream(photoB64Data.getBytes(StandardCharsets.UTF_8)));
359 byte[] photoBytes = IOUtils.toByteArray(is);
360 sendPhoto = new SendPhoto(chatId, photoBytes);
361 } catch (IOException e) {
362 logger.warn("Malformed base64 string: {}", e.getMessage());
366 sendPhoto.caption(caption);
367 if (localHandler.getParseMode() != null) {
368 sendPhoto.parseMode(localHandler.getParseMode());
370 return evaluateResponse(localHandler.execute(sendPhoto));
375 @RuleAction(label = "send a photo", description = "Send a Picture using the Telegram API.")
376 public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable String photoURL,
377 @ActionInput(name = "caption") @Nullable String caption,
378 @ActionInput(name = "username") @Nullable String username,
379 @ActionInput(name = "password") @Nullable String password) {
380 TelegramHandler localHandler = handler;
381 if (localHandler != null) {
382 for (Long chatId : localHandler.getReceiverChatIds()) {
383 if (!sendTelegramPhoto(chatId, photoURL, caption, username, password)) {
391 @RuleAction(label = "send a photo", description = "Send a Picture using the Telegram API.")
392 public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable String photoURL,
393 @ActionInput(name = "caption") @Nullable String caption) {
394 return sendTelegramPhoto(photoURL, caption, null, null);
397 // legacy delegate methods
398 /* APIs without chatId parameter */
399 public static boolean sendTelegram(ThingActions actions, @Nullable String format, @Nullable Object... args) {
400 return ((TelegramActions) actions).sendTelegram(format, args);
403 public static boolean sendTelegramQuery(ThingActions actions, @Nullable String message, @Nullable String replyId,
404 @Nullable String... buttons) {
405 return ((TelegramActions) actions).sendTelegramQuery(message, replyId, buttons);
408 public static boolean sendTelegramPhoto(ThingActions actions, @Nullable String photoURL, @Nullable String caption) {
409 return ((TelegramActions) actions).sendTelegramPhoto(photoURL, caption, null, null);
412 public static boolean sendTelegramPhoto(ThingActions actions, @Nullable String photoURL, @Nullable String caption,
413 @Nullable String username, @Nullable String password) {
414 return ((TelegramActions) actions).sendTelegramPhoto(photoURL, caption, username, password);
417 public static boolean sendTelegramAnswer(ThingActions actions, @Nullable String replyId, @Nullable String message) {
418 return ((TelegramActions) actions).sendTelegramAnswer(replyId, message);
421 /* APIs with chatId parameter */
423 public static boolean sendTelegram(ThingActions actions, @Nullable Long chatId, @Nullable String format,
424 @Nullable Object... args) {
425 return ((TelegramActions) actions).sendTelegram(chatId, format, args);
428 public static boolean sendTelegramQuery(ThingActions actions, @Nullable Long chatId, @Nullable String message,
429 @Nullable String replyId, @Nullable String... buttons) {
430 return ((TelegramActions) actions).sendTelegramQuery(chatId, message, replyId, buttons);
433 public static boolean sendTelegramPhoto(ThingActions actions, @Nullable Long chatId, @Nullable String photoURL,
434 @Nullable String caption) {
435 return ((TelegramActions) actions).sendTelegramPhoto(chatId, photoURL, caption, null, null);
438 public static boolean sendTelegramPhoto(ThingActions actions, @Nullable Long chatId, @Nullable String photoURL,
439 @Nullable String caption, @Nullable String username, @Nullable String password) {
440 return ((TelegramActions) actions).sendTelegramPhoto(chatId, photoURL, caption, username, password);
443 public static boolean sendTelegramAnswer(ThingActions actions, @Nullable Long chatId, @Nullable String replyId,
444 @Nullable String message) {
445 return ((TelegramActions) actions).sendTelegramAnswer(chatId, replyId, message);
448 public static boolean sendTelegramAnswer(ThingActions actions, @Nullable String chatId, @Nullable String replyId,
449 @Nullable String message) {
450 if (actions instanceof TelegramActions) {
451 if (chatId == null) {
454 return ((TelegramActions) actions).sendTelegramAnswer(Long.valueOf(chatId), replyId, message);
456 throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
461 public void setThingHandler(@Nullable ThingHandler handler) {
462 this.handler = (TelegramHandler) handler;
466 public @Nullable ThingHandler getThingHandler() {