diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d5a5d1710..2bf5ab0e0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # TelegramBotAPI changelog +## 0.33.1 + +* `Common`: + * `Version`: + * `Kotlin`: `1.4.31` -> `1.4.32` + * `MicroUtils`: `0.4.29` -> `0.4.30` + * `Klocks`: `2.0.6` -> `2.0.7` +* `Utils Extensions`: + * Add extensions `parseCommandsWithParams` + ## 0.33.0 **UPDATE UP TO Telegram Bot API 5.1** diff --git a/gradle.properties b/gradle.properties index d338fd8535..3163a8b867 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,18 +5,18 @@ kotlin.js.generate.externals=true kotlin.incremental=true kotlin.incremental.js=true -kotlin_version=1.4.31 +kotlin_version=1.4.32 kotlin_coroutines_version=1.4.3 kotlin_serialisation_runtime_version=1.1.0 -klock_version=2.0.6 +klock_version=2.0.7 uuid_version=0.2.3 ktor_version=1.5.2 -micro_utils_version=0.4.29 +micro_utils_version=0.4.30 javax_activation_version=1.1.1 library_group=dev.inmo -library_version=0.33.0 +library_version=0.33.1 github_release_plugin_version=2.2.12 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/Captioned.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/Captioned.kt index 7cee50ae0b..0b5ed05d2c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/Captioned.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/CommonAbstracts/Captioned.kt @@ -6,6 +6,7 @@ interface Captioned { val caption: String? } +@Deprecated("This interface is not used in library and will be removed soon") interface CaptionedOutput : Captioned { val parseMode: ParseMode? } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt index 687392c696..a9c6ec5f56 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt @@ -12,7 +12,7 @@ fun newRequestException( cause: Throwable? = null ) = response.description ?.let { description -> when { - description == "Bad Request: reply message not found" -> ReplyMessageNotFoundException(response, plainAnswer, message, cause) + description == "Bad Request: reply message not found" || description == "Bad Request: replied message not found" -> ReplyMessageNotFoundException(response, plainAnswer, message, cause) description == "Bad Request: message to edit not found" -> MessageToEditNotFoundException(response, plainAnswer, message, cause) description.contains("Bad Request: message is not modified") -> MessageIsNotModifiedException(response, plainAnswer, message, cause) description == "Unauthorized" -> UnauthorizedException(response, plainAnswer, message, cause) diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/TextCaptionBotCommandsParser.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/TextCaptionBotCommandsParser.kt new file mode 100644 index 0000000000..86a63efb23 --- /dev/null +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/TextCaptionBotCommandsParser.kt @@ -0,0 +1,62 @@ +package dev.inmo.tgbotapi.extensions.utils.extensions + +import dev.inmo.tgbotapi.CommonAbstracts.* +import dev.inmo.tgbotapi.types.MessageEntity.textsources.BotCommandTextSource +import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage +import dev.inmo.tgbotapi.types.message.content.TextContent +import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent + + +val defaultArgsSeparator = Regex(" ") +/** + * Parse commands and their args. Logic will find command, get all subsequent data as args until new command + */ +fun List.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +): MutableMap> { + val result = mutableMapOf>() + var currentBotCommandSource: BotCommandTextSource? = null + var currentArgs = "" + fun includeCurrent() = currentBotCommandSource ?.let { + result[it.command] = currentArgs.split(argsSeparator).toTypedArray() + currentArgs = "" + } + for (textSource in this) { + if (textSource is BotCommandTextSource) { + includeCurrent() + currentBotCommandSource = textSource + } else { + currentArgs += textSource.source + } + } + includeCurrent() + return result +} + +/** + * Parse commands and their args. Logic will find command, get all subsequent data as args until new command + */ +fun TextedInput.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +) = textSources.parseCommandsWithParams(argsSeparator) + +/** + * Parse commands and their args. Logic will find command, get all subsequent data as args until new command + */ +fun TextedOutput.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +) = entities ?.parseCommandsWithParams(argsSeparator) ?: emptyMap() + +/** + * Parse commands and their args. Logic will find command, get all subsequent data as args until new command + */ +fun CaptionedInput.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +) = textSources.parseCommandsWithParams(argsSeparator) + +/** + * Parse commands and their args. Logic will find command, get all subsequent data as args until new command + */ +fun ContentMessage.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +) = content.parseCommandsWithParams(argsSeparator)