diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index 6a81f224..e76e0a06 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -63,5 +63,7 @@ ### 3.0.2 ### 1. Bug Fixing: #250 2. Added new module `telegrambots-extensions` that should contains any extensions of the API such as CommandBot or TimedBot. +3. `TelegramLongPollingCommandBot` receives now the bot username as constructor parameters, all deprecated constructors will be removed in next mayor release. +4. `getUsername` method from `TelegramLongPollingCommandBot` can be considered `final` and will be so in next mayor release. **[[How to update to version 3.0.2|How-To-Update#3.0.2]]** diff --git a/TelegramBots.wiki/How-To-Update.md b/TelegramBots.wiki/How-To-Update.md index 4ebb7fd9..a09bfae3 100644 --- a/TelegramBots.wiki/How-To-Update.md +++ b/TelegramBots.wiki/How-To-Update.md @@ -26,4 +26,5 @@ 1. In `Message` object, field `new_chat_member` was replaced by `new_chat_members` that is now an array of users. ### To version 3.0.2 ### -1. If you were using `TelegramLongPollingCommandBot`, add the new [extensions dependency](https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-extensions) to your maven and fix import statements in your project. \ No newline at end of file +1. If you were using `TelegramLongPollingCommandBot`, add the new [extensions dependency](https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-extensions) to your maven and fix import statements in your project. +2. If you were using `TelegramLongPollingCommandBot`, make sure you start using constructors with username and prevent overriding `getUsername` method. \ No newline at end of file diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java index 12ef2bec..428e8279 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/bots/commandbot/TelegramLongPollingCommandBot.java @@ -22,32 +22,63 @@ 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 + * + * @deprecated Uses {@link #TelegramLongPollingCommandBot(String)} instead */ + @Deprecated public TelegramLongPollingCommandBot() { 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 + */ + public TelegramLongPollingCommandBot(String botUsername) { + this(ApiContext.getInstance(DefaultBotOptions.class), botUsername); + } + /** * Creates a TelegramLongPollingCommandBot with custom options and allowing commands with * usernames * Use ICommandRegistry's methods on this bot to register commands * @param options Bot options + * + * @deprecated Use {@link #TelegramLongPollingCommandBot(DefaultBotOptions, String)} instead */ + @Deprecated public TelegramLongPollingCommandBot(DefaultBotOptions options) { this(options, true); } + /** + * Creates a TelegramLongPollingCommandBot with custom options and allowing commands with + * usernames + * 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); + } + /** * Creates a TelegramLongPollingCommandBot * Use ICommandRegistry's methods on this bot to register commands * @param options Bot options * @param allowCommandsWithUsername true to allow commands with parameters (default), * false otherwise + * + * @deprecated Use {@link #TelegramLongPollingCommandBot(DefaultBotOptions, boolean, String)} instead */ + @Deprecated public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) { super(options); this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, getBotUsername()); @@ -63,6 +94,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); } @@ -131,6 +163,15 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB return commandRegistry.getRegisteredCommand(commandIdentifier); } + /** + * TODO This method will become final in next mayor release, avoid overriding it + * @return Bot username + */ + @Override + public String getBotUsername() { + return botUsername; + } + /** * Process all updates, that are not commands. * @warning Commands that have valid syntax but are not registered on this bot,