gpt4 book ai didi

kotlin - 使用Ktor将文件上传到Telegram Bot API

转载 作者:行者123 更新时间:2023-12-02 12:53:46 29 4
gpt4 key购买 nike

我正在使用kotlinktor为telegram-bot-api写包装器。
我有一个问题-无法找到上传文件的有效方法。

(来自tg bot api api docs)
有三种发送文件的方法(照片,贴纸,音频,媒体等):

  • 如果文件已经存储在Telegram服务器上的某个位置,则无需重新上传它:每个文件对象都有一个file_id字段,只需将此file_id作为参数传递而不是上传。以这种方式发送的文件没有限制。
  • 为电报提供要发送文件的HTTP URL。电报将下载并发送文件。照片的最大大小为5 MB,其他类型的内容的最大大小为20 MB。
  • 以multipart / form-data的方式发布文件,使用的方式通常是通过浏览器上传文件。照片最大大小为10 MB,其他文件最大为50 MB。

  • 通过1和2方式,我没有任何问题。

    现在,我有一个丑陋的函数,它向tg请求并解析答案:
    internal suspend inline fun <reified T> makeRequest(token: String, method: TelegramMethod, vararg params: Pair<String, Any?>, files: Map<String, String> = emptyMap()): T {
    try {
    val data: List<PartData> = formData {
    files.forEach { key, fileName ->
    append(key, Files.newInputStream(Paths.get(fileName)).asInput())
    }
    }
    val response = client.submitFormWithBinaryData<HttpResponse>(data) {
    this.method = HttpMethod.Post
    url {
    protocol = URLProtocol("https", 42)
    host = API_HOST
    encodedPath = API_PATH_PATTERN.format(token, method.methodName)
    params.forEach { (name, value) ->
    if (value != null) { this.parameters[name] = value as String }
    }
    }
    }
    val result = response.receive<String>()
    return parseTelegramAnswer<T>(response, result)
    } catch (e: BadResponseStatusException) {
    val answer = mapper.readValue<TResult<T>>(e.response.content.readUTF8Line()!!)
    throw checkTelegramError(e.response.status, answer)
    }
    }

    没有文件,它可以工作,而有文件,则行不通。 (我认为我做错了一切)

    用法示例:
    suspend fun getUpdates(offset: Long? = null, limit: Int? = null, timeout: Int? = null, allowedUpdates: List<String>? = null): List<Update> =
    api.makeRequest(
    token,
    TelegramMethod.getUpdates,
    "offset" to offset?.toString(),
    "limit" to limit?.toString(),
    "timeout" to timeout?.toString(),
    "allowed_updates" to allowedUpdates
    )

    我已经在不同的文件上进行了测试,发现:
  • (如果我在17,9 KiB56,6 KiB之间发送文件,则我从tg获取以下错误:Bad Request: wrong URL host
  • 如果我在75,6 KiB913,2 KiB之间发送文件,则会收到错误413 Request Entity Too Large

  • *我正在使用 sendDocument方法

    使用 ktor发送文件的正确方法是什么?

    最佳答案

    好的,我终于找到答案了。修复了makeRequest函数:

    internal suspend inline fun <reified T> makeRequest(token: String, method: TelegramMethod, vararg params: Pair<String, Any?>): T {
    try {
    val response = client.submitForm<HttpResponse> {
    this.method = HttpMethod.Post
    url {
    protocol = URLProtocol.HTTPS
    host = API_HOST
    encodedPath = API_PATH_PATTERN.format(token, method.methodName)
    }
    body = MultiPartFormDataContent(
    formData {
    params.forEach { (key, value) ->
    when (value) {
    null -> {}
    is MultipartFile -> append(
    key,
    value.file.inputStream().asInput(),
    Headers.build {
    append(HttpHeaders.ContentType, value.mimeType)
    append(HttpHeaders.ContentDisposition, "filename=${value.filename}")
    }
    )
    is FileId -> append(key, value.fileId)
    else -> append(key, value.toString())
    }
    }
    }
    )
    }
    val result = response.receive<String>()
    val r = parseTelegramAnswer<T>(response, result)
    return r
    } catch (e: BadResponseStatusException) {
    val answer = mapper.readValue<TResult<T>>(e.response.content.readUTF8Line()!!)
    throw checkTelegramError(e.response.status, answer)
    }
    }

    关于kotlin - 使用Ktor将文件上传到Telegram Bot API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53639980/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com