diff --git a/CHANGELOG.md b/CHANGELOG.md index e5615a4694..62c7cfebec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,13 @@ __All the `tgbotapi.extensions.*` packages have been removed__ * `BehaviourBuilder`: * `SimpleFilter` now is a `fun interface` instead of just callback (fix of [#546](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/546)) +## 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)) + ## 0.38.17 * `Core`: 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 6f7dc422f9..afb5ac8a2a 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 1199669657..7bd59ad4f9 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 fc446fdf36..b49e812220 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 e74804e85a..a0ffdf6d3c 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 @@ -27,5 +27,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 c9ffc38286..1dfe697c08 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 @@ -19,7 +19,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") @@ -28,10 +29,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"