From e616ebbddb754c5b86a462cae8d40e1998e13244 Mon Sep 17 00:00:00 2001 From: "j.r@wiuwiu.de" Date: Thu, 27 Jun 2019 20:51:03 +0200 Subject: [PATCH 1/4] Made usernames case insensitive Fixed because username in Telegram are not case sensitive, eg @fooBar is the same user as @Foobar --- .../extensions/bots/commandbot/commands/CommandRegistry.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java index fdd3771b..f06e15fb 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistry.java @@ -8,6 +8,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; +import java.util.regex.Pattern; /** * This class manages all the commands for a bot. You can register and deregister commands on demand @@ -122,7 +123,7 @@ public final class CommandRegistry implements ICommandRegistry { */ private String removeUsernameFromCommandIfNeeded(String command) { if (allowCommandsWithUsername) { - return command.replace("@" + botUsername, "").trim(); + return command.replaceAll("(?i)@" + Pattern.quote(botUsername), "").trim(); } return command; } From 01a7e7dc2120fbe010b1626661003fe692b3b53e Mon Sep 17 00:00:00 2001 From: "j.r@wiuwiu.de" Date: Thu, 27 Jun 2019 20:58:16 +0200 Subject: [PATCH 2/4] Fixed bot username Let the programmer of the bot decide where the username should come from --- .../bots/commandbot/TelegramLongPollingCommandBot.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java index 8bb39742..88f8b5c5 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java @@ -58,7 +58,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername, String botUsername) { super(options); this.botUsername = botUsername; - this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, botUsername); + this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this.getBotUsername()); } @Override @@ -143,9 +143,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB * @return Bot username */ @Override - public final String getBotUsername() { - return botUsername; - } + public abstract String getBotUsername(); /** * Process all updates, that are not commands. From 91508aeebbdead012bf698b725d180d3c674f7f1 Mon Sep 17 00:00:00 2001 From: "j.r@wiuwiu.de" Date: Thu, 27 Jun 2019 21:06:08 +0200 Subject: [PATCH 3/4] Removed unnecesarry botUsername argument --- .../commandbot/TelegramLongPollingCommandBot.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java index 88f8b5c5..05de2c72 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java @@ -22,16 +22,14 @@ import java.util.function.BiConsumer; */ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry { private final CommandRegistry commandRegistry; - private String botUsername; /** * Creates a TelegramLongPollingCommandBot using default options * Use ICommandRegistry's methods on this bot to register commands * - * @param botUsername Username of the bot */ - public TelegramLongPollingCommandBot(String botUsername) { - this(ApiContext.getInstance(DefaultBotOptions.class), botUsername); + public TelegramLongPollingCommandBot() { + this(ApiContext.getInstance(DefaultBotOptions.class)); } /** @@ -40,10 +38,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB * Use ICommandRegistry's methods on this bot to register commands * * @param options Bot options - * @param botUsername Username of the bot */ - public TelegramLongPollingCommandBot(DefaultBotOptions options, String botUsername) { - this(options, true, botUsername); + public TelegramLongPollingCommandBot(DefaultBotOptions options) { + this(options, true); } /** @@ -53,11 +50,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB * @param options Bot options * @param allowCommandsWithUsername true to allow commands with parameters (default), * false otherwise - * @param botUsername bot username of this bot */ - public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername, String botUsername) { + public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) { super(options); - this.botUsername = botUsername; this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this.getBotUsername()); } From e526b9db142624c47e168c766b36b0357160ab68 Mon Sep 17 00:00:00 2001 From: "j.r@wiuwiu.de" Date: Sun, 13 Oct 2019 15:12:22 +0200 Subject: [PATCH 4/4] Added deprecated constructor with the old style --- .../TelegramLongPollingCommandBot.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java index 05de2c72..1796b364 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramLongPollingCommandBot.java @@ -22,6 +22,7 @@ import java.util.function.BiConsumer; */ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry { private final CommandRegistry commandRegistry; + private String botUsername; /** * Creates a TelegramLongPollingCommandBot using default options @@ -32,6 +33,19 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB this(ApiContext.getInstance(DefaultBotOptions.class)); } + /** + * Creates a TelegramLongPollingCommandBot using default options + * Use ICommandRegistry's methods on this bot to register commands + * + * @param botUsername Username of the bot + * @deprecated Overwrite {@link #getBotUsername() getBotUsername} instead + */ + @Deprecated + public TelegramLongPollingCommandBot(String botUsername){ + this(); + this.botUsername = botUsername; + } + /** * Creates a TelegramLongPollingCommandBot with custom options and allowing commands with * usernames @@ -138,7 +152,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB * @return Bot username */ @Override - public abstract String getBotUsername(); + public String getBotUsername(){ + return this.botUsername; + }; /** * Process all updates, that are not commands.