【发布时间】:2019-02-16 16:26:26
【问题描述】:
Telegram bot 发送的文件大小限制为 50MB。
我需要发送大文件。有没有办法解决这个问题?
我知道这个项目https://github.com/pwrtelegram/pwrtelegram,但我无法让它工作。
也许有人已经解决了这样的问题?
有一个选项可以通过 Telegram API 实现文件上传,然后通过 file_id 与 bot 发送。
我使用库 https://github.com/rubenlagus/TelegramBots 用 Java 编写了一个机器人
更新
为了解决这个问题,我使用了电报 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 可以在文档中创建。
不要复制粘贴,根据需要重写。
【问题讨论】:
-
正如您所说,您可以亲自将文件发送到您的机器人,然后使用他们的文件 ID 尝试将文件发送到特定的聊天 ID(包括人员、组和频道)。
-
我可以使用 Telegram API github.com/ex3ndr/telegram-api 的项目,但它已经 5 年没有更新了。我稍后会尝试。并且使用 Telegram API 发送文件并不是一个简单的任务,其中上传算法
-
嗯,我明白了。然后你可以有一个小型下载服务器并将文件放在那里并尝试在 Telegram 中分享它们的链接。
标签: java telegram telegram-bot