diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java index 4b0c03fd..d4d2c2ea 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java @@ -260,13 +260,14 @@ public abstract class AbilityBot extends TelegramLongPollingBot { * Gets the user with the specified username. If user was not found, the bot will send a message on Telegram. * * @param username the username of the required user + * @param ctx the message context with the originating user * @return the id of the user */ - protected int getUserIdSendError(String username, long chatId) { + protected int getUserIdSendError(String username, MessageContext ctx) { try { return getUser(username).getId(); } catch (IllegalStateException ex) { - silent.send(getLocalizedMessage(USER_NOT_FOUND, "", username), chatId); // TODO how to retrieve language? + silent.send(getLocalizedMessage(USER_NOT_FOUND, ctx.user().getLanguageCode(), username), ctx.chatId()); throw propagate(ex); } } @@ -361,21 +362,24 @@ public abstract class AbilityBot extends TelegramLongPollingBot { .action(ctx -> silent.forceReply( getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().getLanguageCode()), ctx.chatId())) .reply(update -> { - Long chatId = update.getMessage().getChatId(); - String fileId = update.getMessage().getDocument().getFileId(); + String replyToMsg = update.getMessage().getReplyToMessage().getText(); + String recoverMessage = getLocalizedMessage(ABILITY_RECOVER_MESSAGE, AbilityUtils.getUser(update).getLanguageCode()); + if (!replyToMsg.equals(recoverMessage)) + return; + String fileId = update.getMessage().getDocument().getFileId(); try (FileReader reader = new FileReader(downloadFileWithId(fileId))) { String backupData = IOUtils.toString(reader); if (db.recover(backupData)) { - send(ABILITY_RECOVER_SUCCESS, update, chatId); + send(ABILITY_RECOVER_SUCCESS, update); } else { - send(ABILITY_RECOVER_FAIL, update, chatId); + send(ABILITY_RECOVER_FAIL, update); } } catch (Exception e) { BotLogger.error("Could not recover DB from backup", TAG, e); - send(ABILITY_RECOVER_ERROR, update, chatId); + send(ABILITY_RECOVER_ERROR, update); } - }, MESSAGE, DOCUMENT, REPLY, isReplyTo(getLocalizedMessage(ABILITY_RECOVER_SUCCESS, ""))) // TODO how to retrieve language? + }, MESSAGE, DOCUMENT, REPLY) .build(); } @@ -396,7 +400,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { .input(1) .action(ctx -> { String username = stripTag(ctx.firstArg()); - int userId = getUserIdSendError(username, ctx.chatId()); + int userId = getUserIdSendError(username, ctx); String bannedUser; // Protection from abuse @@ -432,7 +436,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { .input(1) .action(ctx -> { String username = stripTag(ctx.firstArg()); - Integer userId = getUserIdSendError(username, ctx.chatId()); + Integer userId = getUserIdSendError(username, ctx); Set blacklist = blacklist(); @@ -457,7 +461,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { .input(1) .action(ctx -> { String username = stripTag(ctx.firstArg()); - Integer userId = getUserIdSendError(username, ctx.chatId()); + Integer userId = getUserIdSendError(username, ctx); Set admins = admins(); if (admins.contains(userId)) @@ -481,7 +485,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { .input(1) .action(ctx -> { String username = stripTag(ctx.firstArg()); - Integer userId = getUserIdSendError(username, ctx.chatId()); + Integer userId = getUserIdSendError(username, ctx); Set admins = admins(); if (admins.remove(userId)) { @@ -533,7 +537,8 @@ public abstract class AbilityBot extends TelegramLongPollingBot { return silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId()); } - private Optional send(String message, Update upd, Long chatId) { + private Optional send(String message, Update upd) { + Long chatId = upd.getMessage().getChatId(); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); } diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java index fdb8f92c..808b4eda 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java @@ -12,7 +12,8 @@ import org.telegram.telegrambots.api.objects.User; import java.io.IOException; import static org.apache.commons.lang3.StringUtils.EMPTY; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.internal.verification.VerificationModeFactory.times; import static org.telegram.abilitybots.api.bot.AbilityBotTest.mockContext; import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance; @@ -37,6 +38,7 @@ public class AbilityBotI18nTest { bot.sender = sender; bot.silent = silent; + } @Test @@ -59,7 +61,6 @@ public class AbilityBotI18nTest { .send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId()); } - @After public void tearDown() throws IOException { db.clear(); @@ -68,13 +69,13 @@ public class AbilityBotI18nTest { public static class NoPublicCommandsBot extends AbilityBot { - protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) { - super(botToken, botUsername, db); - } + protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) { + super(botToken, botUsername, db); + } - @Override - public int creatorId() { - return 0; - } + @Override + public int creatorId() { + return 1; + } } }