From d2ffe8b73fd3d77e9733155e961554d89f683f18 Mon Sep 17 00:00:00 2001 From: Victor Melnik Date: Mon, 22 Jan 2024 21:10:10 +0100 Subject: [PATCH 01/31] Add default constructor to WriteAccessAllowed (#1273) --- .../telegrambots/meta/api/objects/WriteAccessAllowed.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java index e1e1a983f..26a406b94 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java @@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import org.telegram.telegrambots.meta.api.interfaces.BotApiObject; @@ -19,6 +20,7 @@ @Getter @Setter @ToString +@NoArgsConstructor @AllArgsConstructor public class WriteAccessAllowed implements BotApiObject { From ac32664804678aa2a20ad90045fb8cb0d9070b47 Mon Sep 17 00:00:00 2001 From: Bryan <128009301+bryan-zhu-dev@users.noreply.github.com> Date: Tue, 23 Jan 2024 04:12:34 +0800 Subject: [PATCH 02/31] Update AllowedUpdates (#1276) --- .../meta/api/methods/updates/AllowedUpdates.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updates/AllowedUpdates.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updates/AllowedUpdates.java index b2ce70cd8..875774a73 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updates/AllowedUpdates.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updates/AllowedUpdates.java @@ -12,4 +12,11 @@ public final class AllowedUpdates { public static final String INLINEQUERY = "inline_query"; public static final String CHOSENINLINERESULT = "chosen_inline_result"; public static final String CALLBACKQUERY = "callback_query"; + public static final String SHIPPINGQUERY = "shipping_query"; + public static final String PRECHECKOUTQUERY = "pre_checkout_query"; + public static final String POLL = "poll"; + public static final String POLLANSWER = "poll_answer"; + public static final String MYCHATMEMBER = "my_chat_member"; + public static final String CHATMEMBER = "chat_member"; + public static final String CHATJOINREQUEST = "chat_join_request"; } From 47be445c71d31ecb42123527168203b9710f95f6 Mon Sep 17 00:00:00 2001 From: Moon Moon Date: Sun, 28 Jan 2024 15:02:04 +0300 Subject: [PATCH 03/31] fix ReactionType validations (#1303) --- .../meta/api/objects/reactions/ReactionTypeCustomEmoji.java | 2 +- .../meta/api/objects/reactions/ReactionTypeEmoji.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeCustomEmoji.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeCustomEmoji.java index c48ac9e9a..2f8c235e4 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeCustomEmoji.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeCustomEmoji.java @@ -48,7 +48,7 @@ public void validate() throws TelegramApiValidationException { if (customEmoji.isEmpty()) { throw new TelegramApiValidationException("CustomEmoji parameter can't be empty", this); } - if (ReactionType.CUSTOM_EMOJI_TYPE.equals(type)) { + if (!ReactionType.CUSTOM_EMOJI_TYPE.equals(type)) { throw new TelegramApiValidationException("Type must be \"custom_emoji\"", this); } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeEmoji.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeEmoji.java index daab0c653..fbcced9d3 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeEmoji.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/reactions/ReactionTypeEmoji.java @@ -52,7 +52,7 @@ public void validate() throws TelegramApiValidationException { if (emoji.isEmpty()) { throw new TelegramApiValidationException("Emoji parameter can't be empty", this); } - if (ReactionType.EMOJI_TYPE.equals(type)) { + if (!ReactionType.EMOJI_TYPE.equals(type)) { throw new TelegramApiValidationException("Type must be \"emoji\"", this); } } From b0d3989e3fd3f5bd5b351f69bea374d11d9c465e Mon Sep 17 00:00:00 2001 From: ChelouteVR <149147350+ChelouteVR@users.noreply.github.com> Date: Sun, 28 Jan 2024 13:04:07 +0100 Subject: [PATCH 04/31] Update Using-Replies.md (#1282) A semicolon were missing in the Standalone reply code. The reply signature changed and needs a BiConsumer instead of Consumer in the Ability reply --- TelegramBots.wiki/abilities/Using-Replies.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TelegramBots.wiki/abilities/Using-Replies.md b/TelegramBots.wiki/abilities/Using-Replies.md index 0067a67b5..a5fde1653 100644 --- a/TelegramBots.wiki/abilities/Using-Replies.md +++ b/TelegramBots.wiki/abilities/Using-Replies.md @@ -12,7 +12,7 @@ public Reply sayYuckOnImage() { // getChatId is a public utility function in rg.telegram.abilitybots.api.util.AbilityUtils Consumer action = upd -> silent.send("Yuck", getChatId(upd)); - return Reply.of(action, Flag.PHOTO) + return Reply.of(action, Flag.PHOTO); } ``` @@ -36,7 +36,7 @@ public Ability playWithMe() { // The signature of a reply is -> (Consumer action, Predicate... conditions) // So, we first declare the action that takes an update (NOT A MESSAGECONTEXT) like the action above // The reason of that is that a reply can be so versatile depending on the message, context becomes an inefficient wrapping - .reply(upd -> { + .reply((abilityBot,upd) -> { // Prints to console System.out.println("I'm in a reply!"); // Sends message From 870d1e5be103720dd0d80c79d4b49561f04a5221 Mon Sep 17 00:00:00 2001 From: Chase Date: Fri, 2 Feb 2024 23:44:25 +0000 Subject: [PATCH 05/31] Update MaybeInaccessibleMessage.java (#1310) Add getMessageId to MaybeInaccessibleMessage According to https://core.telegram.org/bots/api#maybeinaccessiblemessage --- .../telegrambots/meta/api/objects/MaybeInaccessibleMessage.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java index 7a6b88aa5..fbb1fdd11 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java @@ -22,4 +22,6 @@ default boolean isSuperGroupMessage() { } Long getChatId(); + + Integer getMessageId(); } From 6b50d6805671a784012b88488bbdd4ea3f605bad Mon Sep 17 00:00:00 2001 From: link1107 <66795347+link1107@users.noreply.github.com> Date: Sun, 4 Feb 2024 14:47:28 +0100 Subject: [PATCH 06/31] Fixed incorrect Chat#profileBackgroundCustomEmojiId field type. Changed from 'Boolean' to 'String' (according to TelegramBotAPI Official Documentation). (#1315) --- .../java/org/telegram/telegrambots/meta/api/objects/Chat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java index 5391443b7..88fa77081 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java @@ -303,7 +303,7 @@ public class Chat implements BotApiObject { * Returned only in getChat. */ @JsonProperty(PROFILE_BACKGROUND_CUSTOM_EMOJI_ID_FIELD) - private Boolean profileBackgroundCustomEmojiId; + private String profileBackgroundCustomEmojiId; /** * Optional. * True, if new chat members will have access to old messages; available only to chat administrators. From d615fbf09ba6f492ab249b0ae92c287d3ca28d45 Mon Sep 17 00:00:00 2001 From: rbondar Date: Sat, 17 Feb 2024 14:24:14 +0300 Subject: [PATCH 07/31] Missing fields and constructor update for API 7.0 (#1322) * fix force=true for WriteAccessAllowed * + test and fixtures on write access allowed --- .../meta/api/objects/WriteAccessAllowed.java | 2 +- .../api/methods/PartialBotApiMethodTest.java | 43 +++++++++++++++++++ ...-with-write-access-allowed-for-webapp.json | 28 ++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodTest.java create mode 100644 telegrambots-meta/src/test/resources/fixtures/update-with-write-access-allowed-for-webapp.json diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java index 26a406b94..e8338a83f 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/WriteAccessAllowed.java @@ -20,7 +20,7 @@ @Getter @Setter @ToString -@NoArgsConstructor +@NoArgsConstructor(force = true) @AllArgsConstructor public class WriteAccessAllowed implements BotApiObject { diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodTest.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodTest.java new file mode 100644 index 000000000..9e3457981 --- /dev/null +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodTest.java @@ -0,0 +1,43 @@ +package org.telegram.telegrambots.meta.api.methods; + + +import org.junit.jupiter.api.Test; +import org.telegram.telegrambots.meta.api.objects.Update; +import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; + +import java.io.IOException; +import java.io.Serializable; +import java.nio.file.Files; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * @author Roman Bondar + */ + +public class PartialBotApiMethodTest { + + @Test + public void deserializeUpdateWithWriteAccessAllowedNotThrowApiException() throws IOException { + + String answer = new String(Files.readAllBytes(Paths.get("src/test/resources/fixtures/update-with-write-access-allowed-for-webapp.json"))); + + PartialBotApiMethod method = new PartialBotApiMethod() { + @Override + public Serializable deserializeResponse(String answer) throws TelegramApiRequestException { + return deserializeResponseArray(answer, Update.class); + } + + @Override + public String getMethod() { + return "method"; + } + }; + + String finalAnswer = answer; + assertDoesNotThrow(() -> method.deserializeResponse(finalAnswer), + "deserializeResponse should not throw if write access allowed passed into it "); + + } +} diff --git a/telegrambots-meta/src/test/resources/fixtures/update-with-write-access-allowed-for-webapp.json b/telegrambots-meta/src/test/resources/fixtures/update-with-write-access-allowed-for-webapp.json new file mode 100644 index 000000000..573d7d001 --- /dev/null +++ b/telegrambots-meta/src/test/resources/fixtures/update-with-write-access-allowed-for-webapp.json @@ -0,0 +1,28 @@ +{ + "ok": true, + "result": [ + { + "update_id": 329421221, + "message": { + "message_id": 180, + "from": { + "id": 11111111, + "is_bot": false, + "first_name": "Alice Bob", + "username": "alicebob", + "language_code": "ru" + }, + "chat": { + "id": 11111111, + "first_name": "Alice Bob", + "username": "alicebob", + "type": "private" + }, + "date": 1707944280, + "write_access_allowed": { + "web_app_name": "alicebobshop" + } + } + } + ] +} \ No newline at end of file From ed75ff92b978bea25c23398ecea980a3b5cb0337 Mon Sep 17 00:00:00 2001 From: Victor Melnik Date: Sat, 17 Feb 2024 12:49:17 +0100 Subject: [PATCH 08/31] Add missing methods from API 6.6 (#1323) * Fix path for GetMyName method * Add missing sticker methods from API 6.6 * Fix possible NPE in InputSticker validation * Add ReplyParameters to SendInvoice API method * Removed mysterious fields in SetChatTitle --- .../groupadministration/SetChatTitle.java | 24 ------ .../api/methods/invoices/SendInvoice.java | 10 +++ .../meta/api/methods/name/GetMyName.java | 2 +- .../methods/stickers/DeleteStickerSet.java | 46 ++++++++++++ .../methods/stickers/SetStickerEmojiList.java | 61 ++++++++++++++++ .../methods/stickers/SetStickerKeywords.java | 63 ++++++++++++++++ .../stickers/SetStickerMaskPosition.java | 61 ++++++++++++++++ .../methods/stickers/SetStickerSetThumb.java | 2 + .../stickers/SetStickerSetThumbnail.java | 73 +++++++++++++++++++ .../methods/stickers/SetStickerSetTitle.java | 57 +++++++++++++++ .../api/objects/stickers/InputSticker.java | 16 ++-- .../telegrambots/meta/bots/AbsSender.java | 24 +++++- .../telegrambots/bots/DefaultAbsSender.java | 52 ++++++++++++- 13 files changed, 447 insertions(+), 44 deletions(-) create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/DeleteStickerSet.java create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerEmojiList.java create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerKeywords.java create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerMaskPosition.java create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumbnail.java create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetTitle.java diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/SetChatTitle.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/SetChatTitle.java index 0e08cb782..f16add037 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/SetChatTitle.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/SetChatTitle.java @@ -13,8 +13,6 @@ import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; -import java.util.List; - /** * @author Ruben Bermudez * @version 3.1 @@ -49,9 +47,6 @@ public void setChatId(@NonNull Long chatId) { this.chatId = chatId.toString(); } - private long[] otherUserIds; - private byte[] fileHashes; - @Override public String getMethod() { return PATH; @@ -74,24 +69,5 @@ public SetChatTitleBuilder chatId(@NonNull Long chatId) { this.chatId = chatId.toString(); return this; } - - @Tolerate - public SetChatTitleBuilder ids(List otherUserIds) { - this.otherUserIds = new long[otherUserIds.size()]; - for (int i = 0; i < otherUserIds.size(); i++) { - this.otherUserIds[i] = otherUserIds.get(i); - } - return this; - } - - @Tolerate - public SetChatTitleBuilder otherUserId(Long otherUserId) { - long[] newOtherUserIds = new long[this.otherUserIds.length + 1]; - System.arraycopy(this.otherUserIds, 0, newOtherUserIds, 0, this.otherUserIds.length); - newOtherUserIds[newOtherUserIds.length - 1] = otherUserId; - this.otherUserIds = newOtherUserIds; - return this; - } - } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/SendInvoice.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/SendInvoice.java index 2535a38e4..b08758c88 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/SendInvoice.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/SendInvoice.java @@ -14,6 +14,7 @@ import lombok.experimental.Tolerate; import org.apache.commons.lang3.StringUtils; import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodMessage; +import org.telegram.telegrambots.meta.api.objects.ReplyParameters; import org.telegram.telegrambots.meta.api.objects.payments.LabeledPrice; import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; @@ -64,6 +65,7 @@ public class SendInvoice extends BotApiMethodMessage { private static final String MAXTIPAMOUNT_FIELD = "max_tip_amount"; private static final String SUGGESTEDTIPAMOUNTS_FIELD = "suggested_tip_amounts"; private static final String PROTECTCONTENT_FIELD = "protect_content"; + private static final String REPLY_PARAMETERS_FIELD = "reply_parameters"; @JsonProperty(CHATID_FIELD) @NonNull @@ -167,6 +169,11 @@ public class SendInvoice extends BotApiMethodMessage { private List suggestedTipAmounts; @JsonProperty(PROTECTCONTENT_FIELD) private Boolean protectContent; ///< Optional. Protects the contents of sent messages from forwarding and saving + /** + * Optional + * Description of the message to reply to + */ + private ReplyParameters replyParameters; @Tolerate public void setChatId(@NonNull Long chatId) { @@ -211,6 +218,9 @@ public void validate() throws TelegramApiValidationException { if (replyMarkup != null) { replyMarkup.validate(); } + if (replyParameters != null) { + replyParameters.validate(); + } } public static class SendInvoiceBuilder { diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/name/GetMyName.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/name/GetMyName.java index ac92a901d..0ee60952c 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/name/GetMyName.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/name/GetMyName.java @@ -27,7 +27,7 @@ @AllArgsConstructor @Builder public class GetMyName extends BotApiMethod { - public static final String PATH = "getMyDescription"; + public static final String PATH = "getMyName"; private static final String LANGUAGE_CODE_FIELD = "language_code"; diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/DeleteStickerSet.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/DeleteStickerSet.java new file mode 100644 index 000000000..b332aef43 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/DeleteStickerSet.java @@ -0,0 +1,46 @@ +package org.telegram.telegrambots.meta.api.methods.stickers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; + +/** + * @author Ruben Bermudez + * @version 6.6 + * Use this method to delete a sticker set that was created by the bot. + * + * Returns True on success. + * + */ +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@RequiredArgsConstructor +@Builder +public class DeleteStickerSet extends BotApiMethodBoolean { + public static final String PATH = "deleteStickerSet"; + + public static final String NAME_FIELD = "name"; + + /** + * Sticker set name + */ + @JsonProperty(NAME_FIELD) + @NonNull + private String name; + + @Override + public String getMethod() { + return PATH; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (name.isEmpty() || name.length() > 64) { + throw new TelegramApiValidationException("name must be between 1 and 64 characters", this); + } + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerEmojiList.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerEmojiList.java new file mode 100644 index 000000000..580e78c10 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerEmojiList.java @@ -0,0 +1,61 @@ +package org.telegram.telegrambots.meta.api.methods.stickers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; + +import java.util.List; + +/** + * @author Ruben Bermudez + * @version 6.6 + * Use this method to change the list of emoji assigned to a regular or custom emoji sticker. + * The sticker must belong to a sticker set created by the bot. + * + * Returns True on success. + * + */ +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@RequiredArgsConstructor +@Builder +public class SetStickerEmojiList extends BotApiMethodBoolean { + public static final String PATH = "setStickerEmojiList"; + + public static final String STICKER_FIELD = "sticker"; + public static final String EMOJI_LIST_FIELD = "emoji_list"; + + /** + * File identifier of the sticker + */ + @JsonProperty(STICKER_FIELD) + @NonNull + private String sticker; + + /** + * List of 1-20 emoji associated with the sticker + */ + @JsonProperty(EMOJI_LIST_FIELD) + @NonNull + @Singular("emoji") + private List emojiList; + + @Override + public String getMethod() { + return PATH; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (sticker.isEmpty()) { + throw new TelegramApiValidationException("sticker can't be null", this); + } + if (emojiList.isEmpty() || emojiList.size() > 20) { + throw new TelegramApiValidationException("Emoji list must have between 1 and 20 items", this); + } + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerKeywords.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerKeywords.java new file mode 100644 index 000000000..231416acc --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerKeywords.java @@ -0,0 +1,63 @@ +package org.telegram.telegrambots.meta.api.methods.stickers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; + +import java.util.List; + +/** + * @author Ruben Bermudez + * @version 6.6 + * Use this method to change search keywords assigned to a regular or custom emoji sticker. + * The sticker must belong to a sticker set created by the bot. + * + * Returns True on success. + * + */ +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@RequiredArgsConstructor +@AllArgsConstructor +@Builder +public class SetStickerKeywords extends BotApiMethodBoolean { + public static final String PATH = "setStickerKeywords"; + + public static final String STICKER_FIELD = "sticker"; + public static final String KEYWORDS_FIELD = "keywords"; + + /** + * File identifier of the sticker + */ + @JsonProperty(STICKER_FIELD) + @NonNull + private String sticker; + + /** + * Optional. + * List of 0-20 search keywords for the sticker with total length of up to 64 characters. + * For “regular” and “custom_emoji” stickers only. + */ + @JsonProperty(KEYWORDS_FIELD) + @Singular + private List keywords; + + @Override + public String getMethod() { + return PATH; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (sticker.isEmpty()) { + throw new TelegramApiValidationException("sticker can't be null", this); + } + if (keywords != null && keywords.size() > 20) { + throw new TelegramApiValidationException("Keywords list must have between 0 and 20 items", this); + } + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerMaskPosition.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerMaskPosition.java new file mode 100644 index 000000000..b73aa0e61 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerMaskPosition.java @@ -0,0 +1,61 @@ +package org.telegram.telegrambots.meta.api.methods.stickers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; +import org.telegram.telegrambots.meta.api.objects.stickers.MaskPosition; +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; + +/** + * @author Ruben Bermudez + * @version 6.6 + * Use this method to change the mask position of a mask sticker. + * The sticker must belong to a sticker set that was created by the bot. + * + * Returns True on success. + * + */ +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@RequiredArgsConstructor +@AllArgsConstructor +@Builder +public class SetStickerMaskPosition extends BotApiMethodBoolean { + public static final String PATH = "setStickerMaskPosition"; + + public static final String STICKER_FIELD = "sticker"; + public static final String MASK_POSITION_FIELD = "mask_position"; + + /** + * File identifier of the sticker + */ + @JsonProperty(STICKER_FIELD) + @NonNull + private String sticker; + + /** + * Optional. + * Position where the mask should be placed on faces. For “mask” stickers only. + * Omit the parameter to remove the mask position. + */ + @JsonProperty(MASK_POSITION_FIELD) + private MaskPosition maskPosition; + + @Override + public String getMethod() { + return PATH; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (sticker.isEmpty()) { + throw new TelegramApiValidationException("sticker can't be null", this); + } + if (maskPosition != null) { + maskPosition.validate(); + } + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumb.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumb.java index 161017fef..00bbea4cb 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumb.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumb.java @@ -19,6 +19,7 @@ * Use this method to set the thumbnail of a regular or mask sticker set. * The format of the thumbnail file must match the format of the stickers in the set. * Returns True on success. + * @deprecated Use {@link SetStickerSetThumbnail} */ @EqualsAndHashCode(callSuper = false) @Getter @@ -28,6 +29,7 @@ @RequiredArgsConstructor @AllArgsConstructor @Builder +@Deprecated public class SetStickerSetThumb extends BotApiMethodBoolean { public static final String PATH = "setStickerSetThumb"; diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumbnail.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumbnail.java new file mode 100644 index 000000000..5c84039d2 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetThumbnail.java @@ -0,0 +1,73 @@ +package org.telegram.telegrambots.meta.api.methods.stickers; + +import lombok.*; +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; +import org.telegram.telegrambots.meta.api.objects.InputFile; +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; + +/** + * @author Ruben Bermudez + * @version 6.6 + * Use this method to set the thumbnail of a regular or mask sticker set. + * The format of the thumbnail file must match the format of the stickers in the set. + * Returns True on success. + */ +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@RequiredArgsConstructor +@AllArgsConstructor +@Builder +public class SetStickerSetThumbnail extends BotApiMethodBoolean { + public static final String PATH = "setStickerSetThumbnail"; + + public static final String NAME_FIELD = "name"; + public static final String USERID_FIELD = "user_id"; + public static final String THUMBNAIL_FIELD = "thumbnail"; + + /** + * Sticker set name + */ + @NonNull + private String name; + /** + * User identifier of the sticker set owner + */ + @NonNull + private Long userId; + /** + * Optional + * A .WEBP or .PNG image with the thumbnail, must be up to 128 kilobytes in size and have a width and height of + * exactly 100px, or a .TGS animation with a thumbnail up to 32 kilobytes in size + * (see https://core.telegram.org/stickers#animated-sticker-requirements for animated sticker technical requirements), + * or a WEBM video with the thumbnail up to 32 kilobytes in size; + * see https://core.telegram.org/stickers#video-sticker-requirements for video sticker technical requirements. + * Pass a file_id as a String to send a file that already exists on the Telegram servers, + * pass an HTTP URL as a String for Telegram to get a file from the Internet, + * or upload a new one using multipart/form-data. More information on Sending Files ». + * Animated and video sticker set thumbnails can't be uploaded via HTTP URL. + * + * If omitted, then the thumbnail is dropped and the first sticker is used as the thumbnail. + */ + private InputFile thumbnail; + + @Override + public String getMethod() { + return PATH; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (name.isEmpty()) { + throw new TelegramApiValidationException("name can't be null", this); + } + if (userId <= 0) { + throw new TelegramApiValidationException("userId can't be null", this); + } + if (thumbnail != null) { + thumbnail.validate(); + } + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetTitle.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetTitle.java new file mode 100644 index 000000000..d0c4c41a0 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/stickers/SetStickerSetTitle.java @@ -0,0 +1,57 @@ +package org.telegram.telegrambots.meta.api.methods.stickers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethodBoolean; +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; + +/** + * @author Ruben Bermudez + * @version 6.6 + * Use this method to set the title of a created sticker set. + * + * Returns True on success. + * + */ +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@RequiredArgsConstructor +@Builder +public class SetStickerSetTitle extends BotApiMethodBoolean { + public static final String PATH = "setStickerSetTitle"; + + public static final String NAME_FIELD = "name"; + public static final String TITLE_FIELD = "title"; + + /** + * Sticker set name + */ + @JsonProperty(NAME_FIELD) + @NonNull + private String name; + + /** + * Sticker set title, 1-64 characters + */ + @JsonProperty(TITLE_FIELD) + @NonNull + private String title; + + @Override + public String getMethod() { + return PATH; + } + + @Override + public void validate() throws TelegramApiValidationException { + if (name.isEmpty() || name.length() > 64) { + throw new TelegramApiValidationException("name must be between 1 and 64 characters", this); + } + if (title.isEmpty() || title.length() > 64) { + throw new TelegramApiValidationException("title must be between 1 and 64 characters", this); + } + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stickers/InputSticker.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stickers/InputSticker.java index c8e514766..047ae5601 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stickers/InputSticker.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stickers/InputSticker.java @@ -1,14 +1,7 @@ package org.telegram.telegrambots.meta.api.objects.stickers; import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.NonNull; -import lombok.Setter; -import lombok.Singular; -import lombok.ToString; +import lombok.*; import org.telegram.telegrambots.meta.api.interfaces.BotApiObject; import org.telegram.telegrambots.meta.api.interfaces.Validable; import org.telegram.telegrambots.meta.api.objects.InputFile; @@ -28,6 +21,7 @@ @ToString @NoArgsConstructor(force = true) @AllArgsConstructor +@Builder public class InputSticker implements BotApiObject, Validable { private static final String STICKER_FIELD = "sticker"; @@ -49,7 +43,8 @@ public class InputSticker implements BotApiObject, Validable { * List of 1-20 emoji associated with the sticker */ @JsonProperty(EMOJI_LIST_FIELD) - @Singular + @NonNull + @Singular("emoji") private List emojiList; /** @@ -73,11 +68,10 @@ public void validate() throws TelegramApiValidationException { throw new TelegramApiValidationException("Emoji list must have between 1 and 20 items", this); } - if (keywords.size() > 20) { + if (keywords != null && keywords.size() > 20) { throw new TelegramApiValidationException("Keywords list must have between 0 and 20 items", this); } - if (maskPosition != null) { maskPosition.validate(); } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/bots/AbsSender.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/bots/AbsSender.java index ccf9e8aeb..eaa53ba66 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/bots/AbsSender.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/bots/AbsSender.java @@ -12,10 +12,7 @@ import org.telegram.telegrambots.meta.api.methods.send.SendVideo; import org.telegram.telegrambots.meta.api.methods.send.SendVideoNote; import org.telegram.telegrambots.meta.api.methods.send.SendVoice; -import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; -import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; -import org.telegram.telegrambots.meta.api.methods.stickers.SetStickerSetThumb; -import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; +import org.telegram.telegrambots.meta.api.methods.stickers.*; import org.telegram.telegrambots.meta.api.methods.updates.GetWebhookInfo; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageMedia; import org.telegram.telegrambots.meta.api.objects.File; @@ -157,9 +154,19 @@ public final void getWebhookInfoAsync(SentCallback sentCallback) th * @param setStickerSetThumb Information of the sticker to set * @return If success, true is returned * @throws TelegramApiException If there is any error setting the thumb to the set + * @deprecated Use {{@link #execute(SetStickerSetThumbnail)}} */ + @Deprecated public abstract Boolean execute(SetStickerSetThumb setStickerSetThumb) throws TelegramApiException; + /** + * Set sticker set thumbnail (https://core.telegram.org/bots/api#setstickersetthumbnail) + * @param setStickerSetThumbnail Information of the sticker's thumbnail to set + * @return If success, true is returned + * @throws TelegramApiException If there is any error setting the thumbnail to the set + */ + public abstract Boolean execute(SetStickerSetThumbnail setStickerSetThumbnail) throws TelegramApiException; + /** * Creates a new sticker set (https://core.telegram.org/bots/api#createNewStickerSet) * @param createNewStickerSet Information of the sticker set to create @@ -244,9 +251,18 @@ public final void getWebhookInfoAsync(SentCallback sentCallback) th * Set sticker set thumb (https://core.telegram.org/bots/api#setStickerSetThumb) * @param setStickerSetThumb Information of the sticker to set * @return If success, true is returned + * @deprecated Use {@link #executeAsync(SetStickerSetThumbnail)} */ + @Deprecated public abstract CompletableFuture executeAsync(SetStickerSetThumb setStickerSetThumb); + /** + * Set sticker set thumb (https://core.telegram.org/bots/api#setstickersetthumbnail) + * @param setStickerSetThumbnail Information of the sticker's thumbnail to set + * @return If success, true is returned + */ + public abstract CompletableFuture executeAsync(SetStickerSetThumbnail setStickerSetThumbnail); + /** * Creates a new sticker set (https://core.telegram.org/bots/api#createNewStickerSet) * @param createNewStickerSet Information of the sticker set to create 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 e1a584be1..007a918b8 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -25,10 +25,7 @@ import org.telegram.telegrambots.meta.api.methods.send.SendVideo; import org.telegram.telegrambots.meta.api.methods.send.SendVideoNote; import org.telegram.telegrambots.meta.api.methods.send.SendVoice; -import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; -import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; -import org.telegram.telegrambots.meta.api.methods.stickers.SetStickerSetThumb; -import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; +import org.telegram.telegrambots.meta.api.methods.stickers.*; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageMedia; import org.telegram.telegrambots.meta.api.objects.File; import org.telegram.telegrambots.meta.api.objects.InputFile; @@ -666,6 +663,7 @@ public List execute(SendMediaGroup sendMediaGroup) throws TelegramApiEx } } + @SuppressWarnings("deprecation") @Override public Boolean execute(AddStickerToSet addStickerToSet) throws TelegramApiException { assertParamNotNull(addStickerToSet, "addStickerToSet"); @@ -705,6 +703,10 @@ public Boolean execute(AddStickerToSet addStickerToSet) throws TelegramApiExcept } } + /** + * {@inheritDoc} + */ + @Deprecated @Override public Boolean execute(SetStickerSetThumb setStickerSetThumb) throws TelegramApiException { assertParamNotNull(setStickerSetThumb, "setStickerSetThumb"); @@ -729,6 +731,31 @@ public Boolean execute(SetStickerSetThumb setStickerSetThumb) throws TelegramApi } } + @Override + public Boolean execute(SetStickerSetThumbnail setStickerSetThumbnail) throws TelegramApiException { + assertParamNotNull(setStickerSetThumbnail, "setStickerSetThumbnail"); + setStickerSetThumbnail.validate(); + try { + String url = getBaseUrl() + SetStickerSetThumbnail.PATH; + HttpPost httppost = configuredHttpPost(url); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setLaxMode(); + builder.setCharset(StandardCharsets.UTF_8); + builder.addTextBody(SetStickerSetThumbnail.USERID_FIELD, setStickerSetThumbnail.getUserId().toString(), TEXT_PLAIN_CONTENT_TYPE); + builder.addTextBody(SetStickerSetThumbnail.NAME_FIELD, setStickerSetThumbnail.getName(), TEXT_PLAIN_CONTENT_TYPE); + if (setStickerSetThumbnail.getThumbnail() != null) { + addInputFile(builder, setStickerSetThumbnail.getThumbnail(), SetStickerSetThumbnail.THUMBNAIL_FIELD, true); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); + + return setStickerSetThumbnail.deserializeResponse(sendHttpPostRequest(httppost)); + } catch (IOException e) { + throw new TelegramApiException("Unable to set sticker set thumbnail", e); + } + } + + @SuppressWarnings("deprecation") @Override public Boolean execute(CreateNewStickerSet createNewStickerSet) throws TelegramApiException { assertParamNotNull(createNewStickerSet, "createNewStickerSet"); @@ -1028,6 +1055,10 @@ public CompletableFuture executeAsync(AddStickerToSet addStickerToSet) return completableFuture; } + /** + * {@inheritDoc} + */ + @Deprecated @Override public CompletableFuture executeAsync(SetStickerSetThumb setStickerSetThumb) { CompletableFuture completableFuture = new CompletableFuture<>(); @@ -1041,6 +1072,19 @@ public CompletableFuture executeAsync(SetStickerSetThumb setStickerSetT return completableFuture; } + @Override + public CompletableFuture executeAsync(SetStickerSetThumbnail setStickerSetThumbnail) { + CompletableFuture completableFuture = new CompletableFuture<>(); + exe.submit(() -> { + try { + completableFuture.complete(execute(setStickerSetThumbnail)); + } catch (TelegramApiException e) { + completableFuture.completeExceptionally(e); + } + }); + return completableFuture; + } + @Override public CompletableFuture executeAsync(CreateNewStickerSet createNewStickerSet) { CompletableFuture completableFuture = new CompletableFuture<>(); From 0e967789fbd019b221fd63aea3e92811595d16c7 Mon Sep 17 00:00:00 2001 From: ruben Date: Sat, 17 Feb 2024 12:15:12 +0000 Subject: [PATCH 09/31] API 7.1 --- README.md | 8 ++--- TelegramBots.wiki/Changelog.md | 3 ++ TelegramBots.wiki/Getting-Started.md | 4 +-- TelegramBots.wiki/abilities/Simple-Example.md | 4 +-- pom.xml | 2 +- telegrambots-abilities/README.md | 4 +-- telegrambots-abilities/pom.xml | 4 +-- telegrambots-chat-session-bot/README.md | 4 +-- telegrambots-chat-session-bot/pom.xml | 4 +-- telegrambots-extensions/README.md | 4 +-- telegrambots-extensions/pom.xml | 4 +-- telegrambots-meta/pom.xml | 2 +- .../PromoteChatMember.java | 25 +++++++++++-- .../telegrambots/meta/api/objects/Chat.java | 20 ++++++++++- .../meta/api/objects/InaccessibleMessage.java | 2 +- .../api/objects/MaybeInaccessibleMessage.java | 2 ++ .../meta/api/objects/Message.java | 32 +++++++++++++++++ .../adminrights/ChatAdministratorRights.java | 14 ++++---- .../api/objects/boost/ChatBoostAdded.java | 35 +++++++++++++++++++ .../chatmember/ChatMemberAdministrator.java | 15 ++++---- .../meta/api/objects/stories/Story.java | 25 +++++++++++-- telegrambots-spring-boot-starter/README.md | 4 +-- telegrambots-spring-boot-starter/pom.xml | 4 +-- telegrambots/pom.xml | 4 +-- 24 files changed, 181 insertions(+), 48 deletions(-) create mode 100644 telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/boost/ChatBoostAdded.java diff --git a/README.md b/README.md index b7571426d..bacdcbfd1 100644 --- a/README.md +++ b/README.md @@ -27,18 +27,18 @@ Just import add the library to your project with one of these options: org.telegram telegrambots - 6.9.7.0 + 6.9.7.1 ``` 2. Using Gradle: ```gradle - implementation 'org.telegram:telegrambots:6.9.7.0' + implementation 'org.telegram:telegrambots:6.9.7.1' ``` - 3. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/6.9.7.0) - 4. Download the jar(including all dependencies) from [here](https://mvnrepository.com/artifact/org.telegram/telegrambots/6.9.7.0) + 3. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/6.9.7.1) + 4. Download the jar(including all dependencies) from [here](https://mvnrepository.com/artifact/org.telegram/telegrambots/6.9.7.1) In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index 085a2a814..8eab19607 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -1,3 +1,6 @@ +### 6.9.7.1 ### +1. Update Api version [7.1](https://core.telegram.org/bots/api-changelog#february-16-2024) + ### 6.9.7.0 ### 1. Update Api version [7.0](https://core.telegram.org/bots/api-changelog#december-29-2023) diff --git a/TelegramBots.wiki/Getting-Started.md b/TelegramBots.wiki/Getting-Started.md index 3cf08aaa8..a5c753d43 100644 --- a/TelegramBots.wiki/Getting-Started.md +++ b/TelegramBots.wiki/Getting-Started.md @@ -11,13 +11,13 @@ First you need to acquire the library and add it to your project. There are seve org.telegram telegrambots - 6.9.7.0 + 6.9.7.1 ``` * With **Gradle**: ```gradle - implementation 'org.telegram:telegrambots:6.9.7.0' + implementation 'org.telegram:telegrambots:6.9.7.1' ``` 2. Don't like the **Maven Central Repository**? It can also be grabbed from [Jitpack](https://jitpack.io/#rubenlagus/TelegramBots). diff --git a/TelegramBots.wiki/abilities/Simple-Example.md b/TelegramBots.wiki/abilities/Simple-Example.md index 4ab870386..d0d59cca0 100644 --- a/TelegramBots.wiki/abilities/Simple-Example.md +++ b/TelegramBots.wiki/abilities/Simple-Example.md @@ -9,12 +9,12 @@ As with any Java project, you will need to set your dependencies. org.telegram telegrambots-abilities - 6.9.7.0 + 6.9.7.1 ``` * **Gradle** ```gradle - implementation 'org.telegram:telegrambots-abilities:6.9.7.0' + implementation 'org.telegram:telegrambots-abilities:6.9.7.1' ``` * [JitPack](https://jitpack.io/#rubenlagus/TelegramBots) diff --git a/pom.xml b/pom.xml index bfefec003..2f1b44e2a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots pom - 6.9.7.0 + 6.9.7.1 telegrambots diff --git a/telegrambots-abilities/README.md b/telegrambots-abilities/README.md index 050b0a9c8..b001067c5 100644 --- a/telegrambots-abilities/README.md +++ b/telegrambots-abilities/README.md @@ -18,14 +18,14 @@ Usage org.telegram telegrambots-abilities - 6.9.7.0 + 6.9.7.1 ``` **Gradle** ```gradle - implementation 'org.telegram:telegrambots-abilities:6.9.7.0' + implementation 'org.telegram:telegrambots-abilities:6.9.7.1' ``` **JitPack** - [JitPack](https://jitpack.io/#rubenlagus/TelegramBots/v5.0.1) diff --git a/telegrambots-abilities/pom.xml b/telegrambots-abilities/pom.xml index c5dfd6f9e..176e8e573 100644 --- a/telegrambots-abilities/pom.xml +++ b/telegrambots-abilities/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots - 6.9.7.0 + 6.9.7.1 telegrambots-abilities @@ -83,7 +83,7 @@ org.telegram telegrambots - 6.9.7.0 + 6.9.7.1 org.apache.commons diff --git a/telegrambots-chat-session-bot/README.md b/telegrambots-chat-session-bot/README.md index 4af19ea15..bc3aeaeee 100644 --- a/telegrambots-chat-session-bot/README.md +++ b/telegrambots-chat-session-bot/README.md @@ -15,14 +15,14 @@ Usage org.telegram telegrambots-chat-session-bot - 6.9.7.0 + 6.9.7.1 ``` **Gradle** ```gradle - implementation 'org.telegram:telegrambots-chat-session-bot:6.9.7.0' + implementation 'org.telegram:telegrambots-chat-session-bot:6.9.7.1' ``` Motivation diff --git a/telegrambots-chat-session-bot/pom.xml b/telegrambots-chat-session-bot/pom.xml index b1239052c..e0b0e1c32 100644 --- a/telegrambots-chat-session-bot/pom.xml +++ b/telegrambots-chat-session-bot/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots - 6.9.7.0 + 6.9.7.1 telegrambots-chat-session-bot @@ -84,7 +84,7 @@ org.telegram telegrambots - 6.9.7.0 + 6.9.7.1 diff --git a/telegrambots-extensions/README.md b/telegrambots-extensions/README.md index df96a3470..e08465fe1 100644 --- a/telegrambots-extensions/README.md +++ b/telegrambots-extensions/README.md @@ -16,12 +16,12 @@ Just import add the library to your project with one of these options: org.telegram telegrambotsextensions - 6.9.7.0 + 6.9.7.1 ``` 2. Using Gradle: ```gradle - implementation 'org.telegram:telegrambotsextensions:6.9.7.0' + implementation 'org.telegram:telegrambotsextensions:6.9.7.1' ``` \ No newline at end of file diff --git a/telegrambots-extensions/pom.xml b/telegrambots-extensions/pom.xml index 89f4691e4..7d034276f 100644 --- a/telegrambots-extensions/pom.xml +++ b/telegrambots-extensions/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots - 6.9.7.0 + 6.9.7.1 telegrambotsextensions @@ -75,7 +75,7 @@ org.telegram telegrambots - 6.9.7.0 + 6.9.7.1 diff --git a/telegrambots-meta/pom.xml b/telegrambots-meta/pom.xml index e71774e59..20deb38ea 100644 --- a/telegrambots-meta/pom.xml +++ b/telegrambots-meta/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots - 6.9.7.0 + 6.9.7.1 telegrambots-meta diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/PromoteChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/PromoteChatMember.java index f2cfa74be..caf44d705 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/PromoteChatMember.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/PromoteChatMember.java @@ -44,9 +44,12 @@ public class PromoteChatMember extends BotApiMethodBoolean { private static final String CANPINMESSAGES_FIELD = "can_pin_messages"; private static final String CANPROMOTEMEMBERS_FIELD = "can_promote_members"; private static final String ISANONYMOUS_FIELD = "is_anonymous"; - private static final String CANMANAGECHAT_FIELD = "can_manage_chat"; + private static final String CAN_MANAGE_CHAT_FIELD = "can_manage_chat"; private static final String CANMANAGEVIDEOCHATS_FIELD = "can_manage_video_chats"; private static final String CANMANAGETOPICS_FIELD = "can_manage_topics"; + private static final String CAN_POST_STORIES_FIELD = "can_post_stories"; + private static final String CAN_EDIT_STORIES_FIELD = "can_edit_stories"; + private static final String CAN_DELETE_STORIES_FIELD = "can_delete_stories"; @JsonProperty(CHATID_FIELD) @NonNull @@ -80,7 +83,7 @@ public class PromoteChatMember extends BotApiMethodBoolean { * * Implied by any other administrator privilege */ - @JsonProperty(CANMANAGECHAT_FIELD) + @JsonProperty(CAN_MANAGE_CHAT_FIELD) private Boolean canManageChat; /** * Optional @@ -94,6 +97,24 @@ public class PromoteChatMember extends BotApiMethodBoolean { */ @JsonProperty(CANMANAGETOPICS_FIELD) private Boolean canManageTopics; + /** + * Optional + * True if the administrator can post stories to the chat + */ + @JsonProperty(CAN_POST_STORIES_FIELD) + private Boolean canPostStories; + /** + * Optional + * True if the administrator can edit stories posted by other users + */ + @JsonProperty(CAN_EDIT_STORIES_FIELD) + private Boolean canEditStories; + /** + * Optional + * True if the administrator can delete stories posted by other users + */ + @JsonProperty(CAN_DELETE_STORIES_FIELD) + private Boolean canDeleteStories; @Tolerate public void setChatId(@NonNull Long chatId) { diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java index 88fa77081..e270625f5 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java @@ -16,7 +16,7 @@ import java.util.List; /** - * This object represents a Telegram chat with an user or a group + * This object represents a Telegram chat with a user or a group * @author Ruben Bermudez * @version 6.1 */ @@ -65,6 +65,8 @@ public class Chat implements BotApiObject { private static final String HAS_VISIBLE_HISTORY_FIELD = "has_visible_history"; private static final String EMOJI_STATUS_CUSTOM_ID_FIELD = "emoji_status_custom_emoji_id"; private static final String EMOJI_STATUS_EXPIRATION_DATE_FIELD = "emoji_status_expiration_date"; + private static final String UNRESTRICT_BOOST_COUNT_FIELD = "unrestrict_boost_count"; + private static final String CUSTOM_EMOJI_STICKER_SET_NAME_FIELD = "custom_emoji_sticker_set_name"; private static final String USERCHATTYPE = "private"; @@ -311,6 +313,22 @@ public class Chat implements BotApiObject { */ @JsonProperty(HAS_VISIBLE_HISTORY_FIELD) private Boolean hasVisibleHistory; + /** + * Optional. + * For supergroups, the minimum number of boosts that a non-administrator + * user needs to add in order to ignore slow mode and chat permissions. + * Returned only in getChat. + */ + @JsonProperty(UNRESTRICT_BOOST_COUNT_FIELD) + private Integer unrestrictBoostCount; + /** + * Optional. + * For supergroups, the name of the group's custom emoji sticker set. Custom emoji from + * this set can be used by all users and bots in the group. + * Returned only in getChat. + */ + @JsonProperty(CUSTOM_EMOJI_STICKER_SET_NAME_FIELD) + private Boolean customEmojiStickerSetName; @JsonIgnore public Boolean isGroupChat() { diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/InaccessibleMessage.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/InaccessibleMessage.java index 88c04d7ca..262eaaaba 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/InaccessibleMessage.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/InaccessibleMessage.java @@ -41,7 +41,7 @@ public class InaccessibleMessage implements MaybeInaccessibleMessage { * The field can be used to differentiate regular and inaccessible messages. */ @JsonProperty(DATE_FIELD) - private Integer DATE; + private Integer date; @Override public Long getChatId() { diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java index fbb1fdd11..1d578a3ad 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MaybeInaccessibleMessage.java @@ -24,4 +24,6 @@ default boolean isSuperGroupMessage() { Long getChatId(); Integer getMessageId(); + + Integer getDate(); } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Message.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Message.java index 4628c1a4d..9826cce92 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Message.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Message.java @@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import org.telegram.telegrambots.meta.api.objects.boost.ChatBoostAdded; import org.telegram.telegrambots.meta.api.objects.forum.ForumTopicClosed; import org.telegram.telegrambots.meta.api.objects.forum.ForumTopicCreated; import org.telegram.telegrambots.meta.api.objects.forum.ForumTopicEdited; @@ -133,6 +134,9 @@ public class Message implements MaybeInaccessibleMessage { private static final String GIVEAWAY_FIELD = "giveaway"; private static final String GIVEAWAY_WINNERS_FIELD = "giveaway_winners"; private static final String GIVEAWAY_COMPLETED_FIELD = "giveaway_completed"; + private static final String REPLY_TO_STORY_FIELD = "reply_to_story"; + private static final String BOOST_ADDED_FIELD = "boost_added"; + private static final String SENDER_BOOST_COUNT_FIELD = "sender_boost_count"; /** * Integer Unique message identifier @@ -640,6 +644,24 @@ public class Message implements MaybeInaccessibleMessage { */ @JsonProperty(GIVEAWAY_COMPLETED_FIELD) private GiveawayCompleted giveawayCompleted; + /** + * Optional. + * For replies to a story, the original message + */ + @JsonProperty(REPLY_TO_STORY_FIELD) + private Story replyToStory; + /** + * Optional. + * The message is a service message about a user boosting the chat + */ + @JsonProperty(BOOST_ADDED_FIELD) + private ChatBoostAdded boostAdded; + /** + * Optional. + * If the sender of the message boosted the chat, the number of boosts added by the user + */ + @JsonProperty(SENDER_BOOST_COUNT_FIELD) + private Integer senderBoostCount; public List getEntities() { @@ -877,4 +899,14 @@ private boolean hasStory() { private boolean hasWriteAccessAllowed() { return writeAccessAllowed != null; } + + @JsonIgnore + private boolean hasReplyToStory() { + return replyToStory != null; + } + + @JsonIgnore + private boolean hasBoostAdded() { + return boostAdded != null; + } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/adminrights/ChatAdministratorRights.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/adminrights/ChatAdministratorRights.java index 3a9c2d2bb..78a246fc7 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/adminrights/ChatAdministratorRights.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/adminrights/ChatAdministratorRights.java @@ -20,7 +20,7 @@ public class ChatAdministratorRights implements BotApiObject, Validable { private static final String ISANONYMOUS_FIELD = "is_anonymous"; - private static final String CANMANAGECHAT_FIELD = "can_manage_chat"; + private static final String CAN_MANAGE_CHAT_FIELD = "can_manage_chat"; private static final String CANDELETEMESSAGES_FIELD = "can_delete_messages"; private static final String CANMANAGEVIDEOCHATS_FIELD = "can_manage_video_chats"; private static final String CANRESTRICTMEMBERS_FIELD = "can_restrict_members"; @@ -42,11 +42,11 @@ public class ChatAdministratorRights implements BotApiObject, Validable { @NonNull private Boolean isAnonymous; /** - * True, if the administrator can access the chat event log, chat statistics, boost list in channels, - * message statistics in channels, see channel members, see anonymous administrators in supergroups and ignore slow mode. - * Implied by any other administrator privilege + * True, if the administrator can access the chat event log, get boost list, + * see hidden supergroup and channel members, report spam messages and ignore slow mode. + * Implied by any other administrator privilege. */ - @JsonProperty(CANMANAGECHAT_FIELD) + @JsonProperty(CAN_MANAGE_CHAT_FIELD) @NonNull private Boolean canManageChat; /** @@ -113,13 +113,13 @@ public class ChatAdministratorRights implements BotApiObject, Validable { private Boolean canManageTopics; /** * Optional. - * True, if the administrator can post stories in the channel; channels only + * True, if the administrator can post stories to the chat */ @JsonProperty(CAN_POST_STORIES_FIELD) private Boolean canPostStories; /** * Optional. - * True, if the administrator can edit stories posted by other users; channels only + * True, if the administrator can edit stories posted by other users */ @JsonProperty(CAN_EDIT_STORIES_FIELD) private Boolean canEditStories; diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/boost/ChatBoostAdded.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/boost/ChatBoostAdded.java new file mode 100644 index 000000000..8c436eeb3 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/boost/ChatBoostAdded.java @@ -0,0 +1,35 @@ +package org.telegram.telegrambots.meta.api.objects.boost; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import org.telegram.telegrambots.meta.api.interfaces.BotApiObject; + +/** + * @author Ruben Bermudez + * @version 7.1 + * This object represents a service message about a user boosting a chat. + */ +@JsonDeserialize +@EqualsAndHashCode(callSuper = false) +@Getter +@Setter +@ToString +@NoArgsConstructor(force = true) +@AllArgsConstructor +@Builder +public class ChatBoostAdded implements BotApiObject { + private static final String BOOST_COUNT_FIELD = "boost_count"; + + /** + * Number of boosts added by the user + */ + @JsonProperty(BOOST_COUNT_FIELD) + private Integer boostCount; +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/chatmember/ChatMemberAdministrator.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/chatmember/ChatMemberAdministrator.java index 8258604bd..1a0d38c9c 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/chatmember/ChatMemberAdministrator.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/chatmember/ChatMemberAdministrator.java @@ -33,7 +33,7 @@ public class ChatMemberAdministrator implements ChatMember { private static final String CANBEEDITED_FIELD = "can_be_edited"; private static final String CUSTOMTITLE_FIELD = "custom_title"; private static final String ISANONYMOUS_FIELD = "is_anonymous"; - private static final String CANMANAGECHAT_FIELD = "can_manage_chat"; + private static final String CAN_MANAGE_CHAT_FIELD = "can_manage_chat"; private static final String CANPOSTMESSAGES_FIELD = "can_post_messages"; private static final String CANEDITMESSAGES_FIELD = "can_edit_messages"; private static final String CANDELETEMESSAGES_FIELD = "can_delete_messages"; @@ -74,11 +74,12 @@ public class ChatMemberAdministrator implements ChatMember { @JsonProperty(ISANONYMOUS_FIELD) private Boolean isAnonymous; /** - * True, if the administrator can access the chat event log, chat statistics, boost list in channels, - * message statistics in channels, see channel members, see anonymous administrators in supergroups and ignore slow mode. - * Implied by any other administrator privilege + * True, if the administrator can access the chat event log, get boost list, + * see hidden supergroup and channel members, report spam messages and ignore slow mode. + * + * Implied by any other administrator privilege. */ - @JsonProperty(CANMANAGECHAT_FIELD) + @JsonProperty(CAN_MANAGE_CHAT_FIELD) private Boolean canManageChat; /** * True, if the administrator can post messages in the channel; channels only @@ -135,13 +136,13 @@ public class ChatMemberAdministrator implements ChatMember { private Boolean canManageTopics; /** * Optional. - * True, if the administrator can post stories in the channel; channels only + * True, if the administrator can post stories to the chat */ @JsonProperty(CAN_POST_STORIES_FIELD) private Boolean canPostStories; /** * Optional. - * True, if the administrator can edit stories posted by other users; channels only + * True, if the administrator can edit stories posted by other users */ @JsonProperty(CAN_EDIT_STORIES_FIELD) private Boolean canEditStories; diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java index f968dcaeb..e9a04ebb8 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java @@ -1,18 +1,39 @@ package org.telegram.telegrambots.meta.api.objects.stories; -import lombok.Builder; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import org.telegram.telegrambots.meta.api.interfaces.BotApiObject; +import org.telegram.telegrambots.meta.api.objects.Chat; +/** + * @author Ruben Bermudez + * @version 7.1 + * + * This object represents a story. + */ @EqualsAndHashCode(callSuper = false) @Getter @Setter @ToString @NoArgsConstructor -@Builder +@AllArgsConstructor public class Story implements BotApiObject { + private static final String CHAT_FIELD = "chat"; + private static final String ID_FIELD = "id"; + + /** + * Chat that posted the story + */ + @JsonProperty(CHAT_FIELD) + private Chat chat; + /** + * Unique identifier for the story in the chat + */ + @JsonProperty(ID_FIELD) + private Integer id; } diff --git a/telegrambots-spring-boot-starter/README.md b/telegrambots-spring-boot-starter/README.md index 27a6e80d5..3a34a7571 100644 --- a/telegrambots-spring-boot-starter/README.md +++ b/telegrambots-spring-boot-starter/README.md @@ -18,14 +18,14 @@ Usage org.telegram telegrambots-spring-boot-starter - 6.9.7.0 + 6.9.7.1 ``` **Gradle** ```gradle - implementation 'org.telegram:telegrambots-spring-boot-starter:6.9.7.0' + implementation 'org.telegram:telegrambots-spring-boot-starter:6.9.7.1' ``` Motivation diff --git a/telegrambots-spring-boot-starter/pom.xml b/telegrambots-spring-boot-starter/pom.xml index 2e4a95951..7d4648cdf 100644 --- a/telegrambots-spring-boot-starter/pom.xml +++ b/telegrambots-spring-boot-starter/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots - 6.9.7.0 + 6.9.7.1 telegrambots-spring-boot-starter @@ -70,7 +70,7 @@ UTF-8 UTF-8 - 6.9.7.0 + 6.9.7.1 2.7.18 1.6 diff --git a/telegrambots/pom.xml b/telegrambots/pom.xml index d3b1276f8..4f996f2aa 100644 --- a/telegrambots/pom.xml +++ b/telegrambots/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots - 6.9.7.0 + 6.9.7.1 telegrambots @@ -91,7 +91,7 @@ org.telegram telegrambots-meta - 6.9.7.0 + 6.9.7.1 org.projectlombok From e344f9c8407663ca57d7200dc995cace430bed3e Mon Sep 17 00:00:00 2001 From: ruben Date: Sat, 17 Feb 2024 12:17:01 +0000 Subject: [PATCH 10/31] API 7.1 --- TelegramBots.wiki/Changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index 8eab19607..1645f96dc 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -1,5 +1,6 @@ ### 6.9.7.1 ### 1. Update Api version [7.1](https://core.telegram.org/bots/api-changelog#february-16-2024) +2. Bug fixing: #1276 #1273 #1303 #1282 #1310 #1315 #1322 #1323 ### 6.9.7.0 ### 1. Update Api version [7.0](https://core.telegram.org/bots/api-changelog#december-29-2023) From 6406b1f559f2ee8fdca330c390580bac6493cdf9 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Thu, 29 Feb 2024 23:50:29 -0400 Subject: [PATCH 11/31] Test cases for ASDC assignment-1 --- .../commandbot/commands/BotCommandTest.java | 44 +++++++++++++++++++ .../commands/TimedSendLongPollingBotTest.java | 42 ++++++++++++++++++ ....java => PartialBotApiMethodJsonTest.java} | 0 .../starter/TestSpringWebhookBot.java | 34 ++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java create mode 100644 telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java rename telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/{PartialBotApiMethodTest.java => PartialBotApiMethodJsonTest.java} (100%) create mode 100644 telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java diff --git a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java new file mode 100644 index 000000000..21c1de1b8 --- /dev/null +++ b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java @@ -0,0 +1,44 @@ +package org.telegram.telegrambots.extensions.bots.commandbot.commands; +import org.junit.jupiter.api.Test; +import org.telegram.telegrambots.meta.api.objects.Chat; +import org.telegram.telegrambots.meta.api.objects.User; +import org.telegram.telegrambots.meta.bots.AbsSender; +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class BotCommandTest +{ + + @Test + public void testToString() + { + // Act + BotCommand commandidentifier = new TestBotCommand("test","Description"); + + // Assert + assertEquals("/test\nDescription",commandidentifier.toString()); + } + + @Test + public void testcommandIdentifierStartsWithCOMMAND_INIT_CHARACTER() + { + // Arrange + String commandidentifier = "/YuktaGurnani"; + + // Act + String result = commandidentifier.substring(1); + + // Assert + assertEquals("YuktaGurnani",result); + } + private static class TestBotCommand extends BotCommand + { + public TestBotCommand(String commandIdentifier, String description){ + super(commandIdentifier,description); + } + public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) + { + + } + } +} diff --git a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java new file mode 100644 index 000000000..3f628010c --- /dev/null +++ b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java @@ -0,0 +1,42 @@ +package org.telegram.telegrambots.extensions.bots.commandbot.commands; + +import org.junit.jupiter.api.Test; +import org.telegram.telegrambots.extensions.bots.timedbot.TimedSendLongPollingBot; +import org.telegram.telegrambots.meta.api.objects.Update; +import static org.mockito.Mockito.mock; + + +public class TimedSendLongPollingBotTest +{ + @Test + public void testMessageProcessing() + { + // Arrange + TimedSendLongPollingBot testbot = new TimedSendLongPollingBot() + { + @Override + public String getBotUsername() { + return null; + } + + @Override + public void onUpdateReceived(Update update) + { + + } + @Override + public void sendMessageCallback(Long chatId, Object messageRequest) + { + + } + }; + + // Act + Long chatId = 002L; + Object messageRequest = mock(Object.class); + + // Assert + testbot.sendTimed(chatId,messageRequest); + testbot.finish(); + } +} diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodTest.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.java similarity index 100% rename from telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodTest.java rename to telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.java diff --git a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java new file mode 100644 index 000000000..f5b57dd35 --- /dev/null +++ b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java @@ -0,0 +1,34 @@ +package org.telegram.telegrambots.starter; + +import org.junit.jupiter.api.Test; +import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class TestSpringWebhookBot +{ + @Test + void nullTestSpringWebhookBot() + { + // Arrange and Act + SpringWebhookBot.TestSpringWebhookBot webhook = new SpringWebhookBot.TestSpringWebhookBot(null); + + // Assert + assertNull(webhook.getSetWebhook()); + } + + @Test + void NotnullTestSpringWebhookBot() + { + // Arrange + SetWebhook setWebhook = new SetWebhook(); + + // Act + SpringWebhookBot.TestSpringWebhookBot webhook = new SpringWebhookBot.TestSpringWebhookBot(setWebhook); + + //Assert + assertNotNull(webhook.getSetWebhook()); + } + +} From 11b556937df368f5d747dae5cc5ede0404f35eb3 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:51:11 -0300 Subject: [PATCH 12/31] Implementation code smell - 1 - Refactored code for Magic number using explaining variables technique --- .../telegram/abilitybots/api/objects/MessageContext.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java index fc650f854..144d3078e 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java @@ -22,6 +22,9 @@ public class MessageContext { private final Update update; private final BaseAbilityBot bot; + // Refactored code-Added thirdArg as explaining variable to remove magic number 2 + int thirdArg = 2; + private MessageContext(Update update, User user, Long chatId, BaseAbilityBot bot, String[] arguments) { this.user = user; this.chatId = chatId; @@ -88,7 +91,9 @@ public String secondArg() { */ public String thirdArg() { checkLength(); - return arguments[2 % arguments.length]; + + // Replaced magic number 2 with thirdArg + return arguments[thirdArg % arguments.length]; } /** From e80a519561c2bf2c9fb77b43cdddeb112852f73c Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:55:07 -0300 Subject: [PATCH 13/31] Implementation code smell - 2 - Refactored code for Complex Method using decomposing conditional technique --- pom.xml | 29 ++++++++ telegrambots-abilities/pom.xml | 5 ++ .../methods/invoices/CreateInvoiceLink.java | 72 +++++++++++++++++-- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 2f1b44e2a..4c15ebb27 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,11 @@ + + org.telegram + telegrambots + 6.9.7.1 + org.projectlombok lombok @@ -135,6 +140,11 @@ slf4j-api ${slf4j.version} + + ch.qos.logback + logback-classic + 1.2.6 + org.apache.commons commons-lang3 @@ -218,6 +228,25 @@ ${java.home}/bin/javadoc + + org.jacoco + jacoco-maven-plugin + 0.8.5 + + + + prepare-agent + + + + report + test + + report + + + + org.sonatype.plugins nexus-staging-maven-plugin diff --git a/telegrambots-abilities/pom.xml b/telegrambots-abilities/pom.xml index 176e8e573..f4f4300ee 100644 --- a/telegrambots-abilities/pom.xml +++ b/telegrambots-abilities/pom.xml @@ -80,6 +80,11 @@ + + ch.qos.logback + logback-classic + 1.2.3 + org.telegram telegrambots diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java index d2b3e5c8c..ff760fea0 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java @@ -127,35 +127,95 @@ public class CreateInvoiceLink extends BotApiMethod { @Singular private List suggestedTipAmounts; + @Override public String getMethod() { return PATH; } - @Override - public void validate() throws TelegramApiValidationException { + // Old code - validate() method was containing complex method and complex conditional code smells + +// @Override +// public void validate() throws TelegramApiValidationException { +// if (StringUtils.isEmpty(title) || title.length() > 32) { +// throw new TelegramApiValidationException("Title parameter can't be empty or longer than 32 chars", this); +// } +// if (StringUtils.isEmpty(description) || description.length() > 255) { +// throw new TelegramApiValidationException("Description parameter can't be empty or longer than 255 chars", this); +// } +// if (StringUtils.isEmpty(payload)) { +// throw new TelegramApiValidationException("Payload parameter can't be empty", this); +// } +// if (StringUtils.isEmpty(providerToken)) { +// throw new TelegramApiValidationException("ProviderToken parameter can't be empty", this); +// } +// if (StringUtils.isEmpty(currency)) { +// throw new TelegramApiValidationException("Currency parameter can't be empty", this); +// } +// if (prices.isEmpty()) { +// throw new TelegramApiValidationException("Prices parameter can't be empty", this); +// } else { +// for (LabeledPrice price : prices) { +// price.validate(); +// } +// } +// if (suggestedTipAmounts != null && !suggestedTipAmounts.isEmpty() && suggestedTipAmounts.size() > 4) { +// throw new TelegramApiValidationException("No more that 4 suggested tips allowed", this); +// } +// } + + // Refactored validate method using decompose conditional refactoring technique +@Override +public void validate() throws TelegramApiValidationException { + validateTitle(); + validateDescription(); + validatePayload(); + validateProviderToken(); + validateCurrency(); + validatePrices(); + validateSuggestedTipAmounts(); +} + + private void validateTitle() throws TelegramApiValidationException { if (StringUtils.isEmpty(title) || title.length() > 32) { throw new TelegramApiValidationException("Title parameter can't be empty or longer than 32 chars", this); } + } + + private void validateDescription() throws TelegramApiValidationException { if (StringUtils.isEmpty(description) || description.length() > 255) { throw new TelegramApiValidationException("Description parameter can't be empty or longer than 255 chars", this); } + } + + private void validatePayload() throws TelegramApiValidationException { if (StringUtils.isEmpty(payload)) { throw new TelegramApiValidationException("Payload parameter can't be empty", this); } + } + + private void validateProviderToken() throws TelegramApiValidationException { if (StringUtils.isEmpty(providerToken)) { throw new TelegramApiValidationException("ProviderToken parameter can't be empty", this); } + } + + private void validateCurrency() throws TelegramApiValidationException { if (StringUtils.isEmpty(currency)) { throw new TelegramApiValidationException("Currency parameter can't be empty", this); } + } + + private void validatePrices() throws TelegramApiValidationException { if (prices.isEmpty()) { throw new TelegramApiValidationException("Prices parameter can't be empty", this); - } else { - for (LabeledPrice price : prices) { - price.validate(); - } } + for (LabeledPrice price : prices) { + price.validate(); + } + } + + private void validateSuggestedTipAmounts() throws TelegramApiValidationException { if (suggestedTipAmounts != null && !suggestedTipAmounts.isEmpty() && suggestedTipAmounts.size() > 4) { throw new TelegramApiValidationException("No more that 4 suggested tips allowed", this); } From 3e8d8c73e0b2b8787e09a11f67a4347d6656f7b3 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:55:42 -0300 Subject: [PATCH 14/31] Implementation code smell - 3 - Refactored code for Long Statement using renaming method and introducing explaining variable technique --- .../telegram/abilitybots/api/objects/Ability.java | 14 +++++++++++--- .../abilitybots/api/util/AbilityUtils.java | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java index 6d77687a8..326ee7145 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java @@ -19,7 +19,7 @@ import static java.lang.String.format; import static java.util.Objects.hash; import static java.util.Optional.ofNullable; -import static org.telegram.abilitybots.api.util.AbilityUtils.isValidCommandName; +import static org.telegram.abilitybots.api.util.AbilityUtils.isValCmdName; /** * An ability is a fully-fledged bot action that contains all the necessary information to process: @@ -50,10 +50,18 @@ public final class Ability { private final List replies; private final List> flags; + @SafeVarargs private Ability(String name, String info, Locality locality, Privacy privacy, int argNum, boolean statsEnabled, Consumer action, Consumer postAction, List replies, Predicate... flags) { - checkArgument(isValidCommandName(name), "Method name can only contain alpha-numeric characters and underscores," + - " cannot be longer than 31 characters, empty or null", name); + // Refactored checkArgument to remove long statement code smell by renaming method name and introducing explaining variables. + // Explaining variables - INVALID_METHOD_NAME_ERROR and METHOD_NAME_CONSTRAINT + final String INVALID_METHOD_NAME_ERROR = "Method name can only contain alpha-numeric characters and underscores, " ; + final String METHOD_NAME_CONSTRAINT = "cannot be longer than 31 characters, and cannot be null"; + + // isValueCommandName renamed to isValCmdName + checkArgument(isValCmdName(name), INVALID_METHOD_NAME_ERROR+METHOD_NAME_CONSTRAINT, name); + + this.name = name; this.info = info; diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java index 39f0f2101..1dea62748 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java @@ -302,7 +302,9 @@ public static boolean isValidCommand(String command){ * @param commandName the command name to be checked for validity * @return whether the command name is valid */ - public static boolean isValidCommandName(String commandName){ + + // Refactored code by renaming isValueCommandName to isValCmdName method + public static boolean isValCmdName(String commandName){ if (commandName == null || commandName.length() > 31) return false; return commandName.matches("[A-Za-z_0-9]+"); } From aafebe61b4da01526e757db91a31b5bf86da130d Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:56:04 -0300 Subject: [PATCH 15/31] Implementation code smell - 4 - Refactored test code for Magic number using explaining variables technique --- .../org/telegram/abilitybots/api/objects/AbilityTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java index 3f8d09550..41de5d281 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java @@ -10,9 +10,11 @@ import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder; class AbilityTest { + // Refactored magic number -4 using extract method technique + final int inputNegativeNumber = -4; @Test void argumentsCannotBeNegative() { - Assertions.assertThrows(IllegalArgumentException.class, () -> getDefaultBuilder().input(-4).build()); + Assertions.assertThrows(IllegalArgumentException.class, () -> getDefaultBuilder().input(inputNegativeNumber).build()); } @Test From 4344b84ae4fa22d8fb68fe8912a324afd7e77209 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:56:24 -0300 Subject: [PATCH 16/31] Implementation code smell - 5 - Refactored test code for Empty Catch clause using explaining variables technique --- .../telegram/telegrambots/meta/test/TestTelegramApi.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java index 38f155ee1..3c7ea3692 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java @@ -14,12 +14,16 @@ import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Ruben Bermudez * @version 1.0 */ class TestTelegramApi { + private static final Logger logger = LoggerFactory.getLogger(TestTelegramApi.class); private Webhook webhook; @BeforeEach @@ -77,7 +81,9 @@ public String getBotToken() { }, new SetWebhook()); fail(); } catch (TelegramApiException e) { - // Ignore + // Introducing logger as explaining variable to resolve empty catch clause code smell + logger.error("Exception occurred while registering bot: {}", e.getMessage()); + } } } From b9c94fd45cc010c3738c52d55f06e1926ce30e0c Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:56:46 -0300 Subject: [PATCH 17/31] Implementation code smell - 6 - Refactored test code for Long Identifier using renaming variables technique --- .../adminrights/GetMyDefaultAdministratorRightsTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java index 08d4943ce..e6583ae42 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java @@ -52,12 +52,13 @@ public void testGetMyDefaultAdministratorRightsDeserializeValidResponse() { " }\n" + "}"; - GetMyDefaultAdministratorRights getMyDefaultAdministratorRights = GetMyDefaultAdministratorRights + // Refactored long identifier code smell by renaming getMyDefaultAdministratorRights variable name to administratorRights + GetMyDefaultAdministratorRights administratorRights = GetMyDefaultAdministratorRights .builder() .build(); try { - ChatAdministratorRights result = getMyDefaultAdministratorRights.deserializeResponse(responseText); + ChatAdministratorRights result = administratorRights.deserializeResponse(responseText); assertNotNull(result); assertEquals(true, result.getIsAnonymous()); assertEquals(true, result.getCanManageChat()); From 3d3099be9dac850374f92937e67e00386fc63a84 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:57:26 -0300 Subject: [PATCH 18/31] Design code smell - 1 - Refactored code for Insufficient Modularization using extract class technique --- .../abilitybots/api/sender/DefaultSender.java | 34 ++---------- .../api/sender/DefaultSender1.java | 53 +++++++++++++++++++ .../abilitybots/api/sender/MessageSender.java | 11 ---- .../api/sender/MessageSender1.java | 39 ++++++++++++++ 4 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java create mode 100644 telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java index 8e0071a5c..d4bb2cbc2 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java @@ -27,6 +27,10 @@ public class DefaultSender implements MessageSender { private DefaultAbsSender bot; + /* + * Refactored code - Insufficient Modularization design smell refactored by moving the methods related to send functionality + * and extracting class DefaultSender1. + */ public DefaultSender(DefaultAbsSender bot) { this.bot = bot; } @@ -106,34 +110,4 @@ public void getWebhookInfoAsync(SentCallback sentCallback) throws T public Message sendDocument(SendDocument sendDocument) throws TelegramApiException { return bot.execute(sendDocument); } - - @Override - public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException { - return bot.execute(sendPhoto); - } - - @Override - public Message sendVideo(SendVideo sendVideo) throws TelegramApiException { - return bot.execute(sendVideo); - } - - @Override - public Message sendSticker(SendSticker sendSticker) throws TelegramApiException { - return bot.execute(sendSticker); - } - - @Override - public Message sendAudio(SendAudio sendAudio) throws TelegramApiException { - return bot.execute(sendAudio); - } - - @Override - public Message sendVoice(SendVoice sendVoice) throws TelegramApiException { - return bot.execute(sendVoice); - } - - @Override - public Message sendVideoNote(SendVideoNote sendVideoNote) { - return null; - } } diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java new file mode 100644 index 000000000..9516ef530 --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java @@ -0,0 +1,53 @@ +package org.telegram.abilitybots.api.sender; +import org.telegram.telegrambots.meta.api.methods.BotApiMethod; +import org.telegram.telegrambots.meta.api.methods.send.*; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.bots.DefaultAbsSender; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.meta.updateshandlers.SentCallback; + +import java.io.Serializable; +public class DefaultSender1 implements MessageSender1{ + private static final String TAG = MessageSender.class.getName(); + + private DefaultAbsSender bot; + + @Override + public , Callback extends SentCallback> void executeAsync(Method method, Callback callback) throws TelegramApiException { + + } + + @Override + public > T execute(Method method) throws TelegramApiException { + return null; + } + + @Override + public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException { + return bot.execute(sendPhoto); + } + + @Override + public Message sendVideo(SendVideo sendVideo) throws TelegramApiException { + return bot.execute(sendVideo); + } + + @Override + public Message sendSticker(SendSticker sendSticker) throws TelegramApiException { + return bot.execute(sendSticker); + } + + @Override + public Message sendAudio(SendAudio sendAudio) throws TelegramApiException { + return bot.execute(sendAudio); + } + + @Override + public Message sendVoice(SendVoice sendVoice) throws TelegramApiException { + return bot.execute(sendVoice); + } + @Override + public Message sendVideoNote(SendVideoNote sendVideoNote) { + return null; + } +} diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java index b3ba74bcc..372734a6c 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java @@ -54,15 +54,4 @@ public interface MessageSender { Message sendDocument(SendDocument sendDocument) throws TelegramApiException; - Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException; - - Message sendVideo(SendVideo sendVideo) throws TelegramApiException; - - Message sendAudio(SendAudio sendAudio) throws TelegramApiException; - - Message sendVoice(SendVoice sendVoice) throws TelegramApiException; - - Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException; - - Message sendSticker(SendSticker sendSticker) throws TelegramApiException; } diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java new file mode 100644 index 000000000..53611c6bb --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java @@ -0,0 +1,39 @@ +package org.telegram.abilitybots.api.sender; + + +import org.telegram.telegrambots.meta.api.methods.BotApiMethod; +import org.telegram.telegrambots.meta.api.methods.send.*; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.bots.DefaultAbsSender; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.meta.updateshandlers.SentCallback; + +import java.io.Serializable; + +/** + * A sender interface that replicates {@link DefaultAbsSender} methods. + * + * @author Abbas Abou Daya + */ +public interface MessageSender1 { + /* + * Refactored code - Interfaces related to send functionality. + */ + , Callback extends SentCallback> void executeAsync(Method method, Callback callback) throws TelegramApiException; + + > T execute(Method method) throws TelegramApiException; + + + Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException; + + Message sendVideo(SendVideo sendVideo) throws TelegramApiException; + + Message sendAudio(SendAudio sendAudio) throws TelegramApiException; + + Message sendVoice(SendVoice sendVoice) throws TelegramApiException; + + Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException; + + Message sendSticker(SendSticker sendSticker) throws TelegramApiException; +} + From 848266c985b329314629b3a17a64f15eaae0dcb8 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:57:59 -0300 Subject: [PATCH 19/31] Design code smell - 2 - Refactored code for Unnecessary Abstraction using moving pull-up method technique --- .../meta/api/methods/BotApiMethod.java | 16 ++++++++++++++-- .../meta/api/methods/PartialBotApiMethod.java | 6 +++++- .../api/methods/PartialBotApiMethodJsonTest.java | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java index 6ca13881c..46d8d992c 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; +import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import java.io.Serializable; @@ -13,7 +14,18 @@ */ @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) -public abstract class BotApiMethod extends PartialBotApiMethod { - protected static final String METHOD_FIELD = "method"; +public class BotApiMethod extends PartialBotApiMethod { + /* + * Refactored code to remove unnecessary abstraction by moving METHOD_FIELD to PartialBotApiMethod class. + */ + @Override + public T deserializeResponse(String answer) throws TelegramApiRequestException { + return null; + } + + @Override + public String getMethod() { + return null; + } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java index a1e9d1f19..740134077 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java @@ -22,6 +22,9 @@ public abstract class PartialBotApiMethod implements Val @JsonIgnore protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + // Refactored code - Pulled up METHOD_FIELD variable from BotApiMethod + protected static final String METHOD_FIELD = "method"; + /** * Deserialize a json answer to the response type to a method * @param answer Json answer received @@ -62,6 +65,7 @@ private T deserializeResponseInternal(String answer, JavaType type) throws Teleg * Getter for method path (that is the same as method name) * @return Method path */ - @JsonProperty(BotApiMethod.METHOD_FIELD) + // Refactored Code + @JsonProperty(METHOD_FIELD) public abstract String getMethod(); } diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.java index 9e3457981..23b9bf55c 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.java @@ -16,7 +16,7 @@ * @author Roman Bondar */ -public class PartialBotApiMethodTest { +public class PartialBotApiMethodJsonTest { @Test public void deserializeUpdateWithWriteAccessAllowedNotThrowApiException() throws IOException { From 240fb70556973b9f7fe72db33ead7f9da0307dfa Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Sat, 23 Mar 2024 08:58:19 -0300 Subject: [PATCH 20/31] Design code smell - 3 - Refactored code for Cyclically-dependent Modularization using change bidirectional association to unidirectional association technique --- .../telegram/telegrambots/meta/api/objects/Chat.java | 5 +++-- .../telegrambots/meta/api/objects/stories/Story.java | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java index e270625f5..d3b3001a2 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java @@ -30,7 +30,8 @@ @AllArgsConstructor public class Chat implements BotApiObject { - private static final String ID_FIELD = "id"; + // Variable name changed from CHAT_FIELD to CHAT_ID_FIELD for refactoring + private static final String CHAT_ID_FIELD = "id"; private static final String TYPE_FIELD = "type"; private static final String TITLE_FIELD = "title"; private static final String USERNAME_FIELD = "username"; @@ -80,7 +81,7 @@ public class Chat implements BotApiObject { * have difficulty/silent defects in interpreting it. But it smaller than 52 bits, * so a signed 64 bit integer or double-precision float type are safe for storing this identifier. */ - @JsonProperty(ID_FIELD) + @JsonProperty(CHAT_ID_FIELD) @NonNull private Long id; /** diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java index e9a04ebb8..f16bfe95e 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java @@ -23,14 +23,18 @@ @NoArgsConstructor @AllArgsConstructor public class Story implements BotApiObject { - private static final String CHAT_FIELD = "chat"; + /* + * Refactored code - Using CHAT_ID_FIELD instead of CHAT_FIELD to change bidirectional association to unidirectional association + */ + + private static final String CHAT_ID_FIELD = "chat_id"; private static final String ID_FIELD = "id"; /** * Chat that posted the story */ - @JsonProperty(CHAT_FIELD) - private Chat chat; + @JsonProperty(CHAT_ID_FIELD) + private Long chatId; /** * Unique identifier for the story in the chat */ From 5fba3b27f03d170169f959552d611783066fa082 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:13:43 -0300 Subject: [PATCH 21/31] Removed/renamed files for design smell-1 --- .../abilitybots/api/sender/DefaultSender.java | 54 +++++++++++-------- .../api/sender/DefaultSender1.java | 53 ------------------ .../api/sender/DefaultSticker.java | 41 ++++++++++++++ .../abilitybots/api/sender/MessageSender.java | 22 ++++---- ...essageSender1.java => StickerMessage.java} | 26 ++++----- .../commandbot/commands/BotCommandTest.java | 44 --------------- .../starter/TestSpringWebhookBot.java | 34 ------------ 7 files changed, 93 insertions(+), 181 deletions(-) delete mode 100644 telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java create mode 100644 telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSticker.java rename telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/{MessageSender1.java => StickerMessage.java} (52%) delete mode 100644 telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java delete mode 100644 telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java index d4bb2cbc2..0599e24f7 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender.java @@ -3,9 +3,6 @@ import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto; import org.telegram.telegrambots.meta.api.methods.send.*; -import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; -import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; -import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; import org.telegram.telegrambots.meta.api.objects.File; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.User; @@ -27,10 +24,6 @@ public class DefaultSender implements MessageSender { private DefaultAbsSender bot; - /* - * Refactored code - Insufficient Modularization design smell refactored by moving the methods related to send functionality - * and extracting class DefaultSender1. - */ public DefaultSender(DefaultAbsSender bot) { this.bot = bot; } @@ -45,21 +38,6 @@ public > T execute(Method return bot.execute(method); } - @Override - public Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException { - return bot.execute(addStickerToSet); - } - - @Override - public Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException { - return bot.execute(createNewStickerSet); - } - - @Override - public File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException { - return bot.execute(uploadStickerFile); - } - @Override public Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException { return bot.execute(setChatPhoto); @@ -110,4 +88,34 @@ public void getWebhookInfoAsync(SentCallback sentCallback) throws T public Message sendDocument(SendDocument sendDocument) throws TelegramApiException { return bot.execute(sendDocument); } -} + + @Override + public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException { + return bot.execute(sendPhoto); + } + + @Override + public Message sendVideo(SendVideo sendVideo) throws TelegramApiException { + return bot.execute(sendVideo); + } + + @Override + public Message sendSticker(SendSticker sendSticker) throws TelegramApiException { + return bot.execute(sendSticker); + } + + @Override + public Message sendAudio(SendAudio sendAudio) throws TelegramApiException { + return bot.execute(sendAudio); + } + + @Override + public Message sendVoice(SendVoice sendVoice) throws TelegramApiException { + return bot.execute(sendVoice); + } + + @Override + public Message sendVideoNote(SendVideoNote sendVideoNote) { + return null; + } +} \ No newline at end of file diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java deleted file mode 100644 index 9516ef530..000000000 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSender1.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.telegram.abilitybots.api.sender; -import org.telegram.telegrambots.meta.api.methods.BotApiMethod; -import org.telegram.telegrambots.meta.api.methods.send.*; -import org.telegram.telegrambots.meta.api.objects.Message; -import org.telegram.telegrambots.bots.DefaultAbsSender; -import org.telegram.telegrambots.meta.exceptions.TelegramApiException; -import org.telegram.telegrambots.meta.updateshandlers.SentCallback; - -import java.io.Serializable; -public class DefaultSender1 implements MessageSender1{ - private static final String TAG = MessageSender.class.getName(); - - private DefaultAbsSender bot; - - @Override - public , Callback extends SentCallback> void executeAsync(Method method, Callback callback) throws TelegramApiException { - - } - - @Override - public > T execute(Method method) throws TelegramApiException { - return null; - } - - @Override - public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException { - return bot.execute(sendPhoto); - } - - @Override - public Message sendVideo(SendVideo sendVideo) throws TelegramApiException { - return bot.execute(sendVideo); - } - - @Override - public Message sendSticker(SendSticker sendSticker) throws TelegramApiException { - return bot.execute(sendSticker); - } - - @Override - public Message sendAudio(SendAudio sendAudio) throws TelegramApiException { - return bot.execute(sendAudio); - } - - @Override - public Message sendVoice(SendVoice sendVoice) throws TelegramApiException { - return bot.execute(sendVoice); - } - @Override - public Message sendVideoNote(SendVideoNote sendVideoNote) { - return null; - } -} diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSticker.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSticker.java new file mode 100644 index 000000000..31126c70e --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/DefaultSticker.java @@ -0,0 +1,41 @@ +package org.telegram.abilitybots.api.sender; +import org.telegram.telegrambots.meta.api.methods.BotApiMethod; +import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; +import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; +import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; +import org.telegram.telegrambots.meta.api.objects.File; +import org.telegram.telegrambots.bots.DefaultAbsSender; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.meta.updateshandlers.SentCallback; + +import java.io.Serializable; +public class DefaultSticker implements StickerMessage { + private static final String TAG = MessageSender.class.getName(); + + private DefaultAbsSender bot; + + @Override + public , Callback extends SentCallback> void executeAsync(Method method, Callback callback) throws TelegramApiException { + + } + + @Override + public > T execute(Method method) throws TelegramApiException { + return null; + } + + @Override + public Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException { + return bot.execute(addStickerToSet); + } + + @Override + public Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException { + return bot.execute(createNewStickerSet); + } + + @Override + public File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException { + return bot.execute(uploadStickerFile); + } +} diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java index 372734a6c..f496c28e3 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender.java @@ -3,9 +3,6 @@ import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto; import org.telegram.telegrambots.meta.api.methods.send.*; -import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; -import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; -import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; import org.telegram.telegrambots.meta.api.objects.File; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.User; @@ -28,12 +25,6 @@ public interface MessageSender { > T execute(Method method) throws TelegramApiException; - Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException; - - Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException; - - File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException; - Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException; java.io.File downloadFile(String path) throws TelegramApiException; @@ -54,4 +45,15 @@ public interface MessageSender { Message sendDocument(SendDocument sendDocument) throws TelegramApiException; -} + Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException; + + Message sendVideo(SendVideo sendVideo) throws TelegramApiException; + + Message sendAudio(SendAudio sendAudio) throws TelegramApiException; + + Message sendVoice(SendVoice sendVoice) throws TelegramApiException; + + Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException; + + Message sendSticker(SendSticker sendSticker) throws TelegramApiException; +} \ No newline at end of file diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/StickerMessage.java similarity index 52% rename from telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java rename to telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/StickerMessage.java index 53611c6bb..d846e7e82 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/MessageSender1.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/StickerMessage.java @@ -2,8 +2,10 @@ import org.telegram.telegrambots.meta.api.methods.BotApiMethod; -import org.telegram.telegrambots.meta.api.methods.send.*; -import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; +import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; +import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; +import org.telegram.telegrambots.meta.api.objects.File; import org.telegram.telegrambots.bots.DefaultAbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.updateshandlers.SentCallback; @@ -15,25 +17,15 @@ * * @author Abbas Abou Daya */ -public interface MessageSender1 { - /* - * Refactored code - Interfaces related to send functionality. - */ +public interface StickerMessage { + , Callback extends SentCallback> void executeAsync(Method method, Callback callback) throws TelegramApiException; > T execute(Method method) throws TelegramApiException; + Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException; + Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException; - Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException; - - Message sendVideo(SendVideo sendVideo) throws TelegramApiException; - - Message sendAudio(SendAudio sendAudio) throws TelegramApiException; - - Message sendVoice(SendVoice sendVoice) throws TelegramApiException; - - Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException; - - Message sendSticker(SendSticker sendSticker) throws TelegramApiException; + File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException; } diff --git a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java deleted file mode 100644 index 21c1de1b8..000000000 --- a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/BotCommandTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.telegram.telegrambots.extensions.bots.commandbot.commands; -import org.junit.jupiter.api.Test; -import org.telegram.telegrambots.meta.api.objects.Chat; -import org.telegram.telegrambots.meta.api.objects.User; -import org.telegram.telegrambots.meta.bots.AbsSender; -import static org.junit.jupiter.api.Assertions.assertEquals; - - -public class BotCommandTest -{ - - @Test - public void testToString() - { - // Act - BotCommand commandidentifier = new TestBotCommand("test","Description"); - - // Assert - assertEquals("/test\nDescription",commandidentifier.toString()); - } - - @Test - public void testcommandIdentifierStartsWithCOMMAND_INIT_CHARACTER() - { - // Arrange - String commandidentifier = "/YuktaGurnani"; - - // Act - String result = commandidentifier.substring(1); - - // Assert - assertEquals("YuktaGurnani",result); - } - private static class TestBotCommand extends BotCommand - { - public TestBotCommand(String commandIdentifier, String description){ - super(commandIdentifier,description); - } - public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) - { - - } - } -} diff --git a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java deleted file mode 100644 index f5b57dd35..000000000 --- a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestSpringWebhookBot.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.telegram.telegrambots.starter; - -import org.junit.jupiter.api.Test; -import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - -public class TestSpringWebhookBot -{ - @Test - void nullTestSpringWebhookBot() - { - // Arrange and Act - SpringWebhookBot.TestSpringWebhookBot webhook = new SpringWebhookBot.TestSpringWebhookBot(null); - - // Assert - assertNull(webhook.getSetWebhook()); - } - - @Test - void NotnullTestSpringWebhookBot() - { - // Arrange - SetWebhook setWebhook = new SetWebhook(); - - // Act - SpringWebhookBot.TestSpringWebhookBot webhook = new SpringWebhookBot.TestSpringWebhookBot(setWebhook); - - //Assert - assertNotNull(webhook.getSetWebhook()); - } - -} From e70155ad784600df87f2fc5c8d9e0bb93d77bb5f Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:17:25 -0300 Subject: [PATCH 22/31] Reset code changed in implementation smell-1 --- .../api/objects/MessageContext.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java index 144d3078e..ab76a423b 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java @@ -22,9 +22,6 @@ public class MessageContext { private final Update update; private final BaseAbilityBot bot; - // Refactored code-Added thirdArg as explaining variable to remove magic number 2 - int thirdArg = 2; - private MessageContext(Update update, User user, Long chatId, BaseAbilityBot bot, String[] arguments) { this.user = user; this.chatId = chatId; @@ -91,9 +88,7 @@ public String secondArg() { */ public String thirdArg() { checkLength(); - - // Replaced magic number 2 with thirdArg - return arguments[thirdArg % arguments.length]; + return arguments[2 % arguments.length]; } /** @@ -111,11 +106,11 @@ private void checkLength() { @Override public String toString() { return MoreObjects.toStringHelper(this) - .add("user", user) - .add("chatId", chatId) - .add("arguments", arguments) - .add("update", update) - .toString(); + .add("user", user) + .add("chatId", chatId) + .add("arguments", arguments) + .add("update", update) + .toString(); } @Override @@ -127,13 +122,13 @@ public boolean equals(Object o) { MessageContext that = (MessageContext) o; return Objects.equal(user, that.user) && - Objects.equal(chatId, that.chatId) && - Arrays.equals(arguments, that.arguments) && - Objects.equal(update, that.update); + Objects.equal(chatId, that.chatId) && + Arrays.equals(arguments, that.arguments) && + Objects.equal(update, that.update); } @Override public int hashCode() { return Objects.hashCode(user, chatId, Arrays.hashCode(arguments), update); } -} +} \ No newline at end of file From 1b6598aa5491bff283562850309c44bf4e2b4208 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:20:00 -0300 Subject: [PATCH 23/31] Removed unnecessary comments from refactored code for implementation smell -2 --- .../methods/invoices/CreateInvoiceLink.java | 52 ++++--------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java index ff760fea0..b71fcb228 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/invoices/CreateInvoiceLink.java @@ -133,48 +133,16 @@ public String getMethod() { return PATH; } - // Old code - validate() method was containing complex method and complex conditional code smells - -// @Override -// public void validate() throws TelegramApiValidationException { -// if (StringUtils.isEmpty(title) || title.length() > 32) { -// throw new TelegramApiValidationException("Title parameter can't be empty or longer than 32 chars", this); -// } -// if (StringUtils.isEmpty(description) || description.length() > 255) { -// throw new TelegramApiValidationException("Description parameter can't be empty or longer than 255 chars", this); -// } -// if (StringUtils.isEmpty(payload)) { -// throw new TelegramApiValidationException("Payload parameter can't be empty", this); -// } -// if (StringUtils.isEmpty(providerToken)) { -// throw new TelegramApiValidationException("ProviderToken parameter can't be empty", this); -// } -// if (StringUtils.isEmpty(currency)) { -// throw new TelegramApiValidationException("Currency parameter can't be empty", this); -// } -// if (prices.isEmpty()) { -// throw new TelegramApiValidationException("Prices parameter can't be empty", this); -// } else { -// for (LabeledPrice price : prices) { -// price.validate(); -// } -// } -// if (suggestedTipAmounts != null && !suggestedTipAmounts.isEmpty() && suggestedTipAmounts.size() > 4) { -// throw new TelegramApiValidationException("No more that 4 suggested tips allowed", this); -// } -// } - - // Refactored validate method using decompose conditional refactoring technique -@Override -public void validate() throws TelegramApiValidationException { - validateTitle(); - validateDescription(); - validatePayload(); - validateProviderToken(); - validateCurrency(); - validatePrices(); - validateSuggestedTipAmounts(); -} + @Override + public void validate() throws TelegramApiValidationException { + validateTitle(); + validateDescription(); + validatePayload(); + validateProviderToken(); + validateCurrency(); + validatePrices(); + validateSuggestedTipAmounts(); + } private void validateTitle() throws TelegramApiValidationException { if (StringUtils.isEmpty(title) || title.length() > 32) { From 423ceca1e9cf7d55a930c2b2a0281b4a9260be7b Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:22:57 -0300 Subject: [PATCH 24/31] Made necessary changes for refactoring implementation smell-3 --- .../telegram/abilitybots/api/objects/Ability.java | 13 ++++--------- .../telegram/abilitybots/api/util/AbilityUtils.java | 4 ++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java index 326ee7145..bd832d4d6 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/Ability.java @@ -19,7 +19,7 @@ import static java.lang.String.format; import static java.util.Objects.hash; import static java.util.Optional.ofNullable; -import static org.telegram.abilitybots.api.util.AbilityUtils.isValCmdName; +import static org.telegram.abilitybots.api.util.AbilityUtils.isValidCommandName; /** * An ability is a fully-fledged bot action that contains all the necessary information to process: @@ -53,14 +53,9 @@ public final class Ability { @SafeVarargs private Ability(String name, String info, Locality locality, Privacy privacy, int argNum, boolean statsEnabled, Consumer action, Consumer postAction, List replies, Predicate... flags) { - // Refactored checkArgument to remove long statement code smell by renaming method name and introducing explaining variables. - // Explaining variables - INVALID_METHOD_NAME_ERROR and METHOD_NAME_CONSTRAINT - final String INVALID_METHOD_NAME_ERROR = "Method name can only contain alpha-numeric characters and underscores, " ; - final String METHOD_NAME_CONSTRAINT = "cannot be longer than 31 characters, and cannot be null"; - - // isValueCommandName renamed to isValCmdName - checkArgument(isValCmdName(name), INVALID_METHOD_NAME_ERROR+METHOD_NAME_CONSTRAINT, name); - + String ERROR_MESSAGE_PREFIX = "Method name can only contain alpha-numeric characters and underscores," ; + String ERROR_MESSAGE_SUFFIX = " cannot be longer than 31 characters, empty or null"; + checkArgument(isValidCommandName(name), "%s%s", ERROR_MESSAGE_PREFIX, ERROR_MESSAGE_SUFFIX, name); this.name = name; this.info = info; diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java index 1dea62748..9c539fa75 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java @@ -303,8 +303,8 @@ public static boolean isValidCommand(String command){ * @return whether the command name is valid */ - // Refactored code by renaming isValueCommandName to isValCmdName method - public static boolean isValCmdName(String commandName){ + + public static boolean isValidCommandName(String commandName){ if (commandName == null || commandName.length() > 31) return false; return commandName.matches("[A-Za-z_0-9]+"); } From 3729e7bec144548c060ce1e73c338b3374a82292 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:25:39 -0300 Subject: [PATCH 25/31] Reset code changed for refactoring implementation smell-4 --- .../org/telegram/abilitybots/api/objects/AbilityTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java index 41de5d281..ad8fa77df 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/objects/AbilityTest.java @@ -10,11 +10,9 @@ import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder; class AbilityTest { - // Refactored magic number -4 using extract method technique - final int inputNegativeNumber = -4; @Test void argumentsCannotBeNegative() { - Assertions.assertThrows(IllegalArgumentException.class, () -> getDefaultBuilder().input(inputNegativeNumber).build()); + Assertions.assertThrows(IllegalArgumentException.class, () -> getDefaultBuilder().input(-4).build()); } @Test @@ -72,4 +70,3 @@ void abilityBuilderSetStatsEnabledFalseTest() { assertFalse(statsDisabledAbility.statsEnabled()); } } - From fb128badf4443c7c1cb73912353ed635e9d5b2de Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:26:57 -0300 Subject: [PATCH 26/31] Removed unncessary comments --- .../org/telegram/telegrambots/meta/test/TestTelegramApi.java | 1 - 1 file changed, 1 deletion(-) diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java index 3c7ea3692..db5a69b4c 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/test/TestTelegramApi.java @@ -81,7 +81,6 @@ public String getBotToken() { }, new SetWebhook()); fail(); } catch (TelegramApiException e) { - // Introducing logger as explaining variable to resolve empty catch clause code smell logger.error("Exception occurred while registering bot: {}", e.getMessage()); } From f4cef7728ae8d091e9a47d902b242a5137c1ccc6 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:28:30 -0300 Subject: [PATCH 27/31] Removed unnecessary comments --- .../adminrights/GetMyDefaultAdministratorRightsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java index e6583ae42..b0acf2fc3 100644 --- a/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/adminrights/GetMyDefaultAdministratorRightsTest.java @@ -52,7 +52,7 @@ public void testGetMyDefaultAdministratorRightsDeserializeValidResponse() { " }\n" + "}"; - // Refactored long identifier code smell by renaming getMyDefaultAdministratorRights variable name to administratorRights + GetMyDefaultAdministratorRights administratorRights = GetMyDefaultAdministratorRights .builder() .build(); From 3c98bfac57e191d766a4abd9fc48ca0c9762222e Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:30:43 -0300 Subject: [PATCH 28/31] Removed unnecessary comments --- pom.xml | 2 +- telegrambots-abilities/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 4c15ebb27..be9ef33de 100644 --- a/pom.xml +++ b/pom.xml @@ -143,7 +143,7 @@ ch.qos.logback logback-classic - 1.2.6 + 1.2.6 org.apache.commons diff --git a/telegrambots-abilities/pom.xml b/telegrambots-abilities/pom.xml index f4f4300ee..d4984eeeb 100644 --- a/telegrambots-abilities/pom.xml +++ b/telegrambots-abilities/pom.xml @@ -83,7 +83,7 @@ ch.qos.logback logback-classic - 1.2.3 + 1.2.3 org.telegram From 5b97c3023f0ee8f2151c11cc7de6676a555d56f2 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:32:47 -0300 Subject: [PATCH 29/31] Removed unnecessary comments --- .../commandbot/commands/TimedSendLongPollingBotTest.java | 5 +---- .../telegram/telegrambots/meta/api/methods/BotApiMethod.java | 3 --- .../telegrambots/meta/api/methods/PartialBotApiMethod.java | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java index 3f628010c..7edd7dceb 100644 --- a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java +++ b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java @@ -11,7 +11,6 @@ public class TimedSendLongPollingBotTest @Test public void testMessageProcessing() { - // Arrange TimedSendLongPollingBot testbot = new TimedSendLongPollingBot() { @Override @@ -31,11 +30,9 @@ public void sendMessageCallback(Long chatId, Object messageRequest) } }; - // Act - Long chatId = 002L; + Long chatId = 2L; Object messageRequest = mock(Object.class); - // Assert testbot.sendTimed(chatId,messageRequest); testbot.finish(); } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java index 46d8d992c..36f23d4eb 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/BotApiMethod.java @@ -16,9 +16,6 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class BotApiMethod extends PartialBotApiMethod { - /* - * Refactored code to remove unnecessary abstraction by moving METHOD_FIELD to PartialBotApiMethod class. - */ @Override public T deserializeResponse(String answer) throws TelegramApiRequestException { return null; diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java index 740134077..b5026e732 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethod.java @@ -22,7 +22,6 @@ public abstract class PartialBotApiMethod implements Val @JsonIgnore protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - // Refactored code - Pulled up METHOD_FIELD variable from BotApiMethod protected static final String METHOD_FIELD = "method"; /** @@ -65,7 +64,7 @@ private T deserializeResponseInternal(String answer, JavaType type) throws Teleg * Getter for method path (that is the same as method name) * @return Method path */ - // Refactored Code + @JsonProperty(METHOD_FIELD) public abstract String getMethod(); } From 645191912f8e1029523700032a7d1acd8e04ecdf Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:33:50 -0300 Subject: [PATCH 30/31] Code reset for story and chat files --- .../telegrambots/meta/api/objects/Chat.java | 7 +++---- .../meta/api/objects/stories/Story.java | 14 +++++--------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java index d3b3001a2..b5e770a5f 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/Chat.java @@ -30,8 +30,7 @@ @AllArgsConstructor public class Chat implements BotApiObject { - // Variable name changed from CHAT_FIELD to CHAT_ID_FIELD for refactoring - private static final String CHAT_ID_FIELD = "id"; + private static final String ID_FIELD = "id"; private static final String TYPE_FIELD = "type"; private static final String TITLE_FIELD = "title"; private static final String USERNAME_FIELD = "username"; @@ -81,7 +80,7 @@ public class Chat implements BotApiObject { * have difficulty/silent defects in interpreting it. But it smaller than 52 bits, * so a signed 64 bit integer or double-precision float type are safe for storing this identifier. */ - @JsonProperty(CHAT_ID_FIELD) + @JsonProperty(ID_FIELD) @NonNull private Long id; /** @@ -350,4 +349,4 @@ public Boolean isUserChat() { public Boolean isSuperGroupChat() { return SUPERGROUPCHATTYPE.equals(type); } -} +} \ No newline at end of file diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java index e531d64fb..d3ac67fe3 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/stories/Story.java @@ -8,7 +8,7 @@ import lombok.Setter; import lombok.ToString; import org.telegram.telegrambots.meta.api.interfaces.BotApiObject; - +import org.telegram.telegrambots.meta.api.objects.Chat; /** * @author Ruben Bermudez @@ -23,21 +23,17 @@ @NoArgsConstructor @AllArgsConstructor public class Story implements BotApiObject { - /* - * Refactored code - Using CHAT_ID_FIELD instead of CHAT_FIELD to change bidirectional association to unidirectional association - */ - - private static final String CHAT_ID_FIELD = "chat_id"; + private static final String CHAT_FIELD = "chat"; private static final String ID_FIELD = "id"; /** * Chat that posted the story */ - @JsonProperty(CHAT_ID_FIELD) - private String chatId; + @JsonProperty(CHAT_FIELD) + private Chat chat; /** * Unique identifier for the story in the chat */ @JsonProperty(ID_FIELD) private Integer id; -} +} \ No newline at end of file From 127858621241d5606bb7859e217c0fafdd808d29 Mon Sep 17 00:00:00 2001 From: Yukta Gurnani Date: Fri, 29 Mar 2024 14:36:13 -0300 Subject: [PATCH 31/31] Design code smell - 4 - Refactored code for unexploited encapsulation using replace conditional with polymorphism technique --- .../telegrambots/bots/DefaultAbsSender.java | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) 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 007a918b8..6c8be358a 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -1224,7 +1224,18 @@ private HttpPost configuredHttpPost(String url) { return httppost; } - private void addInputData(MultipartEntityBuilder builder, InputMedia media, String mediaField, boolean addField) throws JsonProcessingException { +private void addInputData(MultipartEntityBuilder builder, InputMedia media, String mediaField, boolean addField) throws JsonProcessingException { + addNewMedia(builder, media); + addThumbnail(builder, media); + addDocument(builder,media); + addVideo(builder,media); + addAnimation(builder,media); + if (addField) { + builder.addTextBody(mediaField, objectMapper.writeValueAsString(media), TEXT_PLAIN_CONTENT_TYPE); + } +} + + private void addNewMedia(MultipartEntityBuilder builder, InputMedia media) { if (media.isNewMedia()) { if (media.getNewMediaFile() != null) { builder.addBinaryBody(media.getMediaName(), media.getNewMediaFile(), ContentType.APPLICATION_OCTET_STREAM, media.getMediaName()); @@ -1232,32 +1243,42 @@ private void addInputData(MultipartEntityBuilder builder, InputMedia media, Stri builder.addBinaryBody(media.getMediaName(), media.getNewMediaStream(), ContentType.APPLICATION_OCTET_STREAM, media.getMediaName()); } } + } + private void addThumbnail(MultipartEntityBuilder builder, InputMedia media) { if (media instanceof InputMediaAudio) { InputMediaAudio audio = (InputMediaAudio) media; if (audio.getThumbnail() != null) { addInputFile(builder, audio.getThumbnail(), InputMediaAudio.THUMBNAIL_FIELD, false); } - } else if (media instanceof InputMediaDocument) { + } + } + + private void addDocument(MultipartEntityBuilder builder, InputMedia media) { + if (media instanceof InputMediaDocument) { InputMediaDocument document = (InputMediaDocument) media; if (document.getThumbnail() != null) { addInputFile(builder, document.getThumbnail(), InputMediaDocument.THUMBNAIL_FIELD, false); } - } else if (media instanceof InputMediaVideo) { + } + } + + private void addVideo(MultipartEntityBuilder builder, InputMedia media) { + if (media instanceof InputMediaVideo) { InputMediaVideo video = (InputMediaVideo) media; if (video.getThumbnail() != null) { addInputFile(builder, video.getThumbnail(), InputMediaVideo.THUMBNAIL_FIELD, false); } - } else if (media instanceof InputMediaAnimation) { + } + } + + private void addAnimation(MultipartEntityBuilder builder, InputMedia media) { + if (media instanceof InputMediaAnimation) { InputMediaAnimation animation = (InputMediaAnimation) media; if (animation.getThumbnail() != null) { addInputFile(builder, animation.getThumbnail(), InputMediaAnimation.THUMBNAIL_FIELD, false); } } - - if (addField) { - builder.addTextBody(mediaField, objectMapper.writeValueAsString(media), TEXT_PLAIN_CONTENT_TYPE); - } } private void addInputData(MultipartEntityBuilder builder, List media, String mediaField) throws JsonProcessingException {