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

96 lines
3.2 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.methods.send;
2016-01-14 01:14:53 +01:00
import com.fasterxml.jackson.annotation.JsonIgnore;
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;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.methods.ActionType;
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;
2016-01-14 01:14:53 +01:00
import java.io.IOException;
2017-05-03 01:40:55 +02:00
2016-01-14 01:14:53 +01:00
/**
* @author Ruben Bermudez
* @version 1.0
2017-05-03 01:40:55 +02:00
* Use this method when you need to tell the user that something is happening on the bot's
2016-04-11 02:53:53 +02:00
* side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram
* clients clear its typing status).
2016-01-14 01:14:53 +01:00
*/
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
2016-04-11 02:53:53 +02:00
public class SendChatAction extends BotApiMethod<Boolean> {
2016-01-14 01:14:53 +01:00
public static final String PATH = "sendChatAction";
public static final String CHATID_FIELD = "chat_id";
2019-06-08 21:33:28 +02:00
private static final String ACTION_FIELD = "action";
@JsonProperty(CHATID_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2016-04-02 18:20:49 +02:00
private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels)
2016-01-14 01:14:53 +01:00
/**
2017-05-03 01:40:55 +02:00
* Type of action to broadcast. Choose one, depending on what the user is about to receive: typing for text messages,
* upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files,
* upload_document for general files, find_location for location data,
* record_video_note or upload_video_note for video notes.
2016-01-14 01:14:53 +01:00
*/
@JsonProperty(ACTION_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
private String action;
@JsonIgnore
2020-11-01 23:46:36 +01:00
public ActionType getActionType() {
return ActionType.get(action);
}
@JsonIgnore
2020-11-01 23:46:36 +01:00
public void setAction(ActionType action) {
this.action = action.toString();
2016-06-20 21:33:14 +02:00
}
2016-01-14 01:14:53 +01:00
@Override
public String getMethod() {
2016-01-14 01:14:53 +01:00
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 {
throw new TelegramApiRequestException("Error sending chat action", result);
}
} catch (IOException e) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
2016-01-14 01:14:53 +01:00
}
}
@Override
public void validate() throws TelegramApiValidationException {
if (chatId == null) {
throw new TelegramApiValidationException("ChatId parameter can't be empty", this);
}
if (action == null) {
throw new TelegramApiValidationException("Action parameter can't be empty", this);
}
}
2016-01-14 01:14:53 +01:00
}