TDLightTelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/objects/MessageEntity.java

126 lines
3.9 KiB
Java
Raw Normal View History

2018-07-08 01:41:21 +02:00
package org.telegram.telegrambots.meta.api.objects;
2016-04-11 02:53:53 +02:00
import com.fasterxml.jackson.annotation.JsonIgnore;
2016-04-11 02:53:53 +02:00
import com.fasterxml.jackson.annotation.JsonProperty;
2022-01-22 01:22:42 +01:00
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
2022-02-09 21:06:02 +01:00
import java.util.function.Supplier;
import lombok.AccessLevel;
2020-11-01 23:46:36 +01:00
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
2022-02-09 21:06:02 +01:00
import org.telegram.telegrambots.meta.MemoizedUTF16String;
import org.telegram.telegrambots.meta.MemoizedUTF16Substring;
2018-07-08 01:41:21 +02:00
import org.telegram.telegrambots.meta.api.interfaces.BotApiObject;
2016-04-11 02:53:53 +02:00
/**
2018-07-27 00:27:26 +02:00
* This object represents one special entity in a text message. For example, hashtags,
2016-04-11 02:53:53 +02:00
* usernames, URL.
* @author Ruben Bermudez
* @version 1.0
2016-04-11 02:53:53 +02:00
*/
2018-07-27 00:27:26 +02:00
@SuppressWarnings("WeakerAccess")
2020-11-01 23:46:36 +01:00
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MessageEntity implements BotApiObject {
2016-04-11 02:53:53 +02:00
private static final String TYPE_FIELD = "type";
private static final String OFFSET_FIELD = "offset";
private static final String LENGTH_FIELD = "length";
private static final String URL_FIELD = "url";
2016-05-22 11:42:04 +02:00
private static final String USER_FIELD = "user";
2020-01-24 00:23:29 +01:00
private static final String LANGUAGE_FIELD = "language";
2022-08-15 02:42:50 +02:00
private static final String CUSTOMEMOJI_FIELD = "custom_emoji_id";
2016-04-11 02:53:53 +02:00
/**
2022-01-02 00:06:28 +01:00
* Type of the entity.
* Currently, can be:
* - mention (@username)
* - hashtag (#hashtag)
* - cashtag ($USD)
* - bot_command (/start@jobs_bot)
* - url (https://telegram.org)
* - email (do-not-reply@telegram.org)
* - phone_number (+1-212-555-0123),
* - bold (bold text)
* - italic (italic text)
* - underline (underlined text)
* - strikethrough (strikethrough text)
* - spoiler (spoiler message)
* - code (monowidth string)
* - pre (monowidth block)
* - text_link (for clickable text URLs)
* - text_mention (for users without usernames)
2022-08-15 02:42:50 +02:00
* - "custom_emoji" (for inline custom emoji stickers)
2016-04-11 02:53:53 +02:00
*/
@JsonProperty(TYPE_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
2016-04-11 02:53:53 +02:00
private String type;
/**
* Offset in UTF-16 code units to the start of the entity
*/
2016-04-11 02:53:53 +02:00
@JsonProperty(OFFSET_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
private Integer offset;
/**
* Length of the entity in UTF-16 code units
*/
2016-04-11 02:53:53 +02:00
@JsonProperty(LENGTH_FIELD)
2020-11-01 23:46:36 +01:00
@NonNull
private Integer length;
/**
* Optional.
* For text_link only, url that will be opened after user taps on the text
*/
2016-04-11 02:53:53 +02:00
@JsonProperty(URL_FIELD)
private String url;
/**
* Optional.
* For text_mention only, the mentioned user
*/
2016-05-22 11:42:04 +02:00
@JsonProperty(USER_FIELD)
private User user;
/**
* Optional.
* For pre only, the programming language of the entity text
*/
2020-01-24 00:23:29 +01:00
@JsonProperty(LANGUAGE_FIELD)
private String language;
2022-08-15 02:42:50 +02:00
/**
* Optional.
* For custom_emoji only, unique identifier of the custom emoji.
* Use getCustomEmojiStickers to get full information about the sticker
*/
@JsonProperty(CUSTOMEMOJI_FIELD)
private String customEmojiId;
/**
* Text present in the entity. Computed from offset and length
*/
2022-02-09 21:06:02 +01:00
@Setter(AccessLevel.NONE)
@JsonIgnore
2022-02-09 21:06:02 +01:00
private MemoizedUTF16Substring text; ///< Text present in the entity. Computed from offset and length
2022-02-09 21:06:02 +01:00
public String getText() {
if (text == null) {
return "";
} else {
return text.getString();
2016-04-11 02:53:53 +02:00
}
}
2022-02-09 21:06:02 +01:00
public void computeText(String message) {
text = new MemoizedUTF16Substring(new MemoizedUTF16String(message), offset, length);
}
2016-04-11 02:53:53 +02:00
}