【问题标题】:How to send large file with Telegram Bot API?如何使用 Telegram Bot API 发送大文件?
【发布时间】:2019-02-16 16:26:26
【问题描述】:

Telegram bot 发送的文件大小限制为 50MB。

我需要发送大文件。有没有办法解决这个问题?

我知道这个项目https://github.com/pwrtelegram/pwrtelegram,但我无法让它工作。

也许有人已经解决了这样的问题?

有一个选项可以通过 Telegram API 实现文件上传,然后通过 file_id 与 bot 发送。

我使用库 https://github.com/rubenlagus/TelegramBots 用 Ja​​va 编写了一个机器人

更新

为了解决这个问题,我使用了电报 API,它对大文件有 1.5 GB 的限制。

我更喜欢 kotlogram - 具有良好文档的完美库 https://github.com/badoualy/kotlogram

更新 2

我如何使用这个库的示例:

private void uploadToServer(TelegramClient telegramClient, TLInputPeerChannel tlInputPeerChannel, Path pathToFile, int partSize) {
    File file = pathToFile.toFile();
    long fileId = getRandomId();
    int totalParts = Math.toIntExact(file.length() / partSize + 1);
    int filePart = 0;
    int offset = filePart * partSize;
    try (InputStream is = new FileInputStream(file)) {

        byte[] buffer = new byte[partSize];
        int read;
        while ((read = is.read(buffer, offset, partSize)) != -1) {
            TLBytes bytes = new TLBytes(buffer, 0, read);
            TLBool tlBool = telegramClient.uploadSaveBigFilePart(fileId, filePart, totalParts, bytes);
            telegramClient.clearSentMessageList();
            filePart++;
        }
    } catch (Exception e) {
        log.error("Error uploading file to server", e);
    } finally {
        telegramClient.close();
    }
    sendToChannel(telegramClient, tlInputPeerChannel, "FILE_NAME.zip", fileId, totalParts)
}


private void sendToChannel(TelegramClient telegramClient, TLInputPeerChannel tlInputPeerChannel, String name, long fileId, int totalParts) {
    try {
        String mimeType = name.substring(name.indexOf(".") + 1);

        TLVector<TLAbsDocumentAttribute> attributes = new TLVector<>();
        attributes.add(new TLDocumentAttributeFilename(name));

        TLInputFileBig inputFileBig = new TLInputFileBig(fileId, totalParts, name);
        TLInputMediaUploadedDocument document = new TLInputMediaUploadedDocument(inputFileBig, mimeType, attributes, "", null);
        TLAbsUpdates tlAbsUpdates = telegramClient.messagesSendMedia(false, false, false,
                tlInputPeerChannel, null, document, getRandomId(), null);
    } catch (Exception e) {
        log.error("Error sending file by id into channel", e);
    } finally {
        telegramClient.close();
    }
}

TelegramClient telegramClientTLInputPeerChannel tlInputPeerChannel 可以在文档中创建。

不要复制粘贴,根据需要重写。

【问题讨论】:

  • 正如您所说,您可以亲自将文件发送到您的机器人,然后使用他们的文件 ID 尝试将文件发送到特定的聊天 ID(包括人员、组和频道)。
  • 我可以使用 Telegram API github.com/ex3ndr/telegram-api 的项目,但它已经 5 年没有更新了。我稍后会尝试。并且使用 Telegram API 发送文件并不是一个简单的任务,其中上传算法
  • 嗯,我明白了。然后你可以有一个小型下载服务器并将文件放在那里并尝试在 Telegram 中分享它们的链接。

标签: java telegram telegram-bot


【解决方案1】:

使用local Telegram Bot API server,您可以发送具有 2000Mb 文件大小限制的 InputStream,默认为 50Mb。

【讨论】:

    【解决方案2】:

    如果你想通过电报机器人发送文件,你有three options:

    1. InputStream(照片限制10 MB,其他文件限制50 MB
    2. 来自 http url(Telegram 将下载并发送文件。5 MB 照片的最大大小和 20 MB 其他类型的内容。)
    3. 按缓存文件的 file_id 发送缓存文件。(以这种方式发送的文件没有限制

    因此,我建议您预先存储 file_ids 并通过这些 ids 发送文件(api docs 也建议这样做)。

    【讨论】:

    • 通过 file_id 发送需要文件已经在服务器上。
    • @TimPotapov 是的,您将通过电报客户端(手动或以编程方式)将文件发送给机器人,机器人将接收它作为您可以访问 file_id 的消息
    • Telegram 客户端没有文件大小限制(至少它必须是一个巨大的限制)
    • 50MB 视频限制
    • @TimPotapov 你一定是在阅读 3-4 岁以上的东西tech.hindustantimes.com/tech/news/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2016-07-09
    • 1970-01-01
    • 2019-08-23
    • 2015-10-04
    • 2015-09-20
    • 2021-04-01
    相关资源
    最近更新 更多