diff --git a/Bots.ipr b/Bots.ipr index 04178b7e..fe9e67d4 100644 --- a/Bots.ipr +++ b/Bots.ipr @@ -838,28 +838,6 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/README.md b/README.md index bbad1838..13f41236 100644 --- a/README.md +++ b/README.md @@ -27,16 +27,16 @@ Just import add the library to your project with one of these options: org.telegram telegrambots - 3.0.2 + 3.1.0 ``` ```gradle - compile "org.telegram:telegrambots:3.0.2" + compile "org.telegram:telegrambots:3.1.0" ``` - 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/3.0.2) - 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/3.0.2) + 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/3.1.0) + 3. Download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v3.1.0) In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. diff --git a/TelegramBots.wiki/Changelog.md b/TelegramBots.wiki/Changelog.md index e76e0a06..821a0f33 100644 --- a/TelegramBots.wiki/Changelog.md +++ b/TelegramBots.wiki/Changelog.md @@ -63,7 +63,16 @@ ### 3.0.2 ### 1. Bug Fixing: #250 2. Added new module `telegrambots-extensions` that should contains any extensions of the API such as CommandBot or TimedBot. -3. `TelegramLongPollingCommandBot` receives now the bot username as constructor parameters, all deprecated constructors will be removed in next mayor release. -4. `getUsername` method from `TelegramLongPollingCommandBot` can be considered `final` and will be so in next mayor release. +3. `TelegramLongPollingCommandBot` receives now the bot username as constructor parameters, all deprecated constructors will be removed in next major release. +4. `getUsername` method from `TelegramLongPollingCommandBot` can be considered `final` and will be so in next major release. **[[How to update to version 3.0.2|How-To-Update#3.0.2]]** + +### 3.1.0 ### +1. Support for Api Version [3.1](https://core.telegram.org/bots/api-changelog#june-30-2017) +2. Simplified `DefaultAbsSender` +3. Added new abstract method `setChatPhoto` to AbsSender. +4. Added new method `execute` and `executeAsync` that can be used to send any api method that extends `BotApiMethod` class. +5. Added new constructors to `GetChat`, `GetChatAdministrators`, `GetChatMember`, `GetChatMemberCount`, `KickChatMember`, `LeaveChat` and `UnbanChatMember` with mandatory fields as parameters. + +**[[How to update to version 3.1.0|How-To-Update#3.1.0]]** \ No newline at end of file diff --git a/TelegramBots.wiki/Getting-Started.md b/TelegramBots.wiki/Getting-Started.md index e60b719b..db39ec70 100644 --- a/TelegramBots.wiki/Getting-Started.md +++ b/TelegramBots.wiki/Getting-Started.md @@ -11,13 +11,13 @@ First you need ot get the library and add it to your project. There are few poss org.telegram telegrambots - 3.0.2 + 3.1.0 ``` * With **Gradle**: ```groovy - compile group: 'org.telegram', name: 'telegrambots', version: '3.0.2' + compile group: 'org.telegram', name: 'telegrambots', version: '3.1.0' ``` 2. Don't like **Maven Central Repository**? It can also be taken from [Jitpack](https://jitpack.io/#rubenlagus/TelegramBots). diff --git a/pom.xml b/pom.xml index 8f92bf11..d8d9a07d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.telegram Bots pom - 3.0.2 + 3.1.0 telegrambots @@ -25,6 +25,6 @@ true - 3.0.2 + 3.1.0 \ No newline at end of file diff --git a/telegrambots-extensions/README.md b/telegrambots-extensions/README.md index a117e067..7ffad33e 100644 --- a/telegrambots-extensions/README.md +++ b/telegrambots-extensions/README.md @@ -16,12 +16,12 @@ Just import add the library to your project with one of these options: org.telegram telegrambotsextensions - 3.0.2 + 3.1.0 ``` 2. Using Gradle: ```gradle - compile "org.telegram:telegrambotsextensions:3.0.2" + compile "org.telegram:telegrambotsextensions:3.1.0" ``` \ No newline at end of file diff --git a/telegrambots-extensions/pom.xml b/telegrambots-extensions/pom.xml index 07de16e6..7d0aadde 100644 --- a/telegrambots-extensions/pom.xml +++ b/telegrambots-extensions/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambotsextensions - 3.0.2 + 3.1.0 jar Telegram Bots Extensions @@ -59,7 +59,7 @@ UTF-8 UTF-8 - 3.0.2 + 3.1.0 diff --git a/telegrambots-meta/pom.xml b/telegrambots-meta/pom.xml index ce6c9519..9bccc144 100644 --- a/telegrambots-meta/pom.xml +++ b/telegrambots-meta/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambots-meta - 3.0.2 + 3.1.0 jar Telegram Bots Meta diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/DeleteChatPhoto.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/DeleteChatPhoto.java new file mode 100644 index 00000000..270e5362 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/DeleteChatPhoto.java @@ -0,0 +1,94 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 1.0 + * Use this method to delete a chat photo. Photos can't be changed for private chats. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Returns True on success. + * + * @apiNote In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is off in the target group. + */ +public class DeleteChatPhoto extends BotApiMethod { + public static final String PATH = "deleteChatPhoto"; + + private static final String CHATID_FIELD = "chat_id"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Unique identifier for the target chat or username of the target channel (in the format @channelusername) + + public DeleteChatPhoto() { + super(); + } + + public DeleteChatPhoto(String chatId) { + super(); + this.chatId = checkNotNull(chatId); + } + + public DeleteChatPhoto(Long chatId) { + super(); + this.chatId = checkNotNull(chatId).toString(); + } + + public String getChatId() { + return chatId; + } + + public DeleteChatPhoto setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public DeleteChatPhoto setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error deleting chat photo", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null) { + throw new TelegramApiValidationException("ChatId can't be null", this); + } + } + + @Override + public String toString() { + return "DeleteChatPhoto{" + + "chatId='" + chatId + '\'' + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/ExportChatInviteLink.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/ExportChatInviteLink.java new file mode 100644 index 00000000..bcd1dcce --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/ExportChatInviteLink.java @@ -0,0 +1,91 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to export an invite link to a supergroup or a channel. The bot must be an administrator in the + * chat for this to work and must have the appropriate admin rights. Returns exported invite link as String on success. + */ +public class ExportChatInviteLink extends BotApiMethod { + public static final String PATH = "exportChatInviteLink"; + + private static final String CHATID_FIELD = "chat_id"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) + + public ExportChatInviteLink() { + super(); + } + + public ExportChatInviteLink(String chatId) { + super(); + this.chatId = checkNotNull(chatId); + } + + public ExportChatInviteLink(Long chatId) { + super(); + this.chatId = checkNotNull(chatId).toString(); + } + + public String getChatId() { + return chatId; + } + + public ExportChatInviteLink setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public ExportChatInviteLink setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public String deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error exporting invite link", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + } + + @Override + public String toString() { + return "ExportChatInviteLink{" + + "chatId='" + chatId + '\'' + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java index f5e43108..6bb4970e 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChat.java @@ -12,11 +12,12 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.Objects; +import static com.google.common.base.Preconditions.checkNotNull; + /** * @author Ruben Bermudez * @version 1.0 - * @brief Use this method to get information about the chat. Returns Chat object on success. - * @date 20 of May of 2016 + * Use this method to get information about the chat. Returns Chat object on success. */ public class GetChat extends BotApiMethod { public static final String PATH = "getChat"; @@ -30,6 +31,16 @@ public class GetChat extends BotApiMethod { super(); } + public GetChat(String chatId) { + super(); + this.chatId = checkNotNull(chatId); + } + + public GetChat(Long chatId) { + super(); + this.chatId = checkNotNull(chatId).toString(); + } + public String getChatId() { return chatId; } @@ -67,8 +78,8 @@ public class GetChat extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { - throw new TelegramApiValidationException("ChatId can't be null", this); + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java index 2e8be8e0..d1ff6c9f 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatAdministrators.java @@ -72,8 +72,8 @@ public class GetChatAdministrators extends BotApiMethod> { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { - throw new TelegramApiValidationException("ChatId can't be null", this); + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java index 26e3555e..7a76864e 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMember.java @@ -80,8 +80,8 @@ public class GetChatMember extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { - throw new TelegramApiValidationException("ChatId can't be null", this); + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); } if (userId == null) { throw new TelegramApiValidationException("UserId can't be null", this); diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java index 23f2068d..8c153f76 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/GetChatMemberCount.java @@ -66,8 +66,8 @@ public class GetChatMemberCount extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { - throw new TelegramApiValidationException("ChatId can't be null", this); + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java index b55027d7..e490ed4b 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/KickChatMember.java @@ -11,33 +11,48 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.Objects; +import static com.google.common.base.Preconditions.checkNotNull; + /** * @author Ruben Bermudez * @version 1.0 - * @brief Use this method to kick a user from a group or a supergroup. In the case of supergroups, + * Use this method to kick a user from a group, a supergroup or a channel. In the case of supergroups, * the user will not be able to return to the group on their own using invite links, etc., unless * unbanned first. The bot must be an administrator in the group for this to work. Returns True on * success. - * @note This will method only work if the ‘All Members Are Admins’ setting is off in the target + * @apiNote This will method only work if the ‘All Members Are Admins’ setting is off in the target * group. Otherwise members may only be removed by the group's creator or by the member that added * them. - * @date 10 of April of 2016 */ public class KickChatMember extends BotApiMethod { public static final String PATH = "kickchatmember"; private static final String CHATID_FIELD = "chat_id"; private static final String USER_ID_FIELD = "user_id"; + private static final String UNTILDATE_FIELD = "until_date"; @JsonProperty(CHATID_FIELD) - private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) + private String chatId; ///< Required. Unique identifier for the chat to send the message to (Or username for channels) @JsonProperty(USER_ID_FIELD) - private Integer userId; ///< Unique identifier of the target user + private Integer userId; ///< Required. Unique identifier of the target user + @JsonProperty(UNTILDATE_FIELD) + private Integer untilDate; ///< Date when the user will be unbanned, unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever public KickChatMember() { super(); } + public KickChatMember(String chatId, Integer userId) { + this.chatId = checkNotNull(chatId); + this.userId = checkNotNull(userId); + } + + public KickChatMember(Long chatId, Integer userId) { + this.chatId = checkNotNull(chatId).toString(); + this.userId = checkNotNull(userId); + } + + public String getChatId() { return chatId; } @@ -62,6 +77,15 @@ public class KickChatMember extends BotApiMethod { return this; } + public Integer getUntilDate() { + return untilDate; + } + + public KickChatMember setUntilDate(Integer untilDate) { + this.untilDate = untilDate; + return this; + } + @Override public String getMethod() { return PATH; @@ -84,8 +108,8 @@ public class KickChatMember extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { - throw new TelegramApiValidationException("ChatId can't be null", this); + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); } if (userId == null) { throw new TelegramApiValidationException("UserId can't be null", this); @@ -96,7 +120,8 @@ public class KickChatMember extends BotApiMethod { public String toString() { return "KickChatMember{" + "chatId='" + chatId + '\'' + - ", userId='" + userId + + ", userId=" + userId + + ", untilDate=" + untilDate + '}'; } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java index 10a2861a..410f3659 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java @@ -66,7 +66,7 @@ public class LeaveChat extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { + if (chatId == null || chatId.isEmpty()) { throw new TelegramApiValidationException("ChatId can't be null", this); } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/PromoteChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/PromoteChatMember.java new file mode 100644 index 00000000..f241e89c --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/PromoteChatMember.java @@ -0,0 +1,214 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to promote or demote a user in a supergroup or a channel. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Pass False for all boolean parameters to demote a user. Returns True on success. + * + */ +public class PromoteChatMember extends BotApiMethod { + public static final String PATH = "promoteChatMember"; + + private static final String CHATID_FIELD = "chat_id"; + private static final String USER_ID_FIELD = "user_id"; + private static final String CANCHANGEINFORMATION_FIELD = "can_change_information"; + private static final String CANPOSTMESSAGES_FIELD = "can_post_messages"; + private static final String CANEDITMESSAGES_FIELD = "can_edit_messages"; + private static final String CANDELETEMESSAGES_FIELD = "can_delete_messages"; + private static final String CANINVITEUSERS_FIELD = "can_invite_users"; + private static final String CANRESTRICTMEMBERS_FIELD = "can_restrict_members"; + private static final String CANPINMESSAGES_FIELD = "can_pin_messages"; + private static final String CANPROMOTEMEMBERS_FIELD = "can_promote_members"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Required. Unique identifier for the chat to send the message to (Or username for channels) + @JsonProperty(USER_ID_FIELD) + private Integer userId; ///< Required. Unique identifier of the target user + @JsonProperty(CANCHANGEINFORMATION_FIELD) + private Boolean canChangeInformation; ///< Pass True, if the administrator can change chat title, photo and other settings + @JsonProperty(CANPOSTMESSAGES_FIELD) + private Boolean canPostMessages; ///< Pass True, if the administrator can create channel posts, channels only + @JsonProperty(CANEDITMESSAGES_FIELD) + private Boolean canEditMessages; ///< Pass True, if the administrator can edit messages of other users, channels only + @JsonProperty(CANDELETEMESSAGES_FIELD) + private Boolean canDeleteMessages; ///< Pass True, if the administrator can delete messages of other users + @JsonProperty(CANINVITEUSERS_FIELD) + private Boolean canInviteUsers; ///< Pass True, if the administrator can invite new users to the chat + @JsonProperty(CANRESTRICTMEMBERS_FIELD) + private Boolean canRestrictMembers; ///< Pass True, if the administrator can restrict, ban or unban chat members + @JsonProperty(CANPINMESSAGES_FIELD) + private Boolean canPinMessages; ///< Pass True, if the administrator can pin messages, supergroups only + @JsonProperty(CANPROMOTEMEMBERS_FIELD) + private Boolean canPromoteMembers; ///< Pass True, if the administrator can add new administrators with a subset of his own privileges or demote administrators that he has promoted, directly or indirectly (promoted by administators that were appointed by the him) + + public PromoteChatMember() { + super(); + } + + public PromoteChatMember(String chatId, Integer userId) { + this.chatId = checkNotNull(chatId); + this.userId = checkNotNull(userId); + } + + public PromoteChatMember(Long chatId, Integer userId) { + this.chatId = checkNotNull(chatId).toString(); + this.userId = checkNotNull(userId); + } + + public String getChatId() { + return chatId; + } + + public PromoteChatMember setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public PromoteChatMember setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + public Integer getUserId() { + return userId; + } + + public PromoteChatMember setUserId(Integer userId) { + this.userId = userId; + return this; + } + + public Boolean getCanChangeInformation() { + return canChangeInformation; + } + + public PromoteChatMember setCanChangeInformation(Boolean canChangeInformation) { + this.canChangeInformation = canChangeInformation; + return this; + } + + public Boolean getCanPostMessages() { + return canPostMessages; + } + + public PromoteChatMember setCanPostMessages(Boolean canPostMessages) { + this.canPostMessages = canPostMessages; + return this; + } + + public Boolean getCanEditMessages() { + return canEditMessages; + } + + public PromoteChatMember setCanEditMessages(Boolean canEditMessages) { + this.canEditMessages = canEditMessages; + return this; + } + + public Boolean getCanDeleteMessages() { + return canDeleteMessages; + } + + public PromoteChatMember setCanDeleteMessages(Boolean canDeleteMessages) { + this.canDeleteMessages = canDeleteMessages; + return this; + } + + public Boolean getCanInviteUsers() { + return canInviteUsers; + } + + public PromoteChatMember setCanInviteUsers(Boolean canInviteUsers) { + this.canInviteUsers = canInviteUsers; + return this; + } + + public Boolean getCanRestrictMembers() { + return canRestrictMembers; + } + + public PromoteChatMember setCanRestrictMembers(Boolean canRestrictMembers) { + this.canRestrictMembers = canRestrictMembers; + return this; + } + + public Boolean getCanPinMessages() { + return canPinMessages; + } + + public PromoteChatMember setCanPinMessages(Boolean canPinMessages) { + this.canPinMessages = canPinMessages; + return this; + } + + public Boolean getCanPromoteMembers() { + return canPromoteMembers; + } + + public PromoteChatMember setCanPromoteMembers(Boolean canPromoteMembers) { + this.canPromoteMembers = canPromoteMembers; + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>() { + }); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error promoting chat member", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + if (userId == null) { + throw new TelegramApiValidationException("UserId can't be null", this); + } + } + + @Override + public String toString() { + return "PromoteChatMember{" + + "chatId='" + chatId + '\'' + + ", userId=" + userId + + ", canChangeInformation=" + canChangeInformation + + ", canPostMessages=" + canPostMessages + + ", canEditMessages=" + canEditMessages + + ", canDeleteMessages=" + canDeleteMessages + + ", canInviteUsers=" + canInviteUsers + + ", canRestrictMembers=" + canRestrictMembers + + ", canPinMessages=" + canPinMessages + + ", canPromoteMembers=" + canPromoteMembers + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/RestrictChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/RestrictChatMember.java new file mode 100644 index 00000000..1b0a6630 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/RestrictChatMember.java @@ -0,0 +1,175 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to restrict a user in a supergroup. + * The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights. + * Pass True for all boolean parameters to lift restrictions from a user. Returns True on success. + * + */ +public class RestrictChatMember extends BotApiMethod { + public static final String PATH = "kickchatmember"; + + private static final String CHATID_FIELD = "chat_id"; + private static final String USER_ID_FIELD = "user_id"; + private static final String UNTILDATE_FIELD = "until_date"; + private static final String CANSENDMESSAGES_FIELD = "can_send_messages"; + private static final String CANSENDMEDIAMESSAGES_FIELD = "can_send_media_messages"; + private static final String CANSENDOTHERMESSAGES_FIELD = "can_send_other_messages"; + private static final String CANADDWEBPAGEPREVIEWS_FIELD = "can_add_web_page_previews"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Required. Unique identifier for the chat to send the message to (Or username for channels) + @JsonProperty(USER_ID_FIELD) + private Integer userId; ///< Required. Unique identifier of the target user + @JsonProperty(UNTILDATE_FIELD) + private Integer untilDate; ///< Date when restrictions will be lifted for the user, unix time. If user is restricted for more than 366 days or less than 30 seconds from the current time, they are considered to be banned forever + @JsonProperty(CANSENDMESSAGES_FIELD) + private Boolean canSendMessages; ///< Pass True, if the user can send text messages, contacts, locations and venues + @JsonProperty(CANSENDMEDIAMESSAGES_FIELD) + private Boolean canSendMediaMessages; ///< Pass True, if the user can send audios, documents, photos, videos, video notes and voice notes, implies can_send_messages + @JsonProperty(CANSENDOTHERMESSAGES_FIELD) + private Boolean canSendOtherMessages; ///< Pass True, if the user can send animations, games, stickers and use inline bots, implies can_send_media_messages + @JsonProperty(CANADDWEBPAGEPREVIEWS_FIELD) + private Boolean canAddWebPagePreviews; ///< Pass True, if the user may add web page previews to their messages, implies can_send_messages + + + public RestrictChatMember() { + super(); + } + + public RestrictChatMember(String chatId, Integer userId) { + this.chatId = checkNotNull(chatId); + this.userId = checkNotNull(userId); + } + + public RestrictChatMember(Long chatId, Integer userId) { + this.chatId = checkNotNull(chatId).toString(); + this.userId = checkNotNull(userId); + } + + public String getChatId() { + return chatId; + } + + public RestrictChatMember setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public RestrictChatMember setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + public Integer getUserId() { + return userId; + } + + public RestrictChatMember setUserId(Integer userId) { + this.userId = userId; + return this; + } + + public Integer getUntilDate() { + return untilDate; + } + + public RestrictChatMember setUntilDate(Integer untilDate) { + this.untilDate = untilDate; + return this; + } + + public Boolean getCanSendMessages() { + return canSendMessages; + } + + public RestrictChatMember setCanSendMessages(Boolean canSendMessages) { + this.canSendMessages = canSendMessages; + return this; + } + + public Boolean getCanSendMediaMessages() { + return canSendMediaMessages; + } + + public RestrictChatMember setCanSendMediaMessages(Boolean canSendMediaMessages) { + this.canSendMediaMessages = canSendMediaMessages; + return this; + } + + public Boolean getCanSendOtherMessages() { + return canSendOtherMessages; + } + + public RestrictChatMember setCanSendOtherMessages(Boolean canSendOtherMessages) { + this.canSendOtherMessages = canSendOtherMessages; + return this; + } + + public Boolean getCanAddWebPagePreviews() { + return canAddWebPagePreviews; + } + + public RestrictChatMember setCanAddWebPagePreviews(Boolean canAddWebPagePreviews) { + this.canAddWebPagePreviews = canAddWebPagePreviews; + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error restricting chat member", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + if (userId == null) { + throw new TelegramApiValidationException("UserId can't be empty", this); + } + } + + @Override + public String toString() { + return "RestrictChatMember{" + + "chatId='" + chatId + '\'' + + ", userId=" + userId + + ", untilDate=" + untilDate + + ", canSendMessages=" + canSendMessages + + ", canSendMediaMessages=" + canSendMediaMessages + + ", canSendOtherMessages=" + canSendOtherMessages + + ", canAddWebPagePreviews=" + canAddWebPagePreviews + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatDescription.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatDescription.java new file mode 100644 index 00000000..bb02ab33 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatDescription.java @@ -0,0 +1,111 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to change the description of a supergroup or channels. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Returns True on success. + */ +public class SetChatDescription extends BotApiMethod { + public static final String PATH = "setChatDescription"; + + private static final String CHATID_FIELD = "chat_id"; + private static final String DESCRIPTION_FIELD = "description"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Unique identifier for the target chat or username of the target channel (in the format @channelusername) + @JsonProperty(DESCRIPTION_FIELD) + private String description; ///< New chat description, 0-255 characters + + public SetChatDescription() { + super(); + } + + public SetChatDescription(String chatId, String description) { + super(); + this.chatId = checkNotNull(chatId); + this.description = checkNotNull(description); + } + + public SetChatDescription(Long chatId, String description) { + super(); + this.chatId = checkNotNull(chatId).toString(); + this.description = checkNotNull(description); + } + + public String getChatId() { + return chatId; + } + + public SetChatDescription setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public SetChatDescription setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + public String getDescription() { + return description; + } + + public SetChatDescription setDescription(String description) { + Objects.requireNonNull(description); + this.description = description; + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error setting chat description", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + if (description == null) { + throw new TelegramApiValidationException("Description can't be null", this); + } + } + + @Override + public String toString() { + return "SetChatDescription{" + + "chatId='" + chatId + '\'' + + ", description='" + description + '\'' + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatPhoto.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatPhoto.java new file mode 100644 index 00000000..4c53f5c8 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatPhoto.java @@ -0,0 +1,145 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.PartialBotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Returns True on success. + * + * @apiNote In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is off in the target group. + */ +public class SetChatPhoto extends PartialBotApiMethod { + public static final String PATH = "setChatPhoto"; + + public static final String CHATID_FIELD = "chat_id"; + public static final String PHOTO_FIELD = "photo"; + + private String chatId; ///< Unique identifier for the target chat or username of the target channel (in the format @channelusername) + private String photoName; ///< Name of new chat photo + private InputStream photoStream; ///< New chat photo as InputStream, uploaded using multipart/form-data + private File photo; ///< New chat photo as File, uploaded using multipart/form-data + + public SetChatPhoto() { + super(); + } + + public SetChatPhoto(String chatId, File photo) { + super(); + this.chatId = checkNotNull(chatId); + this.photo = checkNotNull(photo); + } + + public SetChatPhoto(String chatId, InputStream photoStream, String photoName) { + super(); + this.chatId = checkNotNull(chatId); + this.photoStream = checkNotNull(photoStream); + this.photoName = checkNotNull(photoName); + } + + + public SetChatPhoto(Long chatId, File photo) { + super(); + this.chatId = checkNotNull(chatId).toString(); + this.photo = checkNotNull(photo); + } + + public SetChatPhoto(Long chatId, InputStream photoStream, String photoName) { + super(); + this.chatId = checkNotNull(chatId).toString(); + this.photoStream = checkNotNull(photoStream); + this.photoName = checkNotNull(photoName); + } + + public String getChatId() { + return chatId; + } + + public SetChatPhoto setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public SetChatPhoto setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + public SetChatPhoto setPhoto(File file) { + this.photo = file; + return this; + } + + public SetChatPhoto setNewPhoto(String photoName, InputStream inputStream) { + Objects.requireNonNull(photoName, "photoName cannot be null!"); + Objects.requireNonNull(inputStream, "inputStream cannot be null!"); + this.photoName = photoName; + this.photoStream = inputStream; + return this; + } + + public String getPhotoName() { + return photoName; + } + + public InputStream getPhotoStream() { + return photoStream; + } + + public File getPhoto() { + return photo; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error setting chat photo", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + if (photo == null) { + if (photoStream == null) { + throw new TelegramApiValidationException("Photo parameter is required", this); + } else if (photoName == null || photoName.isEmpty()){ + throw new TelegramApiValidationException("Photo name can't be empty", this); + } + } + } + + @Override + public String toString() { + return "SetChatPhoto{" + + "chatId='" + chatId + '\'' + + ", photoName='" + photoName + '\'' + + ", photoStream=" + photoStream + + ", photo=" + photo + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatTitle.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatTitle.java new file mode 100644 index 00000000..8e87b052 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/SetChatTitle.java @@ -0,0 +1,113 @@ +package org.telegram.telegrambots.api.methods.groupadministration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to change the title of a chat. Titles can't be changed for private chats. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Returns True on success. + * + * @apiNote In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is off in the target group. + */ +public class SetChatTitle extends BotApiMethod { + public static final String PATH = "setChatTitle"; + + private static final String CHATID_FIELD = "chat_id"; + private static final String TITLE_FIELD = "title"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Required. Unique identifier for the target chat or username of the target channel (in the format @channelusername) + @JsonProperty(TITLE_FIELD) + private String title; ///< Required. New chat title, 1-255 characters + + public SetChatTitle() { + super(); + } + + public SetChatTitle(String chatId, String title) { + super(); + this.chatId = checkNotNull(chatId); + this.title = checkNotNull(title); + } + + public SetChatTitle(Long chatId, String title) { + super(); + this.chatId = checkNotNull(chatId).toString(); + this.title = checkNotNull(title); + } + + public String getChatId() { + return chatId; + } + + public SetChatTitle setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public SetChatTitle setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + public String getTitle() { + return title; + } + + public SetChatTitle setTitle(String title) { + Objects.requireNonNull(title); + this.title = title; + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error setting chat title", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); + } + if (title == null || title.isEmpty()) { + throw new TelegramApiValidationException("Title can't be empty", this); + } + } + + @Override + public String toString() { + return "SetChatTitle{" + + "chatId='" + chatId + '\'' + + ", title='" + title + '\'' + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java index 8d992007..7cdc842c 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/UnbanChatMember.java @@ -11,6 +11,8 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.Objects; +import static com.google.common.base.Preconditions.checkNotNull; + /** * @author Ruben Bermudez * @version 1.0 @@ -25,14 +27,26 @@ public class UnbanChatMember extends BotApiMethod { private static final String USER_ID_FIELD = "user_id"; @JsonProperty(CHATID_FIELD) - private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels) + private String chatId; ///< Required. Unique identifier for the chat to send the message to (Or username for channels) @JsonProperty(USER_ID_FIELD) - private Integer userId; ///< Unique identifier of the target user + private Integer userId; ///< Required. Unique identifier of the target user public UnbanChatMember() { super(); } + public UnbanChatMember(String chatId, Integer userId) { + super(); + this.chatId = checkNotNull(chatId); + this.userId = checkNotNull(userId); + } + + public UnbanChatMember(Long chatId, Integer userId) { + super(); + this.chatId = checkNotNull(chatId).toString(); + this.userId = checkNotNull(userId); + } + public String getChatId() { return chatId; } @@ -79,8 +93,8 @@ public class UnbanChatMember extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (chatId == null) { - throw new TelegramApiValidationException("ChatId can't be null", this); + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId can't be empty", this); } if (userId == null) { throw new TelegramApiValidationException("UserId can't be null", this); diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/pinnedmessages/PinChatMessage.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/pinnedmessages/PinChatMessage.java new file mode 100644 index 00000000..296099dc --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/pinnedmessages/PinChatMessage.java @@ -0,0 +1,124 @@ +package org.telegram.telegrambots.api.methods.pinnedmessages; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to pin a message in a supergroup. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Returns True on success. + */ +public class PinChatMessage extends BotApiMethod { + public static final String PATH = "pinChatMessage"; + + private static final String CHATID_FIELD = "chat_id"; + private static final String MESSAGEID_FIELD = "message_id"; + private static final String DISABLENOTIFICATION_FIELD = "disable_notification"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Required. Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername) + @JsonProperty(MESSAGEID_FIELD) + private Integer messageId; ///< Required. Identifier of a message to pin + @JsonProperty(DISABLENOTIFICATION_FIELD) + private Boolean disableNotification; ///< Pass true, if it is not necessary to send a notification to all group members about the new pinned message + + public PinChatMessage() { + super(); + } + + public PinChatMessage(String chatId, Integer messageId) { + super(); + this.chatId = checkNotNull(chatId); + this.messageId = checkNotNull(messageId); + } + + public PinChatMessage(Long chatId, Integer messageId) { + super(); + this.chatId = checkNotNull(chatId).toString(); + this.messageId = checkNotNull(messageId); + } + + public String getChatId() { + return chatId; + } + + public PinChatMessage setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public PinChatMessage setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + public Integer getMessageId() { + return messageId; + } + + public PinChatMessage setMessageId(Integer messageId) { + Objects.requireNonNull(messageId); + this.messageId = messageId; + return this; + } + + public Boolean getDisableNotification() { + return disableNotification; + } + + public PinChatMessage setDisableNotification(Boolean disableNotification) { + this.disableNotification = disableNotification; + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error pinning chat message", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + if (messageId == null) { + throw new TelegramApiValidationException("MessageId parameter can't be null", this); + } + } + + @Override + public String toString() { + return "PinChatMessage{" + + "chatId='" + chatId + '\'' + + ", messageId=" + messageId + + ", disableNotification=" + disableNotification + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/pinnedmessages/UnpinChatMessage.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/pinnedmessages/UnpinChatMessage.java new file mode 100644 index 00000000..fd294604 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/pinnedmessages/UnpinChatMessage.java @@ -0,0 +1,93 @@ +package org.telegram.telegrambots.api.methods.pinnedmessages; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.type.TypeReference; +import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.objects.replykeyboard.ApiResponse; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * @author Ruben Bermudez + * @version 3.1 + * Use this method to unpin a message in a supergroup chat. + * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. + * Returns True on success. + */ +public class UnpinChatMessage extends BotApiMethod { + public static final String PATH = "unpinChatMessage"; + + private static final String CHATID_FIELD = "chat_id"; + + @JsonProperty(CHATID_FIELD) + private String chatId; ///< Required. Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername) + + public UnpinChatMessage() { + super(); + } + + public UnpinChatMessage(String chatId) { + super(); + this.chatId = checkNotNull(chatId); + } + + public UnpinChatMessage(Long chatId) { + super(); + this.chatId = checkNotNull(chatId).toString(); + } + + public String getChatId() { + return chatId; + } + + public UnpinChatMessage setChatId(String chatId) { + this.chatId = chatId; + return this; + } + + public UnpinChatMessage setChatId(Long chatId) { + Objects.requireNonNull(chatId); + this.chatId = chatId.toString(); + return this; + } + + @Override + public String getMethod() { + return PATH; + } + + @Override + public Boolean deserializeResponse(String answer) throws TelegramApiRequestException { + try { + ApiResponse result = OBJECT_MAPPER.readValue(answer, + new TypeReference>(){}); + if (result.getOk()) { + return result.getResult(); + } else { + throw new TelegramApiRequestException("Error unpinning chat message", result); + } + } catch (IOException e) { + throw new TelegramApiRequestException("Unable to deserialize response", e); + } + } + + @Override + public void validate() throws TelegramApiValidationException { + if (chatId == null || chatId.isEmpty()) { + throw new TelegramApiValidationException("ChatId parameter can't be empty", this); + } + } + + @Override + public String toString() { + return "UnpinChatMessage{" + + "chatId='" + chatId + '\'' + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/Chat.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/Chat.java index df34e665..d5b113e8 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/Chat.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/Chat.java @@ -7,18 +7,21 @@ import org.telegram.telegrambots.api.interfaces.BotApiObject; /** * @author Ruben Bermudez * @version 1.0 - * @brief This object represents a Telegram chat with an user or a group - * @date 24 of June of 2015 + * This object represents a Telegram chat with an user or a group */ public class Chat implements BotApiObject { private static final String ID_FIELD = "id"; private static final String TYPE_FIELD = "type"; private static final String TITLE_FIELD = "title"; + private static final String USERNAME_FIELD = "username"; private static final String FIRSTNAME_FIELD = "first_name"; private static final String LASTNAME_FIELD = "last_name"; - private static final String USERNAME_FIELD = "username"; private static final String ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD = "all_members_are_administrators"; + private static final String PHOTO_FIELD = "photo"; + private static final String DESCRIPTION_FIELD = "description"; + private static final String INVITELINK_FIELD = "invite_link"; + private static final String USERCHATTYPE = "private"; private static final String GROUPCHATTYPE = "group"; private static final String CHANNELCHATTYPE = "channel"; @@ -43,10 +46,16 @@ public class Chat implements BotApiObject { @JsonProperty(USERNAME_FIELD) private String userName; ///< Optional. Interlocutor's last name for private chats /** - * Optional. True if the group or supergroup has ‘All Members Are Admins’ enabled. + * Optional. True if a group has ‘All Members Are Admins’ enabled. */ @JsonProperty(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD) private Boolean allMembersAreAdministrators; + @JsonProperty(PHOTO_FIELD) + private ChatPhoto photo; ///< Optional. Chat photo. Returned only in getChat. + @JsonProperty(DESCRIPTION_FIELD) + private String description; ///< Optional. Description, for supergroups and channel chats. Returned only in getChat. + @JsonProperty(INVITELINK_FIELD) + private String inviteLink; ///< Optional. Chat invite link, for supergroups and channel chats. Returned only in getChat. public Chat() { super(); @@ -92,6 +101,18 @@ public class Chat implements BotApiObject { return allMembersAreAdministrators; } + public ChatPhoto getPhoto() { + return photo; + } + + public String getDescription() { + return description; + } + + public String getInviteLink() { + return inviteLink; + } + @Override public String toString() { return "Chat{" + @@ -102,6 +123,9 @@ public class Chat implements BotApiObject { ", lastName='" + lastName + '\'' + ", userName='" + userName + '\'' + ", allMembersAreAdministrators=" + allMembersAreAdministrators + + ", photo=" + photo + + ", description='" + description + '\'' + + ", inviteLink='" + inviteLink + '\'' + '}'; } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatMember.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatMember.java index cde37ea2..9e80c79c 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatMember.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatMember.java @@ -13,11 +13,53 @@ import org.telegram.telegrambots.api.interfaces.BotApiObject; public class ChatMember implements BotApiObject { private static final String USER_FIELD = "user"; private static final String STATUS_FIELD = "status"; + private static final String UNTILDATE_FIELD = "until_date"; + private static final String CANBEEDITED_FIELD = "can_be_edited"; + private static final String CANCHANGEINFORMATION_FIELD = "can_change_information"; + private static final String CANPOSTMESSAGES_FIELD = "can_post_messages"; + private static final String CANEDITMESSAGES_FIELD = "can_edit_messages"; + private static final String CANDELETEMESSAGES_FIELD = "can_delete_messages"; + private static final String CANINVITEUSERS_FIELD = "can_invite_users"; + private static final String CANRESTRICTUSERS_FIELD = "can_restrict_users"; + private static final String CANPINMESSAGES_FIELD = "can_pin_messages"; + private static final String CANPROMOTEMEMBERS_FIELD = "can_promote_members"; + private static final String CANSENDMESSAGES_FIELD = "can_send_messages"; + private static final String CANSENDMEDIAMESSAGES_FIELD = "can_send_media_messages"; + private static final String CANSENDOTHERMESSAGES_FIELD = "can_send_other_messages"; + private static final String CANADDWEBPAGEPREVIEWS_FIELD = "can_add_web_page_previews"; @JsonProperty(USER_FIELD) private User user; ///< Information about the user @JsonProperty(STATUS_FIELD) - private String status; ///< The member's status in the chat. Can be “creator”, “administrator”, “member”, “left” or “kicked” + private String status; ///< The member's status in the chat. Can be “creator”, “administrator”, “member”, “restricted”, “left” or “kicked” + @JsonProperty(UNTILDATE_FIELD) + private Integer untilDate; ///< Optional. Resticted and kicked only. Date when restrictions will be lifted for this user, unix time + @JsonProperty(CANBEEDITED_FIELD) + private Boolean canBeEdited; ///< Optional. Administrators only. True, if the bot is allowed to edit administrator privileges of that user + @JsonProperty(CANCHANGEINFORMATION_FIELD) + private Boolean canChangeInformation; ///< Optional. Administrators only. True, if the administrator can change the chat title, photo and other settings + @JsonProperty(CANPOSTMESSAGES_FIELD) + private Boolean canPostMessages; ///< Optional. Administrators only. True, if the administrator can post in the channel, channels only + @JsonProperty(CANEDITMESSAGES_FIELD) + private Boolean canEditMessages; ///< Optional. Administrators only. True, if the administrator can edit messages of other users, channels only + @JsonProperty(CANDELETEMESSAGES_FIELD) + private Boolean canDeleteMessages; ///< Optional. Administrators only. True, if the administrator can delete messages of other users + @JsonProperty(CANINVITEUSERS_FIELD) + private Boolean canInviteUsers; ///< Optional. Administrators only. True, if the administrator can invite new users to the chat + @JsonProperty(CANRESTRICTUSERS_FIELD) + private Boolean canRestrictUsers; ///< Optional. Administrators only. True, if the administrator can restrict, ban or unban chat members + @JsonProperty(CANPINMESSAGES_FIELD) + private Boolean canPinMessages; ///< Optional. Administrators only. True, if the administrator can pin messages, supergroups only + @JsonProperty(CANPROMOTEMEMBERS_FIELD) + private Boolean canPromoteMembers; ///< Optional. Administrators only. True, if the administrator can add new administrators with a subset of his own privileges or demote administrators that it has promoted, directly or indirectly (promoted by administators that were appointed by the bot) + @JsonProperty(CANSENDMESSAGES_FIELD) + private Boolean canSendMessages; ///< Optional. Restricted only. True, if the user can send text messages, contacts, locations and venues + @JsonProperty(CANSENDMEDIAMESSAGES_FIELD) + private Boolean canSendMediaMessages; ///< Optional. Restricted only. True, if the user can send audios, documents, photos, videos, video notes and voice notes, implies can_send_messages + @JsonProperty(CANSENDOTHERMESSAGES_FIELD) + private Boolean canSendOtherMessages; ///< Optional. Restricted only. True, if the user can send animations, games, stickers and use inline bots, implies can_send_media_messages + @JsonProperty(CANADDWEBPAGEPREVIEWS_FIELD) + private Boolean canAddWebPagePreviews; ///< Optional. Restricted only. True, if user may add web page previews Э to his messages, implies can_send_messages public ChatMember() { super(); @@ -31,11 +73,81 @@ public class ChatMember implements BotApiObject { return status; } + public Integer getUntilDate() { + return untilDate; + } + + public Boolean getCanBeEdited() { + return canBeEdited; + } + + public Boolean getCanChangeInformation() { + return canChangeInformation; + } + + public Boolean getCanPostMessages() { + return canPostMessages; + } + + public Boolean getCanEditMessages() { + return canEditMessages; + } + + public Boolean getCanDeleteMessages() { + return canDeleteMessages; + } + + public Boolean getCanInviteUsers() { + return canInviteUsers; + } + + public Boolean getCanRestrictUsers() { + return canRestrictUsers; + } + + public Boolean getCanPinMessages() { + return canPinMessages; + } + + public Boolean getCanPromoteMembers() { + return canPromoteMembers; + } + + public Boolean getCanSendMessages() { + return canSendMessages; + } + + public Boolean getCanSendMediaMessages() { + return canSendMediaMessages; + } + + public Boolean getCanSendOtherMessages() { + return canSendOtherMessages; + } + + public Boolean getCanAddWebPagePreviews() { + return canAddWebPagePreviews; + } + @Override public String toString() { return "ChatMember{" + "user=" + user + ", status='" + status + '\'' + + ", untilDate=" + untilDate + + ", canBeEdited=" + canBeEdited + + ", canChangeInformation=" + canChangeInformation + + ", canPostMessages=" + canPostMessages + + ", canEditMessages=" + canEditMessages + + ", canDeleteMessages=" + canDeleteMessages + + ", canInviteUsers=" + canInviteUsers + + ", canRestrictUsers=" + canRestrictUsers + + ", canPinMessages=" + canPinMessages + + ", canPromoteMembers=" + canPromoteMembers + + ", canSendMessages=" + canSendMessages + + ", canSendMediaMessages=" + canSendMediaMessages + + ", canSendOtherMessages=" + canSendOtherMessages + + ", canAddWebPagePreviews=" + canAddWebPagePreviews + '}'; } } diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatPhoto.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatPhoto.java new file mode 100644 index 00000000..521b89c1 --- /dev/null +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/objects/ChatPhoto.java @@ -0,0 +1,39 @@ +package org.telegram.telegrambots.api.objects; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.telegram.telegrambots.api.interfaces.BotApiObject; + +/** + * @author Ruben Bermudez + * @version 1.0 + * This object represents a chat photo. + */ +public class ChatPhoto implements BotApiObject { + private static final String SMALLFILEID_FIELD = "small_file_id"; + private static final String BIGFILEID_FIELD = "big_file_id"; + + @JsonProperty(SMALLFILEID_FIELD) + private String smallFileId; ///< Unique file identifier of small (160x160) chat photo. This file_id can be used only for photo download. + @JsonProperty(BIGFILEID_FIELD) + private String bigFileId; ///< Unique file identifier of big (640x640) chat photo. This file_id can be used only for photo download. + + public ChatPhoto() { + super(); + } + + public String getSmallFileId() { + return smallFileId; + } + + public String getBigFileId() { + return bigFileId; + } + + @Override + public String toString() { + return "ChatPhoto{" + + "smallFileId='" + smallFileId + '\'' + + ", bigFileId='" + bigFileId + '\'' + + '}'; + } +} diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index 63c768ec..09a0995f 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -4,6 +4,8 @@ import org.telegram.telegrambots.api.methods.*; import org.telegram.telegrambots.api.methods.games.GetGameHighScores; import org.telegram.telegrambots.api.methods.games.SetGameScore; import org.telegram.telegrambots.api.methods.groupadministration.*; +import org.telegram.telegrambots.api.methods.pinnedmessages.PinChatMessage; +import org.telegram.telegrambots.api.methods.pinnedmessages.UnpinChatMessage; import org.telegram.telegrambots.api.methods.send.*; import org.telegram.telegrambots.api.methods.updates.DeleteWebhook; import org.telegram.telegrambots.api.methods.updates.GetWebhookInfo; @@ -29,6 +31,25 @@ public abstract class AbsSender { protected AbsSender() { } + // General methods to execute BotApiMethods + + public , Callback extends SentCallback> void executeAsync(Method method, Callback callback) throws TelegramApiException { + if (method == null) { + throw new TelegramApiException("Parameter method can not be null"); + } + if (callback == null) { + throw new TelegramApiException("Parameter callback can not be null"); + } + sendApiMethodAsync(method, callback); + } + + public > T execute(Method method) throws TelegramApiException { + if (method == null) { + throw new TelegramApiException("Parameter method can not be null"); + } + return sendApiMethod(method); + } + // Send Requests public final Message sendMessage(SendMessage sendMessage) throws TelegramApiException { @@ -115,6 +136,13 @@ public abstract class AbsSender { return sendApiMethod(getChat); } + public final String exportChatInviteLink(ExportChatInviteLink exportChatInviteLink) throws TelegramApiException { + if (exportChatInviteLink == null) { + throw new TelegramApiException("Parameter exportChatInviteLink can not be null"); + } + return sendApiMethod(exportChatInviteLink); + } + public final List getChatAdministrators(GetChatAdministrators getChatAdministrators) throws TelegramApiException { if (getChatAdministrators == null) { throw new TelegramApiException("Parameter getChatAdministrators can not be null"); @@ -249,6 +277,55 @@ public abstract class AbsSender { return sendApiMethod(deleteMessage); } + public final Boolean deleteChatPhoto(DeleteChatPhoto deleteChatPhoto) throws TelegramApiException { + if(deleteChatPhoto == null){ + throw new TelegramApiException("Parameter deleteChatPhoto can not be null"); + } + return sendApiMethod(deleteChatPhoto); + } + + public final Boolean pinChatMessage(PinChatMessage pinChatMessage) throws TelegramApiException { + if(pinChatMessage == null){ + throw new TelegramApiException("Parameter pinChatMessage can not be null"); + } + return sendApiMethod(pinChatMessage); + } + + public final Boolean unpinChatMessage(UnpinChatMessage unpinChatMessage) throws TelegramApiException { + if(unpinChatMessage == null){ + throw new TelegramApiException("Parameter unpinChatMessage can not be null"); + } + return sendApiMethod(unpinChatMessage); + } + + public final Boolean promoteChatMember(PromoteChatMember promoteChatMember) throws TelegramApiException { + if(promoteChatMember == null){ + throw new TelegramApiException("Parameter promoteChatMember can not be null"); + } + return sendApiMethod(promoteChatMember); + } + + public final Boolean restrictChatMember(RestrictChatMember restrictChatMember) throws TelegramApiException { + if(restrictChatMember == null){ + throw new TelegramApiException("Parameter restrictChatMember can not be null"); + } + return sendApiMethod(restrictChatMember); + } + + public final Boolean setChatDescription(SetChatDescription setChatDescription) throws TelegramApiException { + if(setChatDescription == null){ + throw new TelegramApiException("Parameter setChatDescription can not be null"); + } + return sendApiMethod(setChatDescription); + } + + public final Boolean setChatTitle(SetChatTitle setChatTitle) throws TelegramApiException { + if(setChatTitle == null){ + throw new TelegramApiException("Parameter setChatTitle can not be null"); + } + return sendApiMethod(setChatTitle); + } + // Send Requests Async public final void sendMessageAsync(SendMessage sendMessage, SentCallback sentCallback) throws TelegramApiException { @@ -376,6 +453,16 @@ public abstract class AbsSender { sendApiMethodAsync(getChat, sentCallback); } + public final void exportChatInviteLinkAsync(ExportChatInviteLink exportChatInviteLink, SentCallback sentCallback) throws TelegramApiException { + if (exportChatInviteLink == null) { + throw new TelegramApiException("Parameter exportChatInviteLink can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(exportChatInviteLink, sentCallback); + } + public final void getChatAdministratorsAsync(GetChatAdministrators getChatAdministrators, SentCallback> sentCallback) throws TelegramApiException { if (getChatAdministrators == null) { throw new TelegramApiException("Parameter getChatAdministrators can not be null"); @@ -570,6 +657,76 @@ public abstract class AbsSender { sendApiMethodAsync(deleteMessage, sentCallback); } + public final void deleteChatPhoto(DeleteChatPhoto deleteChatPhoto, SentCallback sentCallback) throws TelegramApiException { + if (deleteChatPhoto == null) { + throw new TelegramApiException("Parameter deleteChatPhoto can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(deleteChatPhoto, sentCallback); + } + + public final void pinChatMessage(PinChatMessage pinChatMessage, SentCallback sentCallback) throws TelegramApiException { + if (pinChatMessage == null) { + throw new TelegramApiException("Parameter pinChatMessage can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(pinChatMessage, sentCallback); + } + + public final void unpinChatMessage(UnpinChatMessage unpinChatMessage, SentCallback sentCallback) throws TelegramApiException { + if (unpinChatMessage == null) { + throw new TelegramApiException("Parameter unpinChatMessage can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(unpinChatMessage, sentCallback); + } + + public final void promoteChatMember(PromoteChatMember promoteChatMember, SentCallback sentCallback) throws TelegramApiException { + if (promoteChatMember == null) { + throw new TelegramApiException("Parameter promoteChatMember can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(promoteChatMember, sentCallback); + } + + public final void restrictChatMember(RestrictChatMember restrictChatMember, SentCallback sentCallback) throws TelegramApiException { + if (restrictChatMember == null) { + throw new TelegramApiException("Parameter restrictChatMember can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(restrictChatMember, sentCallback); + } + + public final void setChatDescription(SetChatDescription setChatDescription, SentCallback sentCallback) throws TelegramApiException { + if (setChatDescription == null) { + throw new TelegramApiException("Parameter setChatDescription can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(setChatDescription, sentCallback); + } + + public final void setChatTitle(SetChatTitle setChatTitle, SentCallback sentCallback) throws TelegramApiException { + if (setChatTitle == null) { + throw new TelegramApiException("Parameter setChatTitle can not be null"); + } + if (sentCallback == null) { + throw new TelegramApiException("Parameter sentCallback can not be null"); + } + sendApiMethodAsync(setChatTitle, sentCallback); + } + // Specific Send Requests public abstract Message sendDocument(SendDocument sendDocument) throws TelegramApiException; @@ -598,6 +755,14 @@ public abstract class AbsSender { */ public abstract Message sendVoice(SendVoice sendVoice) throws TelegramApiException; + /** + * Set chat profile photo (https://core.telegram.org/bots/api#setChatPhoto) + * @param setChatPhoto Information to set the photo + * @return If success, true is returned + * @throws TelegramApiException If there is any error setting the photo. + */ + public abstract Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException; + // Simplified methods protected abstract , Callback extends SentCallback> void sendApiMethodAsync(Method method, Callback callback); diff --git a/telegrambots/pom.xml b/telegrambots/pom.xml index 357ea738..53689be0 100644 --- a/telegrambots/pom.xml +++ b/telegrambots/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.telegram telegrambots - 3.0.2 + 3.1.0 jar Telegram Bots @@ -66,7 +66,7 @@ 2.8.7 2.8.0 2.5 - 3.0.2 + 3.1.0 diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java index d177e778..c044f8cb 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -3,9 +3,7 @@ package org.telegram.telegrambots.bots; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.io.FileUtils; import org.apache.http.HttpEntity; -import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.NoopHostnameVerifier; @@ -15,9 +13,9 @@ import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.telegram.telegrambots.api.methods.BotApiMethod; +import org.telegram.telegrambots.api.methods.groupadministration.SetChatPhoto; import org.telegram.telegrambots.api.methods.send.*; import org.telegram.telegrambots.api.objects.File; import org.telegram.telegrambots.api.objects.Message; @@ -32,8 +30,6 @@ import java.io.Serializable; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -189,9 +185,10 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendDocument.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId()); if (sendDocument.isNewDocument()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId()); if (sendDocument.getNewDocumentFile() != null) { builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentFile()); } else if (sendDocument.getNewDocumentStream() != null) { @@ -199,38 +196,23 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, new java.io.File(sendDocument.getDocument()), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName()); } - if (sendDocument.getReplyMarkup() != null) { - builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendDocument.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendDocument.getReplyToMessageId() != null) { - builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString()); - } - if (sendDocument.getCaption() != null) { - builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendDocument.getDisableNotification() != null) { - builder.addTextBody(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendDocument.CHATID_FIELD, sendDocument.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendDocument.DOCUMENT_FIELD, sendDocument.getDocument())); - if (sendDocument.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendDocument.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendDocument.getReplyMarkup()))); - } - if (sendDocument.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString())); - } - if (sendDocument.getCaption() != null) { - nameValuePairs.add(new BasicNameValuePair(SendDocument.CAPTION_FIELD, sendDocument.getCaption())); - } - if (sendDocument.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendDocument.DOCUMENT_FIELD, sendDocument.getDocument()); } + if (sendDocument.getReplyMarkup() != null) { + builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendDocument.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendDocument.getReplyToMessageId() != null) { + builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString()); + } + if (sendDocument.getCaption() != null) { + builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendDocument.getDisableNotification() != null) { + builder.addTextBody(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -256,9 +238,10 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendPhoto.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); if (sendPhoto.isNewPhoto()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); if (sendPhoto.getNewPhotoFile() != null) { builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoFile()); } else if (sendPhoto.getNewPhotoStream() != null) { @@ -266,38 +249,23 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendPhoto.PHOTO_FIELD, new java.io.File(sendPhoto.getPhoto()), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName()); } - if (sendPhoto.getReplyMarkup() != null) { - builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendPhoto.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendPhoto.getReplyToMessageId() != null) { - builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString()); - } - if (sendPhoto.getCaption() != null) { - builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendPhoto.getDisableNotification() != null) { - builder.addTextBody(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendPhoto.CHATID_FIELD, sendPhoto.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendPhoto.PHOTO_FIELD, sendPhoto.getPhoto())); - if (sendPhoto.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendPhoto.getReplyMarkup()))); - } - if (sendPhoto.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString())); - } - if (sendPhoto.getCaption() != null) { - nameValuePairs.add(new BasicNameValuePair(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption())); - } - if (sendPhoto.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendPhoto.PHOTO_FIELD, sendPhoto.getPhoto()); } + if (sendPhoto.getReplyMarkup() != null) { + builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendPhoto.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendPhoto.getReplyToMessageId() != null) { + builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString()); + } + if (sendPhoto.getCaption() != null) { + builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendPhoto.getDisableNotification() != null) { + builder.addTextBody(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -323,9 +291,10 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendVideo.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); if (sendVideo.isNewVideo()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); if (sendVideo.getNewVideoFile() != null) { builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoFile()); } else if (sendVideo.getNewVideoStream() != null) { @@ -333,56 +302,32 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendVideo.VIDEO_FIELD, new java.io.File(sendVideo.getVideo()), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName()); } - if (sendVideo.getReplyMarkup() != null) { - builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideo.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendVideo.getReplyToMessageId() != null) { - builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString()); - } - if (sendVideo.getCaption() != null) { - builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendVideo.getDuration() != null) { - builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString()); - } - if (sendVideo.getWidth() != null) { - builder.addTextBody(SendVideo.WIDTH_FIELD, sendVideo.getWidth().toString()); - } - if (sendVideo.getHeight() != null) { - builder.addTextBody(SendVideo.HEIGHT_FIELD, sendVideo.getHeight().toString()); - } - if (sendVideo.getDisableNotification() != null) { - builder.addTextBody(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendVideo.CHATID_FIELD, sendVideo.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendVideo.VIDEO_FIELD, sendVideo.getVideo())); - if (sendVideo.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideo.getReplyMarkup()))); - } - if (sendVideo.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString())); - } - if (sendVideo.getCaption() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.CAPTION_FIELD, sendVideo.getCaption())); - } - if (sendVideo.getDuration() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString())); - } - if (sendVideo.getWidth() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.WIDTH_FIELD, sendVideo.getWidth().toString())); - } - if (sendVideo.getHeight() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.HEIGHT_FIELD, sendVideo.getHeight().toString())); - } - if (sendVideo.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendVideo.VIDEO_FIELD, sendVideo.getVideo()); } + if (sendVideo.getReplyMarkup() != null) { + builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideo.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendVideo.getReplyToMessageId() != null) { + builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString()); + } + if (sendVideo.getCaption() != null) { + builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendVideo.getDuration() != null) { + builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString()); + } + if (sendVideo.getWidth() != null) { + builder.addTextBody(SendVideo.WIDTH_FIELD, sendVideo.getWidth().toString()); + } + if (sendVideo.getHeight() != null) { + builder.addTextBody(SendVideo.HEIGHT_FIELD, sendVideo.getHeight().toString()); + } + if (sendVideo.getDisableNotification() != null) { + builder.addTextBody(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -408,9 +353,10 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendVideoNote.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId()); if (sendVideoNote.isNewVideoNote()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId()); if (sendVideoNote.getNewVideoNoteFile() != null) { builder.addBinaryBody(SendVideoNote.VIDEONOTE_FIELD, sendVideoNote.getNewVideoNoteFile()); } else if (sendVideoNote.getNewVideoNoteStream() != null) { @@ -418,44 +364,27 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendVideoNote.VIDEONOTE_FIELD, new java.io.File(sendVideoNote.getVideoNote()), ContentType.APPLICATION_OCTET_STREAM, sendVideoNote.getVideoNoteName()); } - if (sendVideoNote.getReplyMarkup() != null) { - builder.addTextBody(SendVideoNote.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideoNote.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendVideoNote.getReplyToMessageId() != null) { - builder.addTextBody(SendVideoNote.REPLYTOMESSAGEID_FIELD, sendVideoNote.getReplyToMessageId().toString()); - } - if (sendVideoNote.getDuration() != null) { - builder.addTextBody(SendVideoNote.DURATION_FIELD, sendVideoNote.getDuration().toString()); - } - if (sendVideoNote.getLength() != null) { - builder.addTextBody(SendVideoNote.LENGTH_FIELD, sendVideoNote.getLength().toString()); - } - if (sendVideoNote.getDisableNotification() != null) { - builder.addTextBody(SendVideoNote.DISABLENOTIFICATION_FIELD, sendVideoNote.getDisableNotification().toString()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.VIDEONOTE_FIELD, sendVideoNote.getVideoNote())); - if (sendVideoNote.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideoNote.getReplyMarkup()))); - } - if (sendVideoNote.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.REPLYTOMESSAGEID_FIELD, sendVideoNote.getReplyToMessageId().toString())); - } - if (sendVideoNote.getDuration() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.DURATION_FIELD, sendVideoNote.getDuration().toString())); - } - if (sendVideoNote.getLength() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.LENGTH_FIELD, sendVideoNote.getLength().toString())); - } - if (sendVideoNote.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVideoNote.DISABLENOTIFICATION_FIELD, sendVideoNote.getDisableNotification().toString())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendVideoNote.VIDEONOTE_FIELD, sendVideoNote.getVideoNote()); } + if (sendVideoNote.getReplyMarkup() != null) { + builder.addTextBody(SendVideoNote.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideoNote.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendVideoNote.getReplyToMessageId() != null) { + builder.addTextBody(SendVideoNote.REPLYTOMESSAGEID_FIELD, sendVideoNote.getReplyToMessageId().toString()); + } + if (sendVideoNote.getDuration() != null) { + builder.addTextBody(SendVideoNote.DURATION_FIELD, sendVideoNote.getDuration().toString()); + } + if (sendVideoNote.getLength() != null) { + builder.addTextBody(SendVideoNote.LENGTH_FIELD, sendVideoNote.getLength().toString()); + } + if (sendVideoNote.getDisableNotification() != null) { + builder.addTextBody(SendVideoNote.DISABLENOTIFICATION_FIELD, sendVideoNote.getDisableNotification().toString()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); + try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -481,9 +410,10 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendSticker.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); if (sendSticker.isNewSticker()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); if (sendSticker.getNewStickerFile() != null) { builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerFile()); } else if (sendSticker.getNewStickerStream() != null) { @@ -491,32 +421,20 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendSticker.STICKER_FIELD, new java.io.File(sendSticker.getSticker()), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName()); } - if (sendSticker.getReplyMarkup() != null) { - builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendSticker.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendSticker.getReplyToMessageId() != null) { - builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString()); - } - if (sendSticker.getDisableNotification() != null) { - builder.addTextBody(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendSticker.CHATID_FIELD, sendSticker.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendSticker.STICKER_FIELD, sendSticker.getSticker())); - if (sendSticker.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendSticker.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendSticker.getReplyMarkup()))); - } - if (sendSticker.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString())); - } - if (sendSticker.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendSticker.STICKER_FIELD, sendSticker.getSticker()); } + if (sendSticker.getReplyMarkup() != null) { + builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendSticker.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendSticker.getReplyToMessageId() != null) { + builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString()); + } + if (sendSticker.getDisableNotification() != null) { + builder.addTextBody(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -548,9 +466,9 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendAudio.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); if (sendAudio.isNewAudio()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); if (sendAudio.getNewAudioFile() != null) { builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioFile()); } else if (sendAudio.getNewAudioStream() != null) { @@ -558,53 +476,33 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendAudio.AUDIO_FIELD, new java.io.File(sendAudio.getAudio()), ContentType.create("audio/mpeg"), sendAudio.getAudioName()); } - if (sendAudio.getReplyMarkup() != null) { - builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendAudio.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendAudio.getReplyToMessageId() != null) { - builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString()); - } - if (sendAudio.getPerformer() != null) { - builder.addTextBody(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer()); - } - if (sendAudio.getTitle() != null) { - builder.addTextBody(SendAudio.TITLE_FIELD, sendAudio.getTitle()); - } - if(sendAudio.getDuration() != null){ - builder.addTextBody(SendAudio.DURATION_FIELD, sendAudio.getDuration().toString()); - } - if (sendAudio.getDisableNotification() != null) { - builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString()); - } - if (sendAudio.getCaption() != null) { - builder.addTextBody(SendAudio.CAPTION_FIELD, sendAudio.getCaption()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendAudio.CHATID_FIELD, sendAudio.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendAudio.AUDIO_FIELD, sendAudio.getAudio())); - if (sendAudio.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendAudio.getReplyMarkup()))); - } - if (sendAudio.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString())); - } - if (sendAudio.getPerformer() != null) { - nameValuePairs.add(new BasicNameValuePair(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer())); - } - if (sendAudio.getTitle() != null) { - nameValuePairs.add(new BasicNameValuePair(SendAudio.TITLE_FIELD, sendAudio.getTitle())); - } - if (sendAudio.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString())); - } - if (sendAudio.getCaption() != null) { - nameValuePairs.add(new BasicNameValuePair(SendAudio.CAPTION_FIELD, sendAudio.getCaption())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendAudio.AUDIO_FIELD, sendAudio.getAudio()); } + if (sendAudio.getReplyMarkup() != null) { + builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendAudio.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendAudio.getReplyToMessageId() != null) { + builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString()); + } + if (sendAudio.getPerformer() != null) { + builder.addTextBody(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer(), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendAudio.getTitle() != null) { + builder.addTextBody(SendAudio.TITLE_FIELD, sendAudio.getTitle(), TEXT_PLAIN_CONTENT_TYPE); + } + if(sendAudio.getDuration() != null){ + builder.addTextBody(SendAudio.DURATION_FIELD, sendAudio.getDuration().toString()); + } + if (sendAudio.getDisableNotification() != null) { + builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString()); + } + if (sendAudio.getCaption() != null) { + builder.addTextBody(SendAudio.CAPTION_FIELD, sendAudio.getCaption(), TEXT_PLAIN_CONTENT_TYPE); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); + try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -637,9 +535,9 @@ public abstract class DefaultAbsSender extends AbsSender { String url = getBaseUrl() + SendVoice.PATH; HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); if (sendVoice.isNewVoice()) { - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); if (sendVoice.getNewVoiceFile() != null) { builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceFile()); } else if (sendVoice.getNewVoiceStream() != null) { @@ -647,44 +545,26 @@ public abstract class DefaultAbsSender extends AbsSender { } else { builder.addBinaryBody(SendVoice.VOICE_FIELD, new java.io.File(sendVoice.getVoice()), ContentType.create("audio/ogg"), sendVoice.getVoiceName()); } - if (sendVoice.getReplyMarkup() != null) { - builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVoice.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); - } - if (sendVoice.getReplyToMessageId() != null) { - builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplyToMessageId().toString()); - } - if (sendVoice.getDisableNotification() != null) { - builder.addTextBody(SendVoice.DISABLENOTIFICATION_FIELD, sendVoice.getDisableNotification().toString()); - } - if (sendVoice.getDuration() != null) { - builder.addTextBody(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString()); - } - if (sendVoice.getCaption() != null) { - builder.addTextBody(SendVoice.CAPTION_FIELD, sendVoice.getCaption()); - } - HttpEntity multipart = builder.build(); - httppost.setEntity(multipart); } else { - List nameValuePairs = new ArrayList<>(); - nameValuePairs.add(new BasicNameValuePair(SendVoice.CHATID_FIELD, sendVoice.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendVoice.VOICE_FIELD, sendVoice.getVoice())); - if (sendVoice.getReplyMarkup() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVoice.getReplyMarkup()))); - } - if (sendVoice.getReplyToMessageId() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplyToMessageId().toString())); - } - if (sendVoice.getDisableNotification() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVoice.DISABLENOTIFICATION_FIELD, sendVoice.getDisableNotification().toString())); - } - if (sendVoice.getDuration() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString())); - } - if (sendVoice.getCaption() != null) { - nameValuePairs.add(new BasicNameValuePair(SendVoice.CAPTION_FIELD, sendVoice.getCaption())); - } - httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8)); + builder.addTextBody(SendVoice.VOICE_FIELD, sendVoice.getVoice()); } + if (sendVoice.getReplyMarkup() != null) { + builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVoice.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE); + } + if (sendVoice.getReplyToMessageId() != null) { + builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplyToMessageId().toString()); + } + if (sendVoice.getDisableNotification() != null) { + builder.addTextBody(SendVoice.DISABLENOTIFICATION_FIELD, sendVoice.getDisableNotification().toString()); + } + if (sendVoice.getDuration() != null) { + builder.addTextBody(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString()); + } + if (sendVoice.getCaption() != null) { + builder.addTextBody(SendVoice.CAPTION_FIELD, sendVoice.getCaption(), TEXT_PLAIN_CONTENT_TYPE); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); try (CloseableHttpResponse response = httpclient.execute(httppost)) { HttpEntity ht = response.getEntity(); @@ -692,12 +572,47 @@ public abstract class DefaultAbsSender extends AbsSender { responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); } } catch (IOException e) { - throw new TelegramApiException("Unable to send sticker", e); + throw new TelegramApiException("Unable to send voice", e); } return sendVoice.deserializeResponse(responseContent); } + @Override + public Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException { + if(setChatPhoto == null){ + throw new TelegramApiException("Parameter setChatPhoto can not be null"); + } + setChatPhoto.validate(); + String responseContent; + + try { + String url = getBaseUrl() + SetChatPhoto.PATH; + HttpPost httppost = new HttpPost(url); + httppost.setConfig(requestConfig); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SetChatPhoto.CHATID_FIELD, setChatPhoto.getChatId()); + if (setChatPhoto.getPhoto() != null) { + builder.addBinaryBody(SetChatPhoto.PHOTO_FIELD, setChatPhoto.getPhoto()); + } else if (setChatPhoto.getPhotoStream() != null) { + builder.addBinaryBody(SetChatPhoto.PHOTO_FIELD, setChatPhoto.getPhotoStream(), ContentType.APPLICATION_OCTET_STREAM, setChatPhoto.getPhotoName()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); + + try (CloseableHttpResponse response = httpclient.execute(httppost)) { + HttpEntity ht = response.getEntity(); + BufferedHttpEntity buf = new BufferedHttpEntity(ht); + responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); + } + } catch (IOException e) { + throw new TelegramApiException("Unable to set chat photo", e); + } + + return setChatPhoto.deserializeResponse(responseContent); + } + // Simplified methods @Override