如何使用 Telegram Bot API 发送大文件?

问题描述 投票:0回答:4

Telegram 机器人发送的文件大小限制为 50MB。

我需要发送大文件。有什么办法可以解决这个问题吗?

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

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

有一个选项可以通过 Telegram API 实现文件上传,然后使用机器人通过 file_id 发送。

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

更新

为了解决这个问题,我使用 telegram 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 telegramClient
TLInputPeerChannel tlInputPeerChannel
您可以按照文档中的方式进行创建。

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

java telegram telegram-bot
4个回答
9
投票

使用 本地 Telegram Bot API 服务器,您可以发送文件大小限制为 2000Mb 的 InputStream,默认大小为 50Mb。


7
投票

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

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

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


1
投票

使用本地 Bot API 服务器,您可以发送高达 2GB 的大文件。

GitHub 源代码:

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

官方文档

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

您可以按照此链接上的说明将其

build and install
发送到您的服务器https://tdlib.github.io/telegram-bot-api/build.html

基本设置:

  1. https://my.telegram.org/apps
  2. 生成 Telegram 应用程序 ID
  3. 启动服务器
    ./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
  4. 默认地址为 http://127.0.0.1:8081/ 端口为 8081。
  5. 所有官方 API 都适用于此设置。只需将地址更改为 http://127.0.0.1:8081/bot/METHOD_NAME 参考:https://core.telegram.org/bots/api

示例代码:

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

0
投票

您可以创建一个机器人,将提供的文件发送到频道,然后您可以获得消息 ID。 创建一个端点,该端点将接受消息并直接从电报流式传输文件。

这是一个博客,解释了如何做到这一点。 https://www.changeblogger.org/blog/creating-a-telegram-bot-for-large-file-downloads-using-gram-js

© www.soinside.com 2019 - 2024. All rights reserved.