如何使用 Telegram Bot API 发送大文件? [英] How to send large file with Telegram Bot API?

查看:215
本文介绍了如何使用 Telegram Bot API 发送大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Telegram bot 的文件大小限制为 50MB.

Telegram bot has a file size limit for sending in 50MB.

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

I need to send large files. Is there any way around this?

我知道这个项目 https://github.com/pwrtelegram/pwrtelegram 但我不能让它发挥作用.

I know about this project https://github.com/pwrtelegram/pwrtelegram but I couldn't make it work.

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

Maybe someone has already solved such a problem?

可以选择通过 Telegram API 实现文件上传,然后通过 file_id 与 bot 发送.

There is an option to implement the file upload via Telegram API and then send by file_id with bot.

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

I write a bot in Java using the library https://github.com/rubenlagus/TelegramBots

更新

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

For solve this problem i use telegram api, that has limit on 1.5 GB for big files.

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

I prefer kotlogram - the perfect lib with good documentation https://github.com/badoualy/kotlogram

更新 2

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

Example of something how i use this lib:

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 您可以在文档中创建.

where TelegramClient telegramClient and TLInputPeerChannel tlInputPeerChannel you can create as write in documentation.

不要复制粘贴,根据您的需要重写.

DON'T COPY-PASTE, rewrite on your needs.

推荐答案

如果你想通过电报机器人发送文件,你有 三个选项:

IF you want to send file via telegram bot, you have three options:

  1. InputStream(照片限制为 10 MB,其他文件限制为 50 MB)
  2. 来自http url(Telegram 将下载并发送文件.照片最大5 MB,其他类型20 MB最大内容.)
  3. file_ids 发送缓存文件.(以这种方式发送的文件没有限制)
  1. InputStream (10 MB limit for photos, 50 MB for other files)
  2. From http url (Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.)
  3. Send cached files by their file_ids.(There are no limits for files sent this way)

因此,我建议您预先存储 file_ids 并通过这些 id 发送文件(这是由 api 文档).

So, I recommend you to store file_ids beforehand and send files by these ids (this is recommended by api docs too).

这篇关于如何使用 Telegram Bot API 发送大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆