tgbotxx 1.1.6.9
Telegram Bot C++ Library
Loading...
Searching...
No Matches
MessageEntity.hpp
Go to the documentation of this file.
1#pragma once
4
5namespace tgbotxx {
8 struct MessageEntity {
9 MessageEntity() = default;
10 explicit MessageEntity(const nl::json& json) {
11 fromJson(json);
12 }
13
15 enum class Type : std::uint8_t {
17 Mention = 0,
19 Hashtag,
21 Cashtag,
25 Url,
27 Email,
31 Bold,
33 Italic,
39 Spoiler,
41 Code,
43 Pre,
50 };
51 inline static constexpr const char *Type2Str[] = {
52 "mention",
53 "hashtag",
54 "cashtag",
55 "bot_command",
56 "url",
57 "email",
58 "phone_number",
59 "bold",
60 "italic",
61 "underline",
62 "strikethrough",
63 "spoiler",
64 "code",
65 "pre",
66 "text_link",
67 "text_mention",
68 "custom_emoji"};
69
70 inline static constexpr Type Str2Type(const std::string& str) {
71 std::uint8_t i = 0;
72 for (const char *v: Type2Str) {
73 if (str == v)
74 return static_cast<Type>(i);
75 ++i;
76 }
77 throw Exception("Could not convert MessageEntity type string \"" + str + "\" to enum Type");
78 }
79
88
90 std::int32_t offset{};
91
93 std::int32_t length{};
94
96 std::string url;
97
100
102 std::string language;
103
106 std::string customEmojiId;
107
110 nl::json toJson() const {
111 nl::json json = nl::json::object();
112 json["type"] = Type2Str[static_cast<std::size_t>(type)];// enum
113 OBJECT_SERIALIZE_FIELD(json, "offset", offset);
114 OBJECT_SERIALIZE_FIELD(json, "length", length);
115 OBJECT_SERIALIZE_FIELD(json, "url", url);
116 OBJECT_SERIALIZE_FIELD_PTR(json, "user", user);
117 OBJECT_SERIALIZE_FIELD(json, "language", language);
118 OBJECT_SERIALIZE_FIELD(json, "custom_emoji_id", customEmojiId);
119 return json;
120 }
121
123 void fromJson(const nl::json& json) {
124 type = Str2Type(json["type"]);// enum
125 OBJECT_DESERIALIZE_FIELD(json, "offset", offset, 0, false);
126 OBJECT_DESERIALIZE_FIELD(json, "length", length, 0, false);
127 OBJECT_DESERIALIZE_FIELD(json, "url", url, "", true);
128 OBJECT_DESERIALIZE_FIELD_PTR(json, "user", user, true);
129 OBJECT_DESERIALIZE_FIELD(json, "language", language, "", true);
130 OBJECT_DESERIALIZE_FIELD(json, "custom_emoji_id", customEmojiId, "", true);
131 }
132 };
133}
#define OBJECT_SERIALIZE_FIELD_PTR(json, json_field, field)
Definition Object.hpp:22
#define OBJECT_DESERIALIZE_FIELD_PTR(json, json_field, field, optional)
Definition Object.hpp:70
#define OBJECT_SERIALIZE_FIELD(json, json_field, field)
Available objects: https://core.telegram.org/bots/api#available-types.
Definition Object.hpp:19
#define OBJECT_DESERIALIZE_FIELD(json, json_field, field, default_value, optional)
Deserialize.
Definition Object.hpp:44
tgbotxx::Exception
Definition Exception.hpp:91
std::shared_ptr< T > Ptr
Definition Ptr.hpp:6
This object represents one special entity in a text message. For example, hashtags,...
std::int32_t length
Length of the entity in UTF-16 code units.
nl::json toJson() const
Serializes this object to JSON.
std::string url
Optional. For “text_link” only, URL that will be opened after user taps on the text.
void fromJson(const nl::json &json)
Deserializes this object from JSON.
Type
Enum of possible types of MessageEntity.
@ Url
“url” (https://telegram.org)
@ Bold
“bold” (bold text)
@ Italic
“italic” (italic text)
@ TextMention
“text_mention” (for users without usernames)
@ Mention
“mention” (@username)
@ BotCommand
“bot_command” (/start@jobs_bot)
@ PhoneNumber
“phone_number” (+1-212-555-0123)
@ Hashtag
“hashtag” (#hashtag)
@ TextLink
“text_link” (for clickable text URLs)
@ Underline
“underline” (underlined text)
@ Cashtag
“cashtag” ($USD)
@ Strikethrough
“strikethrough” (strikethrough text)
@ Code
“code” (monowidth string)
@ Email
“email” (do-not-reply@telegram.org)
@ CustomEmoji
“custom_emoji” (for inline custom emoji stickers)
@ Spoiler
“spoiler” (spoiler message)
@ Pre
“pre” (monowidth block)
Type type
Type of the entity. Currently, can be “mention” (@username), “hashtag” (#hashtag),...
static constexpr const char * Type2Str[]
MessageEntity(const nl::json &json)
std::string language
Optional. For “pre” only, the programming language of the entity text.
std::int32_t offset
Offset in UTF-16 code units to the start of the entity.
std::string customEmojiId
Optional. For “custom_emoji” only, unique identifier of the custom emoji. Use Api::getCustomEmojiStic...
static constexpr Type Str2Type(const std::string &str)
Ptr< User > user
Optional. For “text_mention” only, the mentioned user.