How to send large file with Telegram Bot API?

丶灬走出姿态 提交于 2021-02-07 02:58:57

问题


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

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

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?

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

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

UPDATE

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

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

UPDATE 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();
    }
}

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

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


回答1:


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

  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)

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



来源:https://stackoverflow.com/questions/52288231/how-to-send-large-file-with-telegram-bot-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!