diff --git a/pom.xml b/pom.xml index 2f1b44e2a..be9ef33de 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..d4984eeeb 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-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..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 @@ -50,10 +50,13 @@ 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); + 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/objects/MessageContext.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/MessageContext.java index fc650f854..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 @@ -106,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 @@ -122,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 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..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; @@ -41,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); @@ -136,4 +118,4 @@ public Message sendVoice(SendVoice sendVoice) throws TelegramApiException { 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/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 b3ba74bcc..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; @@ -65,4 +56,4 @@ public interface MessageSender { 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/StickerMessage.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/StickerMessage.java new file mode 100644 index 000000000..d846e7e82 --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/sender/StickerMessage.java @@ -0,0 +1,31 @@ +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; + +/** + * A sender interface that replicates {@link DefaultAbsSender} methods. + * + * @author Abbas Abou Daya + */ +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; + + File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException; +} + 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..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 @@ -302,6 +302,8 @@ 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){ if (commandName == null || commandName.length() > 31) return false; return commandName.matches("[A-Za-z_0-9]+"); 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..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 @@ -70,4 +70,3 @@ void abilityBuilderSetStatsEnabledFalseTest() { assertFalse(statsDisabledAbility.statsEnabled()); } } - 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..7edd7dceb --- /dev/null +++ b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/TimedSendLongPollingBotTest.java @@ -0,0 +1,39 @@ +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() + { + TimedSendLongPollingBot testbot = new TimedSendLongPollingBot() + { + @Override + public String getBotUsername() { + return null; + } + + @Override + public void onUpdateReceived(Update update) + { + + } + @Override + public void sendMessageCallback(Long chatId, Object messageRequest) + { + + } + }; + + Long chatId = 2L; + Object messageRequest = mock(Object.class); + + 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 6ca13881c..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 @@ -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,15 @@ */ @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 { + @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..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,6 +22,8 @@ public abstract class PartialBotApiMethod implements Val @JsonIgnore protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + 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 +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 */ - @JsonProperty(BotApiMethod.METHOD_FIELD) + + @JsonProperty(METHOD_FIELD) public abstract String getMethod(); } 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..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 @@ -127,6 +127,7 @@ public class CreateInvoiceLink extends BotApiMethod { @Singular private List suggestedTipAmounts; + @Override public String getMethod() { return PATH; @@ -134,28 +135,55 @@ public String getMethod() { @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); } 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..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 @@ -349,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 e9a04ebb8..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 @@ -36,4 +36,4 @@ public class Story implements BotApiObject { */ @JsonProperty(ID_FIELD) private Integer id; -} +} \ No newline at end of file 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 new file mode 100644 index 000000000..23b9bf55c --- /dev/null +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/meta/api/methods/PartialBotApiMethodJsonTest.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 PartialBotApiMethodJsonTest { + + @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/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..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,12 +52,13 @@ public void testGetMyDefaultAdministratorRightsDeserializeValidResponse() { " }\n" + "}"; - GetMyDefaultAdministratorRights getMyDefaultAdministratorRights = GetMyDefaultAdministratorRights + + 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()); 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..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 @@ -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,8 @@ public String getBotToken() { }, new SetWebhook()); fail(); } catch (TelegramApiException e) { - // Ignore + logger.error("Exception occurred while registering bot: {}", e.getMessage()); + } } } 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 {