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