diff --git a/CHANGELOG.md b/CHANGELOG.md index 33578c2c72..d321c74513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## 0.33.2 +* `Behaviour Builder`: + * New typealias `MediaGroupFilter` has been added for `MediaGroup` expectators + * Several typealiases became `suspend`: + * `CallbackQueryMapper` + * `ChatMemberUpdatedMapper` + * `InlineQueryMapper` + * Commands got an additional parameter - `additionalFilter`. It will be called when all command filters were passed + ## 0.33.1 * `Common`: diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt index dc0e8645c9..c279181bb9 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitCallbackQuery.kt @@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.types.CallbackQuery.* import kotlinx.coroutines.flow.toList -typealias CallbackQueryMapper = T.() -> T? +typealias CallbackQueryMapper = suspend T.() -> T? private suspend fun BehaviourContext.waitCallbackQueries( count: Int = 1, diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt index 8e69298190..ccdbd3051b 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt @@ -11,7 +11,7 @@ import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate import kotlinx.coroutines.flow.toList -typealias ChatMemberUpdatedMapper = T.() -> T? +typealias ChatMemberUpdatedMapper = suspend T.() -> T? private suspend inline fun BehaviourContext.waitChatMemberUpdated( count: Int = 1, diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt index 1e34de13eb..c3de533c8e 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitInlineQuery.kt @@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery import dev.inmo.tgbotapi.types.InlineQueries.query.LocationInlineQuery import kotlinx.coroutines.flow.toList -typealias InlineQueryMapper = T.() -> T? +typealias InlineQueryMapper = suspend T.() -> T? private suspend fun BehaviourContext.waitInlineQueries( count: Int = 1, diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt index 5aa0bc8e90..d4602bc334 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt @@ -11,12 +11,14 @@ import dev.inmo.tgbotapi.utils.PreviewFeature import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.toList +typealias MediaGroupFilter = suspend List>.() -> Boolean + @PreviewFeature internal suspend inline fun BehaviourContext.buildMediaGroupWaiter( count: Int = 1, initRequest: Request<*>? = null, noinline errorFactory: NullableRequestBuilder<*> = { null }, - noinline filter: (suspend (List>) -> Boolean)? = null + noinline filter: MediaGroupFilter? = null ) = flowsUpdatesFilter.expectFlow(bot, initRequest, count, errorFactory) { update -> update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup -> if (mediaGroup.all { message -> message.content is T } && (filter == null || filter(mediaGroup as List>))) { @@ -33,35 +35,35 @@ suspend fun BehaviourContext.waitMediaGroup( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, - filter: (suspend (List>) -> Boolean)? = null + filter: MediaGroupFilter? = null ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitPlaylist( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, - filter: (suspend (List>) -> Boolean)? = null + filter: MediaGroupFilter? = null ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitDocumentsGroup( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, - filter: (suspend (List>) -> Boolean)? = null + filter: MediaGroupFilter? = null ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitVisualGallery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, - filter: (suspend (List>) -> Boolean)? = null + filter: MediaGroupFilter? = null ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitPhotoGallery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, - filter: (suspend (List>) -> Boolean)? = null + filter: MediaGroupFilter? = null ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) suspend fun BehaviourContext.waitVideoGallery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, count: Int = 1, - filter: (suspend (List>) -> Boolean)? = null + filter: MediaGroupFilter? = null ) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt index 35c88e1f0e..0946a99140 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CommandHandling.kt @@ -11,6 +11,7 @@ suspend fun BehaviourContext.command( commandRegex: Regex, requireOnlyCommandInMessage: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: CommonMessageFilter? = null, scenarioReceiver: BehaviourContextAndTypeReceiver> ): Job = onText( includeFilterByChatInBehaviourSubContext, @@ -22,7 +23,9 @@ suspend fun BehaviourContext.command( } else { true } - sizeRequirement && textSources.any { commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) } + sizeRequirement && textSources.any { + commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) + } && (additionalFilter ?.invoke(message) != false) }, scenarioReceiver ) @@ -30,19 +33,22 @@ suspend fun BehaviourContext.command( command: String, requireOnlyCommandInMessage: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: CommonMessageFilter? = null, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = command(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver) +) = command(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) suspend inline fun BehaviourContext.onCommand( commandRegex: Regex, requireOnlyCommandInMessage: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true, + noinline additionalFilter: CommonMessageFilter? = null, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> -): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver) +): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver) suspend inline fun BehaviourContext.onCommand( command: String, requireOnlyCommandInMessage: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true, + noinline additionalFilter: CommonMessageFilter? = null, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> -): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver) +): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)