From 402a4c46ea07b97a4b0aceac389a3491ff3290c6 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 4 Jan 2023 12:27:28 +0100 Subject: [PATCH] Pass token in constructor (#1167) --- .../abilitybots/api/bot/BaseAbilityBot.java | 10 +--- .../TelegramLongPollingSessionBot.java | 33 +++++++++++-- .../commandbot/TelegramWebhookCommandBot.java | 48 ++++++++++++++++++- .../timedbot/TimedSendLongPollingBot.java | 12 +++++ .../starter/SpringWebhookBot.java | 31 ++++++++---- ...stTelegramBotStarterRegistrationHooks.java | 15 ++---- .../telegrambots/bots/DefaultAbsSender.java | 17 ++++++- .../bots/TelegramLongPollingBot.java | 17 +++++++ .../telegrambots/bots/TelegramWebhookBot.java | 18 +++++++ .../TelegramFileDownloader.java | 1 + .../test/TelegramLongPollingBotTest.java | 8 ++-- 11 files changed, 173 insertions(+), 37 deletions(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java index 876fb5f2..ef74a500 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java @@ -127,8 +127,7 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability // Ability toggle private final AbilityToggle toggle; - // Bot token and username - private final String botToken; + // Bot username private final String botUsername; // Ability registry @@ -142,9 +141,8 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability public abstract long creatorId(); protected BaseAbilityBot(String botToken, String botUsername, DBContext db, AbilityToggle toggle, DefaultBotOptions botOptions) { - super(botOptions); + super(botOptions, botToken); - this.botToken = botToken; this.botUsername = botUsername; this.db = db; this.toggle = toggle; @@ -264,10 +262,6 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability log.info(format("[%s] Processing of update [%s] ended at %s%n---> Processing time: [%d ms] <---%n", botUsername, update.getUpdateId(), now(), processingTime)); } - public String getBotToken() { - return botToken; - } - public String getBotUsername() { return botUsername; } diff --git a/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java b/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java index 3f33fba7..35317b90 100644 --- a/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java +++ b/telegrambots-chat-session-bot/src/main/java/org/telegram/telegrambots/session/TelegramLongPollingSessionBot.java @@ -6,9 +6,9 @@ import org.apache.shiro.session.mgt.DefaultSessionManager; import org.apache.shiro.session.mgt.SessionContext; import org.apache.shiro.session.mgt.eis.AbstractSessionDAO; import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Update; -import org.telegram.telegrambots.bots.TelegramLongPollingBot; import java.util.Optional; @@ -17,23 +17,50 @@ public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingB DefaultSessionManager sessionManager; ChatIdConverter chatIdConverter; - + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public TelegramLongPollingSessionBot(){ this(new DefaultChatIdConverter()); } + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){ this(chatIdConverter, new DefaultBotOptions()); } + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, DefaultBotOptions defaultBotOptions){ - super(defaultBotOptions); + this(chatIdConverter, defaultBotOptions, null); + } + + public TelegramLongPollingSessionBot(String botToken){ + this(new DefaultChatIdConverter(), botToken); + } + + public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, String botToken){ + this(chatIdConverter, new DefaultBotOptions(), botToken); + } + + public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, DefaultBotOptions defaultBotOptions, String botToken){ + super(defaultBotOptions, botToken); this.setSessionManager(new DefaultSessionManager()); this.setChatIdConverter(chatIdConverter); AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO(); sessionDAO.setSessionIdGenerator(chatIdConverter); } + public void setSessionManager(DefaultSessionManager sessionManager) { this.sessionManager = sessionManager; } diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramWebhookCommandBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramWebhookCommandBot.java index dcc37380..9d466b3d 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramWebhookCommandBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/commandbot/TelegramWebhookCommandBot.java @@ -27,7 +27,9 @@ public abstract class TelegramWebhookCommandBot extends TelegramWebhookBot imple * Creates a TelegramWebhookCommandBot using default options * Use ICommandRegistry's methods on this bot to register commands * + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead */ + @Deprecated public TelegramWebhookCommandBot() { this(new DefaultBotOptions()); } @@ -37,12 +39,53 @@ public abstract class TelegramWebhookCommandBot extends TelegramWebhookBot imple * usernames * Use ICommandRegistry's methods on this bot to register commands * + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + * * @param options Bot options */ + @Deprecated public TelegramWebhookCommandBot(DefaultBotOptions options) { this(options, true); } + /** + * Creates a TelegramWebhookCommandBot + * Use ICommandRegistry's methods on this bot to register commands + * + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + * + * @param options Bot options + * @param allowCommandsWithUsername true to allow commands with parameters (default), + * false otherwise + */ + @Deprecated + public TelegramWebhookCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) { + super(options); + this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername); + } + + /** + * Creates a TelegramWebhookCommandBot using default options + * Use ICommandRegistry's methods on this bot to register commands + * + * @param botToken the telegram api token + */ + public TelegramWebhookCommandBot(String botToken) { + this(new DefaultBotOptions(), botToken); + } + + /** + * Creates a TelegramWebhookCommandBot with custom options and allowing commands with + * usernames + * Use ICommandRegistry's methods on this bot to register commands + * + * @param options Bot options + * @param botToken the telegram api token + */ + public TelegramWebhookCommandBot(DefaultBotOptions options, String botToken) { + this(options, true, botToken); + } + /** * Creates a TelegramWebhookCommandBot * Use ICommandRegistry's methods on this bot to register commands @@ -50,9 +93,10 @@ public abstract class TelegramWebhookCommandBot extends TelegramWebhookBot imple * @param options Bot options * @param allowCommandsWithUsername true to allow commands with parameters (default), * false otherwise + * @param botToken the telegram api token */ - public TelegramWebhookCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) { - super(options); + public TelegramWebhookCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername, String botToken) { + super(options, botToken); this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername); } diff --git a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/timedbot/TimedSendLongPollingBot.java b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/timedbot/TimedSendLongPollingBot.java index 97ee169c..415a2ff5 100644 --- a/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/timedbot/TimedSendLongPollingBot.java +++ b/telegrambots-extensions/src/main/java/org/telegram/telegrambots/extensions/bots/timedbot/TimedSendLongPollingBot.java @@ -132,8 +132,20 @@ public abstract class TimedSendLongPollingBot extends TelegramLongPollingBot } //Constructor + + /** + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated protected TimedSendLongPollingBot() { + super(); + mSendTimer.schedule(new MessageSenderTask(), MANY_CHATS_SEND_INTERVAL, MANY_CHATS_SEND_INTERVAL); + } + + protected TimedSendLongPollingBot(String botToken) + { + super(botToken); mSendTimer.schedule(new MessageSenderTask(), MANY_CHATS_SEND_INTERVAL, MANY_CHATS_SEND_INTERVAL); } diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java index c271956a..20d2a163 100644 --- a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/SpringWebhookBot.java @@ -11,18 +11,38 @@ import org.telegram.telegrambots.meta.api.objects.Update; * @version 1.0 */ public abstract class SpringWebhookBot extends TelegramWebhookBot { - private SetWebhook setWebhook; + private final SetWebhook setWebhook; + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public SpringWebhookBot(SetWebhook setWebhook) { super(); this.setWebhook = setWebhook; } + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public SpringWebhookBot(DefaultBotOptions options, SetWebhook setWebhook) { super(options); this.setWebhook = setWebhook; } + public SpringWebhookBot(SetWebhook setWebhook, String botToken) { + super(botToken); + this.setWebhook = setWebhook; + } + + public SpringWebhookBot(DefaultBotOptions options, SetWebhook setWebhook, String botToken) { + super(options, botToken); + this.setWebhook = setWebhook; + } + public SetWebhook getSetWebhook() { return setWebhook; } @@ -30,11 +50,11 @@ public abstract class SpringWebhookBot extends TelegramWebhookBot { public class TestSpringWebhookBot extends SpringWebhookBot { public TestSpringWebhookBot(SetWebhook setWebhook) { - super(setWebhook); + super(setWebhook, null); } public TestSpringWebhookBot(DefaultBotOptions options, SetWebhook setWebhook) { - super(options, setWebhook); + super(options, setWebhook, null); } @Override @@ -42,11 +62,6 @@ public abstract class SpringWebhookBot extends TelegramWebhookBot { return null; } - @Override - public String getBotToken() { - return null; - } - @Override public BotApiMethod onWebhookUpdateReceived(Update update) { return null; diff --git a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java index fa2e395c..0ce7680d 100644 --- a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java +++ b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterRegistrationHooks.java @@ -14,12 +14,7 @@ import org.telegram.telegrambots.meta.generics.LongPollingBot; import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; class TestTelegramBotStarterRegistrationHooks { @@ -72,6 +67,9 @@ class TestTelegramBotStarterRegistrationHooks { } static class AnnotatedLongPollingBot extends TelegramLongPollingBot { + public AnnotatedLongPollingBot() { + super((String) null); + } @Override public void onUpdateReceived(final Update update) { @@ -82,11 +80,6 @@ class TestTelegramBotStarterRegistrationHooks { return null; } - @Override - public String getBotToken() { - return null; - } - @AfterBotRegistration public void afterBotHook() { hookCalled = true; diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java index 0a7c24bf..5023a9f3 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -73,9 +73,20 @@ public abstract class DefaultAbsSender extends AbsSender { private final CloseableHttpClient httpClient; private final RequestConfig requestConfig; private final TelegramFileDownloader telegramFileDownloader; + private final String botToken; + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated protected DefaultAbsSender(DefaultBotOptions options) { + this(options, null); + } + + protected DefaultAbsSender(DefaultBotOptions options, String botToken) { super(); + this.botToken = botToken; this.exe = Executors.newFixedThreadPool(options.getMaxThreads()); this.options = options; @@ -98,8 +109,12 @@ public abstract class DefaultAbsSender extends AbsSender { /** * Returns the token of the bot to be able to perform Telegram Api Requests * @return Token of the bot + * @deprecated Overriding this method is deprecated. Pass to constructor instead */ - public abstract String getBotToken(); + @Deprecated + public String getBotToken() { + return botToken; + } public final DefaultBotOptions getOptions() { return options; diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 4468e52b..ecc9d755 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -11,14 +11,31 @@ import org.telegram.telegrambots.util.WebhookUtils; * long-polling method */ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements LongPollingBot { + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated() public TelegramLongPollingBot() { this(new DefaultBotOptions()); } + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated() public TelegramLongPollingBot(DefaultBotOptions options) { super(options); } + public TelegramLongPollingBot(String botToken) { + this(new DefaultBotOptions(), botToken); + } + public TelegramLongPollingBot(DefaultBotOptions options, String botToken) { + super(options, botToken); + } + @Override public void clearWebhook() throws TelegramApiRequestException { WebhookUtils.clearWebhook(this); diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java index 5ddee09b..db05b577 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java @@ -13,14 +13,32 @@ import org.telegram.telegrambots.util.WebhookUtils; */ @SuppressWarnings("WeakerAccess") public abstract class TelegramWebhookBot extends DefaultAbsSender implements WebhookBot { + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public TelegramWebhookBot() { this(new DefaultBotOptions()); } + /** + * If this is used getBotToken has to be overridden in order to return the bot token! + * @deprecated Overwriting the getBotToken() method is deprecated. Use the constructor instead + */ + @Deprecated public TelegramWebhookBot(DefaultBotOptions options) { super(options); } + public TelegramWebhookBot(String botToken) { + this(new DefaultBotOptions(), botToken); + } + + public TelegramWebhookBot(DefaultBotOptions options, String botToken) { + super(options, botToken); + } + @Override public void setWebhook(SetWebhook setWebhook) throws TelegramApiException { WebhookUtils.setWebhook(this, this, setWebhook); diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/facilities/filedownloader/TelegramFileDownloader.java b/telegrambots/src/main/java/org/telegram/telegrambots/facilities/filedownloader/TelegramFileDownloader.java index 10ba1bfa..b750d17f 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/facilities/filedownloader/TelegramFileDownloader.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/facilities/filedownloader/TelegramFileDownloader.java @@ -25,6 +25,7 @@ import static org.apache.http.HttpStatus.SC_OK; */ public class TelegramFileDownloader { private final HttpClient httpClient; + //TODO Replace with concrete token once deprecations are removed private final Supplier botTokenSupplier; public TelegramFileDownloader(final Supplier botTokenSupplier) { diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java index 1a617772..c3070473 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java @@ -47,6 +47,10 @@ public class TelegramLongPollingBotTest { private static class TestBot extends TelegramLongPollingBot { + public TestBot() { + super(""); + } + @Override public void onUpdateReceived(Update update) { } @@ -60,10 +64,6 @@ public class TelegramLongPollingBotTest { return ""; } - @Override - public String getBotToken() { - return ""; - } } }