Fix todos, all ability bot messages are now properly localized

This commit is contained in:
Abbas Abou Daya 2018-05-22 03:12:47 -04:00
parent a819d7f178
commit 7d216a1fae
2 changed files with 28 additions and 22 deletions

View File

@ -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. * 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 username the username of the required user
* @param ctx the message context with the originating user
* @return the id of the user * @return the id of the user
*/ */
protected int getUserIdSendError(String username, long chatId) { protected int getUserIdSendError(String username, MessageContext ctx) {
try { try {
return getUser(username).getId(); return getUser(username).getId();
} catch (IllegalStateException ex) { } 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); throw propagate(ex);
} }
} }
@ -361,21 +362,24 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
.action(ctx -> silent.forceReply( .action(ctx -> silent.forceReply(
getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().getLanguageCode()), ctx.chatId())) getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().getLanguageCode()), ctx.chatId()))
.reply(update -> { .reply(update -> {
Long chatId = update.getMessage().getChatId(); String replyToMsg = update.getMessage().getReplyToMessage().getText();
String fileId = update.getMessage().getDocument().getFileId(); 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))) { try (FileReader reader = new FileReader(downloadFileWithId(fileId))) {
String backupData = IOUtils.toString(reader); String backupData = IOUtils.toString(reader);
if (db.recover(backupData)) { if (db.recover(backupData)) {
send(ABILITY_RECOVER_SUCCESS, update, chatId); send(ABILITY_RECOVER_SUCCESS, update);
} else { } else {
send(ABILITY_RECOVER_FAIL, update, chatId); send(ABILITY_RECOVER_FAIL, update);
} }
} catch (Exception e) { } catch (Exception e) {
BotLogger.error("Could not recover DB from backup", TAG, 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(); .build();
} }
@ -396,7 +400,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
.input(1) .input(1)
.action(ctx -> { .action(ctx -> {
String username = stripTag(ctx.firstArg()); String username = stripTag(ctx.firstArg());
int userId = getUserIdSendError(username, ctx.chatId()); int userId = getUserIdSendError(username, ctx);
String bannedUser; String bannedUser;
// Protection from abuse // Protection from abuse
@ -432,7 +436,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
.input(1) .input(1)
.action(ctx -> { .action(ctx -> {
String username = stripTag(ctx.firstArg()); String username = stripTag(ctx.firstArg());
Integer userId = getUserIdSendError(username, ctx.chatId()); Integer userId = getUserIdSendError(username, ctx);
Set<Integer> blacklist = blacklist(); Set<Integer> blacklist = blacklist();
@ -457,7 +461,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
.input(1) .input(1)
.action(ctx -> { .action(ctx -> {
String username = stripTag(ctx.firstArg()); String username = stripTag(ctx.firstArg());
Integer userId = getUserIdSendError(username, ctx.chatId()); Integer userId = getUserIdSendError(username, ctx);
Set<Integer> admins = admins(); Set<Integer> admins = admins();
if (admins.contains(userId)) if (admins.contains(userId))
@ -481,7 +485,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
.input(1) .input(1)
.action(ctx -> { .action(ctx -> {
String username = stripTag(ctx.firstArg()); String username = stripTag(ctx.firstArg());
Integer userId = getUserIdSendError(username, ctx.chatId()); Integer userId = getUserIdSendError(username, ctx);
Set<Integer> admins = admins(); Set<Integer> admins = admins();
if (admins.remove(userId)) { 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()); return silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId());
} }
private Optional<Message> send(String message, Update upd, Long chatId) { private Optional<Message> send(String message, Update upd) {
Long chatId = upd.getMessage().getChatId();
return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId);
} }

View File

@ -12,7 +12,8 @@ import org.telegram.telegrambots.api.objects.User;
import java.io.IOException; import java.io.IOException;
import static org.apache.commons.lang3.StringUtils.EMPTY; 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.mockito.internal.verification.VerificationModeFactory.times;
import static org.telegram.abilitybots.api.bot.AbilityBotTest.mockContext; import static org.telegram.abilitybots.api.bot.AbilityBotTest.mockContext;
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance; import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
@ -37,6 +38,7 @@ public class AbilityBotI18nTest {
bot.sender = sender; bot.sender = sender;
bot.silent = silent; bot.silent = silent;
} }
@Test @Test
@ -59,7 +61,6 @@ public class AbilityBotI18nTest {
.send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId()); .send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId());
} }
@After @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
db.clear(); db.clear();
@ -68,13 +69,13 @@ public class AbilityBotI18nTest {
public static class NoPublicCommandsBot extends AbilityBot { public static class NoPublicCommandsBot extends AbilityBot {
protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) { protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) {
super(botToken, botUsername, db); super(botToken, botUsername, db);
} }
@Override @Override
public int creatorId() { public int creatorId() {
return 0; return 1;
} }
} }
} }