From 9cf82a35645320b202de867d2d07914c15d60618 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 13 Sep 2021 23:30:01 +0600 Subject: [PATCH] subcontext filter and inBlock filter in behaviour context triggers --- CHANGELOG.md | 2 + .../behaviour_builder/BehaviourContext.kt | 3 + .../CommonMessageFilterExcludeMediaGroups.kt | 8 + .../filters/MessageFilterByChat.kt | 27 + .../MessageFilterExcludingMediaGroups.kt | 10 + .../CallbackQueryTriggers.kt | 218 ++++++-- .../ChatMemberUpdatedTriggers.kt | 80 +-- .../triggers_handling/CommandHandling.kt | 132 ++++- .../triggers_handling/ContentTriggers.kt | 476 +++++++++++++++--- .../EditedContentTriggers.kt | 265 ++++++++++ .../triggers_handling/EventTriggers.kt | 238 ++++++--- .../triggers_handling/InlineQueryTriggers.kt | 69 ++- .../triggers_handling/MainTrigger.kt | 30 ++ .../triggers_handling/MediaGroupTriggers.kt | 104 ++-- .../triggers_handling/PassportTriggers.kt | 47 +- .../behaviour_builder/utils/SimpleFilter.kt | 23 + 16 files changed, 1398 insertions(+), 334 deletions(-) create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/CommonMessageFilterExcludeMediaGroups.kt create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterByChat.kt create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index bddeb85c80..c10648adf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ * `Core`: * New `BotAction` implementation - `CustomBotAction` * `LocationContent` has been divided to two different types: `LiveLocationContent` and `StaticLocationContent` +* `Behaviour Builder`: + * All triggers has been changed to use two filters: filter for in subcontext data and filter for incoming data ## 0.35.8 diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index 35fab43c78..0de9ee16b3 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -11,6 +11,9 @@ import kotlinx.coroutines.flow.filter typealias BehaviourContextReceiver = suspend BehaviourContext.() -> T typealias BehaviourContextAndTypeReceiver = suspend BehaviourContext.(I) -> T typealias BehaviourContextAndTwoTypesReceiver = suspend BehaviourContext.(I1, I2) -> T +internal inline fun BehaviourContextAndTwoTypesReceiver.toOneType( + i1: I1, +): BehaviourContextAndTypeReceiver = { invoke(this, i1, it) } /** * This class contains all necessary tools for work with bots and especially for [buildBehaviour] diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/CommonMessageFilterExcludeMediaGroups.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/CommonMessageFilterExcludeMediaGroups.kt new file mode 100644 index 0000000000..a5b4b1d90a --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/CommonMessageFilterExcludeMediaGroups.kt @@ -0,0 +1,8 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.filters + +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.CommonMessageFilter +import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage + +val CommonMessageFilterExcludeMediaGroups: CommonMessageFilter<*> = { + it !is MediaGroupMessage<*> +} diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterByChat.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterByChat.kt new file mode 100644 index 0000000000..46357d6d5d --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterByChat.kt @@ -0,0 +1,27 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.filters + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver +import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat +import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery +import dev.inmo.tgbotapi.types.ChatMemberUpdated +import dev.inmo.tgbotapi.types.InlineQueries.query.InlineQuery +import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.update.abstracts.Update + +val MessageFilterByChat: BehaviourContextAndTwoTypesReceiver = { message, update -> + update.sourceChat() ?.id == message.chat.id +} +val MessagesFilterByChat: BehaviourContextAndTwoTypesReceiver, Update> = { messages, update -> + val sourceChatId = update.sourceChat() ?.id + sourceChatId != null && messages.all { sourceChatId == it.chat.id } +} + +val CallbackQueryFilterByUser: BehaviourContextAndTwoTypesReceiver = { query, update -> + update.sourceChat() ?.id == query.user.id +} +val InlineQueryFilterByUser: BehaviourContextAndTwoTypesReceiver = { query, update -> + update.sourceChat() ?.id == query.from.id +} +val ChatMemberUpdatedFilterByChat: BehaviourContextAndTwoTypesReceiver = { updated, update -> + update.sourceChat() ?.id == updated.chat.id +} diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt new file mode 100644 index 0000000000..5e3fcdeba8 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt @@ -0,0 +1,10 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.filters + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +import dev.inmo.tgbotapi.types.update.abstracts.Update + +val MessageFilterExcludingMediaGroups: BehaviourContextAndTwoTypesReceiver, Update> = { message, update -> + update !is MediaGroupMessage<*> +} diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CallbackQueryTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CallbackQueryTriggers.kt index 51480d43d4..a08e57f3ff 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CallbackQueryTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/CallbackQueryTriggers.kt @@ -1,97 +1,223 @@ +@file:Suppress("unused") + package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CallbackQueryFilterByUser import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserCallbackQueryMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.types.CallbackQuery.* +import dev.inmo.tgbotapi.types.update.abstracts.Update internal suspend inline fun BehaviourContext.onCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, - noinline additionalFilter: SimpleFilter? = null, + noinline initialFilter: SimpleFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver -) = flowsUpdatesFilter.expectFlow(bot) { - it.asCallbackQueryUpdate() ?.data ?.let { query -> - if (query is T) { - if (additionalFilter == null || additionalFilter(query)) query else null - } else { - null - } - }.let(::listOfNotNull) -}.subscribeSafelyWithoutExceptionsAsync( - scope, - markerFactory::invoke -) { triggerQuery -> - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId } - } else { - null - }, - stopOnCompletion = false - ) { - scenarioReceiver(triggerQuery) - } +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + (it.asCallbackQueryUpdate() ?.data as? T) ?.let(::listOfNotNull) } suspend fun BehaviourContext.onDataCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) - +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onGameShortNameCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onInlineMessageIdCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onInlineMessageIdDataCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onInlineMessageIdGameShortNameCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onMessageCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onMessageDataCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onMessageGameShortNameCallbackQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onUnknownCallbackQueryType( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onCallbackQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onCallbackQuery( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) CallbackQueryFilterByUser else null, + markerFactory, + scenarioReceiver +) + + +suspend fun BehaviourContext.onDataCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onGameShortNameCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onInlineMessageIdCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onInlineMessageIdDataCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onInlineMessageIdGameShortNameCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onMessageCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onMessageDataCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onMessageGameShortNameCallbackQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onUnknownCallbackQueryType( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = CallbackQueryFilterByUser, + markerFactory: MarkerFactory = ByUserCallbackQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onCallbackQuery( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt index 6994853ec7..92da3e84bb 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt @@ -1,74 +1,96 @@ +@file:Suppress("unused") + package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelySkippingExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.ChatMemberUpdatedFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatChatMemberUpdatedMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.types.ChatMemberUpdated import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate +import dev.inmo.tgbotapi.types.update.abstracts.Update internal suspend inline fun BehaviourContext.onChatMemberUpdatedInternal( - includeFilterByChatInBehaviourSubContext: Boolean = true, - noinline additionalFilter: SimpleFilter? = null, + noinline initialFilter: SimpleFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = ChatMemberUpdatedFilterByChat, markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver -) = flowsUpdatesFilter.expectFlow(bot) { - (it as? U) ?.data ?.let { chatMemberUpdated -> - if (additionalFilter == null || additionalFilter(chatMemberUpdated)) chatMemberUpdated else null - }.let(::listOfNotNull) -}.subscribeSafelySkippingExceptionsAsync( - scope, - markerFactory::invoke -) { triggerChatMemberUpdated -> - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId } - } else { - null - }, - stopOnCompletion = false - ) { - scenarioReceiver(triggerChatMemberUpdated) - } +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + ((it as? U) ?.data) ?.let(::listOfNotNull) } suspend fun BehaviourContext.onChatMemberUpdated( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver ) = onChatMemberUpdatedInternal( - includeFilterByChatInBehaviourSubContext, additionalFilter, + if (includeFilterByChatInBehaviourSubContext) ChatMemberUpdatedFilterByChat else null, markerFactory, scenarioReceiver ) suspend fun BehaviourContext.onCommonChatMemberUpdated( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver ) = onChatMemberUpdatedInternal( - includeFilterByChatInBehaviourSubContext, additionalFilter, + if (includeFilterByChatInBehaviourSubContext) ChatMemberUpdatedFilterByChat else null, markerFactory, scenarioReceiver ) suspend fun BehaviourContext.onMyChatMemberUpdated( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver ) = onChatMemberUpdatedInternal( - includeFilterByChatInBehaviourSubContext, additionalFilter, + if (includeFilterByChatInBehaviourSubContext) ChatMemberUpdatedFilterByChat else null, + markerFactory, + scenarioReceiver +) + + +suspend fun BehaviourContext.onChatMemberUpdated( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = ChatMemberUpdatedFilterByChat, + markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onChatMemberUpdatedInternal( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) + +suspend fun BehaviourContext.onCommonChatMemberUpdated( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = ChatMemberUpdatedFilterByChat, + markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onChatMemberUpdatedInternal( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) + +suspend fun BehaviourContext.onMyChatMemberUpdated( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = ChatMemberUpdatedFilterByChat, + markerFactory: MarkerFactory = ByChatChatMemberUpdatedMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onChatMemberUpdatedInternal( + initialFilter, + subcontextUpdatesFilter, markerFactory, scenarioReceiver ) 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 fa81c759df..49b5618ccb 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 @@ -1,24 +1,31 @@ +@file:Suppress("unused") + package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times import dev.inmo.tgbotapi.extensions.utils.asBotCommandTextSource import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.TextContent +import dev.inmo.tgbotapi.types.update.abstracts.Update import kotlinx.coroutines.Job suspend fun BehaviourContext.command( commandRegex: Regex, requireOnlyCommandInMessage: Boolean = true, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> ): Job = onText( includeFilterByChatInBehaviourSubContext, - { message -> + CommonMessageFilter { message -> val content = message.content val textSources = content.textSources val sizeRequirement = if (requireOnlyCommandInMessage) { @@ -29,6 +36,8 @@ suspend fun BehaviourContext.command( sizeRequirement && textSources.any { commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) } && (additionalFilter ?.invoke(message) != false) + }.let { + additionalFilter ?.times(it) ?: it }, markerFactory, scenarioReceiver @@ -37,7 +46,7 @@ suspend fun BehaviourContext.command( suspend fun BehaviourContext.command( command: String, requireOnlyCommandInMessage: Boolean = true, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> @@ -46,7 +55,7 @@ suspend fun BehaviourContext.command( suspend inline fun BehaviourContext.onCommand( commandRegex: Regex, requireOnlyCommandInMessage: Boolean = true, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, noinline additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> @@ -55,7 +64,7 @@ suspend inline fun BehaviourContext.onCommand( suspend inline fun BehaviourContext.onCommand( command: String, requireOnlyCommandInMessage: Boolean = true, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, noinline additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> @@ -63,7 +72,7 @@ suspend inline fun BehaviourContext.onCommand( suspend fun BehaviourContext.commandWithArgs( commandRegex: Regex, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> @@ -83,7 +92,7 @@ suspend fun BehaviourContext.commandWithArgs( suspend fun BehaviourContext.commandWithArgs( command: String, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> @@ -97,7 +106,7 @@ suspend fun BehaviourContext.commandWithArgs( suspend inline fun BehaviourContext.onCommandWithArgs( commandRegex: Regex, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, noinline additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> @@ -105,8 +114,113 @@ suspend inline fun BehaviourContext.onCommandWithArgs( suspend inline fun BehaviourContext.onCommandWithArgs( command: String, - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, noinline additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> ): Job = onCommandWithArgs(command.toRegex(), includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) + + +suspend fun BehaviourContext.command( + commandRegex: Regex, + requireOnlyCommandInMessage: Boolean = true, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +): Job = onText( + CommonMessageFilter { message -> + val content = message.content + val textSources = content.textSources + val sizeRequirement = if (requireOnlyCommandInMessage) { + textSources.size == 1 + } else { + true + } + sizeRequirement && textSources.any { + commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) + } + }.let { + initialFilter ?.times(it) ?: it + }, + updatesFilter, + markerFactory, + scenarioReceiver +) + +suspend fun BehaviourContext.command( + command: String, + requireOnlyCommandInMessage: Boolean = true, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = command(command.toRegex(), requireOnlyCommandInMessage, initialFilter, updatesFilter, markerFactory, scenarioReceiver) + +suspend inline fun BehaviourContext.onCommand( + commandRegex: Regex, + requireOnlyCommandInMessage: Boolean = true, + noinline initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + noinline updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + noinline scenarioReceiver: BehaviourContextAndTypeReceiver> +): Job = command(commandRegex, requireOnlyCommandInMessage, initialFilter, updatesFilter, markerFactory, scenarioReceiver) + +suspend inline fun BehaviourContext.onCommand( + command: String, + requireOnlyCommandInMessage: Boolean = true, + noinline initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + noinline updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + noinline scenarioReceiver: BehaviourContextAndTypeReceiver> +): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, initialFilter, updatesFilter, markerFactory, scenarioReceiver) + +suspend fun BehaviourContext.commandWithArgs( + commandRegex: Regex, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> +) = command( + commandRegex, + requireOnlyCommandInMessage = false, + initialFilter = initialFilter, + updatesFilter = updatesFilter, + markerFactory = markerFactory +) { + val args = it.parseCommandsWithParams().let { commandsWithArgs -> + val key = commandsWithArgs.keys.firstOrNull { it.matches(commandRegex) } ?: return@let null + commandsWithArgs[key] + } ?: emptyArray() + scenarioReceiver(it, args) +} + +suspend fun BehaviourContext.commandWithArgs( + command: String, + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> +) = commandWithArgs( + command.toRegex(), + initialFilter = initialFilter, + updatesFilter = updatesFilter, + markerFactory = markerFactory, + scenarioReceiver = scenarioReceiver +) + +suspend inline fun BehaviourContext.onCommandWithArgs( + commandRegex: Regex, + noinline initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + noinline updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + noinline scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> +): Job = commandWithArgs(commandRegex, initialFilter, updatesFilter, markerFactory, scenarioReceiver) + +suspend inline fun BehaviourContext.onCommandWithArgs( + command: String, + noinline initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + noinline updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + noinline scenarioReceiver: BehaviourContextAndTwoTypesReceiver, Array> +): Job = onCommandWithArgs(command.toRegex(), initialFilter, updatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt index 9c0d76855a..098372705e 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt @@ -2,194 +2,510 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory -import dev.inmo.tgbotapi.extensions.utils.* -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat +import dev.inmo.tgbotapi.extensions.utils.whenCommonMessage import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.abstracts.* import dev.inmo.tgbotapi.types.message.content.media.* import dev.inmo.tgbotapi.types.message.payments.InvoiceContent -import dev.inmo.tgbotapi.utils.PreviewFeature +import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.SentMediaGroupUpdate +import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate +import dev.inmo.tgbotapi.types.update.abstracts.Update typealias CommonMessageFilter = SimpleFilter> +inline fun CommonMessageFilter(noinline block: CommonMessageFilter) = block -@PreviewFeature internal suspend inline fun BehaviourContext.onContent( - includeFilterByChatInBehaviourSubContext: Boolean = true, - includeMediaGroups: Boolean = true, - noinline additionalFilter: CommonMessageFilter? = null, + noinline initialFilter: CommonMessageFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> -) = flowsUpdatesFilter.expectFlow(bot) { - val messages = it.whenBaseSentMessageUpdate { - it.data.whenCommonMessage(::listOfNotNull) - } ?: if (includeMediaGroups) { - it.asSentMediaGroupUpdate() ?.data ?: emptyList() - } else { - emptyList() - } - messages.mapNotNull { message -> - if (message.content is T) { - val adaptedMessage = message as CommonMessage - if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null - } else { - null - } - } -}.subscribeSafelyWithoutExceptionsAsync( - scope, - markerFactory::invoke -) { triggerMessage -> - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } - } else { - null - }, - stopOnCompletion = false - ) { - scenarioReceiver(triggerMessage) +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + when (it) { + is BaseSentMessageUpdate -> it.data.whenCommonMessage(::listOfNotNull) + is SentMediaGroupUpdate -> it.data + else -> null + } ?.mapNotNull { message -> + if (message.content is T) message as CommonMessage else null } } suspend fun BehaviourContext.onContentMessage( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, includeMediaGroups: Boolean = true, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onContact( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onDice( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onGame( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onLocation( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onPoll( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onText( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onVenue( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onAudioMediaGroup( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, true, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onDocumentMediaGroupContent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = true, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onMediaCollection( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = false, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onMedia( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = true, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onAnimation( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onAudio( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = false, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onDocument( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = false, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onPhoto( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = false, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onSticker( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onVideo( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, includeMediaGroups: Boolean = false, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: if (includeMediaGroups) null else CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onVideoNote( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onVoice( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) suspend fun BehaviourContext.onInvoice( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: CommonMessageFilter? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent( + additionalFilter ?: CommonMessageFilterExcludeMediaGroups, + if (includeFilterByChatInBehaviourSubContext) { MessageFilterByChat } else null, + markerFactory, + scenarioReceiver +) + + +suspend fun BehaviourContext.onContentMessage( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onContact( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onDice( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onGame( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onLocation( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onPoll( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onText( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onVenue( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onAudioMediaGroup( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onDocumentMediaGroupContent( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onMediaCollection( + initialFilter: CommonMessageFilter>? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver>, Update> = MessageFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onMedia( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onAnimation( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onAudio( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onDocument( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onPhoto( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onSticker( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onVideo( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onVideoNote( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onVoice( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onInvoice( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt new file mode 100644 index 0000000000..88ef5f6a15 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt @@ -0,0 +1,265 @@ +@file:Suppress("unused", "UNCHECKED_CAST") + +package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling + +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory +import dev.inmo.tgbotapi.extensions.utils.asEditMessageUpdate +import dev.inmo.tgbotapi.extensions.utils.withContent +import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.types.message.content.abstracts.* +import dev.inmo.tgbotapi.types.message.content.media.* +import dev.inmo.tgbotapi.types.message.payments.InvoiceContent +import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate +import dev.inmo.tgbotapi.types.update.abstracts.Update +import dev.inmo.tgbotapi.utils.PreviewFeature + +@PreviewFeature +internal suspend inline fun BehaviourContext.onEditedContent( + noinline initialFilter: CommonMessageFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + noinline scenarioReceiver: BehaviourContextAndTypeReceiver> +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + when (it) { + is BaseEditMessageUpdate -> (it.asEditMessageUpdate() ?.data ?.withContent()) + else -> null + } ?.let(::listOfNotNull) +} + +suspend fun BehaviourContext.onEditedContentMessage( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedContact( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedDice( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedGame( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedLocation( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedPoll( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedText( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedVenue( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedAudioMediaGroup( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedDocumentMediaGroupContent( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedMediaCollection( + initialFilter: CommonMessageFilter>? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver>, Update> = MessageFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedMedia( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedAnimation( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedAudio( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedDocument( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedPhoto( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedSticker( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedVideo( + initialFilter: CommonMessageFilter? = null, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedVideoNote( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedVoice( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) +suspend fun BehaviourContext.onEditedInvoice( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + updatesFilter: BehaviourContextAndTwoTypesReceiver, Update> = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +)= onEditedContent( + initialFilter, + updatesFilter, + markerFactory, + scenarioReceiver +) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EventTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EventTriggers.kt index c72c39d18d..73e76b4b8b 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EventTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EventTriggers.kt @@ -1,166 +1,270 @@ +@file:Suppress("unused") + package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling - -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.types.message.ChatEvents.* import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.* import dev.inmo.tgbotapi.types.message.ChatEvents.voice.* import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage +import dev.inmo.tgbotapi.types.update.abstracts.Update internal suspend inline fun BehaviourContext.onEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, - noinline additionalFilter: SimpleFilter>? = null, + noinline initialFilter: SimpleFilter>? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> -) = flowsUpdatesFilter.expectFlow(bot) { - it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.let { message -> - if (message.chatEvent is T) { - val adaptedMessage = message as ChatEventMessage - if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null - } else { - null - } - }.let(::listOfNotNull) -}.subscribeSafelyWithoutExceptionsAsync( - scope, - markerFactory::invoke -) { triggerMessage -> - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } - } else null, - stopOnCompletion = false - ) { - scenarioReceiver(triggerMessage) - } +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + (it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.takeIf { it.chatEvent is T } as? ChatEventMessage) ?.let(::listOfNotNull) } suspend fun BehaviourContext.onChannelEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onChatEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVoiceChatEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVoiceChatStartedEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVoiceChatEndedEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVoiceChatParticipantsInvitedEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onMessageAutoDeleteTimerChangedEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onCommonEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onGroupEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onSupergroupEvent( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onChannelChatCreated( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onDeleteChatPhoto( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onGroupChatCreated( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onLeftChatMember( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onNewChatMembers( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onNewChatPhoto( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onNewChatTitle( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onPinnedMessage( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onProximityAlertTriggered( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onSupergroupChatCreated( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>? = null, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onEvent(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onEvent(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, markerFactory, scenarioReceiver) + + +suspend fun BehaviourContext.onChannelEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onChatEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVoiceChatEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVoiceChatStartedEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVoiceChatEndedEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVoiceChatParticipantsInvitedEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onMessageAutoDeleteTimerChangedEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onCommonEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onGroupEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onSupergroupEvent( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +suspend fun BehaviourContext.onChannelChatCreated( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onDeleteChatPhoto( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onGroupChatCreated( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onLeftChatMember( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onNewChatMembers( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onNewChatPhoto( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onNewChatTitle( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onPinnedMessage( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onProximityAlertTriggered( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onSupergroupChatCreated( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver, Update>? = MessageFilterByChat, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver> +) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt index 381bff6932..71180767d8 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/InlineQueryTriggers.kt @@ -1,64 +1,63 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.InlineQueryFilterByUser import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserInlineQueryMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.types.InlineQueries.query.* +import dev.inmo.tgbotapi.types.update.abstracts.Update internal suspend inline fun BehaviourContext.onInlineQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, - noinline additionalFilter: SimpleFilter? = null, + noinline initialFilter: SimpleFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = InlineQueryFilterByUser, markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver -) = flowsUpdatesFilter.expectFlow(bot) { - it.asInlineQueryUpdate() ?.data ?.let { query -> - if (query is T) { - if (additionalFilter == null || additionalFilter(query)) query else null - } else { - null - } - }.let(::listOfNotNull) -}.subscribeSafelyWithoutExceptionsAsync( - scope, - markerFactory::invoke -) { triggerQuery -> - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId } - } else { - null - }, - stopOnCompletion = false - ) { - scenarioReceiver(triggerQuery) - } +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + (it.asInlineQueryUpdate() ?.data as? T) ?.let(::listOfNotNull) } suspend fun BehaviourContext.onAnyInlineQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) - +) = onInlineQuery(additionalFilter, if (includeFilterByChatInBehaviourSubContext) InlineQueryFilterByUser else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onBaseInlineQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) - +) = onInlineQuery(additionalFilter, if (includeFilterByChatInBehaviourSubContext) InlineQueryFilterByUser else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onLocationInlineQuery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver -) = onInlineQuery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onInlineQuery(additionalFilter, if (includeFilterByChatInBehaviourSubContext) InlineQueryFilterByUser else null, markerFactory, scenarioReceiver) + + +suspend fun BehaviourContext.onAnyInlineQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = InlineQueryFilterByUser, + markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onInlineQuery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +suspend fun BehaviourContext.onBaseInlineQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = InlineQueryFilterByUser, + markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onInlineQuery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +suspend fun BehaviourContext.onLocationInlineQuery( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = InlineQueryFilterByUser, + markerFactory: MarkerFactory = ByUserInlineQueryMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onInlineQuery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt new file mode 100644 index 0000000000..bdc7bf8058 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -0,0 +1,30 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling + +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory +import dev.inmo.tgbotapi.types.update.abstracts.Update + +internal suspend inline fun BehaviourContext.on( + markerFactory: MarkerFactory, + noinline initialFilter: SimpleFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = null, + noinline scenarioReceiver: BehaviourContextAndTypeReceiver, + noinline updateToData: (Update) -> List? +) = flowsUpdatesFilter.expectFlow(bot) { + updateToData(it) ?.mapNotNull { data -> + if (initialFilter ?.invoke(data) != false) data else null + } ?: emptyList() +}.subscribeSafelyWithoutExceptionsAsync( + scope, + markerFactory::invoke +) { triggerData -> + doInSubContextWithUpdatesFilter( + updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData), + stopOnCompletion = false + ) { + scenarioReceiver(triggerData) + } +} diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt index 305baaddd8..11408f129b 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt @@ -2,90 +2,112 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessagesFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat -import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.abstracts.* import dev.inmo.tgbotapi.types.message.content.media.PhotoContent import dev.inmo.tgbotapi.types.message.content.media.VideoContent +import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.utils.PreviewFeature @PreviewFeature internal suspend inline fun BehaviourContext.buildMediaGroupTrigger( - includeFilterByChatInBehaviourSubContext: Boolean = true, - noinline additionalFilter: SimpleFilter>>? = null, + noinline initialFilter: SimpleFilter>>? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = flowsUpdatesFilter.expectFlow(bot) { update -> - update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup -> - if (mediaGroup.all { message -> message.content is T } && (additionalFilter == null || additionalFilter(mediaGroup as List>))) { - listOf(mediaGroup as List>) - } else { - null - } - } ?: emptyList() -}.subscribeSafelyWithoutExceptionsAsync( - scope, - markerFactory::invoke -) { mediaGroup -> - val mediaGroupChat = mediaGroup.chat!! - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == mediaGroupChat.id.chatId } - } else null, - stopOnCompletion = false - ) { - scenarioReceiver(mediaGroup) - } +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + (it.asSentMediaGroupUpdate() ?.data ?.takeIf { it.all { it is T } } as? List>) ?.let(::listOfNotNull) } suspend fun BehaviourContext.onMediaGroup( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = buildMediaGroupTrigger(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onPlaylist( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = buildMediaGroupTrigger(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onDocumentsGroup( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = buildMediaGroupTrigger(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVisualGallery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = buildMediaGroupTrigger(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVisualMediaGroup( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = onVisualGallery(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = onVisualGallery(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onPhotoGallery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = buildMediaGroupTrigger(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onVideoGallery( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter>>? = null, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(includeFilterByChatInBehaviourSubContext, additionalFilter, markerFactory, scenarioReceiver) +) = buildMediaGroupTrigger(additionalFilter, if (includeFilterByChatInBehaviourSubContext) MessagesFilterByChat else null, markerFactory, scenarioReceiver) + +suspend fun BehaviourContext.onMediaGroup( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onPlaylist( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onDocumentsGroup( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVisualGallery( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVisualMediaGroup( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = onVisualGallery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onPhotoGallery( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +suspend fun BehaviourContext.onVideoGallery( + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver>> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/PassportTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/PassportTriggers.kt index ed130e4f5d..ea4d0141af 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/PassportTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/PassportTriggers.kt @@ -1,52 +1,45 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate import dev.inmo.tgbotapi.extensions.utils.asPassportMessage -import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.types.message.PassportMessage import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement +import dev.inmo.tgbotapi.types.update.abstracts.Update internal suspend inline fun BehaviourContext.onPassportMessageWith( - includeFilterByChatInBehaviourSubContext: Boolean = true, - noinline additionalFilter: SimpleFilter? = null, + noinline initialFilter: SimpleFilter? = null, + noinline subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver -) = flowsUpdatesFilter.expectFlow(bot) { - it.asMessageUpdate() ?.data ?.asPassportMessage() ?.let { message -> - if (message.passportData.data.any { it is T }) { - if (additionalFilter == null || additionalFilter(message)) message else null - } else { - null - } - }.let(::listOfNotNull) -}.subscribeSafelyWithoutExceptionsAsync( - scope, - markerFactory::invoke -) { triggerMessage -> - doInSubContextWithUpdatesFilter( - updatesFilter = if (includeFilterByChatInBehaviourSubContext) { - { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } - } else null, - stopOnCompletion = false - ) { - scenarioReceiver(triggerMessage) - } +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + (it.asMessageUpdate() ?.data ?.asPassportMessage() ?.takeIf { it.passportData.data.any { it is T } }) ?.let(::listOfNotNull) } suspend fun BehaviourContext.onPassportMessage( - includeFilterByChatInBehaviourSubContext: Boolean = true, + includeFilterByChatInBehaviourSubContext: Boolean, additionalFilter: SimpleFilter? = null, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver ) = onPassportMessageWith( - includeFilterByChatInBehaviourSubContext, additionalFilter, + if (includeFilterByChatInBehaviourSubContext) MessageFilterByChat else null, + markerFactory, + scenarioReceiver +) + +suspend fun BehaviourContext.onPassportMessage( + initialFilter: SimpleFilter? = null, + subcontextUpdatesFilter: BehaviourContextAndTwoTypesReceiver? = MessageFilterByChat, + markerFactory: MarkerFactory = ByChatMessageMarkerFactory, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onPassportMessageWith( + initialFilter, + subcontextUpdatesFilter, markerFactory, scenarioReceiver ) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/SimpleFilter.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/SimpleFilter.kt index b7b7ca17e1..8ed5d6ed41 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/SimpleFilter.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/SimpleFilter.kt @@ -1,3 +1,26 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.utils typealias SimpleFilter = suspend (T) -> Boolean + +inline fun SimpleFilter(noinline block: SimpleFilter) = block + +/** + * Represent && operation between [this] and [other] on each invocation + */ +operator fun SimpleFilter.times(other: SimpleFilter): SimpleFilter = { + this(it) && other(it) +} + +/** + * Represent || operation between [this] and [other] on each invocation + */ +operator fun SimpleFilter.plus(other: SimpleFilter): SimpleFilter = { + this(it) || other(it) +} + +/** + * Represent reversing of [this] invocation + */ +operator fun SimpleFilter.not(): SimpleFilter = { + !this(it) +}