From 1a5d1cde78f3da792db30b84a6626b4ffec0d257 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 24 Mar 2021 13:41:37 +0600 Subject: [PATCH] add parseCommandsWithParams for text inputs --- CHANGELOG.md | 2 + .../tgbotapi/CommonAbstracts/Captioned.kt | 1 + .../TextCaptionBotCommandsParser.kt | 63 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/TextCaptionBotCommandsParser.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 93c4a23e0e..2bf5ab0e0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ * `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 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.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..4ad76dddac --- /dev/null +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/TextCaptionBotCommandsParser.kt @@ -0,0 +1,63 @@ +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) + +fun ContentMessage.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +) = content.parseCommandsWithParams(argsSeparator) + +fun ContentMessage.parseCommandsWithParams( + argsSeparator: Regex = defaultArgsSeparator +) where T : CaptionedInput, T : MessageContent = content.parseCommandsWithParams(argsSeparator)