Skip to content

Changed API from bot-centered to method-centered #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 +1,14 @@
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.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
* stas
Expand All @@ -14,11 +18,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 +72,27 @@ 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);
}

public void execute(BiConsumer<T, R> onResponse, BiConsumer<T, IOException> onFailure) {
Callback<T, R> callback = new Callback<T, R>() {
@Override
public void onResponse(T request, R response) {
onResponse.accept(request, response);
}

@Override
public void onFailure(T request, IOException e) {
onFailure.accept(request ,e);
}
};
this.execute(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);
}
}
Loading