|
| 1 | +package org.telegram.telegrambots.meta.api.methods.groupadministration; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import lombok.AllArgsConstructor; |
| 6 | +import lombok.Builder; |
| 7 | +import lombok.EqualsAndHashCode; |
| 8 | +import lombok.Getter; |
| 9 | +import lombok.NonNull; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.Setter; |
| 12 | +import lombok.ToString; |
| 13 | +import lombok.experimental.SuperBuilder; |
| 14 | +import lombok.experimental.Tolerate; |
| 15 | +import lombok.extern.jackson.Jacksonized; |
| 16 | +import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethod; |
| 17 | +import org.telegram.telegrambots.meta.api.objects.ChatInviteLink; |
| 18 | +import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; |
| 19 | +import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Ruben Bermudez |
| 23 | + * @version 7.9 |
| 24 | + * |
| 25 | + * Use this method to create a subscription invite link for a channel chat. |
| 26 | + * Returns the new invite link as a ChatInviteLink object. |
| 27 | + * |
| 28 | + * @apiNote The bot must have can_invite_users administrator rights. |
| 29 | + * @apiNote The link can be edited using the method editChatSubscriptionInviteLink or revoked using the method revokeChatInviteLink. |
| 30 | + */ |
| 31 | +@EqualsAndHashCode(callSuper = false) |
| 32 | +@Getter |
| 33 | +@Setter |
| 34 | +@ToString |
| 35 | +@AllArgsConstructor |
| 36 | +@RequiredArgsConstructor |
| 37 | +@SuperBuilder |
| 38 | +@Jacksonized |
| 39 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 40 | +public class CreateChatSubscriptionInviteLink extends BotApiMethod<ChatInviteLink> { |
| 41 | + public static final String PATH = "createChatSubscriptionInviteLink"; |
| 42 | + |
| 43 | + private static final String CHAT_ID_FIELD = "chat_id"; |
| 44 | + private static final String SUBSCRIPTION_PERIOD_FIELD = "subscription_period"; |
| 45 | + private static final String SUBSCRIPTION_PRICE_FIELD = "subscription_price"; |
| 46 | + private static final String NAME_FIELD = "name"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Unique identifier for the target channel chat or username of the target channel (in the format @channelusername) |
| 50 | + */ |
| 51 | + @JsonProperty(CHAT_ID_FIELD) |
| 52 | + @NonNull |
| 53 | + private String chatId; |
| 54 | + /** |
| 55 | + * The number of seconds the subscription will be active for before the next payment. |
| 56 | + * Currently, it must always be 2592000 (30 days). |
| 57 | + */ |
| 58 | + @JsonProperty(SUBSCRIPTION_PERIOD_FIELD) |
| 59 | + @NonNull |
| 60 | + @Builder.Default |
| 61 | + private Integer subscriptionPeriod = 2592000; |
| 62 | + /** |
| 63 | + * The amount of Telegram Stars a user must pay initially and after each subsequent subscription period to be a member of the chat; 1-2500 |
| 64 | + */ |
| 65 | + @JsonProperty(SUBSCRIPTION_PRICE_FIELD) |
| 66 | + @NonNull |
| 67 | + private Integer subscriptionPrice; |
| 68 | + /** |
| 69 | + * Optional |
| 70 | + * Invite link name; 0-32 characters |
| 71 | + */ |
| 72 | + @JsonProperty(NAME_FIELD) |
| 73 | + private String name; |
| 74 | + |
| 75 | + @Tolerate |
| 76 | + public void setChatId(@NonNull Long chatId) { |
| 77 | + this.chatId = chatId.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String getMethod() { |
| 82 | + return PATH; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public ChatInviteLink deserializeResponse(String answer) throws TelegramApiRequestException { |
| 87 | + return deserializeResponse(answer, ChatInviteLink.class); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void validate() throws TelegramApiValidationException { |
| 92 | + if (chatId.isEmpty()) { |
| 93 | + throw new TelegramApiValidationException("ChatId can't be empty", this); |
| 94 | + } |
| 95 | + if (name != null && name.length() > 32) { |
| 96 | + throw new TelegramApiValidationException("Name must be between 0 and 32 characters", this); |
| 97 | + } |
| 98 | + if (subscriptionPeriod != 2592000) { |
| 99 | + throw new TelegramApiValidationException("SubscriptionPeriod must be 2592000", this); |
| 100 | + } |
| 101 | + if (subscriptionPrice < 1 || subscriptionPrice > 2500) { |
| 102 | + throw new TelegramApiValidationException("SubscriptionPrice must be between 1 and 2500", this); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static abstract class CreateChatSubscriptionInviteLinkBuilder<C extends CreateChatSubscriptionInviteLink, B extends CreateChatSubscriptionInviteLinkBuilder<C, B>> extends BotApiMethodBuilder<ChatInviteLink, C, B> { |
| 107 | + @Tolerate |
| 108 | + public CreateChatSubscriptionInviteLinkBuilder<C, B> chatId(@NonNull Long chatId) { |
| 109 | + this.chatId = chatId.toString(); |
| 110 | + return this; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments