savePhotoFile
Introduction
In this lesson, we will learn how to save photos and files to the local directory
If you didn't open the Lesson 1, don't forget to make a Bot class with a token and name of the bot, as well as connect libraries.
This lesson will present the simplest possible solution to the problem for the simplicity of the lesson. For better code, these methods would be worth rewriting
Process
The main function remains unchanged, so just copy it in previous lessons
Let's look at the HandlerToLessonFive class
We still need to Override onUpdateReceived method
For the sake of simplicity of the lesson, we will not make any commands. To save the sent document, the user will only need to send it to the chat
Before we started writing methods for saving, we will do a simple processing of the received update
public class HandleToLessonFive extends TelegramLongPollingBot {
@Override
public String getBotUsername() {
return Bot.USERNAME;
}
@Override
public String getBotToken() {
return Bot.TOKEN;
}
@Override
public void onUpdateReceived(Update update) {
// Initialize sendMessage
SendMessage sendMessage = new SendMessage();
// If update has document
if (update.getMessage().hasDocument()) {
// Save file method
saveFile(update, sendMessage);
// If update has image
} else if (update.hasMessage() && update.getMessage().hasPhoto()) {
// Save photo method
savePhoto(update, sendMessage);
}
}
}If there is a document in the update, method saveFile will be called, if there is a photo, then method savePhoto
Let's start writing the methods themselves
Let's start by saving the file
Sending a message here is optional, it only serves to notify about a successful save
First we use class Document to create the document itself, then class GetFile to get this document
Saving a photo differs only by the type of object being created
First we create a List of photos, then we get the largest photo from this list, then we get the file id of this photo. Then the same saving procedure is performed as in the previous method
This completes the execution of the bot to save the sent photos or documents!
You can find all the code here
Sending documents

Document in the local directory

Back to Lesson 4 of sending music from the local directory
Last updated