Skip to content

Commit

Permalink
Pass token in constructor (rubenlagus#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase22 authored Jan 4, 2023
1 parent cafe8cc commit 402a4c4
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -264,10 +262,6 @@ public void onUpdateReceived(Update update) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
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;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -37,8 +39,11 @@ public TelegramWebhookCommandBot() {
* 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);
}
Expand All @@ -47,15 +52,54 @@ public TelegramWebhookCommandBot(DefaultBotOptions options) {
* 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
*
* @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, String botToken) {
super(options, botToken);
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this::getBotUsername);
}

@Override
public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
if (update.hasMessage()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,20 @@ public Long getChatId()
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,57 @@
* @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;
}

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
public String getBotUsername() {
return null;
}

@Override
public String getBotToken() {
return null;
}

@Override
public BotApiMethod onWebhookUpdateReceived(Update update) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
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 {

Expand Down Expand Up @@ -72,6 +67,9 @@ public LongPollingBot longPollingBot() {
}

static class AnnotatedLongPollingBot extends TelegramLongPollingBot {
public AnnotatedLongPollingBot() {
super((String) null);
}

@Override
public void onUpdateReceived(final Update update) {
Expand All @@ -82,11 +80,6 @@ public String getBotUsername() {
return null;
}

@Override
public String getBotToken() {
return null;
}

@AfterBotRegistration
public void afterBotHook() {
hookCalled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -98,8 +109,12 @@ protected DefaultAbsSender(DefaultBotOptions options) {
/**
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,31 @@
* <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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@
*/
@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);
Expand Down
Loading

0 comments on commit 402a4c4

Please sign in to comment.