From ec37df82a90e0fbfd9b12cd861a869e1ba55134b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 31 May 2020 22:43:35 +0600 Subject: [PATCH 01/21] start 0.27.5 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b064a4f38..f3fb794008 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ * `closePollExactAfter` * `closePollAfter` +### 0.27.5 + ### 0.27.4 * `TelegramBotAPI-extensions-utils`: diff --git a/gradle.properties b/gradle.properties index 3c87806a9d..f18904205e 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.4 +library_version=0.27.5 gradle_bintray_plugin_version=1.8.4 From 35dcd6ada755d4fefe10a583cae415baa71418dc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 31 May 2020 22:51:42 +0600 Subject: [PATCH 02/21] fix in SendMessage --- CHANGELOG.md | 4 ++++ .../TelegramBotAPI/requests/send/SendMessage.kt | 4 ++-- .../github/insanusmokrassar/TelegramBotAPI/types/Common.kt | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3fb794008..b9bd46603d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,10 @@ ### 0.27.5 +* `TelegramotAPI`: + * Fix: `SendTextMessage` will correctly check the length of incoming text + * Constant `maxTextLength` was added + ### 0.27.4 * `TelegramBotAPI-extensions-utils`: 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 8d91a0eb40..93622ddd01 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 @@ -36,8 +36,8 @@ data class SendTextMessage( DisableWebPagePreview { init { - if (text.length !in textLength) { - throw IllegalArgumentException("Text must be in $textLength range") + if (text.length > maxTextLength) { + throw IllegalArgumentException("Text length must be less than $maxTextLength, but was ${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 441569f66d..4ff6d9e89e 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 @@ -28,6 +28,7 @@ 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 userProfilePhotosRequestLimit = 0 .. 100 val chatTitleLength = 1 until 255 val chatDescriptionLength = 0 until 256 From 7cd5666e88cb5397d510c69ce45e9d0b36cbe322 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 31 May 2020 22:53:04 +0600 Subject: [PATCH 03/21] use maxTextLength inside of CaptionAndTextSourcesToText --- .../utils/CaptionAndTextSourcesToText.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 4a2a9c2def..da9b9ba407 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 = 4096, + partLength: Int = maxTextLength, mode: ParseMode = MarkdownParseMode ): List { val texts = mutableListOf() @@ -49,7 +49,7 @@ fun createFormattedText( fun createMarkdownText( entities: FullTextSourcesList, - partLength: Int = 4096 + partLength: Int = maxTextLength ): 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, - textLength.last + 1 + maxTextLength ) fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts() @@ -73,7 +73,7 @@ fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().t fun createMarkdownV2Text( entities: FullTextSourcesList, - partLength: Int = 4096 + partLength: Int = maxTextLength ): 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, - textLength.last + 1 + maxTextLength ) fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts() @@ -108,7 +108,7 @@ fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCap fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText( this, - textLength.last + 1 + maxTextLength ) fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() From ab9ceba41c2b8ffdff1b548cbe59fb7baf879a87 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 1 Jun 2020 11:44:58 +0600 Subject: [PATCH 04/21] updates in caption and text lengths --- CHANGELOG.md | 3 +-- .../TelegramBotAPI/requests/send/SendMessage.kt | 5 +++-- .../insanusmokrassar/TelegramBotAPI/types/Common.kt | 5 ++--- .../utils/CaptionAndTextSourcesToText.kt | 12 ++++++------ .../TelegramBotAPI/utils/ThrowErrorWithRange.kt | 7 +++++++ 5 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/ThrowErrorWithRange.kt 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") From 0bcc98e126fa443e83866e5bdc990352b345812d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 1 Jun 2020 11:53:33 +0600 Subject: [PATCH 05/21] update caption errors --- .../TelegramBotAPI/requests/send/media/SendAnimation.kt | 3 ++- .../TelegramBotAPI/requests/send/media/SendAudio.kt | 3 ++- .../TelegramBotAPI/requests/send/media/SendDocument.kt | 3 ++- .../TelegramBotAPI/requests/send/media/SendMediaGroup.kt | 9 ++++++--- .../TelegramBotAPI/requests/send/media/SendPhoto.kt | 3 ++- .../TelegramBotAPI/requests/send/media/SendVideo.kt | 3 ++- .../TelegramBotAPI/requests/send/media/SendVideoNote.kt | 3 ++- .../TelegramBotAPI/requests/send/media/SendVoice.kt | 3 ++- .../insanusmokrassar/TelegramBotAPI/types/Common.kt | 2 ++ 9 files changed, 22 insertions(+), 10 deletions(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt index 0de73f8fdc..e094a177b4 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt @@ -11,6 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Conten import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.AnimationContent import com.github.insanusmokrassar.TelegramBotAPI.utils.mapOfNotNull +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendAnimation( @@ -93,7 +94,7 @@ data class SendAnimationData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.length) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt index 5facb438f5..b222130865 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt @@ -12,6 +12,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Conten import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.AudioContent import com.github.insanusmokrassar.TelegramBotAPI.utils.mapOfNotNull +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendAudio( @@ -95,7 +96,7 @@ data class SendAudioData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.length) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt index 44ff036ec2..e7cce4798f 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt @@ -11,6 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Conten import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.DocumentContent import com.github.insanusmokrassar.TelegramBotAPI.utils.mapOfNotNull +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendDocument( @@ -79,7 +80,7 @@ data class SendDocumentData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.length) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt index 4f7054bb29..c28dab19ea 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt @@ -8,12 +8,15 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.InputMedia.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import com.github.insanusmokrassar.TelegramBotAPI.utils.toJsonWithoutNulls import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.json.jsonArray -val membersCountInMediaGroup: IntRange = 2 .. 10 +@Deprecated("Replaced and renamed", ReplaceWith("mediaCountInMediaGroup", "com.github.insanusmokrassar.TelegramBotAPI.types.mediaCountInMediaGroup")) +val membersCountInMediaGroup + get() = mediaCountInMediaGroup fun SendMediaGroup( chatId: ChatIdentifier, @@ -21,8 +24,8 @@ fun SendMediaGroup( disableNotification: Boolean = false, replyToMessageId: MessageIdentifier? = null ): Request> { - if (media.size !in membersCountInMediaGroup) { - throw IllegalArgumentException("Count of members for media group must be in $membersCountInMediaGroup range") + if (media.size !in mediaCountInMediaGroup) { + throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size) } val files: List = media.flatMap { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt index b9b005d8b4..27e6be47fe 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt @@ -10,6 +10,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.media.PhotoContent +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendPhoto( @@ -65,7 +66,7 @@ data class SendPhotoData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.length) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt index 97781a1f7a..6bd81fe76c 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt @@ -11,6 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Conten import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.VideoContent import com.github.insanusmokrassar.TelegramBotAPI.utils.mapOfNotNull +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendVideo( @@ -97,7 +98,7 @@ data class SendVideoData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.length) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt index e4e3d11bfd..93cb292b29 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt @@ -11,6 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Conten import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.VideoNoteContent import com.github.insanusmokrassar.TelegramBotAPI.utils.mapOfNotNull +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendVideoNote( @@ -92,7 +93,7 @@ data class SendVideoNoteData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.length) } } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt index a341372e5c..24272364fd 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt @@ -11,6 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Conten import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.VoiceContent import com.github.insanusmokrassar.TelegramBotAPI.utils.mapOfNotNull +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.* fun SendVoice( @@ -84,7 +85,7 @@ data class SendVoiceData internal constructor( init { text ?.let { if (it.length !in captionLength) { - throw IllegalArgumentException("Caption must be in $captionLength range") + throwRangeError("Caption length", captionLength, it.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 7ec3a22149..52920da548 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 @@ -55,6 +55,8 @@ val botCommandLimit = botCommandLengthLimit val botCommandDescriptionLimit = 3 .. 256 val botCommandsLimit = 0 .. 100 +val mediaCountInMediaGroup: IntRange = 2 .. 10 + val explanationLimit = 0 .. 200 @Deprecated("Will be removed in near updates", ReplaceWith("explanationLimit")) val quizPollExplanationLimit = explanationLimit From 456143a2669100614170141a66c7a46b8b104117 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 1 Jun 2020 11:55:48 +0600 Subject: [PATCH 06/21] complete refresh of CaptinAndTextSourcesToText --- .../utils/CaptionAndTextSourcesToText.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 c83bbe1500..e30280f453 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 @@ -54,7 +54,7 @@ fun createMarkdownText( fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText( this, - captionLength.last + 1 + captionLength.last ) fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMarkdownCaptions() @@ -66,7 +66,7 @@ fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownT fun FullTextSourcesList.toMarkdownExplanations(): List = createMarkdownText( this, - explanationLimit.last + 1 + explanationLimit.last ) fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().toMarkdownTexts() @@ -78,7 +78,7 @@ fun createMarkdownV2Text( fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( this, - captionLength.last + 1 + captionLength.last ) fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toMarkdownV2Captions() @@ -90,19 +90,19 @@ fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdow fun FullTextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text( this, - explanationLimit.last + 1 + explanationLimit.last ) fun ExplainedInput.toMarkdownV2Explanations(): List = fullEntitiesList().toMarkdownV2Texts() fun createHtmlText( entities: FullTextSourcesList, - partLength: Int = 4096 + partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, HTMLParseMode) fun FullTextSourcesList.toHtmlCaptions(): List = createHtmlText( this, - captionLength.last + 1 + captionLength.last ) fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCaptions() @@ -114,7 +114,7 @@ fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() fun FullTextSourcesList.toHtmlExplanations(): List = createHtmlText( this, - explanationLimit.last + 1 + explanationLimit.last ) fun ExplainedInput.toHtmlExplanations(): List = fullEntitiesList().toHtmlTexts() From 9ae252717db05cb3e38900ee22469dc297448db3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 1 Jun 2020 12:00:06 +0600 Subject: [PATCH 07/21] versions update --- CHANGELOG.md | 3 +++ gradle.properties | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1d55f87b7..acd8b4caa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,9 @@ ### 0.27.5 +* `Common`: + * Versions: + * `Klock`: `1.11.1` -> `1.11.3` * `TelegramotAPI`: * Fix: for sending requests caption and text lengths limits were updated diff --git a/gradle.properties b/gradle.properties index f18904205e..3af5538611 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ kotlin.code.style=official kotlin_version=1.3.72 kotlin_coroutines_version=1.3.6 kotlin_serialisation_runtime_version=0.20.0 -klock_version=1.11.1 +klock_version=1.11.3 uuid_version=0.1.0 ktor_version=1.3.2 From 2a276d92720fa94f75ed37d14eea82b653147664 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 1 Jun 2020 12:56:48 +0600 Subject: [PATCH 08/21] safely function --- CHANGELOG.md | 2 ++ .../extensions/utils/SafelyShortcut.kt | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/SafelyShortcut.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index acd8b4caa8..c28c39d133 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,8 @@ * `Klock`: `1.11.1` -> `1.11.3` * `TelegramotAPI`: * Fix: for sending requests caption and text lengths limits were updated +* `TelegramBotAPI-extensions-utils`: + * `safely` function was introduced. It is in `PreviewFeature` state currently ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/SafelyShortcut.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/SafelyShortcut.kt new file mode 100644 index 0000000000..793caa00a4 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/SafelyShortcut.kt @@ -0,0 +1,16 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.utils.* +import kotlinx.coroutines.CoroutineScope + +/** + * Shortcut for [handleSafely]. It was created for more comfortable way of handling different things + */ +@PreviewFeature +suspend inline fun safely( + noinline onException: ExceptionHandler = { throw it }, + noinline block: suspend CoroutineScope.() -> T +): T = handleSafely( + onException, + block +) From 5fbde4bc06803b7719b927792baf5c55ef4257d4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 01:02:38 +0600 Subject: [PATCH 09/21] new "row" --- CHANGELOG.md | 1 + .../github/insanusmokrassar/TelegramBotAPI/utils/Matrix.kt | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c28c39d133..713650d589 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ * `Klock`: `1.11.1` -> `1.11.3` * `TelegramotAPI`: * Fix: for sending requests caption and text lengths limits were updated + * New variant of `row` was added * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Matrix.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Matrix.kt index 08a661542a..2469e74da7 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Matrix.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Matrix.kt @@ -10,6 +10,10 @@ fun MatrixBuilder.row(block: RowBuilder.() -> Unit) { add(RowBuilder().also(block).row) } +fun MatrixBuilder.row(vararg elements: T) { + add(elements.toList()) +} + fun matrix(block: MatrixBuilder.() -> Unit): Matrix { return MatrixBuilder().also(block).matrix } From 648f1b488b7e443fa41fcc5ec939a13424e4f178 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 01:15:08 +0600 Subject: [PATCH 10/21] add makeLinkToAddStickerSet --- CHANGELOG.md | 3 + .../extensions/utils/LinksFormatting.kt | 76 +++++++++++++++++++ .../TelegramBotAPI/utils/LinksFormatting.kt | 2 + 3 files changed, 81 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 713650d589..854d749409 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,8 +57,11 @@ * `TelegramotAPI`: * Fix: for sending requests caption and text lengths limits were updated * New variant of `row` was added + * `makeLinkToMessage` extensions has been deprecated (replaced into `TelegramBotAPI-extensions-utils`) * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently + * `makeLinkToMessage` extensions has been added + * `makeLinkToAddStickerSet` function and its variations were added ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt new file mode 100644 index 0000000000..cca7c6c25d --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt @@ -0,0 +1,76 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* +import com.github.insanusmokrassar.TelegramBotAPI.types.StickerSetName +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.UsernameChat +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat +import com.github.insanusmokrassar.TelegramBotAPI.utils.link + +private const val internalLinkBeginning = "https://t.me" + +fun makeLinkToMessage( + username: String, + messageId: MessageIdentifier +): String = "$internalLinkBeginning/$username/$messageId" + +private val linkIdRedundantPartRegex = Regex("^-100") +private val usernameBeginSymbolRegex = Regex("^@") + +fun makeLinkToMessage( + chat: ExtendedChat, + messageId: MessageIdentifier +): String? { + return when { + chat is UsernameChat && chat.username != null -> { + "$internalLinkBeginning/${chat.username ?.username ?.replace(usernameBeginSymbolRegex, "")}/$messageId" + } + chat !is PrivateChat -> chat.id.chatId.toString().replace( + linkIdRedundantPartRegex, + "" + ).let { bareId -> + "$internalLinkBeginning/c/$bareId/$messageId" + } + else -> return null + } +} + +private const val stickerSetAddingLinkPrefix = "$internalLinkBeginning/addstickers" + +/** + * Create a link for adding of sticker set with name [stickerSetName]. Was added thanks to user Djaler and based on + * https://github.com/Djaler/evil-bot/blob/master/src/main/kotlin/com/github/djaler/evilbot/utils/StickerUtils.kt#L6-L8 + * + * @see [makeLinkToAddStickerSetInMarkdownV2] + * @see [makeLinkToAddStickerSetInMarkdown] + * @see [makeLinkToAddStickerSetInHtml] + */ +fun makeLinkToAddStickerSet( + stickerSetName: StickerSetName, + parseMode: ParseMode +) = (stickerSetName to "$stickerSetAddingLinkPrefix/$stickerSetName").link( + parseMode +) + +/** + * @return Link for adding of sticker set with name [stickerSetName] with formatting for [MarkdownV2] + */ +fun makeLinkToAddStickerSetInMarkdownV2(stickerSetName: StickerSetName) = makeLinkToAddStickerSet( + stickerSetName, + MarkdownV2 +) +/** + * @return Link for adding of sticker set with name [stickerSetName] with formatting for [Markdown] + */ +fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) = makeLinkToAddStickerSet( + stickerSetName, + Markdown +) +/** + * @return Link for adding of sticker set with name [stickerSetName] with formatting for [HTML] + */ +fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) = makeLinkToAddStickerSet( + stickerSetName, + HTML +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt index 29523ab7ea..9bacaf0dc4 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/LinksFormatting.kt @@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended. private const val internalLinkBeginning = "https://t.me" +@Deprecated("Replaced into TelegramBotAPI-extensions-utils project") fun makeLinkToMessage( username: String, messageId: MessageIdentifier @@ -15,6 +16,7 @@ fun makeLinkToMessage( private val linkIdRedundantPartRegex = Regex("^-100") private val usernameBeginSymbolRegex = Regex("^@") +@Deprecated("Replaced into TelegramBotAPI-extensions-utils project") fun makeLinkToMessage( chat: ExtendedChat, messageId: MessageIdentifier From 0a162c412908d0213aefed7f4dfb049944a04035 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 01:26:46 +0600 Subject: [PATCH 11/21] StringFormattiing replacement --- CHANGELOG.md | 2 + .../utils/{ => formatting}/LinksFormatting.kt | 32 ++- .../utils/formatting/StringFormatting.kt | 243 ++++++++++++++++++ .../TelegramBotAPI/utils/StringFormatting.kt | 64 +++++ 4 files changed, 327 insertions(+), 14 deletions(-) rename TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/{ => formatting}/LinksFormatting.kt (88%) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/StringFormatting.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 854d749409..62241d6218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,10 +58,12 @@ * Fix: for sending requests caption and text lengths limits were updated * New variant of `row` was added * `makeLinkToMessage` extensions has been deprecated (replaced into `TelegramBotAPI-extensions-utils`) + * All `String` formatting public extensions and functions was deprecated and replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added * `makeLinkToAddStickerSet` function and its variations were added + * All `String` formatting extensions and functions from `TelegramBotAPI` was added ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/LinksFormatting.kt similarity index 88% rename from TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt rename to TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/LinksFormatting.kt index cca7c6c25d..1bd86d26d5 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/LinksFormatting.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/LinksFormatting.kt @@ -1,4 +1,4 @@ -package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* @@ -24,7 +24,8 @@ fun makeLinkToMessage( ): String? { return when { chat is UsernameChat && chat.username != null -> { - "$internalLinkBeginning/${chat.username ?.username ?.replace(usernameBeginSymbolRegex, "")}/$messageId" + "$internalLinkBeginning/${chat.username ?.username ?.replace( + usernameBeginSymbolRegex, "")}/$messageId" } chat !is PrivateChat -> chat.id.chatId.toString().replace( linkIdRedundantPartRegex, @@ -56,21 +57,24 @@ fun makeLinkToAddStickerSet( /** * @return Link for adding of sticker set with name [stickerSetName] with formatting for [MarkdownV2] */ -fun makeLinkToAddStickerSetInMarkdownV2(stickerSetName: StickerSetName) = makeLinkToAddStickerSet( - stickerSetName, - MarkdownV2 -) +fun makeLinkToAddStickerSetInMarkdownV2(stickerSetName: StickerSetName) = + makeLinkToAddStickerSet( + stickerSetName, + MarkdownV2 + ) /** * @return Link for adding of sticker set with name [stickerSetName] with formatting for [Markdown] */ -fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) = makeLinkToAddStickerSet( - stickerSetName, - Markdown -) +fun makeLinkToAddStickerSetInMarkdown(stickerSetName: StickerSetName) = + makeLinkToAddStickerSet( + stickerSetName, + Markdown + ) /** * @return Link for adding of sticker set with name [stickerSetName] with formatting for [HTML] */ -fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) = makeLinkToAddStickerSet( - stickerSetName, - HTML -) +fun makeLinkToAddStickerSetInHtml(stickerSetName: StickerSetName) = + makeLinkToAddStickerSet( + stickerSetName, + HTML + ) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/StringFormatting.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/StringFormatting.kt new file mode 100644 index 0000000000..24aa8bb1f9 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/StringFormatting.kt @@ -0,0 +1,243 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting + +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.* + +const val markdownBoldControl = "*" +const val markdownItalicControl = "_" +const val markdownCodeControl = "`" +const val markdownPreControl = "```" + +const val markdownV2ItalicUnderlineDelimiter = "\u0013" +const val markdownV2StrikethroughControl = "~" +const val markdownV2UnderlineControl = "__" +const val markdownV2UnderlineEndControl = "$markdownV2UnderlineControl$markdownV2ItalicUnderlineDelimiter" +const val markdownV2ItalicEndControl = "$markdownItalicControl$markdownV2ItalicUnderlineDelimiter" + +const val htmlBoldControl = "b" +const val htmlItalicControl = "i" +const val htmlCodeControl = "code" +const val htmlPreControl = "pre" +const val htmlUnderlineControl = "u" +const val htmlStrikethroughControl = "s" + +private fun String.markdownDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "$openControlSymbol${toMarkdown()}$closeControlSymbol" +private fun String.markdownV2Default( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol, + escapeFun: String.() -> String = String::escapeMarkdownV2Common +) = "$openControlSymbol${escapeFun()}$closeControlSymbol" +private fun String.htmlDefault( + openControlSymbol: String, + closeControlSymbol: String = openControlSymbol +) = "<$openControlSymbol>${toHtml()}" + +fun String.linkMarkdown(link: String): String = "[${toMarkdown()}](${link.toMarkdown()})" +fun String.linkMarkdownV2(link: String): String = "[${escapeMarkdownV2Common()}](${link.escapeMarkdownV2Link()})" +fun String.linkHTML(link: String): String = "${toHtml()}" + + +fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) +fun String.boldMarkdownV2(): String = markdownV2Default(markdownBoldControl) +fun String.boldHTML(): String = htmlDefault(htmlBoldControl) + + +fun String.italicMarkdown(): String = markdownDefault(markdownItalicControl) +fun String.italicMarkdownV2(): String = markdownV2Default(markdownItalicControl, markdownV2ItalicEndControl) +fun String.italicHTML(): String = htmlDefault(htmlItalicControl) + +/** + * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct + */ +fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("") +fun String.strikethroughMarkdownV2(): String = markdownV2Default(markdownV2StrikethroughControl) +fun String.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl) + + +/** + * Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct + */ +fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("") +fun String.underlineMarkdownV2(): String = markdownV2Default(markdownV2UnderlineControl, markdownV2UnderlineEndControl) +fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl) + + +fun String.codeMarkdown(): String = markdownDefault(markdownCodeControl) +fun String.codeMarkdownV2(): String = markdownV2Default(markdownCodeControl, escapeFun = String::escapeMarkdownV2PreAndCode) +fun String.codeHTML(): String = htmlDefault(htmlCodeControl) + + +fun String.preMarkdown(language: String? = null): String = markdownDefault( + "$markdownPreControl${language ?: ""}\n", + "\n$markdownPreControl" +) +fun String.preMarkdownV2(language: String? = null): String = markdownV2Default( + "$markdownPreControl${language ?: ""}\n", + "\n$markdownPreControl", + String::escapeMarkdownV2PreAndCode +) +fun String.preHTML(language: String? = null): String = htmlDefault( + language ?.let { + "$htmlPreControl><$htmlCodeControl class=\"language-$language\"" + } ?: htmlPreControl, + language ?.let { + "$htmlCodeControl> String): String = if (startsWith("@")) { + adapt() +} else { + "@${adapt()}" +} + + +private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { + adapt() +} else { + "#${adapt()}" +} + + +fun String.textMentionMarkdown(userId: UserId): String = linkMarkdown(userId.link) +fun String.textMentionMarkdownV2(userId: UserId): String = linkMarkdownV2(userId.link) +fun String.textMentionHTML(userId: UserId): String = linkHTML(userId.link) + + +fun String.mentionMarkdown(): String = mention(String::toMarkdown) +fun String.mentionMarkdownV2(): String = mention(String::escapeMarkdownV2Common) +fun String.mentionHTML(): String = mention(String::toHtml) + + +fun String.hashTagMarkdown(): String = hashTag(String::toMarkdown) +fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common).escapeMarkdownV2Common() +fun String.hashTagHTML(): String = hashTag(String::toHtml) + + +fun String.phoneMarkdown(): String = toMarkdown() +fun String.phoneMarkdownV2(): String = escapeMarkdownV2Common() +fun String.phoneHTML(): String = toHtml() + + +fun String.command(adapt: String.() -> String): String = if (startsWith("/")) { + adapt() +} else { + "/${adapt()}" +} + +fun String.commandMarkdown(): String = command(String::toMarkdown) +fun String.commandMarkdownV2(): String = command(String::escapeMarkdownV2Common) +fun String.commandHTML(): String = command(String::toHtml) + + +fun String.regularMarkdown(): String = toMarkdown() +fun String.regularMarkdownV2(): String = escapeMarkdownV2Common() +fun String.regularHtml(): String = toHtml() + + +fun String.cashTagMarkdown(): String = toMarkdown() +fun String.cashTagMarkdownV2(): String = escapeMarkdownV2Common() +fun String.cashTagHtml(): String = toHtml() + + +infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { + is HTML -> boldHTML() + is Markdown -> boldMarkdown() + is MarkdownV2 -> boldMarkdownV2() +} + + +infix fun String.italic(parseMode: ParseMode): String = when (parseMode) { + is HTML -> italicHTML() + is Markdown -> italicMarkdown() + is MarkdownV2 -> italicMarkdownV2() +} + +infix fun String.hashTag(parseMode: ParseMode): String = when (parseMode) { + is HTML -> hashTagHTML() + is Markdown -> hashTagMarkdown() + is MarkdownV2 -> hashTagMarkdownV2() +} + +infix fun String.code(parseMode: ParseMode): String = when (parseMode) { + is HTML -> codeHTML() + is Markdown -> codeMarkdown() + is MarkdownV2 -> codeMarkdownV2() +} + +fun String.pre(parseMode: ParseMode, language: String? = null): String = when (parseMode) { + is HTML -> preHTML(language) + is Markdown -> preMarkdown(language) + is MarkdownV2 -> preMarkdownV2(language) +} +infix fun String.pre(parseMode: ParseMode): String = pre(parseMode, null) + +infix fun String.email(parseMode: ParseMode): String = when (parseMode) { + is HTML -> emailHTML() + is Markdown -> emailMarkdown() + is MarkdownV2 -> emailMarkdownV2() +} + +infix fun Pair.link(parseMode: ParseMode): String = when (parseMode) { + is HTML -> first.linkHTML(second) + is Markdown -> first.linkMarkdown(second) + is MarkdownV2 -> first.linkMarkdownV2(second) +} + +infix fun String.mention(parseMode: ParseMode): String = when (parseMode) { + is HTML -> mentionHTML() + is Markdown -> mentionMarkdown() + is MarkdownV2 -> mentionMarkdownV2() +} + +infix fun Pair.mention(parseMode: ParseMode): String = when (parseMode) { + is HTML -> first.textMentionHTML(second) + is Markdown -> first.textMentionMarkdown(second) + is MarkdownV2 -> first.textMentionMarkdownV2(second) +} + +infix fun String.phone(parseMode: ParseMode): String = when (parseMode) { + is HTML -> phoneHTML() + is Markdown -> phoneMarkdown() + is MarkdownV2 -> phoneMarkdownV2() +} + +infix fun String.command(parseMode: ParseMode): String = when (parseMode) { + is HTML -> commandHTML() + is Markdown -> commandMarkdown() + is MarkdownV2 -> commandMarkdownV2() +} + +infix fun String.underline(parseMode: ParseMode): String = when (parseMode) { + is HTML -> underlineHTML() + is Markdown -> underlineMarkdown() + is MarkdownV2 -> underlineMarkdownV2() +} + +infix fun String.strikethrough(parseMode: ParseMode): String = when (parseMode) { + is HTML -> strikethroughHTML() + is Markdown -> strikethroughMarkdown() + is MarkdownV2 -> strikethroughMarkdownV2() +} + +infix fun String.regular(parseMode: ParseMode): String = when (parseMode) { + is HTML -> regularHtml() + is Markdown -> regularMarkdown() + is MarkdownV2 -> regularMarkdownV2() +} + +infix fun String.cashtag(parseMode: ParseMode): String = when (parseMode) { + is HTML -> cashTagHtml() + is Markdown -> cashTagMarkdown() + is MarkdownV2 -> cashTagMarkdownV2() +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt index c112a00955..0dbb355654 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFormatting.kt @@ -36,50 +36,71 @@ private fun String.htmlDefault( closeControlSymbol: String = openControlSymbol ) = "<$openControlSymbol>${toHtml()}" +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.linkMarkdown(link: String): String = "[${toMarkdown()}](${link.toMarkdown()})" +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.linkMarkdownV2(link: String): String = "[${escapeMarkdownV2Common()}](${link.escapeMarkdownV2Link()})" +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.linkHTML(link: String): String = "${toHtml()}" +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.boldMarkdown(): String = markdownDefault(markdownBoldControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.boldMarkdownV2(): String = markdownV2Default(markdownBoldControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.boldHTML(): String = htmlDefault(htmlBoldControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.italicMarkdown(): String = markdownDefault(markdownItalicControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.italicMarkdownV2(): String = markdownV2Default(markdownItalicControl, markdownV2ItalicEndControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.italicHTML(): String = htmlDefault(htmlItalicControl) /** * Crutch for support of strikethrough in default markdown. Simply add modifier, but it will not look like correct */ +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.strikethroughMarkdown(): String = map { it + "\u0336" }.joinToString("") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.strikethroughMarkdownV2(): String = markdownV2Default(markdownV2StrikethroughControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.strikethroughHTML(): String = htmlDefault(htmlStrikethroughControl) /** * Crutch for support of underline in default markdown. Simply add modifier, but it will not look like correct */ +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.underlineMarkdown(): String = map { it + "\u0347" }.joinToString("") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.underlineMarkdownV2(): String = markdownV2Default(markdownV2UnderlineControl, markdownV2UnderlineEndControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.underlineHTML(): String = htmlDefault(htmlUnderlineControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.codeMarkdown(): String = markdownDefault(markdownCodeControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.codeMarkdownV2(): String = markdownV2Default(markdownCodeControl, escapeFun = String::escapeMarkdownV2PreAndCode) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.codeHTML(): String = htmlDefault(htmlCodeControl) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.preMarkdown(language: String? = null): String = markdownDefault( "$markdownPreControl${language ?: ""}\n", "\n$markdownPreControl" ) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.preMarkdownV2(language: String? = null): String = markdownV2Default( "$markdownPreControl${language ?: ""}\n", "\n$markdownPreControl", String::escapeMarkdownV2PreAndCode ) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.preHTML(language: String? = null): String = htmlDefault( language ?.let { "$htmlPreControl><$htmlCodeControl class=\"language-$language\"" @@ -90,11 +111,15 @@ fun String.preHTML(language: String? = null): String = htmlDefault( ) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.emailMarkdown(): String = linkMarkdown("mailto://$${toMarkdown()}") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.emailMarkdownV2(): String = linkMarkdownV2("mailto://$${toMarkdown()}") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.emailHTML(): String = linkHTML("mailto://$${toHtml()}") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") private inline fun String.mention(adapt: String.() -> String): String = if (startsWith("@")) { adapt() } else { @@ -102,6 +127,7 @@ private inline fun String.mention(adapt: String.() -> String): String = if (star } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") private inline fun String.hashTag(adapt: String.() -> String): String = if (startsWith("#")) { adapt() } else { @@ -109,47 +135,70 @@ private inline fun String.hashTag(adapt: String.() -> String): String = if (star } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.textMentionMarkdown(userId: UserId): String = linkMarkdown(userId.link) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.textMentionMarkdownV2(userId: UserId): String = linkMarkdownV2(userId.link) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.textMentionHTML(userId: UserId): String = linkHTML(userId.link) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.mentionMarkdown(): String = mention(String::toMarkdown) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.mentionMarkdownV2(): String = mention(String::escapeMarkdownV2Common) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.mentionHTML(): String = mention(String::toHtml) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.hashTagMarkdown(): String = hashTag(String::toMarkdown) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common).escapeMarkdownV2Common() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.hashTagHTML(): String = hashTag(String::toHtml) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.phoneMarkdown(): String = toMarkdown() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.phoneMarkdownV2(): String = escapeMarkdownV2Common() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.phoneHTML(): String = toHtml() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.command(adapt: String.() -> String): String = if (startsWith("/")) { adapt() } else { "/${adapt()}" } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.commandMarkdown(): String = command(String::toMarkdown) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.commandMarkdownV2(): String = command(String::escapeMarkdownV2Common) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.commandHTML(): String = command(String::toHtml) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.regularMarkdown(): String = toMarkdown() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.regularMarkdownV2(): String = escapeMarkdownV2Common() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.regularHtml(): String = toHtml() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.cashTagMarkdown(): String = toMarkdown() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.cashTagMarkdownV2(): String = escapeMarkdownV2Common() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.cashTagHtml(): String = toHtml() +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { is HTML -> boldHTML() is Markdown -> boldMarkdown() @@ -157,85 +206,100 @@ infix fun String.bold(parseMode: ParseMode): String = when (parseMode) { } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.italic(parseMode: ParseMode): String = when (parseMode) { is HTML -> italicHTML() is Markdown -> italicMarkdown() is MarkdownV2 -> italicMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.hashTag(parseMode: ParseMode): String = when (parseMode) { is HTML -> hashTagHTML() is Markdown -> hashTagMarkdown() is MarkdownV2 -> hashTagMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.code(parseMode: ParseMode): String = when (parseMode) { is HTML -> codeHTML() is Markdown -> codeMarkdown() is MarkdownV2 -> codeMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") fun String.pre(parseMode: ParseMode, language: String? = null): String = when (parseMode) { is HTML -> preHTML(language) is Markdown -> preMarkdown(language) is MarkdownV2 -> preMarkdownV2(language) } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.pre(parseMode: ParseMode): String = pre(parseMode, null) +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.email(parseMode: ParseMode): String = when (parseMode) { is HTML -> emailHTML() is Markdown -> emailMarkdown() is MarkdownV2 -> emailMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun Pair.link(parseMode: ParseMode): String = when (parseMode) { is HTML -> first.linkHTML(second) is Markdown -> first.linkMarkdown(second) is MarkdownV2 -> first.linkMarkdownV2(second) } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.mention(parseMode: ParseMode): String = when (parseMode) { is HTML -> mentionHTML() is Markdown -> mentionMarkdown() is MarkdownV2 -> mentionMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun Pair.mention(parseMode: ParseMode): String = when (parseMode) { is HTML -> first.textMentionHTML(second) is Markdown -> first.textMentionMarkdown(second) is MarkdownV2 -> first.textMentionMarkdownV2(second) } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.phone(parseMode: ParseMode): String = when (parseMode) { is HTML -> phoneHTML() is Markdown -> phoneMarkdown() is MarkdownV2 -> phoneMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.command(parseMode: ParseMode): String = when (parseMode) { is HTML -> commandHTML() is Markdown -> commandMarkdown() is MarkdownV2 -> commandMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.underline(parseMode: ParseMode): String = when (parseMode) { is HTML -> underlineHTML() is Markdown -> underlineMarkdown() is MarkdownV2 -> underlineMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.strikethrough(parseMode: ParseMode): String = when (parseMode) { is HTML -> strikethroughHTML() is Markdown -> strikethroughMarkdown() is MarkdownV2 -> strikethroughMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.regular(parseMode: ParseMode): String = when (parseMode) { is HTML -> regularHtml() is Markdown -> regularMarkdown() is MarkdownV2 -> regularMarkdownV2() } +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") infix fun String.cashtag(parseMode: ParseMode): String = when (parseMode) { is HTML -> cashTagHtml() is Markdown -> cashTagMarkdown() From b484a31a4a1a6b7225b662e32a51e14196905552 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 13:05:15 +0600 Subject: [PATCH 12/21] replacement of CaptionAndTextSourcesToText --- CHANGELOG.md | 2 + .../formatting/ResendingTextFormatting.kt | 121 ++++++++++++++++++ .../utils/CaptionAndTextSourcesToText.kt | 22 ++++ 3 files changed, 145 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 62241d6218..3721a395d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,11 +59,13 @@ * New variant of `row` was added * `makeLinkToMessage` extensions has been deprecated (replaced into `TelegramBotAPI-extensions-utils`) * All `String` formatting public extensions and functions was deprecated and replaced into `TelegramBotAPI-extensions-utils` + * All extensions like `CaptionedInput#toHtmlCaptions` was deprecated and replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added * `makeLinkToAddStickerSet` function and its variations were added * All `String` formatting extensions and functions from `TelegramBotAPI` was added + * All extensions like `CaptionedInput#toHtmlCaptions` from `TelegramBotAPI` was added ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt new file mode 100644 index 0000000000..ed5d0ad118 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/formatting/ResendingTextFormatting.kt @@ -0,0 +1,121 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList + +fun createFormattedText( + entities: FullTextSourcesList, + partLength: Int = textLength.last, + mode: ParseMode = MarkdownParseMode +): List { + val texts = mutableListOf() + val textBuilder = StringBuilder(partLength) + for (entity in entities) { + val string = when (mode) { + is MarkdownParseMode -> entity.asMarkdownSource + is MarkdownV2ParseMode -> entity.asMarkdownV2Source + is HTMLParseMode -> entity.asHtmlSource + } + if (textBuilder.length + string.length > partLength) { + if (textBuilder.isNotEmpty()) { + texts.add(textBuilder.toString()) + textBuilder.clear() + } + val chunked = string.chunked(partLength) + val last = chunked.last() + textBuilder.append(last) + val listToAdd = if (chunked.size > 1) { + chunked.subList(0, chunked.size - 1) + } else { + emptyList() + } + listToAdd.forEach { + texts.add(it) + } + } else { + textBuilder.append(string) + } + } + if (textBuilder.isNotEmpty()) { + texts.add(textBuilder.toString()) + textBuilder.clear() + } + return texts +} + + +fun createMarkdownText( + entities: FullTextSourcesList, + partLength: Int = textLength.last +): List = createFormattedText(entities, partLength, MarkdownParseMode) + +fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText( + this, + captionLength.last +) +fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMarkdownCaptions() + +fun FullTextSourcesList.toMarkdownTexts(): List = createMarkdownText( + this, + textLength.last +) +fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts() + +fun FullTextSourcesList.toMarkdownExplanations(): List = createMarkdownText( + this, + explanationLimit.last +) +fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().toMarkdownTexts() + + +fun createMarkdownV2Text( + entities: FullTextSourcesList, + partLength: Int = textLength.last +): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) + +fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( + this, + captionLength.last +) +fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toMarkdownV2Captions() + +fun FullTextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( + this, + textLength.last +) +fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts() + +fun FullTextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text( + this, + explanationLimit.last +) +fun ExplainedInput.toMarkdownV2Explanations(): List = fullEntitiesList().toMarkdownV2Texts() + + +fun createHtmlText( + entities: FullTextSourcesList, + partLength: Int = textLength.last +): List = createFormattedText(entities, partLength, HTMLParseMode) + +fun FullTextSourcesList.toHtmlCaptions(): List = createHtmlText( + this, + captionLength.last +) +fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCaptions() + +fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText( + this, + textLength.last +) +fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() + +fun FullTextSourcesList.toHtmlExplanations(): List = createHtmlText( + this, + explanationLimit.last +) +fun ExplainedInput.toHtmlExplanations(): List = fullEntitiesList().toHtmlTexts() + + 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 e30280f453..cc6003d826 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 @@ -6,6 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createFormattedText( entities: FullTextSourcesList, partLength: Int = textLength.last, @@ -47,75 +48,96 @@ fun createFormattedText( } +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createMarkdownText( entities: FullTextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownParseMode) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownCaptions(): List = createMarkdownText( this, captionLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun CaptionedInput.toMarkdownCaptions(): List = fullEntitiesList().toMarkdownCaptions() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownTexts(): List = createMarkdownText( this, textLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun TextContent.toMarkdownTexts(): List = fullEntitiesList().toMarkdownTexts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownExplanations(): List = createMarkdownText( this, explanationLimit.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun ExplainedInput.toMarkdownExplanations(): List = fullEntitiesList().toMarkdownTexts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createMarkdownV2Text( entities: FullTextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( this, captionLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun CaptionedInput.toMarkdownV2Captions(): List = fullEntitiesList().toMarkdownV2Captions() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( this, textLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun TextContent.toMarkdownV2Texts(): List = fullEntitiesList().toMarkdownV2Texts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toMarkdownV2Explanations(): List = createMarkdownV2Text( this, explanationLimit.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun ExplainedInput.toMarkdownV2Explanations(): List = fullEntitiesList().toMarkdownV2Texts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun createHtmlText( entities: FullTextSourcesList, partLength: Int = textLength.last ): List = createFormattedText(entities, partLength, HTMLParseMode) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toHtmlCaptions(): List = createHtmlText( this, captionLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun CaptionedInput.toHtmlCaptions(): List = fullEntitiesList().toHtmlCaptions() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toHtmlTexts(): List = createHtmlText( this, textLength.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun TextContent.toHtmlTexts(): List = fullEntitiesList().toHtmlTexts() +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun FullTextSourcesList.toHtmlExplanations(): List = createHtmlText( this, explanationLimit.last ) +@Deprecated("Replaced into TelegramBotAPI-extensions-utils") fun ExplainedInput.toHtmlExplanations(): List = fullEntitiesList().toHtmlTexts() From 16f55d70af17329e54dfb0036718821bd93a20f4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 13:15:22 +0600 Subject: [PATCH 13/21] resend and other media group utils replaced into TelegramBotAPI_extensions-utils --- CHANGELOG.md | 18 ++++-- .../utils/shortcuts/MediaGroupsShortcuts.kt | 56 +++++++++++++++++++ .../TelegramBotAPI/utils/MediaGroupList.kt | 4 ++ 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 3721a395d7..7cd06aa56e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,14 +58,24 @@ * Fix: for sending requests caption and text lengths limits were updated * New variant of `row` was added * `makeLinkToMessage` extensions has been deprecated (replaced into `TelegramBotAPI-extensions-utils`) - * All `String` formatting public extensions and functions was deprecated and replaced into `TelegramBotAPI-extensions-utils` - * All extensions like `CaptionedInput#toHtmlCaptions` was deprecated and replaced into `TelegramBotAPI-extensions-utils` + * Next things was deprecated and replaced into `TelegramBotAPI-extensions-utils`: + * All `String` formatting public extensions and functions + * All extensions like `CaptionedInput#toHtmlCaptions` + * All helper extensions for `List` * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added * `makeLinkToAddStickerSet` function and its variations were added - * All `String` formatting extensions and functions from `TelegramBotAPI` was added - * All extensions like `CaptionedInput#toHtmlCaptions` from `TelegramBotAPI` was added + * Next tools was added from `TelegramBotAPI`: + * All `String` formatting extensions and functions + * All extensions like `CaptionedInput#toHtmlCaptions` + * All helper extensions for `List` + * Several new extensions for `SentMediaGroupUpdate` were added: + * `SentMediaGroupUpdate#forwardInfo` + * `SentMediaGroupUpdate#replyTo` + * `SentMediaGroupUpdate#chat` + * `SentMediaGroupUpdate#mediaGroupId` + * Several `List.createResend` extensions were added ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt new file mode 100644 index 0000000000..cf2b0e0db0 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt @@ -0,0 +1,56 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts + +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.SendMediaGroup +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.message.ForwardInfo +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.SentMediaGroupUpdate + +val List.forwardInfo: ForwardInfo? + get() = firstOrNull() ?.forwardInfo +val List.replyTo: Message? + get() = firstOrNull() ?.replyTo +val List.chat: Chat? + get() = firstOrNull() ?.chat +val List.mediaGroupId: MediaGroupIdentifier? + get() = firstOrNull() ?.mediaGroupId + +val SentMediaGroupUpdate.forwardInfo: ForwardInfo? + get() = data.forwardInfo!! +val SentMediaGroupUpdate.replyTo: Message? + get() = data.replyTo +val SentMediaGroupUpdate.chat: Chat + get() = data.chat!! +val SentMediaGroupUpdate.mediaGroupId: MediaGroupIdentifier + get() = data.mediaGroupId!! + +fun List.createResend( + chatId: ChatId, + disableNotification: Boolean = false, + replyTo: MessageIdentifier? = null +) = SendMediaGroup( + chatId, + map { it.content.toMediaGroupMemberInputMedia() }, + disableNotification, + replyTo +) + +fun List.createResend( + chat: Chat, + disableNotification: Boolean = false, + replyTo: MessageIdentifier? = null +) = createResend( + chat.id, + disableNotification, + replyTo +) + +fun SentMediaGroupUpdate.createResend( + disableNotification: Boolean = false, + replyTo: MessageIdentifier? = null +) = data.createResend( + chat, + disableNotification, + replyTo +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MediaGroupList.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MediaGroupList.kt index bdfd2313ce..fef1514452 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MediaGroupList.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MediaGroupList.kt @@ -6,18 +6,22 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.ForwardInfo import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +@Deprecated("Replaced and updated inside of TelegramBotAPI-extensions-utils") val List.forwarded: ForwardInfo? get() = first().let { (it as? PossiblyForwardedMessage) ?.forwardInfo } +@Deprecated("Replaced and updated inside of TelegramBotAPI-extensions-utils") val List.replyTo: Message? get() = first().let { (it as? PossiblyReplyMessage) ?.replyTo } +@Deprecated("Replaced and updated inside of TelegramBotAPI-extensions-utils") val List.chat: Chat? get() = first().data.chat +@Deprecated("Replaced and updated inside of TelegramBotAPI-extensions-utils") val List.mediaGroupId: MediaGroupIdentifier? get() = (first().data as? MediaGroupMessage) ?.mediaGroupId From 6073d914d5d7900604437f16d691c9c836d976b0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 13:22:36 +0600 Subject: [PATCH 14/21] hotfixes in new media groups shortcuts --- .../extensions/utils/shortcuts/MediaGroupsShortcuts.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt index cf2b0e0db0..ea2fc6bfb8 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/MediaGroupsShortcuts.kt @@ -17,9 +17,9 @@ val List.mediaGroupId: MediaGroupIdentifier? get() = firstOrNull() ?.mediaGroupId val SentMediaGroupUpdate.forwardInfo: ForwardInfo? - get() = data.forwardInfo!! + get() = data.first().forwardInfo val SentMediaGroupUpdate.replyTo: Message? - get() = data.replyTo + get() = data.first().replyTo val SentMediaGroupUpdate.chat: Chat get() = data.chat!! val SentMediaGroupUpdate.mediaGroupId: MediaGroupIdentifier From 1fb2ecf15f25b65bbec0a30e81696aebfc35b73e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 13:39:09 +0600 Subject: [PATCH 15/21] executes was replaced --- CHANGELOG.md | 2 + .../utils/shortcuts/RequestsExecutor.kt | 45 +++++++++++++++++++ .../TelegramBotAPI/bot/RequestsExecutor.kt | 4 +- .../utils/extensions/Executes.kt | 4 +- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/RequestsExecutor.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cd06aa56e..bcff6bdc64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ * All `String` formatting public extensions and functions * All extensions like `CaptionedInput#toHtmlCaptions` * All helper extensions for `List` + * All `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe` * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added @@ -76,6 +77,7 @@ * `SentMediaGroupUpdate#chat` * `SentMediaGroupUpdate#mediaGroupId` * Several `List.createResend` extensions were added + * `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe` ### 0.27.4 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/RequestsExecutor.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/RequestsExecutor.kt new file mode 100644 index 0000000000..5157918216 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/RequestsExecutor.kt @@ -0,0 +1,45 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request +import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely +import kotlinx.coroutines.* + +fun RequestsExecutor.executeAsync( + request: Request, + scope: CoroutineScope +): Deferred = scope.async { + handleSafely { + execute(request) + } +} + +suspend fun RequestsExecutor.executeAsync( + request: Request +): Deferred = coroutineScope { + executeAsync(request, this) +} + +suspend fun RequestsExecutor.executeUnsafe( + request: Request, + retries: Int = 0, + retriesDelay: Long = 1000L, + onAllFailed: (suspend (exceptions: Array) -> Unit)? = null +): T? { + var leftRetries = retries + val exceptions = onAllFailed ?.let { mutableListOf() } + do { + return handleSafely( + { + leftRetries-- + delay(retriesDelay) + exceptions ?.add(it) + null + } + ) { + execute(request) + } ?: continue + } while(leftRetries >= 0) + onAllFailed ?.invoke(exceptions ?.toTypedArray() ?: emptyArray()) + return null +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt index 13c531a527..90fafa2e4c 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt @@ -13,8 +13,8 @@ import io.ktor.utils.io.core.Closeable interface RequestsExecutor : Closeable { /** * Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use - * something like [com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeAsync] or - * [com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe] + * something like [com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.executeAsync] or + * [com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.executeUnsafe] * * @throws Exception */ diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt index 6e4a4f50ef..84904405af 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt @@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.Response import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import kotlinx.coroutines.* - +@Deprecated("Will be removed in next major update") fun RequestsExecutor.executeAsync( request: Request, onFail: (suspend (Response) -> Unit)? = null, @@ -24,6 +24,7 @@ fun RequestsExecutor.executeAsync( } } +@Deprecated("Replaced and modified inside of TelegramBotAPI-extensions-utils") fun RequestsExecutor.executeAsync( request: Request, scope: CoroutineScope = GlobalScope @@ -31,6 +32,7 @@ fun RequestsExecutor.executeAsync( return scope.async { execute(request) } } +@Deprecated("Replaced and modified inside of TelegramBotAPI-extensions-utils") suspend fun RequestsExecutor.executeUnsafe( request: Request, retries: Int = 0, From 8ae2f57d550716ba91912318abc070c55c34a9bd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 13:47:28 +0600 Subject: [PATCH 16/21] update description of PreviewFeature annotation --- .../github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt index 8091ba9794..697092634d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/Annotations.kt @@ -1,7 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils @RequiresOptIn( - "It is possible, that behaviour of this thing will be changed later", + "It is possible, that behaviour of this thing will be changed later or this feature will be removed", RequiresOptIn.Level.WARNING ) @Target( From b477e8d585a480fec4ba2bb95775b68be0c759f4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 13:54:30 +0600 Subject: [PATCH 17/21] extend getChat extensions --- CHANGELOG.md | 2 + .../extensions/api/chat/get/GetChat.kt | 122 +++++++++++++++++- 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcff6bdc64..8e4212b450 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ * All extensions like `CaptionedInput#toHtmlCaptions` * All helper extensions for `List` * All `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe` +* `TelegramBotAPI-extensions-api`: + * A lot of `RequesstExecutor#getChat` extensions was added for more explicit types showing * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/chat/get/GetChat.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/chat/get/GetChat.kt index e7912097da..528bedb4b3 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/chat/get/GetChat.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/chat/get/GetChat.kt @@ -2,8 +2,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.chat.get import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.chat.get.GetChat -import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier -import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.* +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.* +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature suspend fun RequestsExecutor.getChat( chatId: ChatIdentifier @@ -12,3 +16,117 @@ suspend fun RequestsExecutor.getChat( suspend fun RequestsExecutor.getChat( chat: Chat ) = getChat(chat.id) + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedPublicChat] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: PublicChat +) = getChat(chat.id) as ExtendedPublicChat + + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedChannelChat] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: ChannelChat +) = getChat(chat.id) as ExtendedChannelChat + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedChannelChatImpl] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: ChannelChatImpl +) = getChat(chat.id) as ExtendedChannelChatImpl + + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedGroupChat] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: GroupChat +) = getChat(chat.id) as ExtendedGroupChat + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedGroupChatImpl] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: GroupChatImpl +) = getChat(chat.id) as ExtendedGroupChatImpl + + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedSupergroupChat] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: SupergroupChat +) = getChat(chat.id) as ExtendedSupergroupChat + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedSupergroupChatImpl] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: SupergroupChatImpl +) = getChat(chat.id) as ExtendedSupergroupChatImpl + + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedPrivateChat] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: PrivateChat +) = getChat(chat.id) as ExtendedPrivateChat + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedPrivateChatImpl] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: PrivateChatImpl +) = getChat(chat.id) as ExtendedPrivateChatImpl + +/** + * Will cast incoming [com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat] to a + * [ExtendedUser] with unsafe operator "as" + * + * @throws ClassCastException + */ +@PreviewFeature +suspend fun RequestsExecutor.getChat( + chat: CommonUser +) = getChat(chat.id) as ExtendedUser From ca9051920d122ff197e8e197b84ce94325ed0170 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 19:54:13 +0600 Subject: [PATCH 18/21] new setMyCommands extension added --- CHANGELOG.md | 1 + .../TelegramBotAPI/extensions/api/bot/SetMyCommands.kt | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e4212b450..242a45f9c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ * All `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe` * `TelegramBotAPI-extensions-api`: * A lot of `RequesstExecutor#getChat` extensions was added for more explicit types showing + * New `RequesstExecutor#setMyCommands` extension was added * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt index 5347510a5f..860f0d8a3b 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt @@ -7,3 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.BotCommand suspend fun RequestsExecutor.setMyCommands( commands: List ) = execute(SetMyCommands(commands)) + +suspend fun RequestsExecutor.setMyCommands( + vararg commands: BotCommand +) = setMyCommands(commands.toList()) From 7ede53fdbbcb021d5f5b2f8dd3f56457ae99537d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 20:16:55 +0600 Subject: [PATCH 19/21] BotCommandNameRegex and strict check of incoming command --- CHANGELOG.md | 2 ++ .../TelegramBotAPI/types/BotCommand.kt | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 242a45f9c3..8aadcf0fef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ * All extensions like `CaptionedInput#toHtmlCaptions` * All helper extensions for `List` * All `RequestsExecutor#executeAsync` and `RequestsExecutor#executeUnsafe` + * `BotCommand` now more strictly check commands which passed to it + * Regex `BotCommandNameRegex` was added * `TelegramBotAPI-extensions-api`: * A lot of `RequesstExecutor#getChat` extensions was added for more explicit types showing * New `RequesstExecutor#setMyCommands` extension was added diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt index 0f52133bfb..f1b26eb5c8 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt @@ -1,8 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types +import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +val BotCommandNameRegex = Regex("[a-z_0-9]{1,32}") + @Serializable data class BotCommand( @SerialName(botCommandField) @@ -12,10 +15,13 @@ data class BotCommand( ) { init { if (command.length !in botCommandLengthLimit) { - error("Command size must be in range $botCommandLengthLimit, but actually have length ${command.length}") + throwRangeError("Command name size", botCommandLengthLimit, command.length) + } + if (!command.matches(BotCommandNameRegex)) { + error("Bot command must contains only lowercase English letters, digits and underscores, but incoming command was $command") } if (description.length !in botCommandDescriptionLimit) { - error("Command description size must be in range $botCommandDescriptionLimit, but actually have length ${description.length}") + throwRangeError("Command description size", botCommandDescriptionLimit, description.length) } } } From 22e8b06fdac2cfd0658875dce776030db6a8bae1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 21:18:49 +0600 Subject: [PATCH 20/21] BotBuilder fixes --- CHANGELOG.md | 2 ++ .../extensions/api/BotBuilder.kt | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8aadcf0fef..7c7264821b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,8 @@ * `TelegramBotAPI-extensions-api`: * A lot of `RequesstExecutor#getChat` extensions was added for more explicit types showing * New `RequesstExecutor#setMyCommands` extension was added + * New field `BotBuilder#ktorClientEngineFactory` introduced + * Field `BotBuilder#ktorClientEngine` now is deprecated * `TelegramBotAPI-extensions-utils`: * `safely` function was introduced. It is in `PreviewFeature` state currently * `makeLinkToMessage` extensions has been added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt index fc8a7f8856..c6af136a44 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt @@ -4,8 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.HttpClientConfig -import io.ktor.client.engine.HttpClientEngine -import io.ktor.client.engine.ProxyConfig +import io.ktor.client.engine.* /** * @param proxy Standard ktor [ProxyConfig] @@ -14,20 +13,30 @@ import io.ktor.client.engine.ProxyConfig */ data class BotBuilder internal constructor( var proxy: ProxyConfig? = null, + @Deprecated("ktorClientEngineFactory parameter will be used preferable. In future this parameter will be removed") var ktorClientEngine: HttpClientEngine? = null, + var ktorClientEngineFactory: HttpClientEngineFactory? = null, var ktorClientConfig: (HttpClientConfig<*>.() -> Unit) ? = null ) { - internal fun createHttpClient(): HttpClient = ktorClientEngine ?.let { engine -> + internal fun createHttpClient(): HttpClient = ktorClientEngineFactory ?.let { + HttpClient( + it.create { + this@create.proxy = this@BotBuilder.proxy ?: this@create.proxy + } + ) { + ktorClientConfig ?.let { it() } + } + } ?: ktorClientEngine ?.let { engine -> HttpClient(engine) { ktorClientConfig ?.let { it() } engine { - proxy = this@BotBuilder.proxy ?: proxy + this@engine.proxy = this@BotBuilder.proxy ?: this@engine.proxy } } } ?: HttpClient { ktorClientConfig ?.let { it() } engine { - proxy = this@BotBuilder.proxy ?: proxy + this@engine.proxy = this@BotBuilder.proxy ?: this@engine.proxy } } } From 69973c597bfe52e2c9863a7560cd9f74e6e6b0bc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 22:10:33 +0600 Subject: [PATCH 21/21] update BotCommandNameRegex --- .../github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt index f1b26eb5c8..3ac9d3470d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/BotCommand.kt @@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -val BotCommandNameRegex = Regex("[a-z_0-9]{1,32}") +val BotCommandNameRegex = Regex("^[a-z_0-9]{${botCommandLengthLimit.first},${botCommandLengthLimit.last}}$") @Serializable data class BotCommand(