TelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/groupadministration/GetChatMembersCount.java

68 lines
2.2 KiB
Java
Raw Normal View History

2018-11-07 11:34:50 +01:00
package org.telegram.telegrambots.meta.api.methods.groupadministration;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
2020-11-01 23:46:36 +01:00
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
2018-11-07 11:34:50 +01:00
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
2019-06-08 21:33:28 +02:00
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
2018-11-07 11:34:50 +01:00
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
2019-01-02 15:16:03 +01:00
import java.io.IOException;
2018-11-07 11:34:50 +01:00
/**
* @author Ruben Bermudez
* @version 1.0
2019-01-02 15:16:03 +01:00
* Use this method to get the number of members in a chat. Returns Int on success.
2018-11-07 11:34:50 +01:00
*/
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
2018-11-07 11:34:50 +01:00
public class GetChatMembersCount extends BotApiMethod<Integer> {
public static final String PATH = "getChatMembersCount";
private static final String CHATID_FIELD = "chat_id";
@JsonProperty(CHATID_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2018-11-07 11:34:50 +01:00
private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels)
@Override
public String getMethod() {
return PATH;
}
@Override
public Integer deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Integer> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Integer>>(){});
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error getting chat members count", 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);
}
}
}