Skip to content

Commit

Permalink
Fix SendPaidMedia attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
pengrad committed Aug 30, 2024
1 parent 0832bf1 commit 70d3863
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public int hashCode() {
@Override
public String toString() {
return "PaidMediaPhoto{" +
"type='" + type() + "\'," +
", photo=" + Arrays.toString(photo) + "\'" +
"photo=" + Arrays.toString(photo) +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ public int hashCode() {
@Override
public String toString() {
return "PaidMediaPreview{" +
"type='" + type() + "\'," +
", width=" + width + "\'," +
", height=" + height + "\'," +
", duration=" + duration + "\'" +
"width=" + width +
", height=" + height +
", duration=" + duration +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public int hashCode() {
@Override
public String toString() {
return "PaidMediaVideo{" +
"type='" + type() + "\'," +
", video=" + video + "\'" +
"video=" + video +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
import java.util.HashMap;
import java.util.Map;

/**
* Stas Parshin
* 23 November 2017
*/
abstract public class InputPaidMedia implements Serializable {
private final static long serialVersionUID = 0L;

private final String type;
private final String media;
private String thumbnail;
transient protected Map<String, Object> attachments = new HashMap<>();
transient private InputFile inputFile;
transient private String inputFileAttachId;
transient private String fileName;
Expand All @@ -39,6 +35,9 @@ abstract public class InputPaidMedia implements Serializable {
}
}

public Map<String, Object> getAttachments() {
return attachments;
}

public InputFile inputFile() {
return inputFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class InputPaidMediaVideo extends InputPaidMedia implements Serializable {
Expand All @@ -14,12 +13,8 @@ public class InputPaidMediaVideo extends InputPaidMedia implements Serializable

private Integer width, height, duration;
private Boolean supports_streaming;

private String thumbnail;

transient private Map<String, Object> attachments = new HashMap<>();


public InputPaidMediaVideo(String media) {
super("video", media);
}
Expand Down Expand Up @@ -66,11 +61,6 @@ public InputPaidMediaVideo thumbnail(byte[] thumbnail) {
return this;
}

public Map<String, Object> getAttachments() {
return attachments;
}


@Override
public String getDefaultFileName() {
return ContentTypes.VIDEO_FILE_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public int hashCode() {
@Override
public String toString() {
return "TransactionPartnerFragment{" +
"type='" + type() + "\'," +
", withdrawal_state=" + withdrawal_state + "\'" +
"withdrawal_state=" + withdrawal_state +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public int hashCode() {

@Override
public String toString() {
return "TransactionPartnerOther{" +
"type='" + type() + "\'" +
'}';
return "TransactionPartnerOther{}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public int hashCode() {

@Override
public String toString() {
return "TransactionPartnerTelegramAds{" +
"type='" + type() + "\'" +
'}';
return "TransactionPartnerTelegramAds{}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public int hashCode() {
@Override
public String toString() {
return "TransactionPartnerUser{" +
"type='" + type() + "\'," +
", user=" + user + "\'," +
", invoice_payload=" + invoice_payload + "\'," +
", paid_media=" + paid_media + "\'" +
"user=" + user +
", invoice_payload='" + invoice_payload + '\'' +
", paid_media=" + Arrays.toString(paid_media) +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ public int hashCode() {
@Override
public String toString() {
return "RevenueWithdrawalStateSucceeded{" +
"type='" + type() + "\'," +
"date='" + date + "\'," +
"url='" + url + "\'" +
"date=" + date +
", url='" + url + '\'' +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@

public class SendPaidMedia extends BaseRequest<SendPaidMedia, SendResponse> {

private boolean isMultipart = false;

public SendPaidMedia(Object chatId, Integer starCount, InputPaidMedia... media) {
super(SendResponse.class);
add("chat_id", chatId).add("star_count", starCount).add("media", media);

for (InputPaidMedia m : media) {
Map<String, Object> attachments = m.getAttachments();
if (attachments != null && !attachments.isEmpty()) {
addAll(attachments);
isMultipart = true;
}
if (m.inputFile() != null) {
add(m.getInputFileId(), m.inputFile());
isMultipart = true;
}
}
}

@Override
public boolean isMultipart() {
return isMultipart;
}

public SendPaidMedia caption(String caption) {
Expand Down
19 changes: 19 additions & 0 deletions library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.pengrad.telegrambot.model.botcommandscope.BotCommandScopeAllChatAdministrators;
import com.pengrad.telegrambot.model.chatboost.ChatBoost;
import com.pengrad.telegrambot.model.giveaway.Giveaway;
import com.pengrad.telegrambot.model.paidmedia.PaidMediaInfo;
import com.pengrad.telegrambot.model.paidmedia.PaidMediaPhoto;
import com.pengrad.telegrambot.model.paidmedia.PaidMediaVideo;
import com.pengrad.telegrambot.model.reaction.ReactionTypeEmoji;
import com.pengrad.telegrambot.model.request.*;
import com.pengrad.telegrambot.passport.*;
Expand Down Expand Up @@ -2416,4 +2419,20 @@ public void answerWebAppQuery() {
assertEquals("Bad Request: query is too old and response timeout expired or query ID is invalid", response.description());
assertNull(response.sentWebAppMessage());
}

@Test
public void sendPaidMedia() {
Integer starCount = 2;
SendResponse response = bot.execute(new SendPaidMedia(chatId, starCount,
new InputPaidMediaVideo(videoFile).thumbnail(thumbFile),
new InputPaidMediaPhoto(photoFileId)));
PaidMediaInfo mediaInfo = response.message().paidMedia();
assertTrue(response.isOk());
assertEquals(starCount, mediaInfo.starCount());
assertEquals(2, mediaInfo.paidMedia().length);
assertEquals("video", mediaInfo.paidMedia()[0].type());
VideoTest.check(((PaidMediaVideo) mediaInfo.paidMedia()[0]).getVideo());
assertEquals("photo", mediaInfo.paidMedia()[1].type());
PhotoSizeTest.checkPhotos(((PaidMediaPhoto) mediaInfo.paidMedia()[1]).getPhoto());
}
}

0 comments on commit 70d3863

Please sign in to comment.