diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt new file mode 100644 index 0000000000..111c400f4a --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt @@ -0,0 +1,51 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull +import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.withContent +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.utils.RiskFeature +import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage +import kotlinx.coroutines.flow.Flow + +@RiskFeature(lowLevelRiskFeatureMessage) +suspend inline fun BehaviourContext.buildMediaGroupWaiter( + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null } +): Flow> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> + update.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull>() ?.let { message -> + if (message.content.group.all { it is T }) { + listOf(message.content as MediaGroupContent) + } else { + null + } + } ?: emptyList() +} + +suspend fun BehaviourContext.waitMediaGroup( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPlaylist( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitDocumentsGroup( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVisualGallery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPhotoGallery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVideoGallery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt new file mode 100644 index 0000000000..2b592a355e --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt @@ -0,0 +1,51 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull +import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.withContent +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.utils.RiskFeature +import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage +import kotlinx.coroutines.flow.Flow + +@RiskFeature(lowLevelRiskFeatureMessage) +suspend inline fun BehaviourContext.buildMediaGroupMessagesWaiter( + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null } +): Flow> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> + update.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull>() ?.let { message -> + if (message.content.group.all { it is T }) { + listOf(message as MediaGroupMessage) + } else { + null + } + } ?: emptyList() +} + +suspend fun BehaviourContext.waitMediaGroupMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPlaylistMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitDocumentsGroupMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVisualGalleryMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPhotoGalleryMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVideoGalleryMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt index 31f52a2e73..80c386c70c 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -13,11 +13,20 @@ internal suspend inline fun BC.on( noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver? = null, noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver, noinline updateToData: (Update) -> List? -) = flowsUpdatesFilter.expectFlow(bot) { - updateToData(it) ?.mapNotNull { data -> - if (initialFilter ?.invoke(data) != false) it to data else null - } ?: emptyList() -}.subscribeSafelyWithoutExceptionsAsync( +) = flowsUpdatesFilter.expectFlow( + bot, + filter = initialFilter ?.let { + { + updateToData(it) ?.mapNotNull { data -> + if (initialFilter(data)) it to data else null + } ?: emptyList() + } + } ?: { + updateToData(it) ?.mapNotNull { data -> + it to data + } ?: emptyList() + } +).subscribeSafelyWithoutExceptionsAsync( scope, { markerFactory(it.second) } ) { (update, triggerData) ->