TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updatingmessages/EditMessageLiveLocation.java
2020-11-03 01:32:26 +00:00

164 lines
6.8 KiB
Java

package org.telegram.telegrambots.meta.api.methods.updatingmessages;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.IOException;
import java.io.Serializable;
/**
* @author Ruben Bermudez
* @version 1.0
* Use this method to edit live location.
* A location can be edited until its live_period expires or editing is explicitly disabled by a call to
* stopMessageLiveLocation. On success, if the edited message is not an inline message, the edited Message is returned,
* otherwise True is returned.
*/
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EditMessageLiveLocation extends BotApiMethod<Serializable> {
public static final String PATH = "editMessageLiveLocation";
private static final String CHATID_FIELD = "chat_id";
private static final String MESSAGEID_FIELD = "message_id";
private static final String INLINE_MESSAGE_ID_FIELD = "inline_message_id";
private static final String LATITUDE_FIELD = "latitude";
private static final String LONGITUDE_FIELD = "longitude";
private static final String REPLYMARKUP_FIELD = "reply_markup";
private static final String HORIZONTALACCURACY_FIELD = "horizontal_accuracy";
private static final String HEADING_FIELD = "heading";
private static final String APPROACHINGNOTIFICATIONDISTANCE_FIELD = "approaching_notification_distance";
/**
* Required if inline_message_id is not specified. Unique identifier for the chat to send the
* message to (Or username for channels)
*/
@JsonProperty(CHATID_FIELD)
private String chatId;
/**
* Required if inline_message_id is not specified. Unique identifier of the sent message
*/
@JsonProperty(MESSAGEID_FIELD)
private Integer messageId;
/**
* Required if chat_id and message_id are not specified. Identifier of the inline message
*/
@JsonProperty(INLINE_MESSAGE_ID_FIELD)
private String inlineMessageId;
@JsonProperty(LATITUDE_FIELD)
@NonNull
private Double latitude; ///< Latitude of new location
@JsonProperty(LONGITUDE_FIELD)
@NonNull
private Double longitude; ///< Longitude of new location
@JsonProperty(REPLYMARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup; ///< Optional. A JSON-serialized object for an inline keyboard.
/**
* Optional.
* The radius of uncertainty for the location, measured in meters; 0-1500
*/
@JsonProperty(HORIZONTALACCURACY_FIELD)
private Double horizontalAccuracy;
/**
* Optional.
* For live locations, a direction in which the user is moving, in degrees.
* Must be between 1 and 360 if specified.
*/
@JsonProperty(HEADING_FIELD)
private Integer heading;
/**
* Optional.
* For live locations, a maximum distance for proximity alerts about approaching another chat member, in meters.
* Must be between 1 and 100000 if specified.
*/
@JsonProperty(APPROACHINGNOTIFICATIONDISTANCE_FIELD)
private Integer approachingNotificationDistance;
@Override
public String getMethod() {
return PATH;
}
@Override
public Serializable deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Message>>(){});
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error editing message live location", result);
}
} catch (IOException e) {
try {
ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Boolean>>() {
});
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error editing message live location", result);
}
} catch (IOException e2) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
}
}
}
@Override
public void validate() throws TelegramApiValidationException {
if (inlineMessageId == null) {
if (chatId == null) {
throw new TelegramApiValidationException("ChatId parameter can't be empty if inlineMessageId is not present", this);
}
if (messageId == null) {
throw new TelegramApiValidationException("MessageId parameter can't be empty if inlineMessageId is not present", this);
}
} else {
if (chatId != null) {
throw new TelegramApiValidationException("ChatId parameter must be empty if inlineMessageId is provided", this);
}
if (messageId != null) {
throw new TelegramApiValidationException("MessageId parameter must be empty if inlineMessageId is provided", this);
}
}
if (latitude == null) {
throw new TelegramApiValidationException("Latitude parameter can't be empty", this);
}
if (longitude == null) {
throw new TelegramApiValidationException("Longitude parameter can't be empty", this);
}
if (horizontalAccuracy != null && (horizontalAccuracy < 0 || horizontalAccuracy > 1500)) {
throw new TelegramApiValidationException("Horizontal Accuracy parameter must be between 0 and 1500", this);
}
if (heading != null && (heading < 1 || heading > 360)) {
throw new TelegramApiValidationException("Heading Accuracy parameter must be between 0 and 1500", this);
}
if (approachingNotificationDistance != null && (approachingNotificationDistance < 1 || approachingNotificationDistance > 100000)) {
throw new TelegramApiValidationException("Approaching notification distance parameter must be between 0 and 1500", this);
}
if (replyMarkup != null) {
replyMarkup.validate();
}
}
}