Skip to content

Commit

Permalink
Changed API from bot-centered to method-centered
Browse files Browse the repository at this point in the history
• Moved execute method from TelegramBot to BaseRequest
• Moved DeleteMyCommands.java from model to request
• Added BaseRequest.api member for execute method
• Added api argument to all constructors and static generator functions and passed them down to BaseRequest.
• Added wrapper functions for all requests in TelegramBot.
• Fixed tests to use new API. NOTE: LogOutCloseTest.java is inherently broken and has to be fixed.
• Deprecated the entire SetStickerSetThumb class, instead of one of the compilers.
• Fixed API usage in sample to match the new API.
  • Loading branch information
HarryZalessky committed May 10, 2023
1 parent bea9e3c commit 9efa799
Show file tree
Hide file tree
Showing 130 changed files with 2,670 additions and 732 deletions.
1,828 changes: 1,814 additions & 14 deletions library/src/main/java/com/pengrad/telegrambot/TelegramBot.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void stop() {
private void getUpdates(GetUpdates request) {
if (bot == null || listener == null) return;

bot.execute(request, new Callback<GetUpdates, GetUpdatesResponse>() {
request.execute(new Callback<GetUpdates, GetUpdatesResponse>() {
@Override
public void onResponse(GetUpdates request, GetUpdatesResponse response) {
if (listener == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DecryptedData decryptData(Credentials credentials) throws Exception {
}

public byte[] decryptFile(PassportFile passportFile, FileCredentials fileCredentials, TelegramBot bot) throws Exception {
File file = bot.execute(new GetFile(passportFile.fileId())).file();
File file = bot.getFile(passportFile.fileId()).execute().file();
byte[] fileData = bot.getFileContent(file);
return decryptFile(fileData, fileCredentials);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.passport;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.request.BaseRequest;
import com.pengrad.telegrambot.response.BaseResponse;

Expand All @@ -9,8 +10,8 @@
*/
public class SetPassportDataErrors extends BaseRequest<SetPassportDataErrors, BaseResponse> {

public SetPassportDataErrors(long userId, PassportElementError... errors) {
super(BaseResponse.class);
public SetPassportDataErrors(TelegramBotClient api, long userId, PassportElementError... errors) {
super(api, BaseResponse.class);
add("user_id", userId).add("errors", errors);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;

import java.io.File;

/**
Expand All @@ -12,8 +14,8 @@ abstract public class AbstractMultipartRequest<T extends AbstractMultipartReques
private String fileName;
private String contentType;

public AbstractMultipartRequest(Object chatId, Object file) {
super(chatId);
public AbstractMultipartRequest(TelegramBotClient api, Object chatId, Object file) {
super(api, chatId);
if (file instanceof String) {
isMultipart = false;
} else if (file instanceof File) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.model.request.Keyboard;
import com.pengrad.telegrambot.response.SendResponse;

Expand All @@ -9,8 +10,8 @@
*/
abstract public class AbstractSendRequest<T extends AbstractSendRequest<T>> extends BaseRequest<T, SendResponse> {

public AbstractSendRequest(Object chatId) {
super(SendResponse.class);
public AbstractSendRequest(TelegramBotClient api, Object chatId) {
super(api, SendResponse.class);
add("chat_id", chatId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

import java.io.File;
Expand All @@ -12,8 +13,8 @@ abstract public class AbstractUploadRequest<T extends BaseRequest<T, R>, R exten

private final boolean isMultipart;

public AbstractUploadRequest(Class<? extends R> responseClass, String paramName, Object data) {
super(responseClass);
public AbstractUploadRequest(TelegramBotClient api, Class<? extends R> responseClass, String paramName, Object data) {
super(api, responseClass);
if (data instanceof String) {
isMultipart = false;
} else if (data instanceof File) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.model.MaskPosition;
import com.pengrad.telegrambot.model.request.InputSticker;
import com.pengrad.telegrambot.response.BaseResponse;
Expand All @@ -14,47 +15,47 @@ public class AddStickerToSet extends AbstractUploadRequest<AddStickerToSet, Base
* @deprecated Use constructor with the InputSticker type (since API v6.6)
*/
@Deprecated
public static AddStickerToSet tgsSticker(Long userId, String name, String emojis, Object tgsSticker) {
return new AddStickerToSet(userId, name, emojis, "tgs_sticker", tgsSticker);
public static AddStickerToSet tgsSticker(TelegramBotClient api, Long userId, String name, String emojis, Object tgsSticker) {
return new AddStickerToSet(api, userId, name, emojis, "tgs_sticker", tgsSticker);
}

/**
* @deprecated Use constructor with the InputSticker type (since API v6.6)
*/
@Deprecated
public static AddStickerToSet pngSticker(Long userId, String name, String emojis, Object pngSticker) {
return new AddStickerToSet(userId, name, emojis, "png_sticker", pngSticker);
public static AddStickerToSet pngSticker(TelegramBotClient api, Long userId, String name, String emojis, Object pngSticker) {
return new AddStickerToSet(api, userId, name, emojis, "png_sticker", pngSticker);
}

/**
* @deprecated Use constructor with the InputSticker type (since API v6.6)
*/
@Deprecated
public static AddStickerToSet webmSticker(Long userId, String name, String emojis, Object webmSticker) {
return new AddStickerToSet(userId, name, emojis, "webm_sticker", webmSticker);
public static AddStickerToSet webmSticker(TelegramBotClient api, Long userId, String name, String emojis, Object webmSticker) {
return new AddStickerToSet(api, userId, name, emojis, "webm_sticker", webmSticker);
}

/**
* @deprecated Use static methods according to sticker set type - {@link #pngSticker(Long, String, String, Object) for png}, {@link #tgsSticker(Long, String, String, Object) for tgs} and {@link #webmSticker(Long, String, String, Object) for webm}
* @deprecated Use static methods according to sticker set type - {@link #pngSticker(TelegramBotClient, Long, String, String, Object) for png}, {@link #tgsSticker(TelegramBotClient, Long, String, String, Object) for tgs} and {@link #webmSticker(TelegramBotClient, Long, String, String, Object) for webm}
*/
@Deprecated
public AddStickerToSet(Long userId, String name, Object pngSticker, String emojis) {
this(userId, name, emojis, "png_sticker", pngSticker);
public AddStickerToSet(TelegramBotClient api, Long userId, String name, Object pngSticker, String emojis) {
this(api, userId, name, emojis, "png_sticker", pngSticker);
}

/**
* @deprecated Use constructor with the InputSticker type (since API v6.6)
*/
@Deprecated
private AddStickerToSet(Long userId, String name, String emojis, String stickerParam, Object sticker) {
super(BaseResponse.class, stickerParam, sticker);
private AddStickerToSet(TelegramBotClient api, Long userId, String name, String emojis, String stickerParam, Object sticker) {
super(api, BaseResponse.class, stickerParam, sticker);
add("user_id", userId);
add("name", name);
add("emojis", emojis);
}

public AddStickerToSet(Long userId, String name, InputSticker sticker) {
super(BaseResponse.class, attachName(sticker), attachment(sticker));
public AddStickerToSet(TelegramBotClient api, Long userId, String name, InputSticker sticker) {
super(api, BaseResponse.class, attachName(sticker), attachment(sticker));
add("user_id", userId);
add("name", name);
add("sticker", sticker);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

/**
Expand All @@ -8,8 +9,8 @@
*/
public class AnswerCallbackQuery extends BaseRequest<AnswerCallbackQuery, BaseResponse> {

public AnswerCallbackQuery(String callbackQueryId) {
super(BaseResponse.class);
public AnswerCallbackQuery(TelegramBotClient api, String callbackQueryId) {
super(api, BaseResponse.class);
add("callback_query_id", callbackQueryId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.model.request.InlineQueryResult;
import com.pengrad.telegrambot.model.request.InlineQueryResultsButton;
import com.pengrad.telegrambot.response.BaseResponse;
Expand All @@ -10,8 +11,8 @@
*/
public class AnswerInlineQuery extends BaseRequest<AnswerInlineQuery, BaseResponse> {

public AnswerInlineQuery(String inlineQueryId, InlineQueryResult<?>... results) {
super(BaseResponse.class);
public AnswerInlineQuery(TelegramBotClient api, String inlineQueryId, InlineQueryResult<?>... results) {
super(api, BaseResponse.class);
add("inline_query_id", inlineQueryId).add("results", results);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

/**
Expand All @@ -8,13 +9,13 @@
*/
public class AnswerPreCheckoutQuery extends BaseRequest<AnswerPreCheckoutQuery, BaseResponse> {

public AnswerPreCheckoutQuery(String preCheckoutQueryId) {
super(BaseResponse.class);
public AnswerPreCheckoutQuery(TelegramBotClient api, String preCheckoutQueryId) {
super(api, BaseResponse.class);
add("pre_checkout_query_id", preCheckoutQueryId).add("ok", true);
}

public AnswerPreCheckoutQuery(String preCheckoutQueryId, String errorMessage) {
super(BaseResponse.class);
public AnswerPreCheckoutQuery(TelegramBotClient api, String preCheckoutQueryId, String errorMessage) {
super(api, BaseResponse.class);
add("pre_checkout_query_id", preCheckoutQueryId).add("ok", false).add("error_message", errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.model.request.ShippingOption;
import com.pengrad.telegrambot.response.BaseResponse;

Expand All @@ -9,13 +10,13 @@
*/
public class AnswerShippingQuery extends BaseRequest<AnswerShippingQuery, BaseResponse> {

public AnswerShippingQuery(String shippingQueryId, ShippingOption... shippingOptions) {
super(BaseResponse.class);
public AnswerShippingQuery(TelegramBotClient api, String shippingQueryId, ShippingOption... shippingOptions) {
super(api, BaseResponse.class);
add("shipping_query_id", shippingQueryId).add("ok", true).add("shipping_options", shippingOptions);
}

public AnswerShippingQuery(String shippingQueryId, String errorMessage) {
super(BaseResponse.class);
public AnswerShippingQuery(TelegramBotClient api, String shippingQueryId, String errorMessage) {
super(api, BaseResponse.class);
add("shipping_query_id", shippingQueryId).add("ok", false).add("error_message", errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.model.request.InlineQueryResult;
import com.pengrad.telegrambot.response.BaseResponse;
import com.pengrad.telegrambot.response.SentWebAppMessageResponse;
Expand All @@ -10,8 +11,8 @@
*/
public class AnswerWebAppQuery extends BaseRequest<AnswerWebAppQuery, SentWebAppMessageResponse> {

public AnswerWebAppQuery(String web_app_query_id, InlineQueryResult<?> result) {
super(SentWebAppMessageResponse.class);
public AnswerWebAppQuery(TelegramBotClient api, String web_app_query_id, InlineQueryResult<?> result) {
super(api, SentWebAppMessageResponse.class);
add("web_app_query_id", web_app_query_id).add("result", result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

/**
Expand All @@ -13,8 +14,8 @@ public class ApproveChatJoinRequest extends BaseRequest<ApproveChatJoinRequest,
* @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param userId Unique identifier of the target user
*/
public ApproveChatJoinRequest(Object chatId, Long userId) {
super(BaseResponse.class);
public ApproveChatJoinRequest(TelegramBotClient api, Object chatId, Long userId) {
super(api, BaseResponse.class);
add("chat_id", chatId);
add("user_id", userId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

public class BanChatMember extends BaseRequest<BanChatMember, BaseResponse> {

public BanChatMember(Object chatId, long userId) {
super(BaseResponse.class);
public BanChatMember(TelegramBotClient api, Object chatId, long userId) {
super(api, BaseResponse.class);
add("chat_id", chatId).add("user_id", userId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

/**
Expand All @@ -8,8 +9,8 @@
*/
public class BanChatSenderChat extends BaseRequest<BanChatSenderChat, BaseResponse> {

public BanChatSenderChat(Object chatId, long sender_chat_id) {
super(BaseResponse.class);
public BanChatSenderChat(TelegramBotClient api, Object chatId, long sender_chat_id) {
super(api, BaseResponse.class);
add("chat_id", chatId).add("sender_chat_id", sender_chat_id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.BotUtils;
import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

import java.util.LinkedHashMap;
Expand All @@ -14,11 +16,12 @@ abstract public class BaseRequest<T extends BaseRequest<T, R>, R extends BaseRes

@SuppressWarnings("unchecked")
protected final T thisAsT = (T) this;

TelegramBotClient api;
private final Class<? extends R> responseClass;
private final Map<String, Object> parameters;

public BaseRequest(Class<? extends R> responseClass) {
public BaseRequest(TelegramBotClient api, Class<? extends R> responseClass) {
this.api = api;
this.responseClass = responseClass;
this.parameters = new LinkedHashMap<>();
}
Expand Down Expand Up @@ -67,4 +70,12 @@ public String toWebhookResponse() {
fullMap.put("method", getMethod());
return BotUtils.toJson(fullMap);
}

public R execute() {
return api.send(this);
}

public void execute(Callback<T, R> callback) {
api.send(this.thisAsT, callback);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

/**
Expand All @@ -8,7 +9,7 @@
*/
public class Close extends BaseRequest<Close, BaseResponse> {

public Close() {
super(BaseResponse.class);
public Close(TelegramBotClient api) {
super(api, BaseResponse.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.impl.TelegramBotClient;
import com.pengrad.telegrambot.response.BaseResponse;

public class CloseForumTopic extends BaseRequest<CloseForumTopic, BaseResponse> {
Expand All @@ -8,16 +9,16 @@ public class CloseForumTopic extends BaseRequest<CloseForumTopic, BaseResponse>
* @deprecated use constructor with Long for future compatibility
*/
@Deprecated
public CloseForumTopic(Integer chatId, Integer messageThreadId) {
this(chatId.toString(), messageThreadId);
public CloseForumTopic(TelegramBotClient api, Integer chatId, Integer messageThreadId) {
this(api, chatId.toString(), messageThreadId);
}

public CloseForumTopic(Long chatId, Integer messageThreadId) {
this(chatId.toString(), messageThreadId);
public CloseForumTopic(TelegramBotClient api, Long chatId, Integer messageThreadId) {
this(api, chatId.toString(), messageThreadId);
}

public CloseForumTopic(String chatId, Integer messageThreadId) {
super(BaseResponse.class);
public CloseForumTopic(TelegramBotClient api, String chatId, Integer messageThreadId) {
super(api, BaseResponse.class);
add("chat_id", chatId);
add("message_thread_id", messageThreadId);
}
Expand Down

0 comments on commit 9efa799

Please sign in to comment.