5903cd6338
2. Update with latest api changes 3. Improve exceptions 4. Added validation in api methods 5. Moved to maven central
87 lines
2.5 KiB
Java
87 lines
2.5 KiB
Java
package org.telegram.telegrambots.api.methods.groupadministration;
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
|
|
|
import org.json.JSONObject;
|
|
import org.telegram.telegrambots.Constants;
|
|
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
|
import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
|
|
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* @author Ruben Bermudez
|
|
* @version 1.0
|
|
* @brief Use this method to get the number of members in a chat. Returns Int on success.
|
|
* @date 20 of May of 2016
|
|
*/
|
|
public class GetChatMemberCount extends BotApiMethod<Integer> {
|
|
public static final String PATH = "getChatMembersCount";
|
|
|
|
private static final String CHATID_FIELD = "chat_id";
|
|
private String chatId; ///< Unique identifier for the chat to send the message to (Or username for channels)
|
|
|
|
public GetChatMemberCount() {
|
|
super();
|
|
}
|
|
|
|
public String getChatId() {
|
|
return chatId;
|
|
}
|
|
|
|
public GetChatMemberCount setChatId(String chatId) {
|
|
this.chatId = chatId;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public JSONObject toJson() {
|
|
JSONObject jsonObject = new JSONObject();
|
|
jsonObject.put(CHATID_FIELD, chatId);
|
|
return jsonObject;
|
|
}
|
|
|
|
@Override
|
|
public String getPath() {
|
|
return PATH;
|
|
}
|
|
|
|
@Override
|
|
public Integer deserializeResponse(JSONObject answer) {
|
|
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
|
|
return answer.getInt(Constants.RESPONSEFIELDRESULT);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void validate() throws TelegramApiValidationException {
|
|
if (chatId == null) {
|
|
throw new TelegramApiValidationException("ChatId can't be null", this);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
|
gen.writeStartObject();
|
|
gen.writeStringField(METHOD_FIELD, PATH);
|
|
gen.writeStringField(CHATID_FIELD, chatId);
|
|
gen.writeEndObject();
|
|
gen.flush();
|
|
}
|
|
|
|
@Override
|
|
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
|
|
serialize(gen, serializers);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "GetChatMemberCount{" +
|
|
"chatId='" + chatId + '\'' +
|
|
'}';
|
|
}
|
|
}
|