]> git.basschouten.com Git - openhab-addons.git/blob
0aad42dde834c1b08052c75fdcd0712624cc9ca0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.telegram.internal.action;
14
15 import static org.openhab.binding.telegram.internal.TelegramBindingConstants.PHOTO_EXTENSIONS;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.MalformedURLException;
21 import java.net.URI;
22 import java.net.URL;
23 import java.nio.charset.StandardCharsets;
24 import java.nio.file.Path;
25 import java.util.Base64;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeUnit;
28
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.client.util.FutureResponseListener;
37 import org.eclipse.jetty.http.HttpHeader;
38 import org.eclipse.jetty.http.HttpMethod;
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;
47
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.SendAnimation;
53 import com.pengrad.telegrambot.request.SendMessage;
54 import com.pengrad.telegrambot.request.SendPhoto;
55 import com.pengrad.telegrambot.request.SendVideo;
56 import com.pengrad.telegrambot.response.BaseResponse;
57 import com.pengrad.telegrambot.response.SendResponse;
58
59 /**
60  * Provides the actions for the Telegram API.
61  *
62  * @author Alexander Krasnogolowy - Initial contribution
63  */
64 @ThingActionsScope(name = "telegram")
65 @NonNullByDefault
66 public class TelegramActions implements ThingActions {
67     private final Logger logger = LoggerFactory.getLogger(TelegramActions.class);
68     private @Nullable TelegramHandler handler;
69
70     private boolean evaluateResponse(@Nullable BaseResponse response) {
71         if (response != null && !response.isOk()) {
72             logger.warn("Failed to send telegram message: {}", response.description());
73             return false;
74         }
75         return true;
76     }
77
78     private static class BasicResult implements Authentication.Result {
79
80         private final HttpHeader header;
81         private final URI uri;
82         private final String value;
83
84         public BasicResult(HttpHeader header, URI uri, String value) {
85             this.header = header;
86             this.uri = uri;
87             this.value = value;
88         }
89
90         @Override
91         public URI getURI() {
92             return this.uri;
93         }
94
95         @Override
96         public void apply(@Nullable Request request) {
97             if (request != null) {
98                 request.header(this.header, this.value);
99             }
100         }
101
102         @Override
103         public String toString() {
104             return String.format("Basic authentication result for %s", this.uri);
105         }
106     }
107
108     @RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
109     public boolean sendTelegramAnswer(@ActionInput(name = "chatId") @Nullable Long chatId,
110             @ActionInput(name = "callbackId") @Nullable String callbackId,
111             @ActionInput(name = "messageId") @Nullable Long messageId,
112             @ActionInput(name = "message") @Nullable String message) {
113         if (chatId == null) {
114             logger.warn("chatId not defined; action skipped.");
115             return false;
116         }
117         if (messageId == null) {
118             logger.warn("messageId not defined; action skipped.");
119             return false;
120         }
121         TelegramHandler localHandler = handler;
122         if (localHandler != null) {
123             if (callbackId != null) {
124                 AnswerCallbackQuery answerCallbackQuery = new AnswerCallbackQuery(callbackId);
125                 // we could directly set the text here, but this
126                 // doesn't result in a real message only in a
127                 // little popup or in an alert, so the only purpose
128                 // is to stop the progress bar on client side
129                 logger.debug("Answering query with callbackId '{}'", callbackId);
130                 if (!evaluateResponse(localHandler.execute(answerCallbackQuery))) {
131                     return false;
132                 }
133             }
134             EditMessageReplyMarkup editReplyMarkup = new EditMessageReplyMarkup(chatId, messageId.intValue())
135                     .replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton[0]));// remove reply markup from
136                                                                                         // old message
137             if (!evaluateResponse(localHandler.execute(editReplyMarkup))) {
138                 return false;
139             }
140             return message != null ? sendTelegram(chatId, message) : true;
141         }
142         return false;
143     }
144
145     @RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
146     public boolean sendTelegramAnswer(@ActionInput(name = "chatId") @Nullable Long chatId,
147             @ActionInput(name = "replyId") @Nullable String replyId,
148             @ActionInput(name = "message") @Nullable String message) {
149         if (replyId == null) {
150             logger.warn("ReplyId not defined; action skipped.");
151             return false;
152         }
153         if (chatId == null) {
154             logger.warn("chatId not defined; action skipped.");
155             return false;
156         }
157         TelegramHandler localHandler = handler;
158         if (localHandler != null) {
159             String callbackId = localHandler.getCallbackId(chatId, replyId);
160             if (callbackId != null) {
161                 logger.debug("AnswerCallbackQuery for chatId {} and replyId {} is the callbackId {}", chatId, replyId,
162                         callbackId);
163             }
164             Integer messageId = localHandler.removeMessageId(chatId, replyId);
165             logger.debug("remove messageId {} for chatId {} and replyId {}", messageId, chatId, replyId);
166
167             return sendTelegramAnswer(chatId, callbackId, messageId != null ? Long.valueOf(messageId) : null, message);
168         }
169         return false;
170     }
171
172     @RuleAction(label = "send an answer", description = "Send a Telegram answer using the Telegram API.")
173     public boolean sendTelegramAnswer(@ActionInput(name = "replyId") @Nullable String replyId,
174             @ActionInput(name = "message") @Nullable String message) {
175         TelegramHandler localHandler = handler;
176         if (localHandler != null) {
177             for (Long chatId : localHandler.getReceiverChatIds()) {
178                 if (!sendTelegramAnswer(chatId, replyId, message)) {
179                     return false;
180                 }
181             }
182         }
183         return true;
184     }
185
186     @RuleAction(label = "send a message", description = "Send a Telegram message using the Telegram API.")
187     public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
188             @ActionInput(name = "message") @Nullable String message) {
189         return sendTelegramGeneral(chatId, message, (String) null);
190     }
191
192     @RuleAction(label = "send a message", description = "Send a Telegram message using the Telegram API.")
193     public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message) {
194         TelegramHandler localHandler = handler;
195         if (localHandler != null) {
196             for (Long chatId : localHandler.getReceiverChatIds()) {
197                 if (!sendTelegram(chatId, message)) {
198                     return false;
199                 }
200             }
201         }
202         return true;
203     }
204
205     @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
206     public boolean sendTelegramQuery(@ActionInput(name = "chatId") @Nullable Long chatId,
207             @ActionInput(name = "message") @Nullable String message,
208             @ActionInput(name = "replyId") @Nullable String replyId,
209             @ActionInput(name = "buttons") @Nullable String... buttons) {
210         return sendTelegramGeneral(chatId, message, replyId, buttons);
211     }
212
213     @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
214     public boolean sendTelegramQuery(@ActionInput(name = "message") @Nullable String message,
215             @ActionInput(name = "replyId") @Nullable String replyId,
216             @ActionInput(name = "buttons") @Nullable String... buttons) {
217         TelegramHandler localHandler = handler;
218         if (localHandler != null) {
219             for (Long chatId : localHandler.getReceiverChatIds()) {
220                 if (!sendTelegramQuery(chatId, message, replyId, buttons)) {
221                     return false;
222                 }
223             }
224         }
225         return true;
226     }
227
228     private boolean sendTelegramGeneral(@ActionInput(name = "chatId") @Nullable Long chatId, @Nullable String message,
229             @Nullable String replyId, @Nullable String... buttons) {
230         if (message == null) {
231             logger.warn("Message not defined; action skipped.");
232             return false;
233         }
234         if (chatId == null) {
235             logger.warn("chatId not defined; action skipped.");
236             return false;
237         }
238         TelegramHandler localHandler = handler;
239         if (localHandler != null) {
240             String escapedMessage = message.replace("_", "\\_");
241             SendMessage sendMessage = new SendMessage(chatId, escapedMessage);
242             if (localHandler.getParseMode() != null) {
243                 sendMessage.parseMode(localHandler.getParseMode());
244             }
245             if (replyId != null) {
246                 if (!replyId.contains(" ")) {
247                     if (buttons.length > 0) {
248                         InlineKeyboardButton[][] keyboard2D = new InlineKeyboardButton[1][];
249                         InlineKeyboardButton[] keyboard = new InlineKeyboardButton[buttons.length];
250                         keyboard2D[0] = keyboard;
251                         for (int i = 0; i < buttons.length; i++) {
252                             keyboard[i] = new InlineKeyboardButton(buttons[i]).callbackData(replyId + " " + buttons[i]);
253                         }
254                         InlineKeyboardMarkup keyBoardMarkup = new InlineKeyboardMarkup(keyboard2D);
255                         sendMessage.replyMarkup(keyBoardMarkup);
256                     } else {
257                         logger.warn(
258                                 "The replyId {} for message {} is given, but no buttons are defined. ReplyMarkup will be ignored.",
259                                 replyId, message);
260                     }
261                 } else {
262                     logger.warn("replyId {} must not contain spaces. ReplyMarkup will be ignored.", replyId);
263                 }
264             }
265             SendResponse retMessage = null;
266             try {
267                 retMessage = localHandler.execute(sendMessage);
268             } catch (Exception e) {
269                 logger.warn("Exception occured whilst sending message:{}", e.getMessage());
270             }
271             if (!evaluateResponse(retMessage)) {
272                 return false;
273             }
274             if (replyId != null && retMessage != null) {
275                 logger.debug("Adding chatId {}, replyId {} and messageId {}", chatId, replyId,
276                         retMessage.message().messageId());
277                 localHandler.addMessageId(chatId, replyId, retMessage.message().messageId());
278             }
279             return true;
280         }
281         return false;
282     }
283
284     @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
285     public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
286             @ActionInput(name = "message") @Nullable String message,
287             @ActionInput(name = "args") @Nullable Object... args) {
288         if (message == null) {
289             return false;
290         }
291         return sendTelegram(chatId, String.format(message, args));
292     }
293
294     @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
295     public boolean sendTelegram(@ActionInput(name = "message") @Nullable String message,
296             @ActionInput(name = "args") @Nullable Object... args) {
297         TelegramHandler localHandler = handler;
298         if (localHandler != null) {
299             for (Long chatId : localHandler.getReceiverChatIds()) {
300                 if (!sendTelegram(chatId, message, args)) {
301                     return false;
302                 }
303             }
304         }
305         return true;
306     }
307
308     @RuleAction(label = "send a photo", description = "Send a picture using the Telegram API.")
309     public boolean sendTelegramPhoto(@ActionInput(name = "chatId") @Nullable Long chatId,
310             @ActionInput(name = "photoURL") @Nullable String photoURL,
311             @ActionInput(name = "caption") @Nullable String caption) {
312         return sendTelegramPhoto(chatId, photoURL, caption, null, null);
313     }
314
315     @RuleAction(label = "send a photo", description = "Send a picture using the Telegram API.")
316     public boolean sendTelegramPhoto(@ActionInput(name = "chatId") @Nullable Long chatId,
317             @ActionInput(name = "photoURL") @Nullable String photoURL,
318             @ActionInput(name = "caption") @Nullable String caption,
319             @ActionInput(name = "username") @Nullable String username,
320             @ActionInput(name = "password") @Nullable String password) {
321         if (photoURL == null) {
322             logger.warn("Photo URL not defined; unable to retrieve photo for sending.");
323             return false;
324         }
325         if (chatId == null) {
326             logger.warn("chatId not defined; action skipped.");
327             return false;
328         }
329         String lowercasePhotoUrl = photoURL.toLowerCase();
330         TelegramHandler localHandler = handler;
331         if (localHandler != null) {
332             final SendPhoto sendPhoto;
333             if (lowercasePhotoUrl.startsWith("http")) {
334                 logger.debug("Http based URL for photo provided.");
335                 HttpClient client = localHandler.getClient();
336                 if (client == null) {
337                     return false;
338                 }
339                 Request request = client.newRequest(photoURL).method(HttpMethod.GET).timeout(30, TimeUnit.SECONDS);
340                 if (username != null && password != null) {
341                     AuthenticationStore auth = client.getAuthenticationStore();
342                     URI uri = URI.create(photoURL);
343                     auth.addAuthenticationResult(
344                             new BasicResult(HttpHeader.AUTHORIZATION, uri, "Basic " + Base64.getEncoder()
345                                     .encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8))));
346                 }
347                 try {
348                     // API has 10mb limit to jpg file size, without this it can only accept 2mb
349                     FutureResponseListener listener = new FutureResponseListener(request, 10 * 1024 * 1024);
350                     request.send(listener);
351                     ContentResponse contentResponse = listener.get();
352                     if (contentResponse.getStatus() == 200) {
353                         byte[] fileContent = contentResponse.getContent();
354                         sendPhoto = new SendPhoto(chatId, fileContent);
355                     } else {
356                         logger.warn("Download from {} failed with status: {}", photoURL, contentResponse.getStatus());
357                         sendTelegram(chatId, caption + ":Download failed with status " + contentResponse.getStatus());
358                         return false;
359                     }
360                 } catch (InterruptedException | ExecutionException e) {
361                     logger.warn("Download from {} failed with exception: {}", photoURL, e.getMessage());
362                     return false;
363                 }
364             } else if (lowercasePhotoUrl.startsWith("file:")
365                     || PHOTO_EXTENSIONS.stream().anyMatch(lowercasePhotoUrl::endsWith)) {
366                 logger.debug("Read file from local file system: {}", photoURL);
367                 String temp = photoURL;
368                 if (!lowercasePhotoUrl.startsWith("file:")) {
369                     temp = "file://" + photoURL;
370                 }
371                 try {
372                     sendPhoto = new SendPhoto(chatId, Path.of(new URL(temp).getPath()).toFile());
373                 } catch (MalformedURLException e) {
374                     logger.warn("Malformed URL: {}", photoURL);
375                     return false;
376                 }
377             } else {
378                 logger.debug("Base64 image provided; converting to binary.");
379                 final String photoB64Data;
380                 if (photoURL.startsWith("data:")) { // support data URI scheme
381                     String[] photoURLParts = photoURL.split(",");
382                     if (photoURLParts.length > 1) {
383                         photoB64Data = photoURLParts[1];
384                     } else {
385                         logger.warn("The provided base64 string is not a valid data URI scheme");
386                         return false;
387                     }
388                 } else {
389                     photoB64Data = photoURL;
390                 }
391                 InputStream is = Base64.getDecoder()
392                         .wrap(new ByteArrayInputStream(photoB64Data.getBytes(StandardCharsets.UTF_8)));
393                 try {
394                     byte[] photoBytes = is.readAllBytes();
395                     sendPhoto = new SendPhoto(chatId, photoBytes);
396                 } catch (IOException e) {
397                     logger.warn("Malformed base64 string: {}", e.getMessage());
398                     return false;
399                 }
400             }
401             if (caption != null) {
402                 sendPhoto.caption(caption);
403             }
404             if (localHandler.getParseMode() != null) {
405                 sendPhoto.parseMode(localHandler.getParseMode());
406             }
407             return evaluateResponse(localHandler.execute(sendPhoto));
408         }
409         return false;
410     }
411
412     @RuleAction(label = "send a photo", description = "Send a Picture using the Telegram API.")
413     public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable String photoURL,
414             @ActionInput(name = "caption") @Nullable String caption,
415             @ActionInput(name = "username") @Nullable String username,
416             @ActionInput(name = "password") @Nullable String password) {
417         TelegramHandler localHandler = handler;
418         if (localHandler != null) {
419             for (Long chatId : localHandler.getReceiverChatIds()) {
420                 if (!sendTelegramPhoto(chatId, photoURL, caption, username, password)) {
421                     return false;
422                 }
423             }
424         }
425         return true;
426     }
427
428     @RuleAction(label = "send a photo", description = "Send a Picture using the Telegram API.")
429     public boolean sendTelegramPhoto(@ActionInput(name = "photoURL") @Nullable String photoURL,
430             @ActionInput(name = "caption") @Nullable String caption) {
431         return sendTelegramPhoto(photoURL, caption, null, null);
432     }
433
434     @RuleAction(label = "send animation", description = "Send an Animation using the Telegram API.")
435     public boolean sendTelegramAnimation(@ActionInput(name = "animationURL") @Nullable String animationURL,
436             @ActionInput(name = "caption") @Nullable String caption) {
437         TelegramHandler localHandler = handler;
438         if (localHandler != null) {
439             for (Long chatId : localHandler.getReceiverChatIds()) {
440                 if (!sendTelegramAnimation(chatId, animationURL, caption)) {
441                     return false;
442                 }
443             }
444         }
445         return true;
446     }
447
448     @RuleAction(label = "send animation", description = "Send an Animation using the Telegram API.")
449     public boolean sendTelegramAnimation(@ActionInput(name = "chatId") @Nullable Long chatId,
450             @ActionInput(name = "animationURL") @Nullable String animationURL,
451             @ActionInput(name = "caption") @Nullable String caption) {
452         if (animationURL == null) {
453             logger.warn("Animation URL not defined; unable to retrieve video for sending.");
454             return false;
455         }
456         if (chatId == null) {
457             logger.warn("chatId not defined; action skipped.");
458             return false;
459         }
460         TelegramHandler localHandler = handler;
461         if (localHandler != null) {
462             final SendAnimation sendAnimation;
463             if (animationURL.toLowerCase().startsWith("http")) {
464                 // load image from url
465                 logger.debug("Animation URL provided.");
466                 HttpClient client = localHandler.getClient();
467                 if (client == null) {
468                     return false;
469                 }
470                 Request request = client.newRequest(animationURL).method(HttpMethod.GET).timeout(30, TimeUnit.SECONDS);
471                 try {
472                     // 50mb limit to file size
473                     FutureResponseListener listener = new FutureResponseListener(request, 50 * 1024 * 1024);
474                     request.send(listener);
475                     ContentResponse contentResponse = listener.get();
476                     if (contentResponse.getStatus() == 200) {
477                         byte[] fileContent = contentResponse.getContent();
478                         sendAnimation = new SendAnimation(chatId, fileContent);
479                     } else {
480                         logger.warn("Download from {} failed with status: {}", animationURL,
481                                 contentResponse.getStatus());
482                         sendTelegram(chatId, caption + ":Download failed with status " + contentResponse.getStatus());
483                         return false;
484                     }
485                 } catch (InterruptedException | ExecutionException e) {
486                     logger.warn("Download from {} failed with exception: {}", animationURL, e.getMessage());
487                     return false;
488                 }
489             } else {
490                 String temp = animationURL;
491                 if (!animationURL.toLowerCase().startsWith("file:")) {
492                     temp = "file://" + animationURL;
493                 }
494                 // Load video from local file system
495                 logger.debug("Read file from local file system: {}", animationURL);
496                 try {
497                     sendAnimation = new SendAnimation(chatId, Path.of(new URL(temp).getPath()).toFile());
498                 } catch (MalformedURLException e) {
499                     logger.warn("Malformed URL, should start with http or file: {}", animationURL);
500                     return false;
501                 }
502             }
503             if (caption != null) {
504                 sendAnimation.caption(caption);
505             }
506             if (localHandler.getParseMode() != null) {
507                 sendAnimation.parseMode(localHandler.getParseMode());
508             }
509             return evaluateResponse(localHandler.execute(sendAnimation));
510         }
511         return false;
512     }
513
514     @RuleAction(label = "send video", description = "Send a Video using the Telegram API.")
515     public boolean sendTelegramVideo(@ActionInput(name = "videoURL") @Nullable String videoURL,
516             @ActionInput(name = "caption") @Nullable String caption) {
517         TelegramHandler localHandler = handler;
518         if (localHandler != null) {
519             for (Long chatId : localHandler.getReceiverChatIds()) {
520                 if (!sendTelegramVideo(chatId, videoURL, caption)) {
521                     return false;
522                 }
523             }
524         }
525         return true;
526     }
527
528     @RuleAction(label = "send video", description = "Send a Video using the Telegram API.")
529     public boolean sendTelegramVideo(@ActionInput(name = "chatId") @Nullable Long chatId,
530             @ActionInput(name = "videoURL") @Nullable String videoURL,
531             @ActionInput(name = "caption") @Nullable String caption) {
532         final SendVideo sendVideo;
533         if (videoURL == null) {
534             logger.warn("Video URL not defined; unable to retrieve video for sending.");
535             return false;
536         }
537         if (chatId == null) {
538             logger.warn("chatId not defined; action skipped.");
539             return false;
540         }
541         TelegramHandler localHandler = handler;
542         if (localHandler != null) {
543             if (videoURL.toLowerCase().startsWith("http")) {
544                 logger.debug("Video http://URL provided.");
545                 HttpClient client = localHandler.getClient();
546                 if (client == null) {
547                     return false;
548                 }
549                 Request request = client.newRequest(videoURL).method(HttpMethod.GET).timeout(30, TimeUnit.SECONDS);
550                 try {
551                     // 50mb limit to file size
552                     FutureResponseListener listener = new FutureResponseListener(request, 50 * 1024 * 1024);
553                     request.send(listener);
554                     ContentResponse contentResponse = listener.get();
555                     if (contentResponse.getStatus() == 200) {
556                         byte[] fileContent = contentResponse.getContent();
557                         sendVideo = new SendVideo(chatId, fileContent);
558                     } else {
559                         logger.warn("Download from {} failed with status: {}", videoURL, contentResponse.getStatus());
560                         sendTelegram(chatId, caption + ":Download failed with status " + contentResponse.getStatus());
561                         return false;
562                     }
563                 } catch (InterruptedException | ExecutionException e) {
564                     logger.warn("Download from {} failed with exception: {}", videoURL, e.getMessage());
565                     return false;
566                 }
567             } else {
568                 String temp = videoURL;
569                 if (!videoURL.toLowerCase().startsWith("file:")) {
570                     temp = "file://" + videoURL;
571                 }
572                 // Load video from local file system with file://path
573                 logger.debug("Read file from local file: {}", videoURL);
574                 try {
575                     sendVideo = new SendVideo(chatId, Path.of(new URL(temp).getPath()).toFile());
576                 } catch (MalformedURLException e) {
577                     logger.warn("Malformed URL, should start with http or file: {}", videoURL);
578                     return false;
579                 }
580             }
581             if (caption != null) {
582                 sendVideo.caption(caption);
583             }
584             if (localHandler.getParseMode() != null) {
585                 sendVideo.parseMode(localHandler.getParseMode());
586             }
587             return evaluateResponse(localHandler.execute(sendVideo));
588         }
589         return false;
590     }
591
592     // legacy delegate methods
593     /* APIs without chatId parameter */
594     public static boolean sendTelegram(ThingActions actions, @Nullable String format, @Nullable Object... args) {
595         return ((TelegramActions) actions).sendTelegram(format, args);
596     }
597
598     public static boolean sendTelegramQuery(ThingActions actions, @Nullable String message, @Nullable String replyId,
599             @Nullable String... buttons) {
600         return ((TelegramActions) actions).sendTelegramQuery(message, replyId, buttons);
601     }
602
603     public static boolean sendTelegramPhoto(ThingActions actions, @Nullable String photoURL, @Nullable String caption) {
604         return ((TelegramActions) actions).sendTelegramPhoto(photoURL, caption, null, null);
605     }
606
607     public static boolean sendTelegramPhoto(ThingActions actions, @Nullable String photoURL, @Nullable String caption,
608             @Nullable String username, @Nullable String password) {
609         return ((TelegramActions) actions).sendTelegramPhoto(photoURL, caption, username, password);
610     }
611
612     public static boolean sendTelegramAnimation(ThingActions actions, @Nullable String animationURL,
613             @Nullable String caption) {
614         return ((TelegramActions) actions).sendTelegramVideo(animationURL, caption);
615     }
616
617     public static boolean sendTelegramVideo(ThingActions actions, @Nullable String videoURL, @Nullable String caption) {
618         return ((TelegramActions) actions).sendTelegramVideo(videoURL, caption);
619     }
620
621     public static boolean sendTelegramAnswer(ThingActions actions, @Nullable String replyId, @Nullable String message) {
622         return ((TelegramActions) actions).sendTelegramAnswer(replyId, message);
623     }
624
625     /* APIs with chatId parameter */
626
627     public static boolean sendTelegram(ThingActions actions, @Nullable Long chatId, @Nullable String format,
628             @Nullable Object... args) {
629         return ((TelegramActions) actions).sendTelegram(chatId, format, args);
630     }
631
632     public static boolean sendTelegramQuery(ThingActions actions, @Nullable Long chatId, @Nullable String message,
633             @Nullable String replyId, @Nullable String... buttons) {
634         return ((TelegramActions) actions).sendTelegramQuery(chatId, message, replyId, buttons);
635     }
636
637     public static boolean sendTelegramPhoto(ThingActions actions, @Nullable Long chatId, @Nullable String photoURL,
638             @Nullable String caption) {
639         return ((TelegramActions) actions).sendTelegramPhoto(chatId, photoURL, caption, null, null);
640     }
641
642     public static boolean sendTelegramPhoto(ThingActions actions, @Nullable Long chatId, @Nullable String photoURL,
643             @Nullable String caption, @Nullable String username, @Nullable String password) {
644         return ((TelegramActions) actions).sendTelegramPhoto(chatId, photoURL, caption, username, password);
645     }
646
647     public static boolean sendTelegramAnimation(ThingActions actions, @Nullable Long chatId,
648             @Nullable String animationURL, @Nullable String caption) {
649         return ((TelegramActions) actions).sendTelegramVideo(chatId, animationURL, caption);
650     }
651
652     public static boolean sendTelegramVideo(ThingActions actions, @Nullable Long chatId, @Nullable String videoURL,
653             @Nullable String caption) {
654         return ((TelegramActions) actions).sendTelegramVideo(chatId, videoURL, caption);
655     }
656
657     public static boolean sendTelegramAnswer(ThingActions actions, @Nullable Long chatId, @Nullable String replyId,
658             @Nullable String message) {
659         return ((TelegramActions) actions).sendTelegramAnswer(chatId, replyId, message);
660     }
661
662     public static boolean sendTelegramAnswer(ThingActions actions, @Nullable String chatId, @Nullable String replyId,
663             @Nullable String message) {
664         if (actions instanceof TelegramActions) {
665             if (chatId == null) {
666                 return false;
667             }
668             return ((TelegramActions) actions).sendTelegramAnswer(Long.valueOf(chatId), replyId, message);
669         } else {
670             throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
671         }
672     }
673
674     public static boolean sendTelegramAnswer(ThingActions actions, @Nullable Long chatId, @Nullable String callbackId,
675             @Nullable Long messageId, @Nullable String message) {
676         return ((TelegramActions) actions).sendTelegramAnswer(chatId, callbackId, messageId, message);
677     }
678
679     public static boolean sendTelegramAnswer(ThingActions actions, @Nullable String chatId, @Nullable String callbackId,
680             @Nullable String messageId, @Nullable String message) {
681         if (actions instanceof TelegramActions) {
682             if (chatId == null) {
683                 return false;
684             }
685             return ((TelegramActions) actions).sendTelegramAnswer(Long.valueOf(chatId), callbackId,
686                     messageId != null ? Long.parseLong(messageId) : null, message);
687         } else {
688             throw new IllegalArgumentException("Actions is not an instance of TelegramActions");
689         }
690     }
691
692     @Override
693     public void setThingHandler(@Nullable ThingHandler handler) {
694         this.handler = (TelegramHandler) handler;
695     }
696
697     @Override
698     public @Nullable ThingHandler getThingHandler() {
699         return handler;
700     }
701 }