TelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/GetFile.java

74 lines
2.4 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.methods;
2016-01-14 01:14:53 +01:00
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
2020-11-01 23:46:36 +01:00
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
2019-06-08 21:33:28 +02:00
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
2020-11-01 23:46:36 +01:00
import org.telegram.telegrambots.meta.api.objects.File;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
2016-01-14 01:14:53 +01:00
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 1.0
2019-06-08 21:33:28 +02:00
* Use this method to get basic info about a file and prepare it for downloading.
2016-01-14 01:14:53 +01:00
* For the moment, bots can download files of up to 20MB in size.
* On success, a File object is returned.
* The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>,
* where <file_path> is taken from the response.
* It is guaranteed that the link will be valid for at least 1 hour.
* When the link expires, a new one can be requested by calling getFile again.
*/
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
2016-01-14 01:14:53 +01:00
public class GetFile extends BotApiMethod<File> {
public static final String PATH = "getFile";
2016-01-14 01:14:53 +01:00
private static final String FILEID_FIELD = "file_id";
@JsonProperty(FILEID_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2016-01-14 01:14:53 +01:00
private String fileId; ///< File identifier to get info about
@Override
public void validate() throws TelegramApiValidationException {
if (fileId == null) {
throw new TelegramApiValidationException("FileId can't be empty", this);
}
}
2016-01-14 01:14:53 +01:00
@Override
public String getMethod() {
2016-01-14 01:14:53 +01:00
return PATH;
}
@Override
public File deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<File> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<File>>(){});
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error getting file", result);
}
} catch (IOException e) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
2016-01-14 01:14:53 +01:00
}
}
}