From 3fa3aa50d93bcdc92517dcfdbce886d970017a59 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 13 Sep 2020 23:42:59 +0600 Subject: [PATCH] commands shortcuts --- CHANGELOG.md | 6 ++ .../utils/shortcuts/CommandsShortcuts.kt | 93 +++++++++++++++++++ .../utils/updates/CommandsFilters.kt | 33 +------ 3 files changed, 103 insertions(+), 29 deletions(-) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/CommandsShortcuts.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 81c7197e04..6ec597eab9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,12 @@ ### 0.28.2 +* `TelegramBotAPI-extensions-utils`: + * Several commands shortcuts for `Flow>` has been added: + * `filterExactCommands` + * `filterCommandsInsideTextMessages` + * `filterCommandsWithArgs` + ## 0.27.0 * `Common`: diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/CommandsShortcuts.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/CommandsShortcuts.kt new file mode 100644 index 0000000000..bcca93193b --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/CommandsShortcuts.kt @@ -0,0 +1,93 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts + +import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList +import kotlinx.coroutines.flow.* + +/** + * Convert incoming [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage.content] of + * messages with [fullEntitiesList] and check that incoming message contains ONLY ONE [TextSource] and that is + * [BotCommandTextSource]. Besides, it is checking that [BotCommandTextSource.command] [Regex.matches] with incoming + * [commandRegex] + * + * @return The same message in case if it contains only [BotCommandTextSource] with [Regex.matches] + * [BotCommandTextSource.command] + * + * @see fullEntitiesList + * @see asContentMessagesFlow + * @see onlyTextContentMessages + * @see textMessages + */ +fun > Flow.filterExactCommands( + commandRegex: Regex +) = filter { contentMessage -> + (contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true +} + +/** + * Convert incoming [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage.content] of + * messages with [fullEntitiesList] and check that incoming message contains [BotCommandTextSource]. Besides, it is + * checking that [BotCommandTextSource.command] [Regex.matches] with incoming [commandRegex] + * + * @return The same message in case if it contains somewhere in text [BotCommandTextSource] with [Regex.matches] + * [BotCommandTextSource.command] + * + * @see fullEntitiesList + * @see asContentMessagesFlow + * @see onlyTextContentMessages + * @see textMessages + */ +fun > Flow.filterCommandsInsideTextMessages( + commandRegex: Regex +) = filter { contentMessage -> + contentMessage.content.fullEntitiesList().any { + (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true + } +} + +/** + * Convert incoming [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage.content] of + * messages with [fullEntitiesList] and check that incoming message contains first [TextSource] as + * [BotCommandTextSource]. Besides, it is checking that [BotCommandTextSource.command] [Regex.matches] with incoming + * [commandRegex] and for other [TextSource] objects used next rules: all incoming text sources will be passed as is, + * [RegularTextSource] will be split by " " for several [RegularTextSource] which will contains not empty args without + * spaces. + * + * @return Converted list with first entity [BotCommandTextSource] and than all others according to rules in description + * + * @see fullEntitiesList + * @see asContentMessagesFlow + * @see onlyTextContentMessages + * @see textMessages + */ +fun > Flow.filterCommandsWithArgs( + commandRegex: Regex +) = mapNotNull { contentMessage -> + val allEntities = contentMessage.content.fullEntitiesList() + (allEntities.firstOrNull() as? BotCommandTextSource) ?.let { + if (commandRegex.matches(it.command)) { + allEntities.flatMap { + when (it) { + is RegularTextSource -> it.source.split(" ").mapNotNull { regularTextSourcePart -> + if (regularTextSourcePart.isNotBlank()) { + RegularTextSource(regularTextSourcePart) + } else { + null + } + } + else -> listOf(it) + } + } + } else { + null + } + } +} + + diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt index 1e3719e5af..826409e9d1 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.* import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList @@ -23,9 +24,7 @@ import kotlinx.coroutines.flow.* */ fun Flow.filterExactCommands( commandRegex: Regex -) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage -> - (contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true -} +) = textMessages().filterExactCommands(commandRegex) /** * Convert incoming [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage.content] of @@ -41,11 +40,7 @@ fun Flow.filterExactCommands( */ fun Flow.filterCommandsInsideTextMessages( commandRegex: Regex -) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage -> - contentMessage.content.fullEntitiesList().any { - (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true - } -} +) = textMessages().filterCommandsInsideTextMessages(commandRegex) /** * Convert incoming [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage.content] of @@ -63,24 +58,4 @@ fun Flow.filterCommandsInsideTextMessages( */ fun Flow.filterCommandsWithArgs( commandRegex: Regex -): Flow> = asContentMessagesFlow().onlyTextContentMessages().mapNotNull { contentMessage -> - val allEntities = contentMessage.content.fullEntitiesList() - (allEntities.firstOrNull() as? BotCommandTextSource) ?.let { - if (commandRegex.matches(it.command)) { - allEntities.flatMap { - when (it) { - is RegularTextSource -> it.source.split(" ").mapNotNull { regularTextSourcePart -> - if (regularTextSourcePart.isNotBlank()) { - RegularTextSource(regularTextSourcePart) - } else { - null - } - } - else -> listOf(it) - } - } - } else { - null - } - } -} +): Flow> = textMessages().filterCommandsWithArgs(commandRegex)