diff --git a/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java b/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java new file mode 100644 index 00000000..fb107abe --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/methods/ActionType.java @@ -0,0 +1,57 @@ +package org.telegram.telegrambots.api.methods; + +/** + * @author Ruben Bermudez + * @version 1.0 + * @brief Types of actions for SendChatAction method. + * @date 20 of June of 2016 + */ +public enum ActionType { + TYPING("typing"), + RECORDVIDEO("record_video"), + RECORDAUDIO("record_audio"), + UPLOADPHOTO("upload_photo"), + UPLOADVIDEO("upload_video"), + UPLOADAUDIO("upload_audio"), + UPLOADDOCUMENT("upload_document"), + FINDLOCATION("find_location"); + + private String text; + + ActionType(String text) { + this.text = text; + } + + /** + * @deprecated Added for backward compatibility, will be dropped in next mayor release + * @param text text of the action + * @return ActionType + */ + @Deprecated + public static ActionType GetActionType(String text) throws IllegalArgumentException { + switch (text) { + case "typing": + return TYPING; + case "record_video": + return RECORDVIDEO; + case "record_audio": + return RECORDAUDIO; + case "upload_photo": + return UPLOADPHOTO; + case "upload_video": + return UPLOADVIDEO; + case "upload_audio": + return UPLOADAUDIO; + case "upload_document": + return UPLOADDOCUMENT; + case "find_location": + return FINDLOCATION; + } + throw new IllegalArgumentException(text + " doesn't match any know ActionType"); + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java index 8e1e55bf..7ae28d9e 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendChatAction.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import org.json.JSONObject; import org.telegram.telegrambots.Constants; +import org.telegram.telegrambots.api.methods.ActionType; import org.telegram.telegrambots.api.methods.BotApiMethod; import java.io.IOException; @@ -31,7 +32,7 @@ public class SendChatAction extends BotApiMethod { * videos 'record_audio' or 'upload_audio' for audio files 'upload_document' for general files, * 'find_location' for location data. */ - private String action; + private ActionType action; public String getChatId() { return chatId; @@ -42,12 +43,28 @@ public class SendChatAction extends BotApiMethod { return this; } + /** + * @deprecated + * @return Action type text + */ + @Deprecated public String getAction() { - return action; + return action.toString(); } - public SendChatAction setAction(String action) { + public void setAction(ActionType action) { this.action = action; + } + + /** + * @deprecated Use {@link #setAction(ActionType)} instead + * @param action Text of the action to create + * @return Reference to this same instance + * @throws IllegalArgumentException if action is not valid + */ + @Deprecated + public SendChatAction setAction(String action) throws IllegalArgumentException { + this.action = ActionType.GetActionType(action); return this; } @@ -76,7 +93,7 @@ public class SendChatAction extends BotApiMethod { public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField(CHATID_FIELD, chatId); - gen.writeStringField(ACTION_FIELD, action); + gen.writeStringField(ACTION_FIELD, action.toString()); gen.writeEndObject(); }