diff --git a/CHANGELOG.md b/CHANGELOG.md index 924e7e3639..f834ad8471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,11 @@ * Interface `PossiblySentViaBot` has been added * Currently, only `ChannelMessage` and `CommonMessageImpl` are implementing interface `PossiblySentViaBot`. It could be changed in future + * Factory `buildMimeType` was added + * `BuiltinMimeTypes` was added + * Abstraction `ThumbedWithMimeTypeInlineQueryResult` with `thumbMimeType` field was added + * `InlineQueryResultGif` and `InlineQueryResultMpeg4Gif` now extend `ThumbedWithMimeTypeInlineQueryResult` + instead of `ThumbedInlineQueryResult` ### 0.27.5 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 52920da548..2fb043f7bd 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -1,5 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types +import com.github.insanusmokrassar.TelegramBotAPI.utils.BuiltinMimeTypes + typealias Identifier = Long typealias MessageIdentifier = Long typealias InlineQueryIdentifier = String @@ -63,6 +65,12 @@ val quizPollExplanationLimit = explanationLimit val openPeriodPollSecondsLimit = 5 .. 600 +val telegramInlineModeGifPermittedMimeTypes = listOf( + BuiltinMimeTypes.Image.Jpg, + BuiltinMimeTypes.Image.Gif, + BuiltinMimeTypes.Video.MP4 +) + const val chatIdField = "chat_id" const val messageIdField = "message_id" const val updateIdField = "update_id" @@ -177,6 +185,7 @@ const val stickerFileIdField = "sticker_file_id" const val gameShortNameField = "game_short_name" const val thumbUrlField = "thumb_url" +const val thumbMimeTypeField = "thumb_mime_type" const val thumbWidthField = "thumb_width" const val thumbHeightField = "thumb_height" diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultGifImpl.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultGifImpl.kt index 4785c3cb33..97e23a5d23 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultGifImpl.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultGifImpl.kt @@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts. import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup +import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -18,6 +19,8 @@ data class InlineQueryResultGifImpl( override val url: String, @SerialName(thumbUrlField) override val thumbUrl: String, + @SerialName(thumbMimeTypeField) + override val thumbMimeType: MimeType? = null, @SerialName(gifWidthField) override val width: Int? = null, @SerialName(gifHeightField) @@ -36,4 +39,10 @@ data class InlineQueryResultGifImpl( override val inputMessageContent: InputMessageContent? = null ) : InlineQueryResultGif { override val type: String = inlineQueryResultGifType + + init { + if (thumbMimeType != null && thumbMimeType !in telegramInlineModeGifPermittedMimeTypes) { + error("Passed thumb mime type is not permitted in Telegram Bot API. Passed $thumbMimeType, but permitted $telegramInlineModeGifPermittedMimeTypes") + } + } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultMpeg4GifImpl.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultMpeg4GifImpl.kt index acf78ca86e..6456378bf9 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultMpeg4GifImpl.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/InlineQueryResultMpeg4GifImpl.kt @@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts. import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup +import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -18,6 +19,8 @@ data class InlineQueryResultMpeg4GifImpl( override val url: String, @SerialName(thumbUrlField) override val thumbUrl: String, + @SerialName(thumbMimeTypeField) + override val thumbMimeType: MimeType? = null, @SerialName(mpeg4GifWidthField) override val width: Int? = null, @SerialName(mpeg4GifHeightField) @@ -36,4 +39,10 @@ data class InlineQueryResultMpeg4GifImpl( override val inputMessageContent: InputMessageContent? = null ) : InlineQueryResultMpeg4Gif { override val type: String = inlineQueryResultMpeg4GifType + + init { + if (thumbMimeType != null && thumbMimeType !in telegramInlineModeGifPermittedMimeTypes) { + error("Passed thumb mime type is not permitted in Telegram Bot API. Passed $thumbMimeType, but permitted $telegramInlineModeGifPermittedMimeTypes") + } + } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/ThumbedInlineQueryResult.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/ThumbedInlineQueryResult.kt index e5a1f58ca1..df829a6b4f 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/ThumbedInlineQueryResult.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/ThumbedInlineQueryResult.kt @@ -1,5 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InlineQueryResult.abstracts +import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType + interface ThumbedInlineQueryResult : InlineQueryResult { val thumbUrl: String? +} + +interface ThumbedWithMimeTypeInlineQueryResult : ThumbedInlineQueryResult { + val thumbMimeType: MimeType? } \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/gif/InlineQueryResultGif.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/gif/InlineQueryResultGif.kt index 215f839003..ed547b05b7 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/gif/InlineQueryResultGif.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/gif/InlineQueryResultGif.kt @@ -2,4 +2,4 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InlineQue import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InlineQueryResult.abstracts.* -interface InlineQueryResultGif : InlineQueryResultGifCommon, UrlInlineQueryResult, ThumbedInlineQueryResult, SizedInlineQueryResult, DuratedInlineResultQuery \ No newline at end of file +interface InlineQueryResultGif : InlineQueryResultGifCommon, UrlInlineQueryResult, ThumbedWithMimeTypeInlineQueryResult, SizedInlineQueryResult, DuratedInlineResultQuery \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/mpeg4gif/InlineQueryResultMpeg4Gif.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/mpeg4gif/InlineQueryResultMpeg4Gif.kt index ffb42e5274..e5d1f07287 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/mpeg4gif/InlineQueryResultMpeg4Gif.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InlineQueryResult/abstracts/results/mpeg4gif/InlineQueryResultMpeg4Gif.kt @@ -2,4 +2,4 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InlineQue import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.InlineQueryResult.abstracts.* -interface InlineQueryResultMpeg4Gif : InlineQueryResultMpeg4GifCommon, UrlInlineQueryResult, ThumbedInlineQueryResult, SizedInlineQueryResult, DuratedInlineResultQuery \ No newline at end of file +interface InlineQueryResultMpeg4Gif : InlineQueryResultMpeg4GifCommon, UrlInlineQueryResult, ThumbedWithMimeTypeInlineQueryResult, SizedInlineQueryResult, DuratedInlineResultQuery \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BuiltinMimeTypes.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BuiltinMimeTypes.kt new file mode 100644 index 0000000000..a3068df617 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BuiltinMimeTypes.kt @@ -0,0 +1,11 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +object BuiltinMimeTypes { + object Image { + val Jpg = buildMimeType("image/jpeg") + val Gif = buildMimeType("image/gif") + } + object Video { + val MP4 = buildMimeType("video/mp4") + } +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt index 65cb95b5ec..e7ffa19f8d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt @@ -7,5 +7,7 @@ expect class MimeType { val raw: String } +expect fun buildMimeType(raw: String): MimeType + @Serializer(MimeType::class) internal expect object MimeTypeSerializer : KSerializer diff --git a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt index a99e334557..dece7f4e4f 100644 --- a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt +++ b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt @@ -23,7 +23,7 @@ internal actual object MimeTypeSerializer : KSerializer { override fun deserialize(decoder: Decoder): MimeType { val mimeType = decoder.decodeString() return mimesCache.getOrPut(mimeType) { - MimeType(mimeType) + buildMimeType(mimeType) } } @@ -31,3 +31,5 @@ internal actual object MimeTypeSerializer : KSerializer { encoder.encodeString(value.raw) } } + +actual fun buildMimeType(raw: String) = MimeType(raw) diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt index b3f85ace6d..a653618e7a 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MimeType.kt @@ -24,3 +24,5 @@ internal actual object MimeTypeSerializer : KSerializer { encoder.encodeString(value.raw) } } + +actual fun buildMimeType(raw: String): MimeType = MimeType(raw)