tgbotxx 1.1.9.2
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,
45 Code,
47 Pre,
54 };
56 static std::optional<std::string> TypeToString(const Type type) noexcept {
57 if (type == Type::Mention) return "mention";
58 if (type == Type::Hashtag) return "hashtag";
59 if (type == Type::Cashtag) return "cashtag";
60 if (type == Type::BotCommand) return "bot_command";
61 if (type == Type::Url) return "url";
62 if (type == Type::Email) return "email";
63 if (type == Type::PhoneNumber) return "phone_number";
64 if (type == Type::Bold) return "bold";
65 if (type == Type::Italic) return "italic";
66 if (type == Type::Underline) return "underline";
67 if (type == Type::Strikethrough) return "strikethrough";
68 if (type == Type::Spoiler) return "spoiler";
69 if (type == Type::Blockquote) return "blockquote";
70 if (type == Type::ExpandableBlockquote) return "expandable_blockquote";
71 if (type == Type::Code) return "code";
72 if (type == Type::Pre) return "pre";
73 if (type == Type::TextLink) return "text_link";
74 if (type == Type::TextMention) return "text_mention";
75 if (type == Type::CustomEmoji) return "custom_emoji";
76 return std::nullopt;
77 }
79 static std::optional<Type> StringToType(const std::string& str) noexcept {
80 if (str == "mention") return Type::Mention;
81 if (str == "hashtag") return Type::Hashtag;
82 if (str == "cashtag") return Type::Cashtag;
83 if (str == "bot_command") return Type::BotCommand;
84 if (str == "url") return Type::Url;
85 if (str == "email") return Type::Email;
86 if (str == "phone_number") return Type::PhoneNumber;
87 if (str == "bold") return Type::Bold;
88 if (str == "italic") return Type::Italic;
89 if (str == "underline") return Type::Underline;
90 if (str == "strikethrough") return Type::Strikethrough;
91 if (str == "spoiler") return Type::Spoiler;
92 if (str == "blockquote") return Type::Blockquote;
93 if (str == "expandable_blockquote") return Type::ExpandableBlockquote;
94 if (str == "code") return Type::Code;
95 if (str == "pre") return Type::Pre;
96 if (str == "text_link") return Type::TextLink;
97 if (str == "text_mention") return Type::TextMention;
98 if (str == "custom_emoji") return Type::CustomEmoji;
99 return std::nullopt;
100 }
101
124
126 std::int32_t offset{};
127
129 std::int32_t length{};
130
132 std::string url;
133
136
138 std::string language;
139
142 std::string customEmojiId;
143
146 nl::json toJson() const {
147 nl::json json = nl::json::object();
148 OBJECT_SERIALIZE_FIELD_ENUM(json, Type, "type", type);
149 OBJECT_SERIALIZE_FIELD(json, "offset", offset);
150 OBJECT_SERIALIZE_FIELD(json, "length", length);
151 OBJECT_SERIALIZE_FIELD(json, "url", url);
152 OBJECT_SERIALIZE_FIELD_PTR(json, "user", user);
153 OBJECT_SERIALIZE_FIELD(json, "language", language);
154 OBJECT_SERIALIZE_FIELD(json, "custom_emoji_id", customEmojiId);
155 return json;
156 }
157
159 void fromJson(const nl::json& json) {
160 OBJECT_DESERIALIZE_FIELD_ENUM(json, Type, "type", type, 0, false);
161 OBJECT_DESERIALIZE_FIELD(json, "offset", offset, 0, false);
162 OBJECT_DESERIALIZE_FIELD(json, "length", length, 0, false);
163 OBJECT_DESERIALIZE_FIELD(json, "url", url, "", true);
164 OBJECT_DESERIALIZE_FIELD_PTR(json, "user", user, true);
165 OBJECT_DESERIALIZE_FIELD(json, "language", language, "", true);
166 OBJECT_DESERIALIZE_FIELD(json, "custom_emoji_id", customEmojiId, "", true);
167 }
168 };
169}
#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:72
#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_ENUM(json, enum_name, json_field, field, default_value, optional)
Definition Object.hpp:137
#define OBJECT_DESERIALIZE_FIELD(json, json_field, field, default_value, optional)
Deserialize.
Definition Object.hpp:46
#define OBJECT_SERIALIZE_FIELD_ENUM(json, enum_name, json_field, field)
Definition Object.hpp:42
std::shared_ptr< T > Ptr
Definition Ptr.hpp:6
This object represents one special entity in a text message. For example, hashtags,...
static std::optional< std::string > TypeToString(const Type type) noexcept
Converts enum Type to a string.
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),
@ Blockquote
“blockquote” (block quotation),
@ Italic
“italic” (italic text),
@ ExpandableBlockquote
“expandable_blockquote” (collapsed-by-default block quotation),
@ 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 or #hashtag@chatusername),
@ TextLink
“text_link” (for clickable text URLs),
@ Underline
“underline” (underlined text),
@ Cashtag
“cashtag” ($USD or $USD@chatusername),
@ 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. Type of the entity. Currently, can be: “mention” (@username), “hashtag” (#hashtag...
static std::optional< Type > StringToType(const std::string &str) noexcept
Converts string to an enum Type.
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...
Ptr< User > user
Optional. For “text_mention” only, the mentioned user.