From 9405aa4467406e4b32deb6e415b1c455037bf356 Mon Sep 17 00:00:00 2001 From: d1snin Date: Wed, 12 Oct 2022 20:57:42 +0300 Subject: [PATCH 1/6] added `EntitiesBuilder.botCommand` and `EntitiesBuilder.botCommandln` overloads supporting `BotCommand` --- .../kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt index 8960195a75..b9aa8e93d5 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.utils import dev.inmo.micro_utils.common.joinTo +import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.CustomEmojiId import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.message.textsources.* @@ -148,6 +149,15 @@ inline fun EntitiesBuilder.botCommand(command: String) = add(dev.inmo.tgbotapi.t * Version of [EntitiesBuilder.botCommand] with new line at the end */ inline fun EntitiesBuilder.botCommandln(command: String) = botCommand(command) + newLine +/** + * Add botCommand using [EntitiesBuilder.add] with [dev.inmo.tgbotapi.types.message.textsources.botCommand] + */ +inline fun EntitiesBuilder.botCommand(botCommand: BotCommand) = add(dev.inmo.tgbotapi.types.message.textsources.botCommand(botCommand.command)) +/** + * Version of [EntitiesBuilder.botCommand] with new line at the end + */ +inline fun EntitiesBuilder.botCommandln(botCommand: BotCommand) = botCommand(botCommand.command) + newLine + /** From 0c9919e9e7a0a742d3384bcbec25f31dd55bbafc Mon Sep 17 00:00:00 2001 From: d1snin Date: Wed, 12 Oct 2022 21:04:20 +0300 Subject: [PATCH 2/6] added command handling functions supporting `BotCommand` --- .../triggers_handling/CommandHandling.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt index f59aa80629..b7b18735b3 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt @@ -12,6 +12,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.Mar import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times import dev.inmo.tgbotapi.extensions.utils.botCommandTextSourceOrNull import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams +import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.content.TextMessage import dev.inmo.tgbotapi.types.update.abstracts.Update @@ -82,6 +83,15 @@ suspend fun BC.command( scenarioReceiver: CustomBehaviourContextAndTypeReceiver ) = command(command.toRegex(), requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BC.command( + botCommand: BotCommand, + requireOnlyCommandInMessage: Boolean = true, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, + markerFactory: MarkerFactory = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver +) = command(botCommand.command.toRegex(), requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + suspend fun BC.onCommand( commandRegex: Regex, requireOnlyCommandInMessage: Boolean = true, @@ -100,6 +110,15 @@ suspend fun BC.onCommand( scenarioReceiver: CustomBehaviourContextAndTypeReceiver ): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BC.onCommand( + botCommand: BotCommand, + requireOnlyCommandInMessage: Boolean = true, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, + markerFactory: MarkerFactory = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver +): Job = onCommand(botCommand.command.toRegex(), requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + suspend fun BC.commandWithArgs( commandRegex: Regex, initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, @@ -134,6 +153,20 @@ suspend fun BC.commandWithArgs( scenarioReceiver = scenarioReceiver ) +suspend fun BC.commandWithArgs( + botCommand: BotCommand, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, + markerFactory: MarkerFactory = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTwoTypesReceiver> +) = commandWithArgs( + botCommand.command.toRegex(), + initialFilter = initialFilter, + subcontextUpdatesFilter = subcontextUpdatesFilter, + markerFactory = markerFactory, + scenarioReceiver = scenarioReceiver +) + suspend fun BC.onCommandWithArgs( commandRegex: Regex, initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, @@ -149,3 +182,11 @@ suspend fun BC.onCommandWithArgs( markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTwoTypesReceiver> ): Job = onCommandWithArgs(command.toRegex(), initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +suspend fun BC.onCommandWithArgs( + botCommand: BotCommand, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, + markerFactory: MarkerFactory = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTwoTypesReceiver> +): Job = onCommandWithArgs(botCommand.command.toRegex(), initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) From 7488eb9d4b4e4355252ac62123800e2d63054710 Mon Sep 17 00:00:00 2001 From: d1snin Date: Thu, 13 Oct 2022 19:56:26 +0300 Subject: [PATCH 3/6] added `botCommand` text source factory function supporting `BotCommand` --- .../message/textsources/BotCommandTextSource.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt index bb143c5fe0..8dbbfe804e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt @@ -1,7 +1,8 @@ package dev.inmo.tgbotapi.types.message.textsources -import dev.inmo.tgbotapi.types.usernameRegex +import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.Username +import dev.inmo.tgbotapi.types.usernameRegex import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.internal.* import kotlinx.serialization.Serializable @@ -12,14 +13,14 @@ private val commandRegex = Regex("[/!][^@\\s]*") * @see botCommand */ @Serializable -data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor ( +data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor( override val source: String ) : TextSource { val command: String by lazy { - commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" + commandRegex.find(source)?.value?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" } val username: Username? by lazy { - Username(usernameRegex.find(source) ?.value ?: return@lazy null) + Username(usernameRegex.find(source)?.value ?: return@lazy null) } override val markdown: String by lazy { source.commandMarkdown() } @@ -32,3 +33,6 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru */ @Suppress("NOTHING_TO_INLINE") inline fun botCommand(command: String) = BotCommandTextSource("/$command") + +@Suppress("NOTHING_TO_INLINE") +inline fun botCommand(botCommand: BotCommand) = botCommand(botCommand.command) \ No newline at end of file From bd91d4a0c014c8f7b464f06bfae86031b00c4384 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Oct 2022 23:29:36 +0600 Subject: [PATCH 4/6] Update CommandHandling.kt --- .../triggers_handling/CommandHandling.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt index b7b18735b3..3802ee5dc1 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt @@ -90,7 +90,7 @@ suspend fun BC.command( subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTypeReceiver -) = command(botCommand.command.toRegex(), requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +) = command(botCommand.command, requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) suspend fun BC.onCommand( commandRegex: Regex, @@ -117,7 +117,7 @@ suspend fun BC.onCommand( subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTypeReceiver -): Job = onCommand(botCommand.command.toRegex(), requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +): Job = onCommand(botCommand.command, requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) suspend fun BC.commandWithArgs( commandRegex: Regex, @@ -160,7 +160,7 @@ suspend fun BC.commandWithArgs( markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTwoTypesReceiver> ) = commandWithArgs( - botCommand.command.toRegex(), + botCommand.command, initialFilter = initialFilter, subcontextUpdatesFilter = subcontextUpdatesFilter, markerFactory = markerFactory, @@ -189,4 +189,4 @@ suspend fun BC.onCommandWithArgs( subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTwoTypesReceiver> -): Job = onCommandWithArgs(botCommand.command.toRegex(), initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +): Job = onCommandWithArgs(botCommand.command, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) From ac7bf60182aeae2d3e2b2dfe9e87155d01eca0f1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Oct 2022 23:30:34 +0600 Subject: [PATCH 5/6] Update BotCommandTextSource.kt --- .../types/message/textsources/BotCommandTextSource.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt index 8dbbfe804e..5c3d49e741 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/BotCommandTextSource.kt @@ -13,14 +13,14 @@ private val commandRegex = Regex("[/!][^@\\s]*") * @see botCommand */ @Serializable -data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor( +data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstructor) constructor ( override val source: String ) : TextSource { val command: String by lazy { - commandRegex.find(source)?.value?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" + commandRegex.find(source) ?.value ?.substring(1) ?: source.substring(1)// skip first symbol like "/" or "!" } val username: Username? by lazy { - Username(usernameRegex.find(source)?.value ?: return@lazy null) + Username(usernameRegex.find(source) ?.value ?: return@lazy null) } override val markdown: String by lazy { source.commandMarkdown() } @@ -35,4 +35,4 @@ data class BotCommandTextSource @RiskFeature(DirectInvocationOfTextSourceConstru inline fun botCommand(command: String) = BotCommandTextSource("/$command") @Suppress("NOTHING_TO_INLINE") -inline fun botCommand(botCommand: BotCommand) = botCommand(botCommand.command) \ No newline at end of file +inline fun botCommand(botCommand: BotCommand) = botCommand(botCommand.command) From 636382fc8fee8253ab30266edfa5b0e975a2bec7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Oct 2022 23:31:41 +0600 Subject: [PATCH 6/6] Update EntitiesBuilder.kt --- .../kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt index b9aa8e93d5..8e6f52a42a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/EntitiesBuilder.kt @@ -152,11 +152,11 @@ inline fun EntitiesBuilder.botCommandln(command: String) = botCommand(command) + /** * Add botCommand using [EntitiesBuilder.add] with [dev.inmo.tgbotapi.types.message.textsources.botCommand] */ -inline fun EntitiesBuilder.botCommand(botCommand: BotCommand) = add(dev.inmo.tgbotapi.types.message.textsources.botCommand(botCommand.command)) +inline fun EntitiesBuilder.botCommand(botCommand: BotCommand) = add(dev.inmo.tgbotapi.types.message.textsources.botCommand(botCommand)) /** * Version of [EntitiesBuilder.botCommand] with new line at the end */ -inline fun EntitiesBuilder.botCommandln(botCommand: BotCommand) = botCommand(botCommand.command) + newLine +inline fun EntitiesBuilder.botCommandln(botCommand: BotCommand) = botCommand(botCommand) + newLine