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
您可以按照文档中的方式进行创建。
不要复制粘贴,根据您的需要重写。
使用 本地 Telegram Bot API 服务器,您可以发送文件大小限制为 2000Mb 的 InputStream,默认大小为 50Mb。
使用本地 Bot API 服务器,您可以发送高达 2GB 的大文件。
GitHub 源代码:
官方文档
https://core.telegram.org/bots/api#using-a-local-bot-api-server
您可以按照此链接上的说明将其
build and install
发送到您的服务器https://tdlib.github.io/telegram-bot-api/build.html
基本设置:
./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
示例代码:
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();
您可以创建一个机器人,将提供的文件发送到频道,然后您可以获得消息 ID。 创建一个端点,该端点将接受消息并直接从电报流式传输文件。
这是一个博客,解释了如何做到这一点。 https://www.changeblogger.org/blog/creating-a-telegram-bot-for-large-file-downloads-using-gram-js