diff --git a/CHANGELOG.md b/CHANGELOG.md index ce05d40c82..5b5223e811 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * `Common`: * `Version`: * `MicroUtils`: `0.4.0` -> `0.4.1` +* `Core`: + * `TelegramAPIUrlsKeeper` will fix ending of host url since this version ## 0.30.6 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 1e8770804d..100781d435 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 @@ -2,10 +2,28 @@ package dev.inmo.tgbotapi.utils const val telegramBotAPIDefaultUrl = "https://api.telegram.org" +private inline val String.withoutLastSlash: String + get() { + var correctedUrl = this + while (true) { + val withoutSuffix = correctedUrl.removeSuffix("/") + if (withoutSuffix == correctedUrl) { + return correctedUrl + } + correctedUrl = withoutSuffix + } + } + class TelegramAPIUrlsKeeper( token: String, hostUrl: String = telegramBotAPIDefaultUrl ) { - val commonAPIUrl = "$hostUrl/bot$token" - val fileBaseUrl = "$hostUrl/file/bot$token" + val commonAPIUrl: String + val fileBaseUrl: String + + init { + val correctedHost = hostUrl.withoutLastSlash + commonAPIUrl = "$correctedHost/bot$token" + fileBaseUrl = "$correctedHost/file/bot$token" + } }