From e6430a729cb99b76932dabff7892aee486b0082d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 13:37:11 +0600 Subject: [PATCH 1/6] work on version 0.27.6 has been started --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c7264821b..2edfeab585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ * `closePollExactAfter` * `closePollAfter` +### 0.27.6 + ### 0.27.5 * `Common`: diff --git a/gradle.properties b/gradle.properties index 3af5538611..7a2af6e143 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,6 +9,6 @@ ktor_version=1.3.2 javax_activation_version=1.1.1 library_group=com.github.insanusmokrassar -library_version=0.27.5 +library_version=0.27.6 gradle_bintray_plugin_version=1.8.4 From 835b8b34f96c4b806ebb28c428e29728501d2f4f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 13:38:05 +0600 Subject: [PATCH 2/6] add via_bot --- CHANGELOG.md | 5 +++++ .../TelegramBotAPI/types/message/ChannelMessage.kt | 9 ++++----- .../TelegramBotAPI/types/message/CommonMessageImpl.kt | 6 +++--- .../TelegramBotAPI/types/message/RawMessage.kt | 3 +++ .../types/message/abstracts/PossiblySentViaBot.kt | 7 +++++++ 5 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/PossiblySentViaBot.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 2edfeab585..924e7e3639 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,11 @@ ### 0.27.6 +* `TelegramBotAPI`: + * Interface `PossiblySentViaBot` has been added + * Currently, only `ChannelMessage` and `CommonMessageImpl` are implementing interface `PossiblySentViaBot`. It + could be changed in future + ### 0.27.5 * `Common`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt index 990804d6b1..bcd9e41b0c 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt @@ -1,11 +1,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message -import com.github.insanusmokrassar.TelegramBotAPI.types.AuthorSignature -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent import com.soywiz.klock.DateTime @@ -18,5 +16,6 @@ data class ChannelMessage( override val forwardInfo: ForwardInfo?, override val replyTo: Message?, override val replyMarkup: InlineKeyboardMarkup?, + override val senderBot: CommonBot?, val authorSignature: AuthorSignature? -) : CommonMessage +) : CommonMessage, PossiblySentViaBot diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt index f9d5950260..9db1300557 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt @@ -1,7 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message -import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* @@ -19,5 +18,6 @@ data class CommonMessageImpl( override val forwardInfo: ForwardInfo?, override val replyTo: Message?, override val replyMarkup: InlineKeyboardMarkup?, + override val senderBot: CommonBot?, val paymentInfo: SuccessfulPaymentInfo? -) : Message, CommonMessage, FromUserMessage \ No newline at end of file +) : Message, CommonMessage, PossiblySentViaBot, FromUserMessage \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt index 2673171eb1..9f9eee6167 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt @@ -42,6 +42,7 @@ internal data class RawMessage( private val forward_sender_name: ForwardSenderName? = null, private val forward_date: TelegramDate? = null, private val reply_to_message: RawMessage? = null, + private val via_bot: CommonBot? = null, private val edit_date: TelegramDate? = null, private val media_group_id: MediaGroupIdentifier? = null, private val author_signature: AuthorSignature? = null, @@ -255,6 +256,7 @@ internal data class RawMessage( forwarded, reply_to_message?.asMessage, reply_markup, + via_bot, author_signature ) else -> CommonMessageImpl( @@ -268,6 +270,7 @@ internal data class RawMessage( forwarded, reply_to_message?.asMessage, reply_markup, + via_bot, paymentInfo ) } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/PossiblySentViaBot.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/PossiblySentViaBot.kt new file mode 100644 index 0000000000..c1580b2a6b --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/PossiblySentViaBot.kt @@ -0,0 +1,7 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts + +import com.github.insanusmokrassar.TelegramBotAPI.types.CommonBot + +interface PossiblySentViaBot { + val senderBot: CommonBot? +} From ca784e67dfb51a41a0807c8eee59da2fc256d240 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 14:01:19 +0600 Subject: [PATCH 3/6] add BuiltInMimeTypes, ThumbedWithMimeTypeInlineQueryResult and buildMimeType factory --- CHANGELOG.md | 5 +++++ .../insanusmokrassar/TelegramBotAPI/types/Common.kt | 9 +++++++++ .../InlineQueryResult/InlineQueryResultGifImpl.kt | 9 +++++++++ .../InlineQueryResultMpeg4GifImpl.kt | 9 +++++++++ .../abstracts/ThumbedInlineQueryResult.kt | 6 ++++++ .../abstracts/results/gif/InlineQueryResultGif.kt | 2 +- .../results/mpeg4gif/InlineQueryResultMpeg4Gif.kt | 2 +- .../TelegramBotAPI/utils/BuiltinMimeTypes.kt | 11 +++++++++++ .../insanusmokrassar/TelegramBotAPI/utils/MimeType.kt | 2 ++ .../insanusmokrassar/TelegramBotAPI/utils/MimeType.kt | 4 +++- .../insanusmokrassar/TelegramBotAPI/utils/MimeType.kt | 2 ++ 11 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BuiltinMimeTypes.kt 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) From ca4beee95f859805b5a5cd7e39ecd23b7e35f27d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 14:25:19 +0600 Subject: [PATCH 4/6] add PossiblySentViaBotCommonMessage and fix error in build --- CHANGELOG.md | 6 ++++-- .../types/message/ChannelMessage.kt | 3 ++- .../types/message/CommonMessageImpl.kt | 3 ++- .../TelegramBotAPI/types/message/RawMessage.kt | 15 +++++++-------- .../abstracts/PossiblySentViaBotCommonMessage.kt | 6 ++++++ gradle/wrapper/gradle-wrapper.properties | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/abstracts/PossiblySentViaBotCommonMessage.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index f834ad8471..25c016e183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,8 +53,10 @@ * `TelegramBotAPI`: * Interface `PossiblySentViaBot` has been added - * Currently, only `ChannelMessage` and `CommonMessageImpl` are implementing interface `PossiblySentViaBot`. It - could be changed in future + * Additional interface `PossiblySentViaBotCommonMessage` was added for more explicit typing declaration for + compiler + * Currently, only `ChannelMessage` and `CommonMessageImpl` are implementing the interface + `PossiblySentViaBotCommonMessage`. It could be changed in future * Factory `buildMimeType` was added * `BuiltinMimeTypes` was added * Abstraction `ThumbedWithMimeTypeInlineQueryResult` with `thumbMimeType` field was added diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt index bcd9e41b0c..15bea82224 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt @@ -5,6 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMa import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.PossiblySentViaBotCommonMessage import com.soywiz.klock.DateTime data class ChannelMessage( @@ -18,4 +19,4 @@ data class ChannelMessage( override val replyMarkup: InlineKeyboardMarkup?, override val senderBot: CommonBot?, val authorSignature: AuthorSignature? -) : CommonMessage, PossiblySentViaBot +) : PossiblySentViaBotCommonMessage diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt index 9db1300557..08c2b32bab 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt @@ -5,6 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMa import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.PossiblySentViaBotCommonMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.SuccessfulPaymentInfo import com.soywiz.klock.DateTime @@ -20,4 +21,4 @@ data class CommonMessageImpl( override val replyMarkup: InlineKeyboardMarkup?, override val senderBot: CommonBot?, val paymentInfo: SuccessfulPaymentInfo? -) : Message, CommonMessage, PossiblySentViaBot, FromUserMessage \ No newline at end of file +) : PossiblySentViaBotCommonMessage, FromUserMessage \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt index 9f9eee6167..92ef9049ea 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt @@ -148,7 +148,7 @@ internal data class RawMessage( ) forward_from_chat is ChannelChat -> ForwardFromChannelInfo( forward_date, - forward_from_message_id ?: throw IllegalStateException("Channel forwarded message must contain message id, but was not"), + forward_from_message_id ?: error("Channel forwarded message must contain message id, but was not"), forward_from_chat, forward_signature ) @@ -209,7 +209,7 @@ internal data class RawMessage( chatEvent as? ChannelEvent ?: throwWrongChatEvent(ChannelEvent::class, chatEvent), date.asDate ) - else -> throw IllegalStateException("Expected one of the public chats, but was $chat (in extracting of chat event message)") + else -> error("Expected one of the public chats, but was $chat (in extracting of chat event message)") } } ?: content?.let { content -> media_group_id?.let { @@ -222,7 +222,7 @@ internal data class RawMessage( when (content) { is PhotoContent -> content is VideoContent -> content - else -> throw IllegalStateException("Unsupported content for media group") + else -> error("Unsupported content for media group") }, edit_date?.asDate, forwarded, @@ -238,7 +238,7 @@ internal data class RawMessage( when (content) { is PhotoContent -> content is VideoContent -> content - else -> throw IllegalStateException("Unsupported content for media group") + else -> error("Unsupported content for media group") }, edit_date?.asDate, forwarded, @@ -261,8 +261,7 @@ internal data class RawMessage( ) else -> CommonMessageImpl( messageId, - from - ?: throw IllegalStateException("Was detected common message, but owner (sender) of the message was not found"), + from ?: error("Was detected common message, but owner (sender) of the message was not found"), chat, content, date.asDate, @@ -274,7 +273,7 @@ internal data class RawMessage( paymentInfo ) } - } ?: throw IllegalStateException("Was not found supported type of data") + } ?: error("Was not found supported type of data") } catch (e: Exception) { UnknownMessageType( messageId, @@ -286,6 +285,6 @@ internal data class RawMessage( } private fun throwWrongChatEvent(expected: KClass<*>, but: ChatEvent): CommonEvent { - throw IllegalStateException("Wrong type of chat event: expected $expected, but was $but") + error("Wrong type of chat event: expected $expected, but was $but") } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/abstracts/PossiblySentViaBotCommonMessage.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/abstracts/PossiblySentViaBotCommonMessage.kt new file mode 100644 index 0000000000..c13c9548c6 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/abstracts/PossiblySentViaBotCommonMessage.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.PossiblySentViaBot + +interface PossiblySentViaBotCommonMessage : CommonMessage, PossiblySentViaBot diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8ef06692c2..3c46198fce 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip From 130e00b62b4978acd05b1f8137ac7867583f7fa1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 14:57:58 +0600 Subject: [PATCH 5/6] extensions for CommonMessage --- CHANGELOG.md | 2 ++ .../utils/CommonMessageConversations.kt | 34 +++++++++++++++++++ .../SentMessageUpdatesConversations.kt | 7 ++++ 3 files changed, 43 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 25c016e183..d0d8af500a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,8 @@ * Abstraction `ThumbedWithMimeTypeInlineQueryResult` with `thumbMimeType` field was added * `InlineQueryResultGif` and `InlineQueryResultMpeg4Gif` now extend `ThumbedWithMimeTypeInlineQueryResult` instead of `ThumbedInlineQueryResult` +* `TelegramBotAPI-extensions-utils`: + * New extensions `onlyCommonMessages`, `onlySentViaBot` and `withoutSentViaBot` was added ### 0.27.5 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt new file mode 100644 index 0000000000..d4fa37e1c0 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt @@ -0,0 +1,34 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.PossiblySentViaBotCommonMessage +import kotlinx.coroutines.flow.* + +/** + * Simple factory to convert [ContentMessage] to a [CommonMessage] + */ +fun > Flow.onlyCommonMessages() = filterIsInstance>() + +/** + * Filter the messages and checking that incoming [CommonMessage] is [PossiblySentViaBotCommonMessage] and its + * [PossiblySentViaBotCommonMessage.senderBot] is not null + */ +fun Flow>.onlySentViaBot() = mapNotNull { + (it as? PossiblySentViaBotCommonMessage) ?.let { possiblySentViaBot -> + if (possiblySentViaBot.senderBot != null) { + possiblySentViaBot + } else { + null + } + } +} + +/** + * Filter the messages and checking that incoming [CommonMessage] not is [PossiblySentViaBotCommonMessage] or its + * [PossiblySentViaBotCommonMessage.senderBot] is null + */ +fun Flow>.withoutSentViaBot() = filter { + it !is PossiblySentViaBotCommonMessage || it.senderBot == null +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt index 15ebf19f76..f88f9f09a8 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt @@ -12,6 +12,13 @@ fun Flow.asContentMessagesFlow() = mapNotNull { it.data as? ContentMessage<*> } +/** + * Will map incoming [BaseSentMessageUpdate]s to [CommonMessage] from [BaseSentMessageUpdate.data] + */ +fun Flow.asCommonMessagesFlow() = mapNotNull { + it.data as? CommonMessage<*> +} + /** * Will map incoming [BaseSentMessageUpdate]s to [ChatEventMessage] from [BaseSentMessageUpdate.data] */ From 670cfcca136c4b3c8693ce79a835fe72035b7c5e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 15:17:14 +0600 Subject: [PATCH 6/6] update kotlin coroutines --- CHANGELOG.md | 3 +++ gradle.properties | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0d8af500a..0d7afbc178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,9 @@ ### 0.27.6 +* `Common`: + * Versions: + * `Kotlin Coroutines`: `1.3.6` -> `1.3.7` * `TelegramBotAPI`: * Interface `PossiblySentViaBot` has been added * Additional interface `PossiblySentViaBotCommonMessage` was added for more explicit typing declaration for diff --git a/gradle.properties b/gradle.properties index 7a2af6e143..cd63466dc7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ kotlin.code.style=official kotlin_version=1.3.72 -kotlin_coroutines_version=1.3.6 +kotlin_coroutines_version=1.3.7 kotlin_serialisation_runtime_version=0.20.0 klock_version=1.11.3 uuid_version=0.1.0