rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.68k stars 1.18k forks source link

how to download file #411

Closed kagaricyan closed 6 years ago

kagaricyan commented 6 years ago

i code this: Document document = update.getMessage().getDocument();. but the document doesn't has the methd like 'getFilePath'; then, where i can get file_path to use the method 'downloadFile(String path)' or downloadFile(File file)'?

McPringle commented 6 years ago

It should be similar to download a photo, I think. If your problem is not solved, you may have a look at my chatbot workshop, there is a section about downloading photos and getting the file path: https://github.com/McPringle/chatbot-workshop/blob/master/WORKSHOP.adoc

I will add a separate section about documents later.

pouraghajanOldAcc commented 6 years ago

You should use getFile() method in order to accomplish this.

  1. Import below class import org.telegram.telegrambots.api.methods.GetFile;

  2. Do the following:

    String uploadedFileId = update.getMessage().getDocument().getFileId();
    GetFile uploadedFile = new GetFile();
    uploadedFile.setFileId(uploadedFileId);
    String uploadedFilePath = getFile(uploadedFile).getFilePath();

Having the filePath string, you can save your file using common java file utils. I have used the following method to do so: Add below dependency into your maven pom.xml file:

<dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
       <version>2.5</version>
</dependency>

Import below classes:

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.io.InputStream;

Do the following:

File localFile = new File("localPath/filename.doc");
InputStream is = new URL("https://api.telegram.org/file/bot"+ BotConfig.BOT_TOKEN+"/"+uploadedFilePath).openStream();
FileUtils.copyInputStreamToFile(is, localFile);
imyoric commented 1 year ago

You should use getFile() method in order to accomplish this.

  1. Import below class import org.telegram.telegrambots.api.methods.GetFile;
  2. Do the following:
String uploadedFileId = update.getMessage().getDocument().getFileId();
GetFile uploadedFile = new GetFile();
uploadedFile.setFileId(uploadedFileId);
String uploadedFilePath = getFile(uploadedFile).getFilePath();

Having the filePath string, you can save your file using common java file utils. I have used the following method to do so: Add below dependency into your maven pom.xml file:

<dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
       <version>2.5</version>
</dependency>

Import below classes:

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.io.InputStream;

Do the following:

File localFile = new File("localPath/filename.doc");
InputStream is = new URL("https://api.telegram.org/file/bot"+ BotConfig.BOT_TOKEN+"/"+uploadedFilePath).openStream();
FileUtils.copyInputStreamToFile(is, localFile);

But the photos are downloaded in poor quality. How to improve quality?

bvn13 commented 9 months ago

Just for information. At the moment using this library version 6.8 a file can be downloaded as following (in Kotlin):

fun downloadFileById(fileId: String): String =
        String(downloadFileAsStream(getFilePath(fileId)).readAllBytes())

private fun getFilePath(fileId: String): String {
    val getFile = GetFile.builder()
        .fileId(fileId)
        .build()
    val response = sendApiMethod(getFile)
    return response.filePath
}