diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d9d5f90e4..1a497e8efc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 7.1.0 +* `BehaviourBuilder`: + * `BehaviourContext` extensions `onDeepLink` and `waitDeepLinks` now can be used with `Regex` or `String` as first parameters + ## 7.0.2 _This update brings experimental support of `linuxX64` and `mingwX64` platforms_ diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitDeepLinks.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitDeepLinks.kt index 129d7e72c1..b494adf0e6 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitDeepLinks.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitDeepLinks.kt @@ -21,3 +21,17 @@ suspend fun BehaviourContext.waitDeepLinks( .flattenCommandsWithParams().mapNotNull { it.first to (it.second.second.singleOrNull() ?.regularTextSourceOrNull() ?.source ?.removePrefix(" ") ?: return@mapNotNull null) } + +suspend fun BehaviourContext.waitDeepLinks( + regex: Regex, + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, +): Flow, String>> = waitDeepLinks(initRequest, errorFactory).filter { + regex.matches(it.second) +} + +suspend fun BehaviourContext.waitDeepLinks( + deepLink: String, + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, +): Flow, String>> = waitDeepLinks(Regex("^$deepLink$"), initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/DeepLinkHandling.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/DeepLinkHandling.kt index b713c2b1c9..62e38cda02 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/DeepLinkHandling.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/DeepLinkHandling.kt @@ -46,3 +46,24 @@ suspend fun BC.onDeepLink( this@onDeepLink.launchSafelyWithoutExceptions { triggersHolder.handleableCommandsHolder.unregisterHandleable(startRegex) } } } + +suspend fun BC.onDeepLink( + regex: Regex, + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update> = { (message, _), update -> MessageFilterByChat(this, message, update) }, + markerFactory: MarkerFactory, Any> = MarkerFactory { (message, _) -> ByChatMessageMarkerFactory(message) }, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +): Job { + val internalFilter = SimpleFilter> { + regex.matches(it.second) + } + return onDeepLink(initialFilter ?.let { internalFilter * it } ?: internalFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) +} + +suspend fun BC.onDeepLink( + deepLink: String, + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update> = { (message, _), update -> MessageFilterByChat(this, message, update) }, + markerFactory: MarkerFactory, Any> = MarkerFactory { (message, _) -> ByChatMessageMarkerFactory(message) }, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +): Job = onDeepLink(Regex("^$deepLink$"), initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)