From 6873c23309da6c9d268b864c5bfe4994c639f4db Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 19 Aug 2023 16:41:08 +0600 Subject: [PATCH] complete story support --- .../expectations/WaitContent.kt | 4 ++++ .../expectations/WaitContentMessage.kt | 4 ++++ .../triggers_handling/ContentTriggers.kt | 24 +++++++++++++++++++ .../types/message/content/Typealiases.kt | 1 + .../extensions/utils/ClassCastsNew.kt | 10 ++++++++ .../utils/ContentMessageConversations.kt | 1 + .../utils/shortcuts/FlowsUpdatesFilter.kt | 5 ++++ 7 files changed, 49 insertions(+) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt index fa854b6c77..6bafc17182 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt @@ -62,6 +62,10 @@ suspend fun BehaviourContext.waitText( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } ) = waitContent(initRequest, errorFactory).mapContent() +suspend fun BehaviourContext.waitStory( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = waitContent(initRequest, errorFactory).mapContent() suspend fun BehaviourContext.waitVenue( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt index 99b8c27a76..d0e151480a 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt @@ -74,6 +74,10 @@ suspend fun BehaviourContext.waitTextMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } ) = waitContentMessage(initRequest, errorFactory).mapWithContent() +suspend fun BehaviourContext.waitStoryMessage( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = waitContentMessage(initRequest, errorFactory).mapWithContent() suspend fun BehaviourContext.waitVenueMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt index 80901c26bf..a2b1a002b4 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt @@ -248,6 +248,30 @@ suspend fun BC.onText( scenarioReceiver ) +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onStory( + initialFilter: CommonMessageFilter? = CommonMessageFilterExcludeMediaGroups, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver? = MessageFilterByChat, + markerFactory: MarkerFactory = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver +) = onContentMessageWithType( + initialFilter, + subcontextUpdatesFilter, + markerFactory, + scenarioReceiver +) + /** * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt index 7cd305fdff..9e9a865f9f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt @@ -11,6 +11,7 @@ typealias DiceMessage = CommonMessage typealias ContactMessage = CommonMessage typealias PollMessage = CommonMessage typealias TextMessage = CommonMessage +typealias StoryMessage = CommonMessage typealias LocationMessage = CommonMessage typealias LiveLocationMessage = CommonMessage diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt index d8cd7d52f4..e0fcb89557 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt @@ -292,6 +292,7 @@ import dev.inmo.tgbotapi.types.message.content.ResendableContent import dev.inmo.tgbotapi.types.message.content.SpoilerableMediaContent import dev.inmo.tgbotapi.types.message.content.StaticLocationContent import dev.inmo.tgbotapi.types.message.content.StickerContent +import dev.inmo.tgbotapi.types.message.content.StoryContent import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.content.TextedContent import dev.inmo.tgbotapi.types.message.content.TextedMediaContent @@ -3480,6 +3481,15 @@ public inline fun ResendableContent.stickerContentOrThrow(): StickerContent = th public inline fun ResendableContent.ifStickerContent(block: (StickerContent) -> T): T? = stickerContentOrNull() ?.let(block) +public inline fun ResendableContent.storyContentOrNull(): StoryContent? = this as? + dev.inmo.tgbotapi.types.message.content.StoryContent + +public inline fun ResendableContent.storyContentOrThrow(): StoryContent = this as + dev.inmo.tgbotapi.types.message.content.StoryContent + +public inline fun ResendableContent.ifStoryContent(block: (StoryContent) -> T): T? = + storyContentOrNull() ?.let(block) + public inline fun ResendableContent.textContentOrNull(): TextContent? = this as? dev.inmo.tgbotapi.types.message.content.TextContent diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt index e9b370dc5a..5366e98fe7 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt @@ -21,6 +21,7 @@ fun Flow>.onlyPhotoContentMessages() = withContentType>.onlyPollContentMessages() = withContentType() fun Flow>.onlyStickerContentMessages() = withContentType() fun Flow>.onlyTextContentMessages() = withContentType() +fun Flow>.onlyStoryContentMessages() = withContentType() fun Flow>.onlyVenueContentMessages() = withContentType() fun Flow>.onlyVideoContentMessages() = withContentType() fun Flow>.onlyVideoNoteContentMessages() = withContentType() diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt index 91acd5419e..566db560a6 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt @@ -120,6 +120,11 @@ fun FlowsUpdatesFilter.textMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.storyMessages() = filterContentMessages() +fun FlowsUpdatesFilter.storyMessages( + scopeToIncludeChannels: CoroutineScope? = null +) = filterContentMessages(scopeToIncludeChannels) + fun Flow.venueMessages() = filterContentMessages() fun FlowsUpdatesFilter.venueMessages( scopeToIncludeChannels: CoroutineScope? = null