1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-02 22:59:48 +00:00

add TelegramAPIUrlsKeeper#fileLinkUrlMapper and fill readme Bot API Server Notice

This commit is contained in:
2025-07-27 15:06:47 +06:00
parent 2f4de37489
commit 1bc9ee4d5a
3 changed files with 22 additions and 4 deletions

View File

@@ -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

View File

@@ -30832,8 +30832,8 @@ public final class dev/inmo/tgbotapi/utils/StringFileExtensionKt {
}
public final class dev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;ZLjava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;ZLjava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun checkWebAppData (Ljava/lang/String;Ljava/lang/String;)Z

View File

@@ -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]