diff --git a/README.md b/README.md index a8e64bd4..d06798f9 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,14 @@ Java library for interacting with [Telegram Bot API](https://core.telegram.org/b Gradle: ```groovy -implementation 'com.github.pengrad:java-telegram-bot-api:6.9.1' +implementation 'com.github.pengrad:java-telegram-bot-api:7.0.0' ``` Maven: ```xml com.github.pengrad java-telegram-bot-api - 6.9.1 + 7.0.0 ``` [JAR with all dependencies on release page](https://github.com/pengrad/java-telegram-bot-api/releases) diff --git a/README_RU.md b/README_RU.md index 22a583f5..177e71f9 100644 --- a/README_RU.md +++ b/README_RU.md @@ -13,14 +13,14 @@ Java библиотека, созданная для работы с [Telegram B Gradle: ```groovy -implementation 'com.github.pengrad:java-telegram-bot-api:6.9.1' +implementation 'com.github.pengrad:java-telegram-bot-api:7.0.0' ``` Maven: ```xml com.github.pengrad java-telegram-bot-api - 6.9.1 + 7.0.0 ``` Также JAR со всеми зависимостями можно найти [в релизах](https://github.com/pengrad/java-telegram-bot-api/releases). diff --git a/gradle.properties b/gradle.properties index 0931ed4f..49e2f173 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ GROUP=com.github.pengrad -VERSION_NAME=6.9.1 +VERSION_NAME=7.0.0 POM_DESCRIPTION=Java API for Telegram Bot API POM_URL=https://github.com/pengrad/java-telegram-bot-api/ diff --git a/library/src/main/java/com/pengrad/telegrambot/TelegramBot.java b/library/src/main/java/com/pengrad/telegrambot/TelegramBot.java index 0d70351e..1ec8dff0 100644 --- a/library/src/main/java/com/pengrad/telegrambot/TelegramBot.java +++ b/library/src/main/java/com/pengrad/telegrambot/TelegramBot.java @@ -8,6 +8,7 @@ import com.pengrad.telegrambot.request.BaseRequest; import com.pengrad.telegrambot.request.GetUpdates; import com.pengrad.telegrambot.response.BaseResponse; +import com.pengrad.telegrambot.utility.BotUtils; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; @@ -166,7 +167,7 @@ private static Interceptor httpLoggingInterceptor() { } private static Gson gson() { - return new Gson(); + return BotUtils.GSON; } private static String apiUrl(String apiUrl, String botToken, boolean useTestServer) { diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Chat.java b/library/src/main/java/com/pengrad/telegrambot/model/Chat.java index 3ed8f0c1..330420d3 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Chat.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Chat.java @@ -1,6 +1,8 @@ package com.pengrad.telegrambot.model; import com.google.gson.annotations.SerializedName; +import com.pengrad.telegrambot.model.reaction.ReactionType; + import java.io.Serializable; import java.util.Arrays; @@ -33,6 +35,7 @@ public enum Type { private Boolean is_forum; private ChatPhoto photo; private String[] active_usernames; + private ReactionType[] available_reactions; private String emoji_status_custom_emoji_id; private Integer emoji_status_expiration_date; private String bio; @@ -89,7 +92,7 @@ public ChatPhoto photo() { /** * @deprecated Use activeUsernames() instead */ - @Deprecated + @Deprecated public String[] getActiveUsernames() { return active_usernames; } @@ -98,10 +101,14 @@ public String[] activeUsernames() { return active_usernames; } + @Deprecated + public ReactionType[] availableReactions() { + return available_reactions; + } + /** * @deprecated Use emojiStatusCustomEmojiId() instead */ - @Deprecated public String getEmojiStatusCustomEmojiId() { return emoji_status_custom_emoji_id; } @@ -200,6 +207,7 @@ public boolean equals(Object o) { Objects.equals(title, chat.title) && Objects.equals(photo, chat.photo) && Arrays.equals(active_usernames, chat.active_usernames) && + Arrays.equals(available_reactions, chat.available_reactions) && Objects.equals(emoji_status_custom_emoji_id, chat.emoji_status_custom_emoji_id) && Objects.equals(emoji_status_expiration_date, chat.emoji_status_expiration_date) && Objects.equals(bio, chat.bio) && @@ -239,6 +247,7 @@ public String toString() { ", title='" + title + '\'' + ", photo=" + photo + ", active_usernames=" + Arrays.toString(active_usernames) + + ", available_reactions=" + Arrays.toString(available_reactions) + ", emoji_status_custom_emoji_id='" + emoji_status_custom_emoji_id + '\'' + ", emoji_status_expiration_date='" + emoji_status_expiration_date + '\'' + ", bio='" + bio + '\'' + diff --git a/library/src/main/java/com/pengrad/telegrambot/model/MessageReactionCountUpdated.java b/library/src/main/java/com/pengrad/telegrambot/model/MessageReactionCountUpdated.java new file mode 100644 index 00000000..55a38694 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/MessageReactionCountUpdated.java @@ -0,0 +1,58 @@ +package com.pengrad.telegrambot.model; + +import com.pengrad.telegrambot.model.reaction.ReactionCount; + +import java.util.Arrays; +import java.util.Objects; + +public class MessageReactionCountUpdated { + + private Chat chat; + private Integer message_id; + private Integer date; + private ReactionCount[] reactions; + + public Chat chat() { + return chat; + } + + public Integer messageId() { + return message_id; + } + + public Integer date() { + return date; + } + + public ReactionCount[] reactions() { + return reactions; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MessageReactionCountUpdated that = (MessageReactionCountUpdated) o; + return Objects.equals(chat, that.chat) && + Objects.equals(message_id, that.message_id) && + Objects.equals(date, that.date) && + Arrays.equals(reactions, that.reactions); + } + + @Override + public int hashCode() { + int result = Objects.hash(chat, message_id, date); + result = 31 * result + Arrays.hashCode(reactions); + return result; + } + + @Override + public String toString() { + return "MessageReactionCountUpdated{" + + "chat=" + chat + + ", message_id=" + message_id + + ", date=" + date + + ", reactions=" + Arrays.toString(reactions) + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/MessageReactionUpdated.java b/library/src/main/java/com/pengrad/telegrambot/model/MessageReactionUpdated.java new file mode 100644 index 00000000..73e60f52 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/MessageReactionUpdated.java @@ -0,0 +1,80 @@ +package com.pengrad.telegrambot.model; + +import com.pengrad.telegrambot.model.reaction.ReactionType; + +import java.util.Arrays; +import java.util.Objects; + +public class MessageReactionUpdated { + + private Chat chat; + private Integer message_id; + private User user; + private Chat actor_chat; + private Integer date; + private ReactionType[] old_reaction; + private ReactionType[] new_reaction; + + public Chat chat() { + return chat; + } + + public Integer messageId() { + return message_id; + } + + public User user() { + return user; + } + + public Chat actorChat() { + return actor_chat; + } + + public Integer date() { + return date; + } + + public ReactionType[] oldReaction() { + return old_reaction; + } + + public ReactionType[] newReaction() { + return new_reaction; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MessageReactionUpdated that = (MessageReactionUpdated) o; + return Objects.equals(chat, that.chat) && + Objects.equals(message_id, that.message_id) && + Objects.equals(user, that.user) && + Objects.equals(actor_chat, that.actor_chat) && + Objects.equals(date, that.date) && + Arrays.equals(old_reaction, that.old_reaction) && + Arrays.equals(new_reaction, that.new_reaction); + } + + @Override + public int hashCode() { + int result = Objects.hash(chat, message_id, user, actor_chat, date); + result = 31 * result + Arrays.hashCode(old_reaction); + result = 31 * result + Arrays.hashCode(new_reaction); + return result; + } + + @Override + public String toString() { + return "MessageReactionUpdated{" + + "chat=" + chat + + ", message_id=" + message_id + + ", user=" + user + + ", actor_chat=" + actor_chat + + ", date=" + date + + ", old_reaction=" + Arrays.toString(old_reaction) + + ", new_reaction=" + Arrays.toString(new_reaction) + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/Update.java b/library/src/main/java/com/pengrad/telegrambot/model/Update.java index 3b220607..ff17a90d 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/Update.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/Update.java @@ -25,6 +25,8 @@ public class Update implements Serializable { private ChatMemberUpdated my_chat_member; private ChatMemberUpdated chat_member; private ChatJoinRequest chat_join_request; + private MessageReactionUpdated message_reaction; + private MessageReactionCountUpdated message_reaction_count; public Integer updateId() { return update_id; @@ -86,6 +88,14 @@ public ChatJoinRequest chatJoinRequest() { return chat_join_request; } + public MessageReactionUpdated messageReaction() { + return message_reaction; + } + + public MessageReactionCountUpdated messageReactionCount() { + return message_reaction_count; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -105,7 +115,9 @@ public boolean equals(Object o) { Objects.equals(poll_answer, update.poll_answer) && Objects.equals(my_chat_member, update.my_chat_member) && Objects.equals(chat_member, update.chat_member) && - Objects.equals(chat_join_request, update.chat_join_request); + Objects.equals(chat_join_request, update.chat_join_request) && + Objects.equals(message_reaction, update.message_reaction) && + Objects.equals(message_reaction_count, update.message_reaction_count); } @Override @@ -131,6 +143,8 @@ public String toString() { ", my_chat_member=" + my_chat_member + ", chat_member=" + chat_member + ", chat_join_request=" + chat_join_request + + ", message_reaction=" + message_reaction + + ", message_reaction_count=" + message_reaction_count + '}'; } } diff --git a/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionCount.java b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionCount.java new file mode 100644 index 00000000..7d50768e --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionCount.java @@ -0,0 +1,39 @@ +package com.pengrad.telegrambot.model.reaction; + +import java.util.Objects; + +public class ReactionCount { + + private ReactionType type; + private Integer total_count; + + public ReactionType type() { + return type; + } + + public Integer totalCount() { + return total_count; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ReactionCount that = (ReactionCount) o; + return Objects.equals(type, that.type) && + Objects.equals(total_count, that.total_count); + } + + @Override + public int hashCode() { + return Objects.hash(type, total_count); + } + + @Override + public String toString() { + return "ReactionCount{" + + "type=" + type + + ", total_count=" + total_count + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionType.java b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionType.java new file mode 100644 index 00000000..93150887 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionType.java @@ -0,0 +1,36 @@ +package com.pengrad.telegrambot.model.reaction; + +import java.util.Objects; + +public abstract class ReactionType { + + private final String type; + + public ReactionType(String type) { + this.type = type; + } + + public String type() { + return type; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ReactionType that = (ReactionType) o; + return Objects.equals(type, that.type); + } + + @Override + public int hashCode() { + return Objects.hash(type); + } + + @Override + public String toString() { + return "ReactionType{" + + "type='" + type + '\'' + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionTypeCustomEmoji.java b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionTypeCustomEmoji.java new file mode 100644 index 00000000..e77b1f35 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionTypeCustomEmoji.java @@ -0,0 +1,44 @@ +package com.pengrad.telegrambot.model.reaction; + +import java.util.Objects; + +public class ReactionTypeCustomEmoji extends ReactionType { + + public static final String CUSTOM_EMOJI_TYPE = "custom_emoji"; + + private String custom_emoji_id; + + public ReactionTypeCustomEmoji(String customEmojiId) { + super(CUSTOM_EMOJI_TYPE); + this.custom_emoji_id = customEmojiId; + } + + public String customEmojiId() { + return custom_emoji_id; + } + + public void customEmojiId(String customEmojiId) { + this.custom_emoji_id = customEmojiId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ReactionTypeCustomEmoji that = (ReactionTypeCustomEmoji) o; + return Objects.equals(custom_emoji_id, that.custom_emoji_id); + } + + @Override + public int hashCode() { + return Objects.hash(custom_emoji_id); + } + + @Override + public String toString() { + return "ReactionTypeCustomEmoji{" + + "type='" + type() + '\'' + + "custom_emoji_id='" + custom_emoji_id + '\'' + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionTypeEmoji.java b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionTypeEmoji.java new file mode 100644 index 00000000..91d6faae --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/reaction/ReactionTypeEmoji.java @@ -0,0 +1,44 @@ +package com.pengrad.telegrambot.model.reaction; + +import java.util.Objects; + +public class ReactionTypeEmoji extends ReactionType { + + public static final String EMOJI_TYPE = "emoji"; + + private String emoji; + + public ReactionTypeEmoji(String emoji) { + super(EMOJI_TYPE); + this.emoji = emoji; + } + + public String emoji() { + return emoji; + } + + public void emoji(String emoji) { + this.emoji = emoji; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ReactionTypeEmoji that = (ReactionTypeEmoji) o; + return Objects.equals(emoji, that.emoji); + } + + @Override + public int hashCode() { + return Objects.hash(emoji); + } + + @Override + public String toString() { + return "ReactionTypeEmoji{" + + "type='" + type() + '\'' + + "emoji='" + emoji + '\'' + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/BaseRequest.java b/library/src/main/java/com/pengrad/telegrambot/request/BaseRequest.java index a5083de2..39e88c87 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/BaseRequest.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/BaseRequest.java @@ -1,6 +1,6 @@ package com.pengrad.telegrambot.request; -import com.pengrad.telegrambot.BotUtils; +import com.pengrad.telegrambot.utility.BotUtils; import com.pengrad.telegrambot.response.BaseResponse; import java.util.LinkedHashMap; diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SetMessageReaction.java b/library/src/main/java/com/pengrad/telegrambot/request/SetMessageReaction.java new file mode 100644 index 00000000..c85e3f24 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SetMessageReaction.java @@ -0,0 +1,21 @@ +package com.pengrad.telegrambot.request; + +import com.pengrad.telegrambot.model.reaction.ReactionType; +import com.pengrad.telegrambot.response.BaseResponse; + +public class SetMessageReaction extends BaseRequest { + + public SetMessageReaction(Object chatId, int messageId, ReactionType... reactions) { + super(BaseResponse.class); + add("chat_id", chatId).add("message_id", messageId).add("reaction", reactions); + } + + public SetMessageReaction(Object chatId, int messageId) { + super(BaseResponse.class); + add("chat_id", chatId).add("message_id", messageId); + } + + public SetMessageReaction isBig(boolean isBig) { + return add("is_big", isBig); + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/BotUtils.java b/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java similarity index 62% rename from library/src/main/java/com/pengrad/telegrambot/BotUtils.java rename to library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java index 474618a8..a9698ea2 100644 --- a/library/src/main/java/com/pengrad/telegrambot/BotUtils.java +++ b/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java @@ -1,7 +1,10 @@ -package com.pengrad.telegrambot; +package com.pengrad.telegrambot.utility; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.reaction.ReactionType; +import com.pengrad.telegrambot.utility.gson.ReactionTypeAdapter; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -16,14 +19,16 @@ public class BotUtils { private BotUtils() {} - private static final Gson gson = new Gson(); + public static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(ReactionType.class, new ReactionTypeAdapter()) + .create(); public static Update parseUpdate(String update) { - return gson.fromJson(update, Update.class); + return GSON.fromJson(update, Update.class); } public static Update parseUpdate(Reader reader) { - return gson.fromJson(reader, Update.class); + return GSON.fromJson(reader, Update.class); } static byte[] getBytesFromInputStream(InputStream is) throws IOException { @@ -36,10 +41,10 @@ static byte[] getBytesFromInputStream(InputStream is) throws IOException { } public static R fromJson(String jsonString, Class resClass) { - return gson.fromJson(jsonString,resClass); + return GSON.fromJson(jsonString,resClass); } public static String toJson(Object obj) { - return gson.toJson(obj); + return GSON.toJson(obj); } } diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/gson/ReactionTypeAdapter.java b/library/src/main/java/com/pengrad/telegrambot/utility/gson/ReactionTypeAdapter.java new file mode 100644 index 00000000..f96f2928 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/utility/gson/ReactionTypeAdapter.java @@ -0,0 +1,25 @@ +package com.pengrad.telegrambot.utility.gson; + +import com.google.gson.*; +import com.pengrad.telegrambot.model.reaction.ReactionType; +import com.pengrad.telegrambot.model.reaction.ReactionTypeCustomEmoji; +import com.pengrad.telegrambot.model.reaction.ReactionTypeEmoji; + +import java.lang.reflect.Type; + +public class ReactionTypeAdapter implements JsonDeserializer { + + @Override + public ReactionType deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { + JsonObject object = element.getAsJsonObject(); + String discriminator = object.getAsJsonPrimitive("type").getAsString(); + + if (ReactionTypeEmoji.EMOJI_TYPE.equals(discriminator)) { + return context.deserialize(object, ReactionTypeEmoji.class); + } else if (ReactionTypeCustomEmoji.CUSTOM_EMOJI_TYPE.equals(discriminator)) { + return context.deserialize(object, ReactionTypeCustomEmoji.class); + } + + throw new JsonParseException("Unknown ReactionType type: " + discriminator); + } +} diff --git a/library/src/test/java/com/pengrad/telegrambot/BotUtilsTest.java b/library/src/test/java/com/pengrad/telegrambot/BotUtilsTest.java index 1ad0f739..4f6108ec 100644 --- a/library/src/test/java/com/pengrad/telegrambot/BotUtilsTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/BotUtilsTest.java @@ -2,6 +2,7 @@ import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.response.SendResponse; +import com.pengrad.telegrambot.utility.BotUtils; import org.junit.Test; import java.io.ByteArrayInputStream; diff --git a/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java b/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java index 490ff24f..e097e13d 100644 --- a/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java @@ -6,6 +6,7 @@ import com.pengrad.telegrambot.request.*; import com.pengrad.telegrambot.response.*; +import com.pengrad.telegrambot.utility.BotUtils; import org.junit.*; import static com.pengrad.telegrambot.TelegramBotTest.getProp; diff --git a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java index 8b846b1a..085a5503 100644 --- a/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java @@ -8,6 +8,7 @@ import com.pengrad.telegrambot.passport.*; import com.pengrad.telegrambot.request.*; import com.pengrad.telegrambot.response.*; +import com.pengrad.telegrambot.utility.BotUtils; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; diff --git a/pom.xml b/pom.xml index daa227e2..972c01d5 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.github.pengrad java-telegram-bot-api - 6.9.1 + 7.0.0 JavaTelegramBotApi Java API for Telegram Bot API https://github.com/pengrad/java-telegram-bot-api/ diff --git a/sample/src/main/java/com/pengrad/telegrambot/sample/spark/BotHandler.java b/sample/src/main/java/com/pengrad/telegrambot/sample/spark/BotHandler.java index 72e0d88a..65821bde 100644 --- a/sample/src/main/java/com/pengrad/telegrambot/sample/spark/BotHandler.java +++ b/sample/src/main/java/com/pengrad/telegrambot/sample/spark/BotHandler.java @@ -1,6 +1,6 @@ package com.pengrad.telegrambot.sample.spark; -import com.pengrad.telegrambot.BotUtils; +import com.pengrad.telegrambot.utility.BotUtils; import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.Update;