2. Update with latest api changes 3. Improve exceptions 4. Added validation in api methods 5. Moved to maven central
99 lines
3.1 KiB
Java
99 lines
3.1 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.JSONArray;
|
|
import org.json.JSONObject;
|
|
import org.telegram.telegrambots.Constants;
|
|
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
|
import org.telegram.telegrambots.api.objects.ChatMember;
|
|
import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
* @author Ruben Bermudez
|
|
* @version 1.0
|
|
* @brief Use this method to get a list of administrators in a chat.
|
|
* An Array of ChatMember objects is returned on success,
|
|
* containing information about all chat administrators except other bots.
|
|
* If the chat is a group or a supergroup and no administrators were appointed,
|
|
* only the creator will be returned.
|
|
* @date 20 of May of 2016
|
|
*/
|
|
public class GetChatAdministrators extends BotApiMethod<ArrayList<ChatMember>> {
|
|
public static final String PATH = "getChatAdministrators";
|
|
|
|
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 GetChatAdministrators() {
|
|
super();
|
|
}
|
|
|
|
public String getChatId() {
|
|
return chatId;
|
|
}
|
|
|
|
public GetChatAdministrators 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 ArrayList<ChatMember> deserializeResponse(JSONObject answer) {
|
|
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
|
|
JSONArray admins = answer.getJSONArray(Constants.RESPONSEFIELDRESULT);
|
|
ArrayList<ChatMember> members = new ArrayList<>();
|
|
for (int i = 0; i < admins.length(); i++) {
|
|
members.add(new ChatMember(admins.getJSONObject(i)));
|
|
}
|
|
return members;
|
|
}
|
|
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 "GetChatAdministrators{" +
|
|
"chatId='" + chatId + '\'' +
|
|
'}';
|
|
}
|
|
}
|