tgbotxx 1.1.9.2
Telegram Bot C++ Library
Loading...
Searching...
No Matches
InputMedia.hpp
Go to the documentation of this file.
1#pragma once
4
5namespace tgbotxx {
13 struct InputMedia {
14 InputMedia() = default;
15 explicit InputMedia(const nl::json& json) {
16 _fromJson(json);
17 }
18 virtual ~InputMedia() = default;
19
21 std::string type;
22
29 std::variant<cpr::File, std::string> media{""};
30
32 std::string caption;
33
36 std::string parseMode;
37
39 std::vector<Ptr<MessageEntity>> captionEntities;
40
41
44 virtual nl::json toJson() const {
45 nl::json json = nl::json::object();
46 OBJECT_SERIALIZE_FIELD(json, "type", type);
47 // media variant
48 if (auto idx = media.index(); idx == 0)
49 json["media"] = std::get<cpr::File>(media).filepath;
50 else if (idx == 1)
51 json["media"] = std::get<std::string>(media);
52 OBJECT_SERIALIZE_FIELD(json, "caption", caption);
53 OBJECT_SERIALIZE_FIELD(json, "parse_mode", parseMode);
54 OBJECT_SERIALIZE_FIELD_PTR_ARRAY(json, "caption_entities", captionEntities);
55 return json;
56 }
57
59 virtual void fromJson(const nl::json& json) {
60 OBJECT_DESERIALIZE_FIELD(json, "type", type, "", false);
61 // media variant, we can't get a local file from remote, so it's always a URL or file id std::string.
62 OBJECT_DESERIALIZE_FIELD(json, "media", std::get<std::string>(media), "", false);
63 OBJECT_DESERIALIZE_FIELD(json, "caption", caption, "", true);
64 OBJECT_DESERIALIZE_FIELD(json, "parse_mode", parseMode, "", true);
65 OBJECT_DESERIALIZE_FIELD_PTR_ARRAY(json, "caption_entities", captionEntities, true);
66 }
67
68 private:
69 void _fromJson(const nl::json& json) {
70 fromJson(json);
71 }
72 };
73
78 InputMedia::type = "animation";
79 }
80 explicit InputMediaAnimation(const nl::json& json) {
82 InputMedia::type = "animation";
83 }
84
90 std::string thumbnail;
91
93 std::int32_t width{};
94
96 std::int32_t height{};
97
99 std::int32_t duration{};
100
103
104
105 nl::json toJson() const override {
106 nl::json json = InputMedia::toJson();
107 OBJECT_SERIALIZE_FIELD(json, "thumbnail", thumbnail);
108 OBJECT_SERIALIZE_FIELD(json, "width", width);
109 OBJECT_SERIALIZE_FIELD(json, "height", height);
110 OBJECT_SERIALIZE_FIELD(json, "duration", duration);
111 OBJECT_SERIALIZE_FIELD(json, "has_spoiler", hasSpoiler);
112 return json;
113 }
114 void fromJson(const nl::json& json) override {
116 OBJECT_DESERIALIZE_FIELD(json, "thumbnail", thumbnail, "", true);
117 OBJECT_DESERIALIZE_FIELD(json, "width", width, 0, true);
118 OBJECT_DESERIALIZE_FIELD(json, "height", height, 0, true);
119 OBJECT_DESERIALIZE_FIELD(json, "duration", duration, 0, true);
120 OBJECT_DESERIALIZE_FIELD(json, "has_spoiler", hasSpoiler, false, true);
121 }
122 };
123
124
129 InputMedia::type = "document";
130 }
131 explicit InputMediaDocument(const nl::json& json) : InputMedia(json) {
132 InputMedia::type = "document";
133 }
134
140 std::string thumbnail;
141
145
146
147 nl::json toJson() const override {
148 nl::json json = InputMedia::toJson();
149 OBJECT_SERIALIZE_FIELD(json, "thumbnail", thumbnail);
150 OBJECT_SERIALIZE_FIELD(json, "disable_content_type_detection", disableContentTypeDetection);
151 return json;
152 }
153 void fromJson(const nl::json& json) override {
155 OBJECT_DESERIALIZE_FIELD(json, "thumbnail", thumbnail, "", true);
156 OBJECT_DESERIALIZE_FIELD(json, "disable_content_type_detection", disableContentTypeDetection, false, true);
157 }
158 };
159
160
165 InputMedia::type = "audio";
166 }
167 explicit InputMediaAudio(const nl::json& json) : InputMedia(json) {
168 InputMedia::type = "audio";
169 }
170
176 std::string thumbnail;
177
179 std::int32_t duration{};
180
182 std::string performer;
183
185 std::string title;
186
187 nl::json toJson() const override {
188 nl::json json = InputMedia::toJson();
189 OBJECT_SERIALIZE_FIELD(json, "thumbnail", thumbnail);
190 OBJECT_SERIALIZE_FIELD(json, "duration", duration);
191 OBJECT_SERIALIZE_FIELD(json, "performer", performer);
192 OBJECT_SERIALIZE_FIELD(json, "title", title);
193 return json;
194 }
195 void fromJson(const nl::json& json) override {
197 OBJECT_DESERIALIZE_FIELD(json, "thumbnail", thumbnail, "", true);
198 OBJECT_DESERIALIZE_FIELD(json, "duration", duration, 0, true);
199 OBJECT_DESERIALIZE_FIELD(json, "performer", performer, "", true);
200 OBJECT_DESERIALIZE_FIELD(json, "title", title, "", true);
201 }
202 };
203
204
209 InputMedia::type = "photo";
210 }
211 explicit InputMediaPhoto(const nl::json& json) : InputMedia(json) {
212 InputMedia::type = "photo";
213 }
214
217
218 nl::json toJson() const override {
219 nl::json json = InputMedia::toJson();
220 OBJECT_SERIALIZE_FIELD(json, "has_spoiler", hasSpoiler);
221 return json;
222 }
223 void fromJson(const nl::json& json) override {
225 OBJECT_DESERIALIZE_FIELD(json, "has_spoiler", hasSpoiler, false, true);
226 }
227 };
228
229
234 InputMedia::type = "video";
235 }
236 explicit InputMediaVideo(const nl::json& json) : InputMedia(json) {
237 InputMedia::type = "video";
238 }
239
245 std::string thumbnail;
246
248 std::int32_t width{};
249
251 std::int32_t height{};
252
254 std::int32_t duration{};
255
258
261
262
263 nl::json toJson() const override {
264 nl::json json = InputMedia::toJson();
265 OBJECT_SERIALIZE_FIELD(json, "thumbnail", thumbnail);
266 OBJECT_SERIALIZE_FIELD(json, "width", width);
267 OBJECT_SERIALIZE_FIELD(json, "height", height);
268 OBJECT_SERIALIZE_FIELD(json, "duration", duration);
269 OBJECT_SERIALIZE_FIELD(json, "supports_streaming", supportsStreaming);
270 OBJECT_SERIALIZE_FIELD(json, "has_spoiler", hasSpoiler);
271 return json;
272 }
273 void fromJson(const nl::json& json) override {
275 OBJECT_DESERIALIZE_FIELD(json, "thumbnail", thumbnail, "", true);
276 OBJECT_DESERIALIZE_FIELD(json, "width", width, 0, true);
277 OBJECT_DESERIALIZE_FIELD(json, "height", height, 0, true);
278 OBJECT_DESERIALIZE_FIELD(json, "duration", duration, 0, true);
279 OBJECT_DESERIALIZE_FIELD(json, "supports_streaming", supportsStreaming, false, true);
280 OBJECT_DESERIALIZE_FIELD(json, "has_spoiler", hasSpoiler, false, true);
281 }
282 };
283
284}
#define OBJECT_SERIALIZE_FIELD_PTR_ARRAY(json, json_field, array_field)
Definition Object.hpp:27
#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:46
#define OBJECT_DESERIALIZE_FIELD_PTR_ARRAY(json, json_field, array_field, optional)
Definition Object.hpp:89
Represents an animation file (GIF or H.264/MPEG-4 AVC video without sound) to be sent....
std::string thumbnail
Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supporte...
nl::json toJson() const override
Serializes this object to JSON.
InputMediaAnimation(const nl::json &json)
std::int32_t duration
Optional. Animation duration in seconds.
void fromJson(const nl::json &json) override
Deserializes this object from JSON.
std::int32_t width
Optional. Animation width.
std::int32_t height
Optional. Animation height.
bool hasSpoiler
Optional. Pass True if the animation needs to be covered with a spoiler animation.
Represents an audio file to be treated as music to be sent. https://core.telegram....
std::string performer
Optional. Performer of the audio.
std::string title
Optional. Title of the audio.
void fromJson(const nl::json &json) override
Deserializes this object from JSON.
std::int32_t duration
Optional. Duration of the audio in seconds.
nl::json toJson() const override
Serializes this object to JSON.
std::string thumbnail
Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supporte...
InputMediaAudio(const nl::json &json)
Represents a general file to be sent. https://core.telegram.org/bots/api#inputmediadocument.
nl::json toJson() const override
Serializes this object to JSON.
std::string thumbnail
Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supporte...
InputMediaDocument(const nl::json &json)
void fromJson(const nl::json &json) override
Deserializes this object from JSON.
bool disableContentTypeDetection
Optional. Disables automatic server-side content type detection for files uploaded using multipart/fo...
This object represents the content of a media message to be sent. It should be one of:
InputMedia(const nl::json &json)
std::string parseMode
Optional. Mode for parsing entities in the media caption. See https://core.telegram....
virtual void fromJson(const nl::json &json)
Deserializes this object from JSON.
std::vector< Ptr< MessageEntity > > captionEntities
Optional. List of special entities that appear in the caption, which can be specified instead of pars...
virtual ~InputMedia()=default
std::variant< cpr::File, std::string > media
File to send.
virtual nl::json toJson() const
Serializes this object to JSON.
std::string type
Type of the result.
std::string caption
Optional. Caption of the media to be sent, 0-1024 characters after entities parsing.
Represents a photo to be sent. https://core.telegram.org/bots/api#inputmediaphoto.
bool hasSpoiler
Optional. Pass True if the photo needs to be covered with a spoiler animation.
nl::json toJson() const override
Serializes this object to JSON.
void fromJson(const nl::json &json) override
Deserializes this object from JSON.
InputMediaPhoto(const nl::json &json)
Represents a video to be sent. https://core.telegram.org/bots/api#inputmediavideo.
bool hasSpoiler
Optional. Pass True if the animation needs to be covered with a spoiler animation.
std::int32_t duration
Optional. Video duration in seconds.
std::string thumbnail
Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supporte...
InputMediaVideo(const nl::json &json)
nl::json toJson() const override
Serializes this object to JSON.
std::int32_t width
Optional. Video width.
bool supportsStreaming
Optional. Pass True if the uploaded video is suitable for streaming.
std::int32_t height
Optional. Video height.
void fromJson(const nl::json &json) override
Deserializes this object from JSON.