From 3a53f41f667715ce006559ad07036edbe11e0965 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 8 Feb 2021 11:35:51 +0600 Subject: [PATCH 1/7] start 0.32.5 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 825bfd2bf7..571029c831 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.32.5 + ## 0.32.4 * `Common`: diff --git a/gradle.properties b/gradle.properties index 20ac5b4bd5..766ffc876a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,6 +17,6 @@ micro_utils_version=0.4.24 javax_activation_version=1.1.1 library_group=dev.inmo -library_version=0.32.4 +library_version=0.32.5 github_release_plugin_version=2.2.12 From 4449893608a4ce5cd897323d892a42df39caca53 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 8 Feb 2021 18:15:54 +0600 Subject: [PATCH 2/7] fix of #294 --- CHANGELOG.md | 3 +++ .../textsources/TextMentionTextSource.kt | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 571029c831..8ae55ef7b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.32.5 +* `Core`: + * Add `mention` variants for user ids and receiver variants ([#294](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/294)) + ## 0.32.4 * `Common`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt index 4166e4b489..4587b20597 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/MessageEntity/textsources/TextMentionTextSource.kt @@ -1,7 +1,7 @@ package dev.inmo.tgbotapi.types.MessageEntity.textsources import dev.inmo.tgbotapi.CommonAbstracts.* -import dev.inmo.tgbotapi.types.User +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.internal.* @@ -21,6 +21,26 @@ data class TextMentionTextSource @RiskFeature(DirectInvocationOfTextSourceConstr @Suppress("NOTHING_TO_INLINE") inline fun mention(parts: List, user: User) = TextMentionTextSource(parts.makeString(), user, parts) @Suppress("NOTHING_TO_INLINE") +inline fun User.mention(parts: List) = mention(parts, this) +@Suppress("NOTHING_TO_INLINE") +inline fun mention(parts: List, userId: UserId) = mention(parts, CommonUser(userId, "")) +@Suppress("NOTHING_TO_INLINE") +inline fun UserId.mention(parts: List) = mention(parts, this) +@Suppress("NOTHING_TO_INLINE") +inline fun mention(parts: List, id: Identifier) = mention(parts, UserId(id)) +@Suppress("NOTHING_TO_INLINE") +inline fun Identifier.mention(parts: List) = mention(parts, this) +@Suppress("NOTHING_TO_INLINE") inline fun mention(user: User, vararg parts: TextSource) = mention(parts.toList(), user) @Suppress("NOTHING_TO_INLINE") inline fun mention(text: String, user: User) = mention(user, regular(text)) +@Suppress("NOTHING_TO_INLINE") +inline fun User.mention(text: String) = mention(this, regular(text)) +@Suppress("NOTHING_TO_INLINE") +inline fun mention(text: String, userId: UserId) = mention(text, CommonUser(userId, "")) +@Suppress("NOTHING_TO_INLINE") +inline fun UserId.mention(text: String) = mention(text, this) +@Suppress("NOTHING_TO_INLINE") +inline fun mention(text: String, id: Identifier) = mention(text, UserId(id)) +@Suppress("NOTHING_TO_INLINE") +inline fun Identifier.mention(text: String) = mention(text, this) From 10f4817283aff3f94e59197a392c2c711d7ddb05 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 8 Feb 2021 19:35:32 +0600 Subject: [PATCH 3/7] solution of #293 --- CHANGELOG.md | 3 + .../Ktor/base/AbstractRequestCallFactory.kt | 7 ++ .../utils/updates/retrieving/LongPolling.kt | 68 ++++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae55ef7b3..d3e828c00e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ * `Core`: * Add `mention` variants for user ids and receiver variants ([#294](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/294)) + * Now `AbstractRequestCallFactory` will set up one-second delay for zero timeouts in `GetUpdate` requests + * Several extensions for `TelegramBotAPI` like `retrieveAccumulatedUpdates` have been added as a solution for + [#293](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/293) ## 0.32.4 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/AbstractRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/AbstractRequestCallFactory.kt index bedbc96283..b6d31e8875 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/AbstractRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/AbstractRequestCallFactory.kt @@ -16,6 +16,8 @@ import io.ktor.http.ContentType import kotlinx.serialization.json.Json import kotlin.collections.set +var defaultUpdateTimeoutForZeroDelay = 1000L + abstract class AbstractRequestCallFactory : KtorCallFactory { private val methodsCache: MutableMap = mutableMapOf() override suspend fun makeCall( @@ -41,6 +43,11 @@ abstract class AbstractRequestCallFactory : KtorCallFactory { requestTimeoutMillis = customTimeoutMillis socketTimeoutMillis = customTimeoutMillis } + } else { + timeout { + requestTimeoutMillis = defaultUpdateTimeoutForZeroDelay + socketTimeoutMillis = defaultUpdateTimeoutForZeroDelay + } } } } diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index 7519638811..c5ac93a0d4 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.extensions.utils.updates.retrieving -import dev.inmo.micro_utils.coroutines.ExceptionHandler -import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.coroutines.* import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.exceptions.RequestException @@ -14,7 +13,10 @@ import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.* import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.* import dev.inmo.tgbotapi.utils.* +import io.ktor.client.features.HttpRequestTimeoutException import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import kotlin.coroutines.coroutineContext fun TelegramBot.startGettingOfUpdatesByLongPolling( timeoutSeconds: Seconds = 30, @@ -66,6 +68,68 @@ fun TelegramBot.startGettingOfUpdatesByLongPolling( } } +fun TelegramBot.retrieveAccumulatedUpdates( + avoidInlineQueries: Boolean = false, + avoidCallbackQueries: Boolean = false, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: (ExceptionHandler)? = null, + allowedUpdates: List? = null, + updatesReceiver: UpdateReceiver +): Job = scope.launch { + safelyWithoutExceptions { + startGettingOfUpdatesByLongPolling( + 0, + CoroutineScope(coroutineContext + SupervisorJob()), + { + if (it is HttpRequestTimeoutException) { + throw CancellationException("Cancel due to absence of new updates") + } else { + exceptionsHandler ?.invoke(it) + } + }, + allowedUpdates + ) { + when { + it is InlineQueryUpdate && avoidInlineQueries || + it is CallbackQueryUpdate && avoidCallbackQueries -> return@startGettingOfUpdatesByLongPolling + else -> updatesReceiver(it) + } + }.join() + } +} + +/** + * @return [kotlinx.coroutines.flow.Flow] which will emit updates to the collector while they will be accumulated. Works + * the same as [retrieveAccumulatedUpdates], but pass [kotlinx.coroutines.flow.FlowCollector.emit] as a callback + */ +fun TelegramBot.createAccumulatedUpdatesRetrieverFlow( + avoidInlineQueries: Boolean = false, + avoidCallbackQueries: Boolean = false, + exceptionsHandler: ExceptionHandler? = null, + allowedUpdates: List? = null +): Flow = flow { + val parentContext = kotlin.coroutines.coroutineContext + retrieveAccumulatedUpdates( + avoidInlineQueries, + avoidCallbackQueries, + CoroutineScope(parentContext), + exceptionsHandler, + allowedUpdates + ) { + withContext(parentContext) { emit(it) } + }.join() +} + +fun TelegramBot.retrieveAccumulatedUpdates( + flowsUpdatesFilter: FlowsUpdatesFilter, + avoidInlineQueries: Boolean = false, + avoidCallbackQueries: Boolean = false, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: ExceptionHandler? = null +) = flowsUpdatesFilter.run { + retrieveAccumulatedUpdates(avoidInlineQueries, avoidCallbackQueries, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) +} + /** * Will [startGettingOfUpdatesByLongPolling] using incoming [flowsUpdatesFilter]. It is assumed that you ALREADY CONFIGURE * all updates receivers, because this method will trigger getting of updates and. From 098b5cc3939854f094d34f58fc95563aed4d28b2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Feb 2021 16:33:48 +0600 Subject: [PATCH 4/7] rewrite createAccumulatedUpdatesRetrieverFlow onto channelFlow --- .../utils/updates/retrieving/LongPolling.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index c5ac93a0d4..5e6057d576 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -14,6 +14,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.* import dev.inmo.tgbotapi.utils.* import io.ktor.client.features.HttpRequestTimeoutException +import io.ktor.utils.io.core.use import kotlinx.coroutines.* import kotlinx.coroutines.flow.* import kotlin.coroutines.coroutineContext @@ -107,17 +108,19 @@ fun TelegramBot.createAccumulatedUpdatesRetrieverFlow( avoidCallbackQueries: Boolean = false, exceptionsHandler: ExceptionHandler? = null, allowedUpdates: List? = null -): Flow = flow { +): Flow = channelFlow { val parentContext = kotlin.coroutines.coroutineContext - retrieveAccumulatedUpdates( - avoidInlineQueries, - avoidCallbackQueries, - CoroutineScope(parentContext), - exceptionsHandler, - allowedUpdates - ) { - withContext(parentContext) { emit(it) } - }.join() + channel.apply { + retrieveAccumulatedUpdates( + avoidInlineQueries, + avoidCallbackQueries, + CoroutineScope(parentContext), + exceptionsHandler, + allowedUpdates, + ::send + ).join() + close() + } } fun TelegramBot.retrieveAccumulatedUpdates( From 2a89c41a58d614cad3814b7353180bcd525465f3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Feb 2021 17:15:28 +0600 Subject: [PATCH 5/7] fix of #292 --- CHANGELOG.md | 1 + .../dev/inmo/tgbotapi/types/ChatIdentifier.kt | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3e828c00e..01bcbea7dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Now `AbstractRequestCallFactory` will set up one-second delay for zero timeouts in `GetUpdate` requests * Several extensions for `TelegramBotAPI` like `retrieveAccumulatedUpdates` have been added as a solution for [#293](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/293) + * Links for `tg://user?id=` have been updated ([#292](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/292)) ## 0.32.4 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatIdentifier.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatIdentifier.kt index 813ac70c44..bdd251b7ed 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatIdentifier.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatIdentifier.kt @@ -1,5 +1,7 @@ package dev.inmo.tgbotapi.types +import dev.inmo.micro_utils.common.Warning +import dev.inmo.tgbotapi.types.chat.abstracts.Chat import kotlinx.serialization.* import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder @@ -17,9 +19,20 @@ data class ChatId( val chatId: Identifier ) : ChatIdentifier() - -val ChatId.link: String - get() = "tg://user?id=$chatId" +/** + * https://core.telegram.org/bots/api#formatting-options + */ +@Warning("This API have restrictions in Telegram System") +val Identifier.link: String + get() = "tg://user?id=$this" +/** + * https://core.telegram.org/bots/api#formatting-options + */ +@Warning("This API have restrictions in Telegram System") +val UserId.link: String + get() = chatId.link +val User.link: String + get() = id.link typealias UserId = ChatId From 8d27349868ea48399358d78a73c9c5654b2ab538 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Feb 2021 18:26:10 +0600 Subject: [PATCH 6/7] update content resends and different other things --- .../types/InputMedia/InputMediaAudio.kt | 12 +++++++ .../types/InputMedia/InputMediaDocument.kt | 8 +++++ .../types/InputMedia/InputMediaPhoto.kt | 7 ++++ .../tgbotapi/types/ParseMode/ParseMode.kt | 5 +-- .../inmo/tgbotapi/types/files/VideoFile.kt | 13 ++++++++ .../types/message/content/TextContent.kt | 33 +++++++------------ .../message/content/media/AnimationContent.kt | 13 ++------ .../message/content/media/AudioContent.kt | 11 ++----- .../message/content/media/DocumentContent.kt | 13 ++------ .../message/content/media/PhotoContent.kt | 11 ++----- .../message/content/media/VideoContent.kt | 11 ++----- .../message/content/media/VoiceContent.kt | 13 ++------ 12 files changed, 72 insertions(+), 78 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt index 01d416f485..5dc1aba16f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaAudio.kt @@ -74,3 +74,15 @@ fun AudioFile.toInputMediaAudio( title, thumb ?.fileId ) + +fun AudioFile.toInputMediaAudio( + textSources: TextSourcesList = emptyList(), + title: String? = this.title +): InputMediaAudio = InputMediaAudio( + fileId, + textSources, + duration, + performer, + title, + thumb ?.fileId +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt index a448046867..94e5af6b31 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaDocument.kt @@ -70,3 +70,11 @@ fun DocumentFile.toInputMediaDocument( parseMode, thumb ?.fileId ) + +fun DocumentFile.toInputMediaDocument( + textSources: TextSourcesList = emptyList() +) = InputMediaDocument( + fileId, + textSources, + thumb ?.fileId +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaPhoto.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaPhoto.kt index 345d60b117..05ba4fb671 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaPhoto.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/InputMedia/InputMediaPhoto.kt @@ -53,3 +53,10 @@ fun PhotoSize.toInputMediaPhoto( caption, parseMode ) + +fun PhotoSize.toInputMediaPhoto( + textSources: TextSourcesList = emptyList() +): InputMediaPhoto = InputMediaPhoto( + fileId, + textSources +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt index 8cd2f094d1..af48b4dbee 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt @@ -39,8 +39,9 @@ typealias HTML = HTMLParseMode internal object ParseModeSerializerObject : KSerializer { override fun deserialize(decoder: Decoder): ParseMode { return when (decoder.decodeString()) { - MarkdownParseMode.parseModeName -> MarkdownParseMode - HTMLParseMode.parseModeName -> HTMLParseMode + Markdown.parseModeName -> Markdown + MarkdownV2.parseModeName -> MarkdownV2 + HTML.parseModeName -> HTML else -> throw IllegalArgumentException("Unknown parse mode") } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/VideoFile.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/VideoFile.kt index eebd6ea018..45b4525896 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/VideoFile.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/VideoFile.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.types.files +import dev.inmo.tgbotapi.CommonAbstracts.TextSourcesList import dev.inmo.tgbotapi.requests.abstracts.FileId import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.InputMedia.InputMediaVideo @@ -44,3 +45,15 @@ inline fun VideoFile.toInputMediaVideo( duration, thumb ?.fileId ) + +@Suppress("NOTHING_TO_INLINE") +inline fun VideoFile.toInputMediaVideo( + textSources: TextSourcesList +) = InputMediaVideo( + fileId, + textSources, + width, + height, + duration, + thumb ?.fileId +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt index 83a5488e46..ff94ffb84e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt @@ -1,16 +1,15 @@ package dev.inmo.tgbotapi.types.message.content -import dev.inmo.tgbotapi.CommonAbstracts.TextPart -import dev.inmo.tgbotapi.CommonAbstracts.TextedInput +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.SendTextMessage import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.* +import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode +import dev.inmo.tgbotapi.types.ParseMode.ParseMode import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent -import dev.inmo.tgbotapi.utils.internal.* data class TextContent( override val text: String, @@ -24,8 +23,7 @@ data class TextContent( replyMarkup: KeyboardMarkup? ): Request> = SendTextMessage( chatId, - toHtmlTexts().first(), - HTMLParseMode, + textSources, false, disableNotification, replyToMessageId, @@ -48,6 +46,10 @@ data class TextContent( HTMLParseMode ) + @Deprecated( + "Useless due to fact that createResend currently use textSource and that will guarantee correct sending of message", + ReplaceWith("createResend") + ) fun createResends( chatId: ChatIdentifier, disableNotification: Boolean, @@ -55,20 +57,7 @@ data class TextContent( allowSendingWithoutReply: Boolean?, replyMarkup: KeyboardMarkup?, parseMode: ParseMode = HTMLParseMode - ): List>> = when (parseMode) { - is MarkdownParseMode -> toMarkdownTexts() - is MarkdownV2ParseMode -> toMarkdownV2Texts() - is HTMLParseMode -> toHtmlTexts() - }.map { - SendTextMessage( - chatId, - it, - parseMode, - false, - disableNotification, - replyToMessageId, - allowSendingWithoutReply, - replyMarkup - ) - } + ): List>> = listOf( + createResend(chatId, disableNotification, replyToMessageId, allowSendingWithoutReply, replyMarkup) + ) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AnimationContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AnimationContent.kt index f0cf652083..edbcb2312b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AnimationContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AnimationContent.kt @@ -1,21 +1,16 @@ package dev.inmo.tgbotapi.types.message.content.media -import dev.inmo.tgbotapi.CommonAbstracts.CaptionedInput -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendAnimation import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.InputMedia.InputMediaAnimation import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode -import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2 import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.AnimationFile import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent -import dev.inmo.tgbotapi.utils.internal.toHtmlCaptions -import dev.inmo.tgbotapi.utils.internal.toMarkdownV2Captions data class AnimationContent( override val media: AnimationFile, @@ -33,8 +28,7 @@ data class AnimationContent( chatId, media.fileId, media.thumb ?.fileId, - toHtmlCaptions().firstOrNull(), - HTMLParseMode, + textSources, media.duration, media.width, media.height, @@ -46,8 +40,7 @@ data class AnimationContent( override fun asInputMedia(): InputMediaAnimation = InputMediaAnimation( media.fileId, - toMarkdownV2Captions().firstOrNull(), - MarkdownV2, + textSources, media.width, media.height, media.duration, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt index b326522b96..2d4f9a7148 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/AudioContent.kt @@ -1,18 +1,17 @@ package dev.inmo.tgbotapi.types.message.content.media import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.textSources import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendAudio import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.InputMedia.InputMediaAudio import dev.inmo.tgbotapi.types.InputMedia.toInputMediaAudio import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.AudioFile import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.AudioMediaGroupContent -import dev.inmo.tgbotapi.utils.internal.toHtmlCaptions data class AudioContent( override val media: AudioFile, @@ -29,8 +28,7 @@ data class AudioContent( chatId, media.fileId, media.thumb ?.fileId, - toHtmlCaptions().firstOrNull(), - HTMLParseMode, + textSources, media.duration, media.performer, media.title, @@ -42,8 +40,5 @@ data class AudioContent( override fun toMediaGroupMemberInputMedia(): InputMediaAudio = asInputMedia() - override fun asInputMedia(): InputMediaAudio = media.toInputMediaAudio( - toHtmlCaptions().firstOrNull(), - HTMLParseMode - ) + override fun asInputMedia(): InputMediaAudio = media.toInputMediaAudio(textSources) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt index 8ff70ac761..747cc77fdb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/DocumentContent.kt @@ -1,21 +1,18 @@ package dev.inmo.tgbotapi.types.message.content.media -import dev.inmo.tgbotapi.CommonAbstracts.CaptionedInput -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendDocument import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument import dev.inmo.tgbotapi.types.InputMedia.toInputMediaDocument import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.files.asDocumentFile import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.DocumentMediaGroupContent import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent -import dev.inmo.tgbotapi.utils.internal.toHtmlCaptions data class DocumentContent( override val media: DocumentFile, @@ -32,8 +29,7 @@ data class DocumentContent( chatId, media.fileId, media.thumb ?.fileId, - toHtmlCaptions().firstOrNull(), - HTMLParseMode, + textSources, disableNotification, replyToMessageId, allowSendingWithoutReply, @@ -42,10 +38,7 @@ data class DocumentContent( override fun toMediaGroupMemberInputMedia(): InputMediaDocument = asInputMedia() - override fun asInputMedia(): InputMediaDocument = media.toInputMediaDocument( - toHtmlCaptions().firstOrNull(), - HTMLParseMode - ) + override fun asInputMedia(): InputMediaDocument = media.toInputMediaDocument(textSources) } @Suppress("NOTHING_TO_INLINE") diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/PhotoContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/PhotoContent.kt index 9a7ceabb4c..27fc6102fa 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/PhotoContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/PhotoContent.kt @@ -1,19 +1,18 @@ package dev.inmo.tgbotapi.types.message.content.media import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.textSources import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendPhoto import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.InputMedia.InputMediaPhoto import dev.inmo.tgbotapi.types.InputMedia.toInputMediaPhoto import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.* import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MediaCollectionContent import dev.inmo.tgbotapi.types.message.content.abstracts.VisualMediaGroupContent -import dev.inmo.tgbotapi.utils.internal.toHtmlCaptions data class PhotoContent( override val mediaCollection: Photo, @@ -31,8 +30,7 @@ data class PhotoContent( ): Request> = SendPhoto( chatId, media.fileId, - toHtmlCaptions().firstOrNull(), - HTMLParseMode, + textSources, disableNotification, replyToMessageId, allowSendingWithoutReply, @@ -41,8 +39,5 @@ data class PhotoContent( override fun toMediaGroupMemberInputMedia(): InputMediaPhoto = asInputMedia() - override fun asInputMedia(): InputMediaPhoto = media.toInputMediaPhoto( - toHtmlCaptions().firstOrNull(), - HTMLParseMode - ) + override fun asInputMedia(): InputMediaPhoto = media.toInputMediaPhoto(textSources) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VideoContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VideoContent.kt index aa5b4185bb..5b1ec9197b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VideoContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VideoContent.kt @@ -1,18 +1,17 @@ package dev.inmo.tgbotapi.types.message.content.media import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.textSources import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendVideo import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.InputMedia.InputMediaVideo import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.VideoFile import dev.inmo.tgbotapi.types.files.toInputMediaVideo import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.VisualMediaGroupContent -import dev.inmo.tgbotapi.utils.internal.toHtmlCaptions data class VideoContent( override val media: VideoFile, @@ -29,8 +28,7 @@ data class VideoContent( chatId, media.fileId, media.thumb ?.fileId, - toHtmlCaptions().firstOrNull(), - HTMLParseMode, + textSources, media.duration, media.width, media.height, @@ -43,8 +41,5 @@ data class VideoContent( override fun toMediaGroupMemberInputMedia(): InputMediaVideo = asInputMedia() - override fun asInputMedia(): InputMediaVideo = media.toInputMediaVideo( - toHtmlCaptions().firstOrNull(), - HTMLParseMode - ) + override fun asInputMedia(): InputMediaVideo = media.toInputMediaVideo(textSources) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VoiceContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VoiceContent.kt index 6a9894444f..421f2b6a75 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VoiceContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/media/VoiceContent.kt @@ -1,20 +1,15 @@ package dev.inmo.tgbotapi.types.message.content.media -import dev.inmo.tgbotapi.CommonAbstracts.CaptionedInput -import dev.inmo.tgbotapi.CommonAbstracts.TextPart +import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.media.SendVoice import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.InputMedia.InputMediaAudio import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode -import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2 import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.VoiceFile import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent -import dev.inmo.tgbotapi.utils.internal.toHtmlCaptions -import dev.inmo.tgbotapi.utils.internal.toMarkdownV2Captions data class VoiceContent( override val media: VoiceFile, @@ -30,8 +25,7 @@ data class VoiceContent( ): Request> = SendVoice( chatId, media.fileId, - toHtmlCaptions().firstOrNull(), - HTMLParseMode, + textSources, media.duration, disableNotification, replyToMessageId, @@ -41,8 +35,7 @@ data class VoiceContent( override fun asInputMedia(): InputMediaAudio = InputMediaAudio( media.fileId, - toMarkdownV2Captions().firstOrNull(), - MarkdownV2, + textSources, media.duration ) } From 4df800eaa92ddf935ee38fd7c76ed2fc796735ce Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Feb 2021 19:07:31 +0600 Subject: [PATCH 7/7] fix of #291 --- CHANGELOG.md | 2 + tgbotapi.core/build.gradle | 1 + .../tgbotapi/types/ParseMode/ParseMode.kt | 8 ++ .../types/message/content/TextContent.kt | 28 +++--- .../internal/CaptionAndTextSourcesToText.kt | 97 ------------------- .../MessageEntity/StringFormattingTests.kt | 2 +- .../MessageEntity/TextPartsCreatingTests.kt | 4 +- tgbotapi.extensions.utils/build.gradle | 18 ++++ .../formatting/ResendingTextFormatting.kt | 14 +-- 9 files changed, 54 insertions(+), 120 deletions(-) delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 01bcbea7dc..8c4afa347e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ * Several extensions for `TelegramBotAPI` like `retrieveAccumulatedUpdates` have been added as a solution for [#293](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/293) * Links for `tg://user?id=` have been updated ([#292](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/292)) + * All usages of captions or texts in resends and same things have been replaced with `textSources` + * Global `defaultParseMode` has been added ([#291](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/291)) ## 0.32.4 diff --git a/tgbotapi.core/build.gradle b/tgbotapi.core/build.gradle index 9646921743..ff1a823d33 100644 --- a/tgbotapi.core/build.gradle +++ b/tgbotapi.core/build.gradle @@ -60,6 +60,7 @@ kotlin { dependencies { implementation kotlin('test-common') implementation kotlin('test-annotations-common') + implementation project(":tgbotapi.extensions.utils") } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt index af48b4dbee..13f9606176 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ParseMode/ParseMode.kt @@ -35,6 +35,14 @@ typealias Markdown = MarkdownParseMode typealias MarkdownV2 = MarkdownV2ParseMode typealias HTML = HTMLParseMode +/** + * This variable respects to default parse mode used in places like next: + * + * * [dev.inmo.tgbotapi.types.message.content.TextContent.createResends] + * * + */ +var defaultParseMode: ParseMode = HTML + @Serializer(ParseMode::class) internal object ParseModeSerializerObject : KSerializer { override fun deserialize(decoder: Decoder): ParseMode { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt index ff94ffb84e..2d098ebc64 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/TextContent.kt @@ -5,8 +5,7 @@ import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.send.SendTextMessage import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.MessageIdentifier -import dev.inmo.tgbotapi.types.ParseMode.HTMLParseMode -import dev.inmo.tgbotapi.types.ParseMode.ParseMode +import dev.inmo.tgbotapi.types.ParseMode.* import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent @@ -31,19 +30,24 @@ data class TextContent( replyMarkup ) + @Deprecated( + "Useless due to fact that createResend currently use textSource and that will guarantee correct sending of message", + ReplaceWith("createResend") + ) override fun createResends( chatId: ChatIdentifier, disableNotification: Boolean, replyToMessageId: MessageIdentifier?, allowSendingWithoutReply: Boolean?, replyMarkup: KeyboardMarkup? - ): List>> = createResends( - chatId, - disableNotification, - replyToMessageId, - allowSendingWithoutReply, - replyMarkup, - HTMLParseMode + ): List>> = listOf( + createResend( + chatId, + disableNotification, + replyToMessageId, + allowSendingWithoutReply, + replyMarkup + ) ) @Deprecated( @@ -56,8 +60,6 @@ data class TextContent( replyToMessageId: MessageIdentifier?, allowSendingWithoutReply: Boolean?, replyMarkup: KeyboardMarkup?, - parseMode: ParseMode = HTMLParseMode - ): List>> = listOf( - createResend(chatId, disableNotification, replyToMessageId, allowSendingWithoutReply, replyMarkup) - ) + parseMode: ParseMode = defaultParseMode + ): List>> = createResends(chatId, disableNotification, replyToMessageId, allowSendingWithoutReply, replyMarkup) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt deleted file mode 100644 index c5347a12ad..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/internal/CaptionAndTextSourcesToText.kt +++ /dev/null @@ -1,97 +0,0 @@ -package dev.inmo.tgbotapi.utils.internal - -import dev.inmo.tgbotapi.CommonAbstracts.* -import dev.inmo.tgbotapi.types.ParseMode.* -import dev.inmo.tgbotapi.types.captionLength -import dev.inmo.tgbotapi.types.message.content.TextContent -import dev.inmo.tgbotapi.types.textLength - -internal fun createFormattedText( - entities: TextSourcesList, - 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.markdown - is MarkdownV2ParseMode -> entity.markdownV2 - is HTMLParseMode -> entity.html - } - 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 -} - - -internal fun createMarkdownText( - entities: TextSourcesList, - partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownParseMode) - -internal fun TextSourcesList.toMarkdownTexts(): List = createMarkdownText( - this, - textLength.last -) -internal fun TextContent.toMarkdownTexts(): List = textSources.toMarkdownTexts() - - -internal fun createMarkdownV2Text( - entities: TextSourcesList, - partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) - -internal fun TextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( - this, - captionLength.last -) -internal fun CaptionedInput.toMarkdownV2Captions(): List = textSources.toMarkdownV2Captions() - -internal fun TextSourcesList.toMarkdownV2Texts(): List = createMarkdownV2Text( - this, - textLength.last -) -internal fun TextContent.toMarkdownV2Texts(): List = textSources.toMarkdownV2Texts() - - -internal fun createHtmlText( - entities: TextSourcesList, - partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, HTMLParseMode) - -internal fun TextSourcesList.toHtmlCaptions(): List = createHtmlText( - this, - captionLength.last -) -internal fun CaptionedInput.toHtmlCaptions(): List = textSources.toHtmlCaptions() - -internal fun TextSourcesList.toHtmlTexts(): List = createHtmlText( - this, - textLength.last -) -internal fun TextContent.toHtmlTexts(): List = textSources.toHtmlTexts() - - diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt index 459bcdf648..1ba73f4c7f 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/StringFormattingTests.kt @@ -2,8 +2,8 @@ package dev.inmo.tgbotapi.types.MessageEntity import dev.inmo.tgbotapi.CommonAbstracts.TextSource import dev.inmo.tgbotapi.CommonAbstracts.plus +import dev.inmo.tgbotapi.extensions.utils.formatting.* import dev.inmo.tgbotapi.types.MessageEntity.textsources.* -import dev.inmo.tgbotapi.utils.internal.* import kotlin.test.Test import kotlin.test.assertEquals diff --git a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt index ee3941f1ff..14e0a82f2e 100644 --- a/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt +++ b/tgbotapi.core/src/commonTest/kotlin/dev/inmo/tgbotapi/types/MessageEntity/TextPartsCreatingTests.kt @@ -1,8 +1,8 @@ package dev.inmo.tgbotapi.types.MessageEntity import dev.inmo.tgbotapi.CommonAbstracts.justTextSources -import dev.inmo.tgbotapi.utils.internal.toHtmlTexts -import dev.inmo.tgbotapi.utils.internal.toMarkdownV2Texts +import dev.inmo.tgbotapi.extensions.utils.formatting.toHtmlTexts +import dev.inmo.tgbotapi.extensions.utils.formatting.toMarkdownV2Texts import kotlin.test.Test import kotlin.test.assertEquals diff --git a/tgbotapi.extensions.utils/build.gradle b/tgbotapi.extensions.utils/build.gradle index 1d557205fe..dca79235e1 100644 --- a/tgbotapi.extensions.utils/build.gradle +++ b/tgbotapi.extensions.utils/build.gradle @@ -44,5 +44,23 @@ kotlin { api project(":tgbotapi.core") } } + + commonTest { + dependencies { + implementation kotlin('test-common') + implementation kotlin('test-annotations-common') + } + } + jvmTest { + dependencies { + implementation kotlin('test-junit') + } + } + jsTest { + dependencies { + implementation kotlin('test-junit') + implementation kotlin('test-js') + } + } } } diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt index 9d28d5215e..a8e0535d8d 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/ResendingTextFormatting.kt @@ -8,15 +8,15 @@ import dev.inmo.tgbotapi.types.message.content.TextContent fun createFormattedText( entities: TextSourcesList, partLength: Int = textLength.last, - mode: ParseMode = MarkdownParseMode + mode: ParseMode = defaultParseMode ): List { val texts = mutableListOf() val textBuilder = StringBuilder(partLength) for (entity in entities) { val string = when (mode) { - is MarkdownParseMode -> entity.markdown - is MarkdownV2ParseMode -> entity.markdownV2 - is HTMLParseMode -> entity.html + is Markdown -> entity.markdown + is MarkdownV2 -> entity.markdownV2 + is HTML -> entity.html } if (textBuilder.length + string.length > partLength) { if (textBuilder.isNotEmpty()) { @@ -49,7 +49,7 @@ fun createFormattedText( fun createMarkdownText( entities: TextSourcesList, partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownParseMode) +): List = createFormattedText(entities, partLength, Markdown) fun TextSourcesList.toMarkdownCaptions(): List = createMarkdownText( this, @@ -73,7 +73,7 @@ fun ExplainedInput.toMarkdownExplanations(): List = textSources.toMarkdo fun createMarkdownV2Text( entities: TextSourcesList, partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, MarkdownV2ParseMode) +): List = createFormattedText(entities, partLength, MarkdownV2) fun TextSourcesList.toMarkdownV2Captions(): List = createMarkdownV2Text( this, @@ -97,7 +97,7 @@ fun ExplainedInput.toMarkdownV2Explanations(): List = textSources.toMark fun createHtmlText( entities: TextSourcesList, partLength: Int = textLength.last -): List = createFormattedText(entities, partLength, HTMLParseMode) +): List = createFormattedText(entities, partLength, HTML) fun TextSourcesList.toHtmlCaptions(): List = createHtmlText( this,