TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/updatingmessages/DeleteMessage.java

102 lines
3.3 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.methods.updatingmessages;
2017-05-03 01:40:55 +02: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;
2022-06-14 22:20:22 +02:00
import lombok.experimental.Tolerate;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
2019-06-08 21:33:28 +02:00
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
2017-05-03 01:40:55 +02:00
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 1.0
*
2019-04-08 02:43:46 +02:00
* Use this method to delete a message, including service messages, with the following limitations:
* - A message can only be deleted if it was sent less than 48 hours ago.
* - Bots can delete outgoing messages in private chats, groups, and supergroups.
* - Bots can delete incoming messages in private chats.
* - Bots granted can_post_messages permissions can delete outgoing messages in channels.
* - If the bot is an administrator of a group, it can delete any message there.
* - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
* Returns True on success.
2017-05-03 01:40:55 +02:00
*/
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
2017-05-03 01:40:55 +02:00
public class DeleteMessage extends BotApiMethod<Boolean> {
public static final String PATH = "deleteMessage";
private static final String CHATID_FIELD = "chat_id";
private static final String MESSAGEID_FIELD = "message_id";
/**
* Unique identifier for the chat to send the message to (Or username for channels)
*/
@JsonProperty(CHATID_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2017-05-03 01:40:55 +02:00
private String chatId;
/**
* Identifier of the message to delete
*/
@JsonProperty(MESSAGEID_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2017-05-03 01:40:55 +02:00
private Integer messageId;
2022-06-14 22:20:22 +02:00
@Tolerate
public void setChatId(@NonNull Long chatId) {
this.chatId = chatId.toString();
}
2017-05-03 01:40:55 +02:00
@Override
public String getMethod() {
return PATH;
}
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Boolean>>() {
});
if (result.getOk()) {
return result.getResult();
} else {
2017-11-15 18:39:27 +01:00
throw new TelegramApiRequestException("Error deleting message", result);
2017-05-03 01:40:55 +02:00
}
} catch (IOException e) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
}
}
@Override
public void validate() throws TelegramApiValidationException {
2022-06-14 22:20:22 +02:00
if (chatId.isEmpty()) {
2017-05-03 01:40:55 +02:00
throw new TelegramApiValidationException("ChatId parameter can't be empty", this);
}
2022-06-14 22:20:22 +02:00
}
public static class DeleteMessageBuilder {
@Tolerate
public DeleteMessageBuilder chatId(@NonNull Long chatId) {
this.chatId = chatId.toString();
return this;
2017-05-03 01:40:55 +02:00
}
}
}