TDLightTelegramBots/src/main/java/org/telegram/telegrambots/api/objects/Chat.java

165 lines
5.7 KiB
Java
Raw Normal View History

2016-01-14 23:09:19 +01:00
package org.telegram.telegrambots.api.objects;
2016-01-14 01:14:53 +01:00
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
2016-04-02 18:20:49 +02:00
2016-01-14 01:14:53 +01:00
import org.json.JSONObject;
2016-01-14 23:09:19 +01:00
import org.telegram.telegrambots.api.interfaces.IBotApiObject;
2016-01-14 01:14:53 +01:00
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object represents a Telegram chat with an user or a group
* @date 24 of June of 2015
*/
2016-01-14 23:09:19 +01:00
public class Chat implements IBotApiObject {
2016-04-11 02:53:53 +02:00
private static final String ID_FIELD = "id";
private static final String TYPE_FIELD = "type";
private static final String TITLE_FIELD = "title";
private static final String FIRSTNAME_FIELD = "first_name";
private static final String LASTNAME_FIELD = "last_name";
private static final String USERNAME_FIELD = "username";
private static final String ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD = "all_members_are_administrators";
2016-01-14 01:14:53 +01:00
private static final String USERCHATTYPE = "private";
private static final String GROUPCHATTYPE = "group";
private static final String CHANNELCHATTYPE = "channel";
private static final String SUPERGROUPCHATTYPE = "supergroup";
@JsonProperty(ID_FIELD)
2016-05-31 19:08:56 +02:00
/**
* Unique identifier for this chat.
* This number may be greater than 32 bits and some programming languages may
* have difficulty/silent defects in interpreting it. But it smaller than 52 bits,
* so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
*/
2016-01-14 01:14:53 +01:00
private Long id; ///< Unique identifier for this chat, not exciding 1e13 by absolute value
@JsonProperty(TYPE_FIELD)
private String type; ///< Type of the chat, one of “private”, “group” or “channel”
@JsonProperty(TITLE_FIELD)
private String title; ///< Optional. Title of the chat, only for channels and group chat
@JsonProperty(FIRSTNAME_FIELD)
private String firstName; ///< Optional. Username of the chat, only for private chats and channels if available
@JsonProperty(LASTNAME_FIELD)
private String lastName; ///< Optional. Interlocutor's first name for private chats
@JsonProperty(USERNAME_FIELD)
private String userName; ///< Optional. Interlocutor's last name for private chats
@JsonProperty(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD)
2016-09-30 23:32:05 +02:00
/**
* Optional. True if the group or supergroup has All Members Are Admins enabled.
*/
private Boolean allMembersAreAdministrators;
2016-01-14 01:14:53 +01:00
public Chat() {
super();
}
public Chat(JSONObject jsonObject) {
super();
this.id = jsonObject.getLong(ID_FIELD);
this.type = jsonObject.getString(TYPE_FIELD);
if (jsonObject.has(TITLE_FIELD)) {
this.title = jsonObject.getString(TITLE_FIELD);
}
if (jsonObject.has(FIRSTNAME_FIELD)) {
this.firstName = jsonObject.getString(FIRSTNAME_FIELD);
}
if (jsonObject.has(LASTNAME_FIELD)) {
this.lastName = jsonObject.getString(LASTNAME_FIELD);
}
if (jsonObject.has(USERNAME_FIELD)) {
this.userName = jsonObject.getString(USERNAME_FIELD);
}
if (jsonObject.has(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD)) {
allMembersAreAdministrators = jsonObject.getBoolean(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD);
}
2016-01-14 01:14:53 +01:00
}
public Long getId() {
return id;
}
public Boolean isGroupChat() {
return GROUPCHATTYPE.equals(type);
}
public Boolean isChannelChat() {
return CHANNELCHATTYPE.equals(type);
}
public Boolean isUserChat() {
return USERCHATTYPE.equals(type);
}
public Boolean isSuperGroupChat() {
return SUPERGROUPCHATTYPE.equals(type);
}
public String getTitle() {
return title;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getUserName() {
return userName;
}
public Boolean getAllMembersAreAdministrators() {
return allMembersAreAdministrators;
}
2016-01-14 01:14:53 +01:00
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField(ID_FIELD, id);
gen.writeStringField(TYPE_FIELD, type);
if (isUserChat()) {
if (firstName != null) {
gen.writeStringField(FIRSTNAME_FIELD, firstName);
}
if (lastName != null) {
gen.writeStringField(LASTNAME_FIELD, lastName);
}
} else {
if (title != null) {
gen.writeStringField(TITLE_FIELD, title);
}
}
if (!isGroupChat() && userName != null) {
gen.writeStringField(USERNAME_FIELD, userName);
}
if (allMembersAreAdministrators != null) {
gen.writeBooleanField(ALL_MEMBERS_ARE_ADMINISTRATORS_FIELD, allMembersAreAdministrators);
}
2016-01-14 01:14:53 +01:00
gen.writeEndObject();
gen.flush();
}
@Override
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
serialize(gen, serializers);
}
@Override
public String toString() {
return "Chat{" +
"id=" + id +
", type='" + type + '\'' +
", title='" + title + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", userName='" + userName + '\'' +
", allMembersAreAdministrators=" + allMembersAreAdministrators +
'}';
}
2016-01-14 01:14:53 +01:00
}