Skip to content

Commit

Permalink
Working on #57
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio <[email protected]>
  • Loading branch information
slux83 committed Nov 22, 2024
1 parent 1c98a66 commit f4c4978
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Long> 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<String, Long> addresses) {
Map<String, String> 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<String> 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);
}
46 changes: 8 additions & 38 deletions src/main/java/com/devpool/thothBot/telegram/command/AssetsCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,26 +47,11 @@ public void execute(Update update, TelegramBot bot) {
}

@Override
public long getCommandExecutionTimeoutSeconds() {
return 4;
}

private InlineKeyboardButton[][] createInlineKeyboardButtons(Map<String, Long> addresses) {
Map<String, String> 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<String> 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);
}
}

0 comments on commit f4c4978

Please sign in to comment.