From 33fb75a5eb4e6c7e61c3412e233b03ebf6b63cdc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 May 2022 12:28:39 +0600 Subject: [PATCH 1/5] start 0.38.18 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ee4d9ba98..b6adba88b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.38.18 + ## 0.38.17 * `Core`: diff --git a/gradle.properties b/gradle.properties index 6d60aca2f8..876f4856d8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,6 +20,6 @@ javax_activation_version=1.1.1 dokka_version=1.6.10 library_group=dev.inmo -library_version=0.38.17 +library_version=0.38.18 github_release_plugin_version=2.3.7 From 619c82bb32f08342003445883112d5bf5f541d32 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 2 May 2022 01:16:24 +0300 Subject: [PATCH 2/5] fix: new BehaviourContext ignored --- .../tgbotapi/extensions/behaviour_builder/BehaviourContext.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index 2426853a51..ccc6ccbbd5 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -128,7 +128,7 @@ suspend fun BC.doInSubContextWithUpdatesFilter( upstreamUpdatesFlow = updatesUpstreamFlow ).run { withContext(coroutineContext) { - behaviourContextReceiver().also { if (stopOnCompletion) stop() } + (this@run as BC).behaviourContextReceiver().also { if (stopOnCompletion) stop() } } } From 484e09374d7125ef3a4657ed16bd62ff10bb998a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 May 2022 08:49:58 +0600 Subject: [PATCH 3/5] small refactor --- .../behaviour_builder/BehaviourContext.kt | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index ccc6ccbbd5..a5d9a0e73d 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -90,7 +90,7 @@ class DefaultBehaviourContext( onBufferOverflow: BufferOverflow, upstreamUpdatesFlow: Flow?, updatesFilter: BehaviourContextAndTypeReceiver? - ): BehaviourContext = DefaultBehaviourContext(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter) + ): DefaultBehaviourContext = DefaultBehaviourContext(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter) } fun BehaviourContext( @@ -116,19 +116,20 @@ suspend fun BC.doInSubContextWithUpdatesFilter( updatesUpstreamFlow: Flow = allUpdatesFlow, scope: CoroutineScope = LinkedSupervisorScope(), behaviourContextReceiver: CustomBehaviourContextReceiver -): T = copy( - scope = scope, - updatesFilter = updatesFilter ?.let { _ -> - { - (this as? BC) ?.run { - updatesFilter(it) - } ?: true - } - }, - upstreamUpdatesFlow = updatesUpstreamFlow -).run { - withContext(coroutineContext) { - (this@run as BC).behaviourContextReceiver().also { if (stopOnCompletion) stop() } +): T { + val newContext = copy( + scope = scope, + updatesFilter = updatesFilter ?.let { _ -> + { + (this as? BC) ?.run { + updatesFilter(it) + } ?: true + } + }, + upstreamUpdatesFlow = updatesUpstreamFlow + ) as BC + return withContext(currentCoroutineContext()) { + newContext.behaviourContextReceiver().also { if (stopOnCompletion) newContext.stop() } } } From 8e7f7a03c8bc410ecad84716aea391a4157b8172 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 May 2022 12:44:20 +0600 Subject: [PATCH 4/5] note about xzima help --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6adba88b6..91bd7b183d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.38.18 +* `BehaviourBuilder`: + * Fixes in extension `BehaviourContext#doInSubContextWithUpdatesFilter` (thanks to [xzima](https://github.com/xzima)) + ## 0.38.17 * `Core`: From 8c9cd9df67dfc5f7b276e17fa959762fb6e831c5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 May 2022 13:21:43 +0600 Subject: [PATCH 5/5] fix of #577 --- CHANGELOG.md | 2 ++ .../dev/inmo/tgbotapi/extensions/api/BotBuilder.kt | 3 ++- .../inmo/tgbotapi/extensions/api/BotExtensions.kt | 12 ++++++++---- .../behaviour_builder/TelegramBotWithFSM.kt | 4 ++++ .../extensions/behaviour_builder/TelegramBot.kt | 4 ++++ .../inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt | 3 ++- .../inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt | 13 ++++++++++--- 7 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91bd7b183d..9f05e77f22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.38.18 +* `Core`: + * Add support of test servers (fix of [#577](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/577)) * `BehaviourBuilder`: * Fixes in extension `BehaviourContext#doInSubContextWithUpdatesFilter` (thanks to [xzima](https://github.com/xzima)) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotBuilder.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotBuilder.kt index ba33c8425f..b83b9a9f01 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotBuilder.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotBuilder.kt @@ -40,8 +40,9 @@ data class BotBuilder internal constructor( fun buildBot( token: String, apiUrl: String = telegramBotAPIDefaultUrl, + testServer: Boolean = false, block: BotBuilder.() -> Unit ) = telegramBot( - TelegramAPIUrlsKeeper(token, apiUrl), + TelegramAPIUrlsKeeper(token, testServer, apiUrl), BotBuilder().apply(block).createHttpClient() ) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt index 4390ab619b..d47d8d85af 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotExtensions.kt @@ -66,17 +66,19 @@ inline fun telegramBot( inline fun telegramBot( token: String, apiUrl: String = telegramBotAPIDefaultUrl, + testServer: Boolean = false, client: HttpClient = HttpClient() -): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), client) +): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, testServer, apiUrl), client) @Suppress("NOTHING_TO_INLINE") inline fun telegramBot( token: String, clientFactory: HttpClientEngineFactory, apiUrl: String = telegramBotAPIDefaultUrl, + testServer: Boolean = false, noinline clientConfig: HttpClientConfig.() -> Unit = {} ) = telegramBot( - TelegramAPIUrlsKeeper(token, apiUrl), + TelegramAPIUrlsKeeper(token, testServer, apiUrl), clientFactory, clientConfig ) @@ -90,9 +92,10 @@ inline fun telegramBot( token: String, clientEngine: HttpClientEngine, apiUrl: String = telegramBotAPIDefaultUrl, + testServer: Boolean = false, noinline clientConfig: HttpClientConfig<*>.() -> Unit = {} ) = telegramBot( - TelegramAPIUrlsKeeper(token, apiUrl), + TelegramAPIUrlsKeeper(token, testServer, apiUrl), clientEngine, clientConfig ) @@ -105,8 +108,9 @@ inline fun telegramBot( inline fun telegramBot( token: String, apiUrl: String = telegramBotAPIDefaultUrl, + testServer: Boolean = false, noinline clientConfig: HttpClientConfig<*>.() -> Unit ) = telegramBot( - TelegramAPIUrlsKeeper(token, apiUrl), + TelegramAPIUrlsKeeper(token, testServer, apiUrl), clientConfig ) diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt index 0c5b881580..edfc67de48 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt @@ -38,10 +38,12 @@ suspend fun telegramBotWithBehaviourAndFSM( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), + testServer: Boolean = false, block: CustomBehaviourContextReceiver, Unit> ): TelegramBot = telegramBot( token, apiUrl, + testServer, builder ).apply { buildBehaviourWithFSMAndStartLongPolling( @@ -73,11 +75,13 @@ suspend fun telegramBotWithBehaviourAndFSMAndStartLongPolling( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), + testServer: Boolean = false, block: CustomBehaviourContextReceiver, Unit> ): Pair { return telegramBot( token, apiUrl, + testServer, builder ).let { it to it.buildBehaviourWithFSMAndStartLongPolling ( diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt index 6ab989cd08..ddc0bdb704 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt @@ -30,10 +30,12 @@ suspend fun telegramBotWithBehaviour( apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder.() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler? = null, + testServer: Boolean = false, block: BehaviourContextReceiver ): TelegramBot = telegramBot( token, apiUrl, + testServer, builder ).apply { buildBehaviour( @@ -63,11 +65,13 @@ suspend fun telegramBotWithBehaviourAndLongPolling( apiUrl: String = telegramBotAPIDefaultUrl, builder: KtorRequestsExecutorBuilder.() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler? = null, + testServer: Boolean = false, block: BehaviourContextReceiver ): Pair { return telegramBot( token, apiUrl, + testServer, builder ).let { it to it.buildBehaviourWithLongPolling( diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt index 1e7da06255..dda3e97fde 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/KtorRequestsExecutor.kt @@ -125,5 +125,6 @@ inline fun telegramBot( inline fun telegramBot( token: String, apiUrl: String = telegramBotAPIDefaultUrl, + testServer: Boolean = false, builder: KtorRequestsExecutorBuilder.() -> Unit = {} -): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), builder) +): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, testServer, apiUrl), builder) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt index 649a8ccc2b..9960a89f02 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt @@ -16,7 +16,8 @@ private inline val String.withoutLastSlash: String class TelegramAPIUrlsKeeper( token: String, - hostUrl: String = telegramBotAPIDefaultUrl + hostUrl: String = telegramBotAPIDefaultUrl, + urlsSuffixes: String = "" ) { val webAppDataSecretKey by lazy { token.hmacSha256("WebAppData") @@ -25,10 +26,16 @@ class TelegramAPIUrlsKeeper( val commonAPIUrl: String val fileBaseUrl: String + constructor(token: String, testServer: Boolean, hostUrl: String = telegramBotAPIDefaultUrl) : this( + token, + hostUrl, + "/test".takeIf { testServer } ?: "" + ) + init { val correctedHost = hostUrl.withoutLastSlash - commonAPIUrl = "$correctedHost/bot$token" - fileBaseUrl = "$correctedHost/file/bot$token" + commonAPIUrl = "$correctedHost/bot$token$urlsSuffixes" + fileBaseUrl = "$correctedHost/file/bot$token$urlsSuffixes" } fun createFileLinkUrl(filePath: String) = "${fileBaseUrl}/$filePath"