Update to Api Layer 4.4

This commit is contained in:
rubenlagus 2019-07-28 21:13:58 +01:00 committed by Ruben Bermudez
parent 830b873776
commit fd342ad7a7
8 changed files with 327 additions and 17 deletions

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.ChatPermissions;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
@ -24,6 +25,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
* Pass True for all boolean parameters to lift restrictions from a user. Returns True on success.
*
*/
@SuppressWarnings("WeakerAccess")
public class RestrictChatMember extends BotApiMethod<Boolean> {
public static final String PATH = "restrictchatmember";
@ -34,6 +36,7 @@ public class RestrictChatMember extends BotApiMethod<Boolean> {
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";
private static final String PERMISSIONS_FIELD = "permissions";
@JsonProperty(CHATID_FIELD)
private String chatId; ///< Required. Unique identifier for the chat to send the message to (Or username for channels)
@ -49,7 +52,14 @@ public class RestrictChatMember extends BotApiMethod<Boolean> {
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
/**
* Optional
* 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 restricted forever
*/
@JsonProperty(PERMISSIONS_FIELD)
private ChatPermissions permissions;
public RestrictChatMember() {
super();
@ -108,46 +118,87 @@ public class RestrictChatMember extends BotApiMethod<Boolean> {
return setUntilDate(date.toInstant());
}
@JsonIgnore
public RestrictChatMember forTimePeriod(Duration duration) {
return setUntilDate(Instant.now().plusMillis(duration.toMillis()));
}
/**
* @deprecated Use {@link #getPermissions()} instead
*/
@Deprecated
public Boolean getCanSendMessages() {
return canSendMessages;
}
/**
* @deprecated Use {@link #setPermissions(ChatPermissions)} instead
*/
@Deprecated
public RestrictChatMember setCanSendMessages(Boolean canSendMessages) {
this.canSendMessages = canSendMessages;
return this;
}
/**
* @deprecated Use {@link #getPermissions()} instead
*/
@Deprecated
public Boolean getCanSendMediaMessages() {
return canSendMediaMessages;
}
/**
* @deprecated Use {@link #setPermissions(ChatPermissions)} instead
*/
@Deprecated
public RestrictChatMember setCanSendMediaMessages(Boolean canSendMediaMessages) {
this.canSendMediaMessages = canSendMediaMessages;
return this;
}
/**
* @deprecated Use {@link #getPermissions()} instead
*/
@Deprecated
public Boolean getCanSendOtherMessages() {
return canSendOtherMessages;
}
/**
* @deprecated Use {@link #setPermissions(ChatPermissions)} instead
*/
@Deprecated
public RestrictChatMember setCanSendOtherMessages(Boolean canSendOtherMessages) {
this.canSendOtherMessages = canSendOtherMessages;
return this;
}
/**
* @deprecated Use {@link #getPermissions()} instead
*/
@Deprecated
public Boolean getCanAddWebPagePreviews() {
return canAddWebPagePreviews;
}
/**
* @deprecated Use {@link #setPermissions(ChatPermissions)} instead
*/
@Deprecated
public RestrictChatMember setCanAddWebPagePreviews(Boolean canAddWebPagePreviews) {
this.canAddWebPagePreviews = canAddWebPagePreviews;
return this;
}
public ChatPermissions getPermissions() {
return permissions;
}
public void setPermissions(ChatPermissions permissions) {
this.permissions = permissions;
}
@Override
public String getMethod() {
return PATH;
@ -176,6 +227,9 @@ public class RestrictChatMember extends BotApiMethod<Boolean> {
if (userId == null) {
throw new TelegramApiValidationException("UserId can't be empty", this);
}
if (permissions == null) {
throw new TelegramApiValidationException("Permissions can't be empty", this);
}
}
@Override
@ -188,6 +242,7 @@ public class RestrictChatMember extends BotApiMethod<Boolean> {
", canSendMediaMessages=" + canSendMediaMessages +
", canSendOtherMessages=" + canSendOtherMessages +
", canAddWebPagePreviews=" + canAddWebPagePreviews +
", permissions=" + permissions +
'}';
}
}

View File

@ -0,0 +1,112 @@
package org.telegram.telegrambots.meta.api.methods.groupadministration;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.ChatPermissions;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.IOException;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Ruben Bermudez
* @version 4.4
* Use this method to set default chat permissions for all members.
* The bot must be an administrator in the group or a supergroup
* for this to work and must have the can_restrict_members admin rights.
*/
public class SetChatPermissions extends BotApiMethod<Boolean> {
public static final String PATH = "setChatPermissions";
private static final String CHAT_ID_FIELD = "chat_id";
private static final String PERMISSIONS_FIELD = "permissions";
@JsonProperty(CHAT_ID_FIELD)
private String chatId; ///< Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
@JsonProperty(PERMISSIONS_FIELD)
private ChatPermissions permissions; ///< New default chat permissions
public SetChatPermissions() {
super();
}
public SetChatPermissions(String chatId, ChatPermissions permissions) {
super();
this.chatId = checkNotNull(chatId);
this.permissions = checkNotNull(permissions);
}
public SetChatPermissions(Long chatId, ChatPermissions permissions) {
super();
this.chatId = checkNotNull(chatId).toString();
this.permissions = checkNotNull(permissions);
}
public String getChatId() {
return chatId;
}
public SetChatPermissions setChatId(String chatId) {
this.chatId = chatId;
return this;
}
public SetChatPermissions setChatId(Long chatId) {
Objects.requireNonNull(chatId);
this.chatId = chatId.toString();
return this;
}
public ChatPermissions getPermissions() {
return permissions;
}
public SetChatPermissions setPermissions(ChatPermissions permissions) {
Objects.requireNonNull(permissions);
this.permissions = permissions;
return this;
}
@Override
public String getMethod() {
return PATH;
}
@Override
public Boolean deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Boolean> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Boolean>>(){});
if (result.getOk()) {
return result.getResult();
} else {
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 (permissions == null) {
throw new TelegramApiValidationException("Permissions can't be null", this);
}
}
@Override
public String toString() {
return "SetChatPermissions{" +
"chatId='" + chatId + '\'' +
", permissions=" + permissions +
'}';
}
}

View File

@ -25,6 +25,7 @@ public class Chat implements BotApiObject {
private static final String PINNEDMESSAGE_FIELD = "pinned_message";
private static final String STICKERSETNAME_FIELD = "sticker_set_name";
private static final String CANSETSTICKERSET_FIELD = "can_set_sticker_set";
private static final String PERMISSIONS_FIELD = "can_set_sticker_set";
private static final String USERCHATTYPE = "private";
private static final String GROUPCHATTYPE = "group";
@ -57,15 +58,23 @@ public class Chat implements BotApiObject {
@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.
private String description; ///< Optional. Description, for groups, supergroups and channel chats. Returned only in getChat.
/**
* Optional. Chat invite link, for groups, supergroups and channel chats.
* Each administrator in a chat generates their own invite links, so the bot must first generate the link using
* exportChatInviteLink.
* Each Returned only in getChat.
*/
@JsonProperty(INVITELINK_FIELD)
private String inviteLink; ///< Optional. Chat invite link, for supergroups and channel chats. Returned only in getChat.
private String inviteLink;
@JsonProperty(PINNEDMESSAGE_FIELD)
private Message pinnedMessage; ///< Optional. Pinned message, for groups, supergroups and channels. Returned only in getChat.
@JsonProperty(STICKERSETNAME_FIELD)
private String stickerSetName; ///< Optional. For supergroups, name of Group sticker set. Returned only in getChat.
@JsonProperty(CANSETSTICKERSET_FIELD)
private Boolean canSetStickerSet; ///< Optional. True, if the bot can change group the sticker set. Returned only in getChat.
@JsonProperty(PERMISSIONS_FIELD)
private ChatPermissions permissions; ///< Optional. Default chat member permissions, for groups and supergroups. Returned only in getChat.
public Chat() {
super();
@ -107,6 +116,14 @@ public class Chat implements BotApiObject {
return userName;
}
public ChatPermissions getPermissions() {
return permissions;
}
/**
* @deprecated Use {@link #getPermissions()} instead
*/
@Deprecated
public Boolean getAllMembersAreAdministrators() {
return allMembersAreAdministrators;
}
@ -151,6 +168,7 @@ public class Chat implements BotApiObject {
", pinnedMessage=" + pinnedMessage +
", stickerSetName='" + stickerSetName + '\'' +
", canSetStickerSet=" + canSetStickerSet +
", permissions=" + permissions +
'}';
}
}

View File

@ -9,8 +9,7 @@ import java.time.Instant;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object contains information about one member of the chat.
* @date 20 of May of 2016
* This object contains information about one member of the chat.
*/
public class ChatMember implements BotApiObject {
private static final String USER_FIELD = "user";
@ -29,18 +28,19 @@ public class ChatMember implements BotApiObject {
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";
private static final String CAN_SEND_POLLS_FIELD = "can_send_polls";
private static final String ISMEMBER_FIELD = "is_member";
@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, restricted, 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. Restricted 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
private Boolean canChangeInformation; ///< Optional. Administrators and restricted 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)
@ -48,21 +48,23 @@ public class ChatMember implements BotApiObject {
@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
private Boolean canInviteUsers; ///< Optional. Administrators and restricted 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, groups and supergroups only
private Boolean canPinMessages; ///< Optional. Administrators and restricted only. True, if the administrator can pin messages, groups and 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 administrators 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
private Boolean canSendMessages; ///< Optional. Restricted only. True, if the user is allowed to 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
private Boolean canSendMediaMessages; ///< Optional. Restricted only. True, if the user is allowed to 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
private Boolean canSendOtherMessages; ///< Optional. Restricted only. True, if the user is allowed to 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
private Boolean canAddWebPagePreviews; ///< Optional. Restricted only. True, if the user is allowed to add web page previews to his messages
@JsonProperty(CAN_SEND_POLLS_FIELD)
private Boolean canSendPolls; ///< Optional. Restricted only. True, if the user is allowed to send polls.
@JsonProperty(ISMEMBER_FIELD)
private Boolean isMemberField; ///< True, if the user is a member of the chat at the moment of the request. For example, it can be false for the chat creator or for a restricted user.
@ -141,6 +143,14 @@ public class ChatMember implements BotApiObject {
return canAddWebPagePreviews;
}
public Boolean getCanSendPolls() {
return canSendPolls;
}
public Boolean getMemberField() {
return isMemberField;
}
@Override
public String toString() {
return "ChatMember{" +
@ -160,6 +170,8 @@ public class ChatMember implements BotApiObject {
", canSendMediaMessages=" + canSendMediaMessages +
", canSendOtherMessages=" + canSendOtherMessages +
", canAddWebPagePreviews=" + canAddWebPagePreviews +
", canSendPolls=" + canSendPolls +
", isMemberField=" + isMemberField +
'}';
}
}

View File

@ -0,0 +1,89 @@
package org.telegram.telegrambots.meta.api.objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.telegram.telegrambots.meta.api.interfaces.BotApiObject;
/**
* @author Ruben Bermudez
* @version 4.4
* Use this method to change the description of a group, supergroup or channel.
* 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 ChatPermissions implements BotApiObject {
private static final String CAN_SEND_MESSAGES_FIELD = "can_send_messages";
private static final String CAN_SEND_MEDIA_MESSAGES_FIELD = "can_send_media_messages";
private static final String CAN_SEND_POLLS_FIELD = "can_send_polls";
private static final String CAN_SEND_OTHER_MESSAGES_FIELD = "can_send_other_messages";
private static final String CAN_ADD_WEB_PAGE_PREVIEWS_FIELD = "can_add_web_page_previews";
private static final String CAN_CHANGE_INFO_FIELD = "can_change_info";
private static final String CAN_INVITE_USERS_FIELD = "can_invite_users";
private static final String CAN_PIN_MESSAGES_FIELD = "can_pin_messages";
@JsonProperty(CAN_SEND_MESSAGES_FIELD)
private Boolean canSendMessages; ///< Optional. True, if the user is allowed to send text messages, contacts, locations and venues
@JsonProperty(CAN_SEND_MEDIA_MESSAGES_FIELD)
private Boolean getCanSendMediaMessages; ///< Optional. True, if the user is allowed to send audios, documents, photos, videos, video notes and voice notes, implies can_send_messages
@JsonProperty(CAN_SEND_POLLS_FIELD)
private Boolean canSendPolls; ///< Optional. True, if the user is allowed to send polls, implies can_send_messages
@JsonProperty(CAN_SEND_OTHER_MESSAGES_FIELD)
private Boolean canSendOtherMessages; ///< Optional. True, if the user is allowed to send animations, games, stickers and use inline bots, implies can_send_media_messages
@JsonProperty(CAN_ADD_WEB_PAGE_PREVIEWS_FIELD)
private Boolean canAddWebPagePreviews; ///< Optional. True, if the user is allowed to add web page previews to their messages, implies can_send_media_messages
@JsonProperty(CAN_CHANGE_INFO_FIELD)
private Boolean canChangeInfo; ///< Optional. True, if the user is allowed to change the chat title, photo and other settings. Ignored in public supergroups
@JsonProperty(CAN_INVITE_USERS_FIELD)
private Boolean canInviteUsers; ///< Optional. True, if the user is allowed to invite new users to the chat
@JsonProperty(CAN_PIN_MESSAGES_FIELD)
private Boolean canPinMessages; ///< Optional. True, if the user is allowed to pin messages. Ignored in public supergroups
public ChatPermissions() {
super();
}
public Boolean getCanSendMessages() {
return canSendMessages;
}
public Boolean getGetCanSendMediaMessages() {
return getCanSendMediaMessages;
}
public Boolean getCanSendPolls() {
return canSendPolls;
}
public Boolean getCanSendOtherMessages() {
return canSendOtherMessages;
}
public Boolean getCanAddWebPagePreviews() {
return canAddWebPagePreviews;
}
public Boolean getCanChangeInfo() {
return canChangeInfo;
}
public Boolean getCanInviteUsers() {
return canInviteUsers;
}
public Boolean getCanPinMessages() {
return canPinMessages;
}
@Override
public String toString() {
return "ChatPermissions{" +
"canSendMessages=" + canSendMessages +
", getCanSendMediaMessages=" + getCanSendMediaMessages +
", canSendPolls=" + canSendPolls +
", canSendOtherMessages=" + canSendOtherMessages +
", canAddWebPagePreviews=" + canAddWebPagePreviews +
", canChangeInfo=" + canChangeInfo +
", canInviteUsers=" + canInviteUsers +
", canPinMessages=" + canPinMessages +
'}';
}
}

View File

@ -6,16 +6,24 @@ import org.telegram.telegrambots.meta.api.interfaces.BotApiObject;
/**
* @author Ruben Bermudez
* @version 1.0
* This object represents a chat photo.
* This object represents a chat photo (profile picture of a user, group or channel)
*/
public class ChatPhoto implements BotApiObject {
private static final String SMALLFILEID_FIELD = "small_file_id";
private static final String BIGFILEID_FIELD = "big_file_id";
/**
* Unique file identifier of a small chat photo (160x160).
* This file_id can be used only for photo download and only for as long as the photo is not changed.
*/
@JsonProperty(SMALLFILEID_FIELD)
private String smallFileId; ///< Unique file identifier of small (160x160) chat photo. This file_id can be used only for photo download.
private String smallFileId;
/**
* Unique file identifier of a big chat photo (640x640).
* This file_id can be used only for photo download and only for as long as the photo is not changed.
*/
@JsonProperty(BIGFILEID_FIELD)
private String bigFileId; ///< Unique file identifier of big (640x640) chat photo. This file_id can be used only for photo download.
private String bigFileId;
public ChatPhoto() {
super();

View File

@ -8,7 +8,7 @@ import org.telegram.telegrambots.meta.api.objects.PhotoSize;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object represents a sticker.
* This object represents a sticker.
*/
public class Sticker implements BotApiObject {
@ -20,6 +20,7 @@ public class Sticker implements BotApiObject {
private static final String EMOJI_FIELD = "emoji";
private static final String SETNAME_FIELD = "set_name";
private static final String MASKPOSITON_FIELD = "mask_position";
private static final String ISANIMATED_FIELD = "is_animated";
@JsonProperty(FILEID_FIELD)
private String fileId; ///< Unique identifier for this file
@ -37,6 +38,8 @@ public class Sticker implements BotApiObject {
private String setName; ///< Optional. Name of the sticker set to which the sticker belongs
@JsonProperty(MASKPOSITON_FIELD)
private MaskPosition maskPosition; ///< Optional. For mask stickers, the position where the mask should be placed
@JsonProperty(ISANIMATED_FIELD)
private Boolean isAnimated; ///< True, if the sticker is animated
public Sticker() {
super();
@ -74,6 +77,10 @@ public class Sticker implements BotApiObject {
return maskPosition;
}
public Boolean getAnimated() {
return isAnimated;
}
@Override
public String toString() {
return "Sticker{" +
@ -85,6 +92,7 @@ public class Sticker implements BotApiObject {
", emoji='" + emoji + '\'' +
", setName='" + setName + '\'' +
", maskPosition=" + maskPosition +
", isAnimated=" + isAnimated +
'}';
}
}

View File

@ -15,6 +15,7 @@ public class StickerSet implements BotApiObject {
private static final String TITLE_FIELD = "title";
private static final String CONTAINSMASKS_FIELD = "contains_masks";
private static final String STICKERS_FIELD = "stickers";
private static final String ISANIMATED_FIELD = "is_animated";
@JsonProperty(NAME_FIELD)
private String name;
@ -24,6 +25,8 @@ public class StickerSet implements BotApiObject {
private Boolean containsMasks;
@JsonProperty(STICKERS_FIELD)
private List<Sticker> stickers;
@JsonProperty(ISANIMATED_FIELD)
private Boolean isAnimated;
public StickerSet() {
super();
@ -45,6 +48,10 @@ public class StickerSet implements BotApiObject {
return stickers;
}
public Boolean getAnimated() {
return isAnimated;
}
@Override
public String toString() {
return "StickerSet{" +
@ -52,6 +59,7 @@ public class StickerSet implements BotApiObject {
", title='" + title + '\'' +
", containsMasks=" + containsMasks +
", stickers=" + stickers +
", isAnimated=" + isAnimated +
'}';
}
}