diff --git a/CHANGELOG.md b/CHANGELOG.md index b9bd46603d..a1d55f87b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,8 +52,7 @@ ### 0.27.5 * `TelegramotAPI`: - * Fix: `SendTextMessage` will correctly check the length of incoming text - * Constant `maxTextLength` was added + * Fix: for sending requests caption and text lengths limits were updated ### 0.27.4 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt index 93622ddd01..9c5ced8cba 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendMessage.kt @@ -9,6 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* internal val TextContentMessageResultDeserializer: DeserializationStrategy> @@ -36,8 +37,8 @@ data class SendTextMessage( DisableWebPagePreview { init { - if (text.length > maxTextLength) { - throw IllegalArgumentException("Text length must be less than $maxTextLength, but was ${text.length}") + if (text.length !in textLength) { + throwRangeError("Text length", textLength, text.length) } } 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 4ff6d9e89e..7ec3a22149 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 @@ -26,9 +26,8 @@ typealias LongSeconds = Long val getUpdatesLimit = 1 .. 100 val callbackQueryAnswerLength = 0 until 200 -val captionLength = 0 until 1024 -val textLength = 0 until 4096 -val maxTextLength = textLength.last + 1 +val captionLength = 0 .. 1024 +val textLength = 1 .. 4096 val userProfilePhotosRequestLimit = 0 .. 100 val chatTitleLength = 1 until 255 val chatDescriptionLength = 0 until 256 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt index da9b9ba407..c83bbe1500 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/CaptionAndTextSourcesToText.kt @@ -8,7 +8,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEnti fun createFormattedText( entities: FullTextSourcesList, - partLength: Int = maxTextLength, + partLength: Int = textLength.last, mode: ParseMode = MarkdownParseMode ): List { val texts = mutableListOf() @@ -49,7 +49,7 @@ fun createFormattedText( fun createMarkdownText( entities: FullTextSourcesList, - partLength: Int = maxTextLength + partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownParseMode) fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText( @@ -60,7 +60,7 @@ fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMar fun FullTextSourcesList.toMarkdownTexts(): List = createMarkdownText( this, - maxTextLength + textLength.last ) fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts() @@ -73,7 +73,7 @@ fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().t fun createMarkdownV2Text( entities: FullTextSourcesList, - partLength: Int = maxTextLength + partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( @@ -84,7 +84,7 @@ fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toM fun FullTextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( this, - maxTextLength + textLength.last ) fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts() @@ -108,7 +108,7 @@ fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCap fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText( this, - maxTextLength + textLength.last ) fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/ThrowErrorWithRange.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/ThrowErrorWithRange.kt new file mode 100644 index 0000000000..2e2e327002 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/ThrowErrorWithRange.kt @@ -0,0 +1,7 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +internal fun throwRangeError( + valueName: String, + range: IntRange, + actualValue: Int +): Nothing = error("$valueName must be in range $range, but was $actualValue")