From 05341be534369d58cc1a48b2c74f8ae91793ad51 Mon Sep 17 00:00:00 2001 From: bpavuk Date: Wed, 10 Jul 2024 15:33:14 +0300 Subject: [PATCH] Implemented paid media reply extension --- .../api/send/RepliesWithChatsAndMessages.kt | 43 +++++++++++++++++++ .../payments/PaidMediaToTelegramPaidMedia.kt | 25 +++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/payments/PaidMediaToTelegramPaidMedia.kt diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt index 78a5205637..af5874b388 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt @@ -26,6 +26,9 @@ import dev.inmo.tgbotapi.types.games.Game import dev.inmo.tgbotapi.types.location.* import dev.inmo.tgbotapi.types.message.abstracts.AccessibleMessage import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.types.message.payments.PaidMedia +import dev.inmo.tgbotapi.types.message.payments.toTelegramMediaPhoto +import dev.inmo.tgbotapi.types.message.payments.toTelegramPaidMediaVideo import dev.inmo.tgbotapi.types.payments.LabeledPrice import dev.inmo.tgbotapi.types.payments.abstracts.Currency import dev.inmo.tgbotapi.types.polls.* @@ -2045,3 +2048,43 @@ suspend fun TelegramBot.reply( ) } } + +suspend fun TelegramBot.reply( + toChatId: IdChatIdentifier, + toMessageId: MessageId, + content: PaidMediaInfoContent, + replyInChatId: IdChatIdentifier = toChatId, + replyInThreadId: MessageThreadId? = replyInChatId.threadId, + replyInBusinessConnectionId: BusinessConnectionId? = replyInChatId.businessConnectionId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + allowSendingWithoutReply: Boolean? = null, + parseMode: ParseMode? = null, + replyMarkup: KeyboardMarkup? = null +) { + val media = content.paidMediaInfo.media.mapNotNull { + when (it) { + is PaidMedia.Video -> it.toTelegramPaidMediaVideo() + is PaidMedia.Photo -> it.toTelegramMediaPhoto() + is PaidMedia.Preview, is PaidMedia.Unknown -> null + } + } + sendPaidMedia( + chatId = replyInChatId, + starCount = content.paidMediaInfo.stars, + media = media, + text = content.text, + parseMode = parseMode, + showCaptionAboveMedia = content.showCaptionAboveMedia, + threadId = replyInThreadId, + businessConnectionId = replyInBusinessConnectionId, + disableNotification = disableNotification, + protectContent = protectContent, + replyMarkup = replyMarkup, + replyParameters = ReplyParameters( + messageId = toMessageId, + chatIdentifier = toChatId, + allowSendingWithoutReply = allowSendingWithoutReply + ) + ) +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/payments/PaidMediaToTelegramPaidMedia.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/payments/PaidMediaToTelegramPaidMedia.kt new file mode 100644 index 0000000000..84be904eb6 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/payments/PaidMediaToTelegramPaidMedia.kt @@ -0,0 +1,25 @@ +package dev.inmo.tgbotapi.types.message.payments + +import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto +import dev.inmo.tgbotapi.types.media.TelegramPaidMedia +import dev.inmo.tgbotapi.types.media.TelegramPaidMediaPhoto +import dev.inmo.tgbotapi.types.media.TelegramPaidMediaVideo + +fun PaidMedia.toTelegramPaidMedia(): TelegramPaidMedia? = when (this) { + is PaidMedia.Photo -> TelegramPaidMediaPhoto( + file = this.photo.biggest.fileId + ) + is PaidMedia.Video -> TelegramPaidMediaPhoto( + file = this.video.fileId + ) + is PaidMedia.Preview, is PaidMedia.Unknown -> null +} + +fun PaidMedia.Video.toTelegramPaidMediaVideo(): TelegramPaidMediaVideo = TelegramPaidMediaVideo( + file = this.video.fileId, + duration = this.video.duration + ) + +fun PaidMedia.Photo.toTelegramMediaPhoto(): TelegramPaidMediaPhoto = TelegramPaidMediaPhoto( + file = this.photo.fileId +)