From f4c49786d3bd308b38476c6894d2c79f84a3838c Mon Sep 17 00:00:00 2001 From: Alessio Date: Fri, 22 Nov 2024 21:12:21 +0100 Subject: [PATCH] Working on #57 Signed-off-by: Alessio --- .../AbstractSubscriptionSelectionCmd.java | 103 ++++++++++++++++++ .../thothBot/telegram/command/AssetsCmd.java | 46 ++------ 2 files changed, 111 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/devpool/thothBot/telegram/command/AbstractSubscriptionSelectionCmd.java diff --git a/src/main/java/com/devpool/thothBot/telegram/command/AbstractSubscriptionSelectionCmd.java b/src/main/java/com/devpool/thothBot/telegram/command/AbstractSubscriptionSelectionCmd.java new file mode 100644 index 00000000..a7bb583a --- /dev/null +++ b/src/main/java/com/devpool/thothBot/telegram/command/AbstractSubscriptionSelectionCmd.java @@ -0,0 +1,103 @@ +package com.devpool.thothBot.telegram.command; + +import com.devpool.thothBot.dao.data.User; +import com.devpool.thothBot.scheduler.AbstractCheckerTask; +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineKeyboardButton; +import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; +import com.pengrad.telegrambot.request.SendMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public abstract class AbstractSubscriptionSelectionCmd extends AbstractCheckerTask implements IBotCommand { + private static final Logger LOG = LoggerFactory.getLogger(AbstractSubscriptionSelectionCmd.class); + + protected final String commandPrefix; + + @Override + public boolean canTrigger(String username, String message) { + return message.trim().equals(commandPrefix); + } + + @Override + public String getCommandPrefix() { + return commandPrefix; + } + + @Override + public boolean showHelp(String username) { + return true; + } + + @Override + public String getDescription() { + return "replace me"; + } + + protected AbstractSubscriptionSelectionCmd(String commandPrefix) { + this.commandPrefix = commandPrefix; + } + + /** + * Replace this to filter the user. Default implementation does not filter anything + * + * @param user the user to filter + * @return true if not filtered, false otherwise + */ + protected boolean acceptAddressSelection(User user) { + return true; + } + + @Override + public void execute(Update update, TelegramBot bot) { + Long chatId = update.message().chat().id(); + + Map addresses = this.userDao.getUsers().stream().filter( + u -> u.getChatId().equals(chatId)) + .filter(this::acceptAddressSelection) + .collect(Collectors.toMap(User::getAddress, User::getId)); + + if (addresses.isEmpty()) { + bot.execute(new SendMessage(update.message().chat().id(), + String.format("You have not yet registered any Cardano account or address. Please try %s", SubscribeCmd.CMD_PREFIX))); + return; + } + + InlineKeyboardMarkup inlineKeyboard = new InlineKeyboardMarkup(createInlineKeyboardButtons(addresses)); + LOG.debug("Created {} inline markup rows", inlineKeyboard.inlineKeyboard().length); + bot.execute(new SendMessage(chatId, "Please select an account").replyMarkup(inlineKeyboard)); + } + + @Override + public long getCommandExecutionTimeoutSeconds() { + return 4; + } + + protected InlineKeyboardButton[][] createInlineKeyboardButtons(Map addresses) { + Map handles = getAdaHandleForAccount(addresses.keySet().toArray(new String[0])); + int inputSize = addresses.size(); + int numArrays = (int) Math.ceil((double) inputSize / 2); + InlineKeyboardButton[][] outputArray = new InlineKeyboardButton[numArrays][2]; + + for (int i = 0; i < numArrays; i++) { + int startIndex = i * 2; + int endIndex = Math.min(startIndex + 2, inputSize); + List sublist = new ArrayList<>(addresses.keySet()).subList(startIndex, endIndex); + outputArray[i] = sublist + .stream() + .map(e -> new InlineKeyboardButton(handles.get(e)) + .callbackData(createCallbackData(addresses.get(e)))) + .toArray(InlineKeyboardButton[]::new); + } + + return outputArray; + } + + protected abstract String createCallbackData(long chatId); +} diff --git a/src/main/java/com/devpool/thothBot/telegram/command/AssetsCmd.java b/src/main/java/com/devpool/thothBot/telegram/command/AssetsCmd.java index 2e311aaa..5beedef1 100644 --- a/src/main/java/com/devpool/thothBot/telegram/command/AssetsCmd.java +++ b/src/main/java/com/devpool/thothBot/telegram/command/AssetsCmd.java @@ -15,32 +15,17 @@ import java.util.stream.Collectors; @Component -public class AssetsCmd extends AbstractCheckerTask implements IBotCommand { +public class AssetsCmd extends AbstractSubscriptionSelectionCmd { private static final Logger LOG = LoggerFactory.getLogger(AssetsCmd.class); public static final String CMD_PREFIX = "/assets"; - @Override - public boolean canTrigger(String username, String message) { - return message.trim().equals(CMD_PREFIX); - } - - @Override - public String getCommandPrefix() { - return CMD_PREFIX; - } - - @Override - public boolean showHelp(String username) { - return true; - } - @Override public String getDescription() { return "Shows the assets of a specific Cardano account/address"; } public AssetsCmd() { - // Empty + super(CMD_PREFIX); } @Override @@ -62,26 +47,11 @@ public void execute(Update update, TelegramBot bot) { } @Override - public long getCommandExecutionTimeoutSeconds() { - return 4; - } - - private InlineKeyboardButton[][] createInlineKeyboardButtons(Map addresses) { - Map handles = getAdaHandleForAccount(addresses.keySet().toArray(new String[0])); - int inputSize = addresses.size(); - int numArrays = (int) Math.ceil((double) inputSize / 2); - InlineKeyboardButton[][] outputArray = new InlineKeyboardButton[numArrays][2]; - - for (int i = 0; i < numArrays; i++) { - int startIndex = i * 2; - int endIndex = Math.min(startIndex + 2, inputSize); - List sublist = addresses.keySet().stream().collect(Collectors.toList()).subList(startIndex, endIndex); - outputArray[i] = sublist.stream().map(e -> new InlineKeyboardButton(handles.get(e)) - .callbackData(AssetsListCmd.CMD_PREFIX + AssetsListCmd.CMD_DATA_SEPARATOR + addresses.get(e) - + AssetsListCmd.CMD_DATA_SEPARATOR + "0")) - .collect(Collectors.toList()).toArray(new InlineKeyboardButton[0]); - } - - return outputArray; + protected String createCallbackData(long chatId) { + return String.format("%s%s%d%s0", + AssetsListCmd.CMD_PREFIX, + AssetsListCmd.CMD_DATA_SEPARATOR, + chatId, + AssetsListCmd.CMD_DATA_SEPARATOR); } }