TelegramBots/src/main/java/org/telegram/telegrambots/api/methods/groupadministration/LeaveChat.java
Rubenlagus 5903cd6338 1. File downloads
2. Update with latest api changes
3. Improve exceptions
4. Added validation in api methods
5. Moved to maven central
2016-09-24 22:37:25 +02:00

87 lines
2.4 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 for your bot to leave a group, supergroup or channel. Returns True on success.
* @date 20 of May of 2016
*/
public class LeaveChat extends BotApiMethod<Boolean> {
public static final String PATH = "leaveChat";
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 LeaveChat() {
super();
}
public String getChatId() {
return chatId;
}
public LeaveChat 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 Boolean deserializeResponse(JSONObject answer) {
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return answer.getBoolean(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 "LeaveChat{" +
"chatId='" + chatId + '\'' +
'}';
}
}