Closed kagaricyan closed 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.
You should use getFile() method in order to accomplish this.
Import below class
import org.telegram.telegrambots.api.methods.GetFile;
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);
You should use getFile() method in order to accomplish this.
- Import below class
import org.telegram.telegrambots.api.methods.GetFile;
- 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?
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
}
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)'?