]> git.basschouten.com Git - openhab-addons.git/commitdiff
[telegram] Functionality added to delete a Query (#16631)
authorChristoph <36006493+fd0cwp@users.noreply.github.com>
Thu, 11 Apr 2024 20:02:52 +0000 (22:02 +0200)
committerGitHub <noreply@github.com>
Thu, 11 Apr 2024 20:02:52 +0000 (22:02 +0200)
* [telegram] Functionality added to delete a Query

Signed-off-by: Christoph <fd0cwp@gmx.de>
bundles/org.openhab.binding.telegram/README.md
bundles/org.openhab.binding.telegram/src/main/java/org/openhab/binding/telegram/internal/action/TelegramActions.java

index b24517f2c25d315e3284b0ce12324c1b15710067..00b82b7cd120e2742c6111c9155a17095d3c4c63 100644 (file)
@@ -197,8 +197,9 @@ These actions will send a message to all chat ids configured for this bot.
 |----------------------------|--------------|
 | sendTelegram(String message) | Sends a message. |
 | sendTelegram(String format, Object... args)          | Sends a formatted message (See <https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Formatter.html> for more information).
-| sendTelegramQuery(String message, String replyId, String... buttons) | Sends a question to the user that can be answered via the defined buttons. The replyId can be freely choosen and is sent back with the answer. Then, the id is required to identify what question has been answered (e.g. in case of multiple open questions). The final result looks like this: ![Telegram Inline Keyboard](doc/queryExample.png). |
+| sendTelegramQuery(String message, String replyId, String... buttons) | Sends a question to the user that can be answered via the defined buttons. The replyId can be freely choosen and is sent back with the answer. Then, the id is required to identify what question has been answered (e.g. in case of multiple open questions). The final result looks like this: ![Telegram Inline Keyboard](doc/queryExample.png) |
 | sendTelegramAnswer(String replyId, String message) | Sends a message after the user has answered a question. You should _always_ call this method after you received an answer. It will remove buttons from the specific question and will also stop the progress bar displayed at the client side. If no message is necessary, just pass `null` here. |
+| deleteTelegramQuery(String replyId) | Deletes a question in the chat. The replyId must be the same as used for the corresponding sendTelegramQuery() action. |
 | sendTelegramPhoto(String photoURL, String caption) | Sends a picture. Can be one of the URL formats, see the Note below, or a base64 encoded image (simple base64 data or data URI scheme). |
 | sendTelegramPhoto(String photoURL, String caption, String username, String password) | Sends a picture which is downloaded from a username/password protected http/https address. |
 | sendTelegramAnimation(String animationURL, String caption) | Send animation files either GIF or H.264/MPEG-4 AVC video without sound. |
index 4165895a11aa7e4aa8058a13b76c91c6559fd9f9..48161074c85a17543110d3aae47924341ea115d7 100644 (file)
@@ -50,6 +50,7 @@ import org.slf4j.LoggerFactory;
 import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
 import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
 import com.pengrad.telegrambot.request.AnswerCallbackQuery;
+import com.pengrad.telegrambot.request.DeleteMessage;
 import com.pengrad.telegrambot.request.EditMessageReplyMarkup;
 import com.pengrad.telegrambot.request.SendAnimation;
 import com.pengrad.telegrambot.request.SendMessage;
@@ -205,7 +206,7 @@ public class TelegramActions implements ThingActions {
         return true;
     }
 
-    @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
+    @RuleAction(label = "send a query", description = "Send a Telegram Query using the Telegram API.")
     public boolean sendTelegramQuery(@ActionInput(name = "chatId") @Nullable Long chatId,
             @ActionInput(name = "message") @Nullable String message,
             @ActionInput(name = "replyId") @Nullable String replyId,
@@ -213,7 +214,7 @@ public class TelegramActions implements ThingActions {
         return sendTelegramGeneral(chatId, message, replyId, buttons);
     }
 
-    @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
+    @RuleAction(label = "send a query", description = "Send a Telegram Query using the Telegram API.")
     public boolean sendTelegramQuery(@ActionInput(name = "message") @Nullable String message,
             @ActionInput(name = "replyId") @Nullable String replyId,
             @ActionInput(name = "buttons") @Nullable String... buttons) {
@@ -283,6 +284,39 @@ public class TelegramActions implements ThingActions {
         return false;
     }
 
+    @RuleAction(label = "delete a query", description = "Delete a Query using the Telegram API.")
+    public boolean deleteTelegramQuery(@ActionInput(name = "replyId") @Nullable String replyId) {
+        if (replyId == null) {
+            logger.warn("deleteTelegramQuery() - replyId not passed!");
+            return false;
+        }
+        TelegramHandler localHandler = handler;
+        if (localHandler == null) {
+            logger.debug("deleteTelegramQuery() - localHandler is null!");
+            return false;
+        }
+
+        Integer messageId = 0;
+        BaseResponse response = null;
+
+        for (Long chatId : localHandler.getReceiverChatIds()) {
+            messageId = localHandler.removeMessageId(chatId, replyId);
+            if (messageId == null) {
+                logger.debug("deleteTelegramQuery() - messageId not found!");
+                return false;
+            }
+            DeleteMessage deleteMessage = new DeleteMessage(chatId, messageId);
+            response = localHandler.execute(deleteMessage);
+
+            if (response == null || response.errorCode() != 0) {
+                logger.debug("deleteTelegramQuery() - DeleteMessage execution not successful! Response: {}", response);
+                return false;
+            }
+        }
+
+        return true;
+    } // public boolean deleteTelegramQuery(String replyId)
+
     @RuleAction(label = "send a message", description = "Send a Telegram using the Telegram API.")
     public boolean sendTelegram(@ActionInput(name = "chatId") @Nullable Long chatId,
             @ActionInput(name = "message") @Nullable String message,
@@ -636,6 +670,14 @@ public class TelegramActions implements ThingActions {
         return ((TelegramActions) actions).sendTelegramAnswer(replyId, message);
     }
 
+    public static boolean deleteTelegramQuery(ThingActions actions, @Nullable String replyId) {
+        if (actions instanceof TelegramActions telegramActions) {
+            return telegramActions.deleteTelegramQuery(replyId);
+        } else {
+            throw new IllegalArgumentException("Instance is not a TelegramActions class.");
+        }
+    }
+
     /* APIs with chatId parameter */
 
     public static boolean sendTelegram(ThingActions actions, @Nullable Long chatId, @Nullable String format,