From e029b29f7f1b0d27bd5d577eeb65a39d4f9b0376 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 26 Aug 2022 12:20:20 +0600 Subject: [PATCH] fixes --- .../behaviour_builder/expectations/WaitDeepLinks.kt | 2 +- .../triggers_handling/DeepLinkHandling.kt | 9 ++++++++- .../extensions/utils/formatting/LinksFormatting.kt | 7 ++++--- 3 files changed, 13 insertions(+), 5 deletions(-) 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 51aa6f3228..129d7e72c1 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 @@ -19,5 +19,5 @@ suspend fun BehaviourContext.waitDeepLinks( .requireSingleCommand() .requireCommandAtStart() .flattenCommandsWithParams().mapNotNull { - it.first to (it.second.second.singleOrNull() ?.regularTextSourceOrNull() ?.source ?: return@mapNotNull null) + it.first to (it.second.second.singleOrNull() ?.regularTextSourceOrNull() ?.source ?.removePrefix(" ") ?: return@mapNotNull null) } 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 00029132c6..cf85ec58f9 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 @@ -17,9 +17,11 @@ import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.content.TextMessage import dev.inmo.tgbotapi.types.message.textsources.RegularTextSource import dev.inmo.tgbotapi.types.update.abstracts.Update +import io.ktor.http.decodeURLQueryComponent import kotlinx.coroutines.Job import kotlinx.coroutines.flow.filter +private val startRegex = Regex("start") suspend fun BC.onDeepLink( initialFilter: SimpleFilter>? = null, subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update> = { (message, _), update -> MessageFilterByChat(this, message, update) }, @@ -36,6 +38,11 @@ suspend fun BC.onDeepLink( scenarioReceiver, ) { (it.messageUpdateOrNull()) ?.data ?.commonMessageOrNull() ?.withContentOrNull() ?.let { message -> - message to message.content.textSources[1].source + message to message.content.textSources[1].source.removePrefix(" ").decodeURLQueryComponent() } ?.let(::listOfNotNull) +}.also { + triggersHolder.handleableCommandsHolder.registerHandleable(startRegex) + it.invokeOnCompletion { + this@onDeepLink.launchSafelyWithoutExceptions { triggersHolder.handleableCommandsHolder.unregisterHandleable(startRegex) } + } } diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/LinksFormatting.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/LinksFormatting.kt index 21ec91f1fc..3e16e1f192 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/LinksFormatting.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/formatting/LinksFormatting.kt @@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.textsources.link +import io.ktor.http.encodeURLQueryComponent fun makeUsernameLink(username: String) = "$internalLinkBeginning/$username" @@ -17,11 +18,11 @@ inline val Username.deepLinkPrefix inline val Username.startattachPrefix get() = makeUsernameStartattachPrefix(usernameWithoutAt) inline fun makeLink(username: Username) = username.link -inline fun makeTelegramDeepLink(username: String, startParameter: String) = "${makeUsernameDeepLinkPrefix(username)}$startParameter" +inline fun makeTelegramDeepLink(username: String, startParameter: String) = "${makeUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent() inline fun makeTelegramStartattach(username: String, data: String? = null) = makeUsernameStartattachLink(username, data) -inline fun makeDeepLink(username: Username, startParameter: String) = "${username.deepLinkPrefix}$startParameter" +inline fun makeDeepLink(username: Username, startParameter: String) = makeTelegramDeepLink(username.usernameWithoutAt, startParameter) inline fun makeTelegramDeepLink(username: Username, startParameter: String) = makeDeepLink(username, startParameter) -inline fun makeTelegramStartattach(username: Username, data: String? = null) = makeTelegramStartattach(username.username, data) +inline fun makeTelegramStartattach(username: Username, data: String? = null) = makeTelegramStartattach(username.usernameWithoutAt, data) fun makeLinkToMessage( username: String,