1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

progress on addinf of paid media

This commit is contained in:
InsanusMokrassar 2024-07-08 02:51:09 +06:00
parent 1eef5bb239
commit 00aefefc31
22 changed files with 89 additions and 56 deletions

View File

@ -855,7 +855,7 @@ suspend inline fun TelegramBot.replyWithPhoto(
suspend inline fun TelegramBot.reply(
to: AccessibleMessage,
photo: Photo,
photo: PhotoFile,
text: String? = null,
parseMode: ParseMode? = null,
showCaptionAboveMedia: Boolean = false,
@ -947,7 +947,7 @@ suspend inline fun TelegramBot.replyWithPhoto(
suspend inline fun TelegramBot.reply(
to: AccessibleMessage,
photo: Photo,
photo: PhotoFile,
entities: TextSourcesList,
showCaptionAboveMedia: Boolean = false,
spoilered: Boolean = false,

View File

@ -806,7 +806,7 @@ suspend inline fun TelegramBot.replyWithPhoto(
suspend inline fun TelegramBot.reply(
toChatId: IdChatIdentifier,
toMessageId: MessageId,
photo: Photo,
photo: PhotoFile,
text: String? = null,
parseMode: ParseMode? = null,
showCaptionAboveMedia: Boolean = false,
@ -860,7 +860,7 @@ suspend inline fun TelegramBot.replyWithPhoto(
suspend inline fun TelegramBot.reply(
toChatId: IdChatIdentifier,
toMessageId: MessageId,
photo: Photo,
photo: PhotoFile,
entities: TextSourcesList,
showCaptionAboveMedia: Boolean = false,
spoilered: Boolean = false,

View File

@ -726,7 +726,7 @@ suspend fun TelegramBot.send(
*/
suspend fun TelegramBot.send(
chatId: ChatIdentifier,
photo: Photo,
photo: PhotoFile,
text: String? = null,
parseMode: ParseMode? = null,
showCaptionAboveMedia: Boolean = false,
@ -747,7 +747,7 @@ suspend fun TelegramBot.send(
*/
suspend fun TelegramBot.send(
chat: Chat,
photo: Photo,
photo: PhotoFile,
text: String? = null,
parseMode: ParseMode? = null,
showCaptionAboveMedia: Boolean = false,
@ -810,7 +810,7 @@ suspend fun TelegramBot.send(
*/
suspend inline fun TelegramBot.send(
chatId: ChatIdentifier,
photo: Photo,
photo: PhotoFile,
entities: TextSourcesList,
showCaptionAboveMedia: Boolean = false,
spoilered: Boolean = false,
@ -830,7 +830,7 @@ suspend inline fun TelegramBot.send(
*/
suspend inline fun TelegramBot.send(
chat: Chat,
photo: Photo,
photo: PhotoFile,
entities: TextSourcesList,
showCaptionAboveMedia: Boolean = false,
spoilered: Boolean = false,

View File

@ -87,7 +87,7 @@ suspend fun TelegramBot.sendPhoto(
*/
suspend fun TelegramBot.sendPhoto(
chatId: ChatIdentifier,
photo: Photo,
photo: PhotoFile,
text: String? = null,
parseMode: ParseMode? = null,
showCaptionAboveMedia: Boolean = false,
@ -121,7 +121,7 @@ suspend fun TelegramBot.sendPhoto(
*/
suspend fun TelegramBot.sendPhoto(
chat: Chat,
photo: Photo,
photo: PhotoFile,
text: String? = null,
parseMode: ParseMode? = null,
showCaptionAboveMedia: Boolean = false,
@ -290,7 +290,7 @@ suspend inline fun TelegramBot.sendPhoto(
*/
suspend inline fun TelegramBot.sendPhoto(
chatId: ChatIdentifier,
photo: Photo,
photo: PhotoFile,
entities: TextSourcesList,
showCaptionAboveMedia: Boolean = false,
spoilered: Boolean = false,
@ -322,7 +322,7 @@ suspend inline fun TelegramBot.sendPhoto(
*/
suspend inline fun TelegramBot.sendPhoto(
chat: Chat,
photo: Photo,
photo: PhotoFile,
entities: TextSourcesList,
showCaptionAboveMedia: Boolean = false,
spoilered: Boolean = false,

View File

@ -606,6 +606,8 @@ const val additionalChatCountField = "additional_chat_count"
const val unclaimedPrizeCountField = "unclaimed_prize_count"
const val wasRefundedField = "was_refunded"
const val isManualField = "is_manual"
const val starCountField = "star_count"
const val paidMediaField = "paid_media"
const val businessConnectionIdField = "business_connection_id"
const val businessIntroField = "business_intro"

View File

@ -92,7 +92,7 @@ sealed interface ReplyInfo {
private val animation: AnimationFile? = null,
private val game: RawGame? = null,
@Serializable(PhotoSerializer::class)
private val photo: Photo? = null,
private val photo: PhotoFile? = null,
private val sticker: Sticker? = null,
private val video: VideoFile? = null,
private val voice: VoiceFile? = null,

View File

@ -1,6 +1,6 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import dev.inmo.tgbotapi.types.files.PhotoSerializer
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.*
@ -11,10 +11,10 @@ data class UserProfilePhotos (
@SerialName("total_count")
val count: Int,
@Serializable(UserProfilePhotosPhotosSerializer::class)
val photos: List<Photo>
val photos: List<PhotoFile>
)
@RiskFeature
object UserProfilePhotosPhotosSerializer : KSerializer<List<Photo>> by ListSerializer(
object UserProfilePhotosPhotosSerializer : KSerializer<List<PhotoFile>> by ListSerializer(
PhotoSerializer
)

View File

@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.types.files
import dev.inmo.tgbotapi.types.message.payments.PaidMedia
import dev.inmo.tgbotapi.types.paidMediaField
import dev.inmo.tgbotapi.types.starCountField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class PaidMediaInfo(
@SerialName(starCountField)
val stars: Int,
@SerialName(paidMediaField)
val media: List<PaidMedia>
)

View File

@ -8,7 +8,7 @@ import kotlin.jvm.JvmInline
@Serializable
@JvmInline
value class Photo(
value class PhotoFile(
val photos: List<PhotoSize>
) : List<PhotoSize> by photos, MediaContentVariant, UsefulAsPaidMediaFile {
val biggest: PhotoSize
@ -27,12 +27,14 @@ value class Photo(
}
}
fun Photo.biggest(): PhotoSize? = maxByOrNull {
typealias Photo = PhotoFile
fun PhotoFile.biggest(): PhotoSize? = maxByOrNull {
it.resolution
}
@RiskFeature
object PhotoSerializer : KSerializer<Photo> by Photo.serializer()
object PhotoSerializer : KSerializer<PhotoFile> by PhotoFile.serializer()
@Serializable
data class PhotoSize(

View File

@ -5,14 +5,14 @@ import dev.inmo.tgbotapi.abstracts.Titled
import dev.inmo.tgbotapi.types.ReplyInfo
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.files.AnimationFile
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import kotlinx.serialization.Serializable
@Serializable
data class Game(
override val title: String,
val description: String,
val photo: Photo,
val photo: PhotoFile,
override val text: String? = null,
override val textSources: TextSourcesList = emptyList(),
val animation: AnimationFile? = null

View File

@ -14,7 +14,7 @@ internal data class RawGame(
private val description: String,
@Serializable(PhotoSerializer::class)
@SerialName(photoField)
private val photo: Photo,
private val photo: PhotoFile,
@SerialName(textField)
private val text: String? = null,
@SerialName(textEntitiesField)

View File

@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.requests.abstracts.InputFile
import dev.inmo.tgbotapi.requests.abstracts.fileIdToSend
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.VideoFile
import dev.inmo.tgbotapi.types.message.payments.PaidMedia
import kotlinx.serialization.*
internal const val videoTelegramPaidMediaType = "video"

View File

@ -1,8 +1,8 @@
package dev.inmo.tgbotapi.types.message.ChatEvents
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PublicChatEvent
data class NewChatPhoto(
val photo: Photo
val photo: PhotoFile
): PublicChatEvent

View File

@ -71,10 +71,11 @@ internal data class RawMessage(
private val story: Story? = null,
private val audio: AudioFile? = null,
private val document: DocumentFile? = null,
private val paid_media: PaidMediaInfoContent? = null,
private val animation: AnimationFile? = null,
private val game: RawGame? = null,
@Serializable(PhotoSerializer::class)
private val photo: Photo? = null,
private val photo: PhotoFile? = null,
private val sticker: Sticker? = null,
private val video: VideoFile? = null,
private val voice: VoiceFile? = null,
@ -87,7 +88,7 @@ internal data class RawMessage(
private val left_chat_member: User? = null,
private val new_chat_title: String? = null,
@Serializable(PhotoSerializer::class)
private val new_chat_photo: Photo? = null,
private val new_chat_photo: PhotoFile? = null,
private val delete_chat_photo: Boolean = false,
private val group_chat_created: Boolean = false,
private val supergroup_chat_created: Boolean = false,
@ -198,6 +199,12 @@ internal data class RawMessage(
adaptedCaptionEntities,
quote
)
paid_media != null -> PaidMediaInfoContent(
document,
caption,
adaptedCaptionEntities,
quote
)
voice != null -> VoiceContent(
voice,
caption,

View File

@ -6,25 +6,27 @@ import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
import dev.inmo.tgbotapi.types.files.*
import dev.inmo.tgbotapi.types.media.PaidMedia
import dev.inmo.tgbotapi.types.media.TelegramPaidMedia
import dev.inmo.tgbotapi.types.media.toTelegramMediaPhoto
import dev.inmo.tgbotapi.types.files.toTelegramPaidMediaVideo
import dev.inmo.tgbotapi.types.media.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.payments.PaidMedia
import kotlinx.serialization.Serializable
@Serializable
data class PaidMediaInfoContent(
override val mediaCollection: List<PaidMedia>,
val paidMediaInfo: PaidMediaInfo,
override val text: String? = null,
override val textSources: TextSourcesList = emptyList(),
override val quote: TextQuote? = null,
override val showCaptionAboveMedia: Boolean = false
) : MediaCollectionContent<UsefulAsPaidMediaFile>, TextedMediaContent, WithCustomizedCaptionMediaContent {
override val media: UsefulAsPaidMediaFile
get() = mediaCollection.first()
override fun asTelegramMedia(): TelegramPaidMedia = when (val media = media) {
is VideoFile -> media.toTelegramPaidMediaVideo()
is Photo -> media.biggest.toTelegramMediaPhoto()
) : TextedMediaContent, WithCustomizedCaptionMediaContent {
override val media: TelegramMediaFile
get() = paidMediaInfo.media.fir
override fun asTelegramMedia(): TelegramMediaFile = when (val media = media) {
is PaidMedia.Photo -> media.photo.biggest.toTelegramPaidMediaPhoto()
is PaidMedia.Preview,
is PaidMedia.Unknown -> error("Unable to create telegram media out of $media")
is PaidMedia.Video -> media.video.toTelegramPaidMediaVideo()
}
override fun createResend(

View File

@ -15,7 +15,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class PhotoContent(
override val mediaCollection: Photo,
override val mediaCollection: PhotoFile,
override val text: String? = null,
override val textSources: TextSourcesList = emptyList(),
override val spoilered: Boolean = false,

View File

@ -1,6 +1,8 @@
package dev.inmo.tgbotapi.types.media
package dev.inmo.tgbotapi.types.message.payments
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.PhotoFile
import dev.inmo.tgbotapi.types.files.TelegramMediaFile
import dev.inmo.tgbotapi.types.files.VideoFile
import dev.inmo.tgbotapi.utils.decodeDataAndJson
import kotlinx.serialization.EncodeDefault
@ -13,7 +15,9 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
@Serializable(PaidMedia.Companion::class)
sealed interface PaidMedia : TelegramMedia {
sealed interface PaidMedia {
val type: String
@Serializable
data class Preview(
@SerialName(widthField)
@ -35,7 +39,7 @@ sealed interface PaidMedia : TelegramMedia {
@Serializable
data class Photo(
@SerialName(photoField)
val photo: Photo
val photo: PhotoFile
) : PaidMedia {
@EncodeDefault
@SerialName(typeField)
@ -79,9 +83,9 @@ sealed interface PaidMedia : TelegramMedia {
@SerialName(durationField)
val duration: Int? = null,
@SerialName(photoField)
val photo: Photo? = null,
val photo: PhotoFile? = null,
@SerialName(videoField)
val video: Video? = null
val video: VideoFile? = null
)
override val descriptor: SerialDescriptor

View File

@ -1,7 +1,7 @@
package dev.inmo.tgbotapi.types.request
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -16,5 +16,5 @@ data class ChatShared(
@SerialName(usernameField)
val username: Username? = null,
@SerialName(photoField)
val photo: Photo? = null
val photo: PhotoFile? = null
) : ChatSharedRequest

View File

@ -1,7 +1,7 @@
package dev.inmo.tgbotapi.types.request
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -16,5 +16,5 @@ data class SharedUser(
@SerialName(usernameField)
val username: Username? = null,
@SerialName(photoField)
val photo: Photo? = null,
val photo: PhotoFile? = null,
)

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi
import dev.inmo.tgbotapi.requests.abstracts.toInputFile
import dev.inmo.tgbotapi.types.TgFileUniqueId
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import dev.inmo.tgbotapi.types.media.MediaGroupMemberTelegramFreeMediaSerializer
import dev.inmo.tgbotapi.types.files.PhotoSize
import dev.inmo.tgbotapi.types.message.content.PhotoContent
@ -22,7 +22,7 @@ class SimpleInputFilesTest {
@Test
fun test_that_creating_of_photo_and_converting_to_input_media_working_correctly() {
val photoContent = PhotoContent(
Photo(
PhotoFile(
listOf(
PhotoSize("example_file_id".toInputFile(), TgFileUniqueId("example_unique_file_id"), 100, 100, 100)
)

View File

@ -193,7 +193,7 @@ import dev.inmo.tgbotapi.types.files.MediaContentVariant
import dev.inmo.tgbotapi.types.files.MimedMediaFile
import dev.inmo.tgbotapi.types.files.PassportFile
import dev.inmo.tgbotapi.types.files.PathedFile
import dev.inmo.tgbotapi.types.files.Photo
import dev.inmo.tgbotapi.types.files.PhotoFile
import dev.inmo.tgbotapi.types.files.PhotoSize
import dev.inmo.tgbotapi.types.files.PlayableMediaFile
import dev.inmo.tgbotapi.types.files.RegularAnimatedSticker
@ -2707,13 +2707,13 @@ public inline fun TelegramMediaFile.pathedFileOrThrow(): PathedFile = this as
public inline fun <T> TelegramMediaFile.ifPathedFile(block: (PathedFile) -> T): T? =
pathedFileOrNull() ?.let(block)
public inline fun TelegramMediaFile.photoOrNull(): Photo? = this as?
dev.inmo.tgbotapi.types.files.Photo
public inline fun TelegramMediaFile.photoOrNull(): PhotoFile? = this as?
dev.inmo.tgbotapi.types.files.PhotoFile
public inline fun TelegramMediaFile.photoOrThrow(): Photo = this as
dev.inmo.tgbotapi.types.files.Photo
public inline fun TelegramMediaFile.photoOrThrow(): PhotoFile = this as
dev.inmo.tgbotapi.types.files.PhotoFile
public inline fun <T> TelegramMediaFile.ifPhoto(block: (Photo) -> T): T? = photoOrNull()
public inline fun <T> TelegramMediaFile.ifPhoto(block: (PhotoFile) -> T): T? = photoOrNull()
?.let(block)
public inline fun TelegramMediaFile.photoSizeOrNull(): PhotoSize? = this as?

View File

@ -103,7 +103,7 @@ inline val Message.animation: AnimationFile?
inline val Message.game: Game?
get() = asContentMessage() ?.content ?.asGameContent() ?.game
@RiskFeature(RawFieldsUsageWarning)
inline val Message.photo: Photo?
inline val Message.photo: PhotoFile?
get() = asContentMessage() ?.content ?.asPhotoContent() ?.mediaCollection
@RiskFeature(RawFieldsUsageWarning)
inline val Message.sticker: Sticker?
@ -145,7 +145,7 @@ inline val Message.left_chat_member: User?
inline val Message.new_chat_title: String?
get() = asChatEventMessage() ?.chatEvent ?.asNewChatTitle() ?.title
@RiskFeature(RawFieldsUsageWarning)
inline val Message.new_chat_photo: Photo?
inline val Message.new_chat_photo: PhotoFile?
get() = asChatEventMessage() ?.chatEvent ?.asNewChatPhoto() ?.photo
@RiskFeature(RawFieldsUsageWarning)
inline val Message.delete_chat_photo: Boolean