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

149 lines
5.1 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-04-11 02:53:53 +02:00
import org.telegram.telegrambots.api.objects.inlinequery.ChosenInlineQuery;
import org.telegram.telegrambots.api.objects.inlinequery.InlineQuery;
2016-01-14 01:14:53 +01:00
import java.io.IOException;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief This object represents an incoming update.
* Only one of the optional parameters can be present in any given update.
* @date 20 of June of 2015
*/
2016-01-14 23:09:19 +01:00
public class Update implements IBotApiObject {
2016-04-11 02:53:53 +02:00
private static final String UPDATEID_FIELD = "update_id";
private static final String MESSAGE_FIELD = "message";
private static final String INLINEQUERY_FIELD = "inline_query";
private static final String CHOSENINLINEQUERY_FIELD = "chosen_inline_result";
private static final String CALLBACKQUERY_FIELD = "callback_query";
2016-05-22 11:42:04 +02:00
private static final String EDITEDMESSAGE_FIELD = "callback_query";
2016-01-14 01:14:53 +01:00
@JsonProperty(UPDATEID_FIELD)
private Integer updateId;
@JsonProperty(MESSAGE_FIELD)
private Message message; ///< Optional. New incoming message of any kind — text, photo, sticker, etc.
@JsonProperty(INLINEQUERY_FIELD)
private InlineQuery inlineQuery; ///< Optional. New incoming inline query
@JsonProperty(CHOSENINLINEQUERY_FIELD)
private ChosenInlineQuery chosenInlineQuery; ///< Optional. The result of a inline query that was chosen by a user and sent to their chat partner
2016-04-11 02:53:53 +02:00
@JsonProperty(CALLBACKQUERY_FIELD)
private CallbackQuery callbackQuery; ///< Optional. New incoming callback query
2016-05-22 11:42:04 +02:00
@JsonProperty(EDITEDMESSAGE_FIELD)
private Message editedMessage; ///< Optional. New version of a message that is known to the bot and was edited
2016-01-14 01:14:53 +01:00
public Update() {
super();
}
public Update(JSONObject jsonObject) {
super();
this.updateId = jsonObject.getInt(UPDATEID_FIELD);
if (jsonObject.has(MESSAGE_FIELD)) {
this.message = new Message(jsonObject.getJSONObject(MESSAGE_FIELD));
}
if (jsonObject.has(INLINEQUERY_FIELD)) {
this.inlineQuery = new InlineQuery(jsonObject.getJSONObject(INLINEQUERY_FIELD));
}
if (jsonObject.has(CHOSENINLINEQUERY_FIELD)) {
this.chosenInlineQuery = new ChosenInlineQuery(jsonObject.getJSONObject(CHOSENINLINEQUERY_FIELD));
}
2016-04-11 02:53:53 +02:00
if (jsonObject.has(CALLBACKQUERY_FIELD)) {
callbackQuery = new CallbackQuery(jsonObject.getJSONObject(CALLBACKQUERY_FIELD));
}
2016-05-22 11:42:04 +02:00
if (jsonObject.has(EDITEDMESSAGE_FIELD)){
editedMessage = new Message(jsonObject.getJSONObject(EDITEDMESSAGE_FIELD));
}
2016-01-14 01:14:53 +01:00
}
public Integer getUpdateId() {
return updateId;
}
public Message getMessage() {
return message;
}
public InlineQuery getInlineQuery() {
return inlineQuery;
}
public ChosenInlineQuery getChosenInlineQuery() {
return chosenInlineQuery;
}
2016-04-11 02:53:53 +02:00
public CallbackQuery getCallbackQuery() {
return callbackQuery;
}
2016-05-22 11:42:04 +02:00
public Message getEditedMessage() {
return editedMessage;
}
2016-01-14 01:14:53 +01:00
public boolean hasMessage() {
return message != null;
}
public boolean hasInlineQuery() {
return inlineQuery != null;
}
public boolean hasChosenInlineQuery() {
return chosenInlineQuery != null;
}
2016-04-11 02:53:53 +02:00
public boolean hasCallbackQuery() {
return callbackQuery != null;
}
2016-05-22 11:42:04 +02:00
public boolean hasEditedMessage() {
return editedMessage != null;
}
2016-01-14 01:14:53 +01:00
@Override
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
gen.writeNumberField(UPDATEID_FIELD, updateId);
if (message != null) {
gen.writeObjectField(MESSAGE_FIELD, message);
}
if (inlineQuery != null) {
gen.writeObjectField(INLINEQUERY_FIELD, inlineQuery);
}
if (chosenInlineQuery != null) {
gen.writeObjectField(CHOSENINLINEQUERY_FIELD, chosenInlineQuery);
}
2016-04-11 02:53:53 +02:00
if (callbackQuery != null) {
gen.writeObjectField(CALLBACKQUERY_FIELD, callbackQuery);
}
2016-05-22 11:42:04 +02:00
if (editedMessage != null) {
gen.writeObjectField(EDITEDMESSAGE_FIELD, editedMessage);
}
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 "Update{" +
"updateId=" + updateId +
", message=" + message +
", inlineQuery=" + inlineQuery +
", chosenInlineQuery=" + chosenInlineQuery +
2016-05-01 23:02:22 +02:00
", callbackQuery=" + callbackQuery +
2016-05-22 11:42:04 +02:00
", editedMessage=" + editedMessage +
'}';
}
2016-01-14 01:14:53 +01:00
}