Pass token in constructor (#1167)

This commit is contained in:
Chase 2023-01-04 12:27:28 +01:00 committed by GitHub
parent cafe8cc3b4
commit 402a4c46ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 173 additions and 37 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -11,14 +11,31 @@ import org.telegram.telegrambots.util.WebhookUtils;
* <a href="https://core.telegram.org/bots/api#getupdates">long-polling</a> 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);

View File

@ -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);

View File

@ -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<String> botTokenSupplier;
public TelegramFileDownloader(final Supplier<String> botTokenSupplier) {

View File

@ -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 "";
}
}
}