From 1bc9ee4d5a23866ba1fb89d8316ca3d9252dcc8e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 27 Jul 2025 15:06:47 +0600 Subject: [PATCH] add TelegramAPIUrlsKeeper#fileLinkUrlMapper and fill readme Bot API Server Notice --- README.md | 17 +++++++++++++++++ tgbotapi.core/api/tgbotapi.core.api | 4 ++-- .../tgbotapi/utils/TelegramAPIUrlsKeeper.kt | 5 +++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1b146b9fb9..3b569135cc 100644 --- a/README.md +++ b/README.md @@ -120,3 +120,20 @@ suspend fun main() { You may find examples in [this project](https://github.com/InsanusMokrassar/TelegramBotAPI-examples). Besides, you are always welcome in our [docs](https://docs.inmo.dev/tgbotapi/index.html) and [chat](https://t.me/InMoTelegramBotAPIChat). + +### Bot API Server Notice + +Under the hood, default bots realizations will try to use links +([PathedFile](tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt)#filePath) as files each +time you are trying to download file from telegram in any way - via saving to file, use stream or download as byte array. +To let bot correctly download files from bot api server, you must: + +* Run bot api server locally +to proxy requests for files to the server where bot api server has been hosted + * In case of local bot api server (shared one host machine) you must ensure that access to bot api server has been + granted for your bot. For example, [aiogram/telegram-bot-api](https://hub.docker.com/r/aiogram/telegram-bot-api) image + use `101` UID/GID in linux for user and group as owners. So, your bot must run under user included in `101` group + (like `systemd-journal`) or be `101` UID user (like `systemd-resolve`) +* **OR** Use some reverse proxy (like nginx). It will allow you to broadcast your bots files without linux rights problems + * Set [TelegramAPIUrlsKeeper](tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper.kt)#fileLinkUrlMapper + to map urls to let bot execute requests to your nginx proxy \ No newline at end of file diff --git a/tgbotapi.core/api/tgbotapi.core.api b/tgbotapi.core/api/tgbotapi.core.api index 493cc8a123..5b4df7f184 100644 --- a/tgbotapi.core/api/tgbotapi.core.api +++ b/tgbotapi.core/api/tgbotapi.core.api @@ -30832,8 +30832,8 @@ public final class dev/inmo/tgbotapi/utils/StringFileExtensionKt { } public final class dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper { - public fun (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V - public synthetic fun (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;)V + public synthetic fun (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Ljava/lang/String;ZLjava/lang/String;)V public synthetic fun (Ljava/lang/String;ZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun checkWebAppData (Ljava/lang/String;Ljava/lang/String;)Z 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 c0ffcf536c..a74d94a870 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 @@ -21,7 +21,8 @@ private inline val String.withoutLastSlash: String class TelegramAPIUrlsKeeper( token: String, hostUrl: String = telegramBotAPIDefaultUrl, - urlsSuffixes: String = "" + urlsSuffixes: String = "", + private val fileLinkUrlMapper: TelegramAPIUrlsKeeper.(String) -> String = { "${fileBaseUrl}/$it" } ) { val webAppDataSecretKeyHash by lazy { HMAC.hmacSHA256("WebAppData".toByteArray(), token.toByteArray()) @@ -44,7 +45,7 @@ class TelegramAPIUrlsKeeper( fileBaseUrl = "$correctedHost/file/bot$token$urlsSuffixes" } - fun createFileLinkUrl(filePath: String) = "${fileBaseUrl}/$filePath" + fun createFileLinkUrl(filePath: String) = fileLinkUrlMapper(filePath) /** * @param rawData Data from [dev.inmo.tgbotapi.webapps.WebApp.initData]