11

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.

bigspawn
  • 1,727
  • 3
  • 16
  • 16
  • As you said you could send the files to your bot personally and then using their file IDs try to send the files to specific chat IDs (including people, groups and channels). – Naser.Sadeghi Sep 12 '18 at 19:35
  • I can use the project with Telegram API https://github.com/ex3ndr/telegram-api but it has not been updated for 5 years. I will try later. And send the file with Telegram API is not such a trivial task with where uploading algorithm – bigspawn Sep 13 '18 at 12:42
  • Hmmm, I see. Then you could have a small download server and put the files there and try to share the link of them in Telegram. – Naser.Sadeghi Sep 13 '18 at 19:53

3 Answers3

9

With local Telegram Bot API server you are allowed to send InputStream with a 2000Mb file size limit, raised from 50Mb default.

Dmitry Verhoturov
  • 5,835
  • 2
  • 32
  • 39
7

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).

valijon
  • 1,304
  • 2
  • 20
  • 35
  • Sending by file_id requires the file to be already on the server. – Tim Potapov Jul 28 '21 at 16:58
  • 1
    @TimPotapov yes, you will send the file to the bot by telegram client(by hand or programmatically) and the bot will receive it as a message where you can access file_id – valijon Jul 29 '21 at 17:28
  • 1
    Telegram client doesn't have file size limit (at least it must be a huge limit) – valijon Jul 29 '21 at 17:30
  • 50MB video limit – Tim Potapov Jul 30 '21 at 18:40
  • 1
    @TimPotapov you must be reading something older than 3-4 years https://tech.hindustantimes.com/tech/news/telegram-now-lets-you-send-2gb-files-trolls-whatsapp-s-16mb-limit-71595828235910.html – valijon Aug 01 '21 at 07:46
  • https://core.telegram.org/bots/api#sending-files search "50 mb" – Tim Potapov Aug 02 '21 at 09:27
  • 1
    @TimPotapov I highly recommend to read difference between telegram client and bot. https://stackoverflow.com/questions/39942771/telegram-api-vs-bot-api As I already explained above you send the file from telegram client to telegram bot. – valijon Aug 03 '21 at 11:20
  • `file_id` is unique to a chat, so you'd have to send the same file with mproto api to every group / user once in order to use it with the bot api – Iuri Guilherme May 31 '23 at 16:07
1

Using a Local Bot API Server you can send a large file up to 2GB.

GitHub Source Code:

https://github.com/tdlib/telegram-bot-api

Official Documentation

https://core.telegram.org/bots/api#using-a-local-bot-api-server

You can build and install this to your server by following the instructions on this link https://tdlib.github.io/telegram-bot-api/build.html

basic setup :

  1. Generate Telegram Applications id from https://my.telegram.org/apps
  2. Start the server ./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
  3. Default address is http://127.0.0.1:8081/ and the port is 8081.
  4. All the official APIs will work with this setup. Just change the address to http://127.0.0.1:8081/bot/METHOD_NAME reference: https://core.telegram.org/bots/api

Example Code:

    OkHttpClient client = new OkHttpClient().newBuilder()
      .build();
    MediaType mediaType = MediaType.parse("text/plain");
    RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
      .addFormDataPart("chat_id","your_chat_id_here")
      .addFormDataPart("video","file_location",
        RequestBody.create(MediaType.parse("application/octet-stream"),
        new File("file_location")))
      .addFormDataPart("supports_streaming","true")
      .build();
    // https://127.0.0.1:8081/bot<token>/METHOD_NAME 
    Request request = new Request.Builder()
      .url("http://127.0.0.1:8081/bot<token>/sendVideo")
      .method("POST", body)
      .build();
    Response response = client.newCall(request).execute();
manukn
  • 2,608
  • 1
  • 13
  • 17