From 903e803745db9feb67ebf2cd1ea485a1890297a4 Mon Sep 17 00:00:00 2001 From: shayaantx <5449086+shayaantx@users.noreply.github.com> Date: Sat, 2 Jul 2022 21:35:20 -0400 Subject: [PATCH] Add button to telegram for one click add of content (#80) --- .../clients/telegram/TelegramBootstrap.java | 24 ++++++++++++++--- .../telegram/TelegramCallbackData.java | 14 ++++++++++ .../clients/telegram/TelegramChatClient.java | 17 +++++++----- .../clients/telegram/TelegramResponse.java | 11 ++++++++ .../telegram/TelegramResponseBuilder.java | 27 ++++++++++++++----- src/main/resources/version.txt | 2 +- 6 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/botdarr/clients/telegram/TelegramCallbackData.java diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java b/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java index 6a85674..41d35bd 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramBootstrap.java @@ -5,14 +5,14 @@ import com.botdarr.clients.ChatClientResponseBuilder; import com.botdarr.commands.CommandContext; import com.botdarr.scheduling.Scheduler; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Splitter; import com.google.common.collect.Sets; import com.pengrad.telegrambot.UpdatesListener; +import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; import org.apache.logging.log4j.util.Strings; -import java.util.HashMap; -import java.util.Map; import java.util.Properties; import java.util.Set; @@ -32,14 +32,30 @@ public void init() throws Exception { try { for (Update update : list) { com.pengrad.telegrambot.model.Message message = isUsingChannels() ? update.channelPost() : update.message(); + final String text; + final Chat chat; if (message != null && !Strings.isEmpty(message.text())) { - String text = message.text(); + // regular commands + text = message.text(); + chat = message.chat(); + } else if (!Strings.isEmpty(update.callbackQuery().data())) { + // interactive commands with callback data + ObjectMapper mapper = new ObjectMapper(); + TelegramCallbackData callbackData = mapper.readValue(update.callbackQuery().data(), TelegramCallbackData.class); + text = callbackData.getCommand(); + chat = update.callbackQuery().message().chat(); + } else { + // couldn't find a message or a chat to reply to + text = ""; + chat = null; + } + if (!Strings.isEmpty(text) && chat != null) { //TODO: the telegram api doesn't seem return "from" field in channel posts for some reason //for now we leave the author as "telegram" till a better solution arises String author = "telegram"; Scheduler.getScheduler().executeCommand(() -> { TelegramBootstrap.this.runAndProcessCommands(CommandContext.getConfig().getPrefix(), text, author, responseChatClientResponseBuilder, chatClientResponse -> { - telegramChatClient.sendMessage(chatClientResponse, message.chat()); + telegramChatClient.sendMessage(chatClientResponse, chat); }); return null; }); diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramCallbackData.java b/src/main/java/com/botdarr/clients/telegram/TelegramCallbackData.java new file mode 100644 index 0000000..9741c93 --- /dev/null +++ b/src/main/java/com/botdarr/clients/telegram/TelegramCallbackData.java @@ -0,0 +1,14 @@ +package com.botdarr.clients.telegram; + +public class TelegramCallbackData { + private String command; + + public TelegramCallbackData() {} + public TelegramCallbackData(String command) { + this.command = command; + } + + public String getCommand() { + return command; + } +} diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java b/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java index 2ee2167..a9738f6 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramChatClient.java @@ -7,7 +7,7 @@ import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.UpdatesListener; import com.pengrad.telegrambot.model.Chat; -import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.model.request.*; import com.pengrad.telegrambot.request.GetChat; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.SendResponse; @@ -31,14 +31,14 @@ public void addUpdateListener(UpdatesListener updatesListener) { public void sendMessage(TelegramResponse telegramResponse, Chat chat) { sendMessages(chatChannel -> { - sendTelegramMessage(chatChannel.id(), telegramResponse.getHtml()); + sendTelegramMessage(chatChannel.id(), telegramResponse.getHtml(), telegramResponse.getInteractiveMarkup()); }, chat); } public void sendMessage(List telegramResponses, Chat chat) { sendMessages(chatChannel -> { for (TelegramResponse telegramResponse : telegramResponses) { - sendTelegramMessage(chatChannel.id(), telegramResponse.getHtml()); + sendTelegramMessage(chatChannel.id(), telegramResponse.getHtml(), telegramResponse.getInteractiveMarkup()); } }, chat); } @@ -51,10 +51,15 @@ private String getMessageEndpoint() { return channels; } - private void sendTelegramMessage(long id, String html) { - SendResponse sendResponse = bot.execute(new SendMessage(id, html).parseMode(ParseMode.HTML)); + private void sendTelegramMessage(long id, String html, Keyboard interactiveMarkup) { + final SendResponse sendResponse; + if (interactiveMarkup != null) { + sendResponse = bot.execute(new SendMessage(id, html).parseMode(ParseMode.HTML).replyMarkup(interactiveMarkup)); + } else { + sendResponse = bot.execute(new SendMessage(id, html).parseMode(ParseMode.HTML)); + } if (LOGGER.isDebugEnabled() && sendResponse.errorCode() == 0) { - LOGGER.debug("send response =" + sendResponse.toString()); + LOGGER.debug("send response =" + sendResponse); } if (sendResponse.errorCode() > 0) { LOGGER.error("Error sending response", sendResponse.toString()); diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramResponse.java b/src/main/java/com/botdarr/clients/telegram/TelegramResponse.java index 48d455e..5952baf 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramResponse.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramResponse.java @@ -1,13 +1,19 @@ package com.botdarr.clients.telegram; import com.botdarr.clients.ChatClientResponse; +import com.pengrad.telegrambot.model.request.Keyboard; import j2html.tags.DomContent; import java.util.List; public class TelegramResponse implements ChatClientResponse { public TelegramResponse(List containerTags) { + this(containerTags, null); + } + + public TelegramResponse(List containerTags, Keyboard interactiveMarkup) { this.domContent = containerTags; + this.interactiveMarkup = interactiveMarkup; } public String getHtml() { @@ -18,5 +24,10 @@ public String getHtml() { return stringBuilder.toString(); } + public Keyboard getInteractiveMarkup() { + return this.interactiveMarkup; + } + + private final Keyboard interactiveMarkup; private final List domContent; } diff --git a/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java b/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java index c238f2f..06b3da8 100644 --- a/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java +++ b/src/main/java/com/botdarr/clients/telegram/TelegramResponseBuilder.java @@ -11,6 +11,10 @@ import com.botdarr.commands.*; import com.botdarr.commands.responses.*; import com.botdarr.utilities.ListUtils; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.pengrad.telegrambot.model.request.InlineKeyboardButton; +import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; import j2html.tags.DomContent; import org.apache.logging.log4j.util.Strings; @@ -232,9 +236,8 @@ public TelegramResponse build(NewShowResponse newShowResponse) { List domContents = new ArrayList<>(); domContents.add(b("*Title* - " + sonarrShow.getTitle())); domContents.add(code("TvdbId - " + sonarrShow.getTvdbId())); - domContents.add(u(ADD_SHOW_COMMAND_FIELD_PREFIX + " - " + SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId()))); domContents.add(a(sonarrShow.getRemoteImage())); - return new TelegramResponse(domContents); + return getAddResponse(domContents, SonarrCommands.getAddShowCommandStr(sonarrShow.getTitle(), sonarrShow.getTvdbId())); } @Override @@ -265,9 +268,8 @@ public TelegramResponse build(NewMovieResponse newMovieResponse) { RadarrMovie lookupMovie = newMovieResponse.getRadarrMovie(); List domContents = new ArrayList<>(); domContents.add(b(lookupMovie.getTitle())); - domContents.add(u(ADD_MOVIE_COMMAND_FIELD_PREFIX + " - " + RadarrCommands.getAddMovieCommandStr(lookupMovie.getTitle(), lookupMovie.getTmdbId()))); domContents.add(a(lookupMovie.getRemoteImage())); - return new TelegramResponse(domContents); + return getAddResponse(domContents, RadarrCommands.getAddMovieCommandStr(lookupMovie.getTitle(), lookupMovie.getTmdbId())); } @Override @@ -289,9 +291,8 @@ public TelegramResponse build(NewMusicArtistResponse newMusicArtistResponse) { List domContents = new ArrayList<>(); String artistDetail = " (" + lookupArtist.getDisambiguation() + ")"; domContents.add(b(lookupArtist.getArtistName() + (Strings.isEmpty(lookupArtist.getDisambiguation()) ? "" : artistDetail))); - domContents.add(u(ADD_ARTIST_COMMAND_FIELD_PREFIX + " - " + LidarrCommands.getAddArtistCommandStr(lookupArtist.getArtistName(), lookupArtist.getForeignArtistId()))); domContents.add(a(lookupArtist.getRemoteImage())); - return new TelegramResponse(domContents); + return getAddResponse(domContents, LidarrCommands.getAddArtistCommandStr(lookupArtist.getArtistName(), lookupArtist.getForeignArtistId())); } @Override @@ -324,6 +325,20 @@ public TelegramResponse build(StatusCommandResponse statusCommandResponse) { return new TelegramResponse(domContents); } + private TelegramResponse getAddResponse(List domContents, String command) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(new TelegramCallbackData(command)); + return new TelegramResponse(domContents, new InlineKeyboardMarkup( + new InlineKeyboardButton[]{ + new InlineKeyboardButton("Add").callbackData(json), + } + )); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + private TelegramResponse getMovieResponse(RadarrMovie radarrMovie) { List domContents = new ArrayList<>(); domContents.add(b(radarrMovie.getTitle())); diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index 04edabd..c7ba1e8 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -5.4.1 \ No newline at end of file +5.5.0 \ No newline at end of file