Cppgram  1.0.0
Easy and modern C++14 Telegram Bot API wrapper
message.hpp
1 #ifndef CPPGRAM_MESSAGE_HPP
2 #define CPPGRAM_MESSAGE_HPP
3 
4 #include <experimental/optional>
5 #include <memory>
6 #include <string>
7 
8 #include <json/json.h>
9 
10 #include "audio.hpp"
11 #include "chat.hpp"
12 #include "contact.hpp"
13 #include "document.hpp"
14 #include "game.hpp"
15 #include "location.hpp"
16 #include "message_entity.hpp"
17 #include "message_entity.hpp"
18 #include "sticker.hpp"
19 #include "user.hpp"
20 #include "venue.hpp"
21 #include "video.hpp"
22 #include "voice.hpp"
23 
24 namespace cppgram
25 {
26 namespace types
27 {
36 class Message
37 {
40  public:
42  uint_fast32_t message_id;
43 
45  std::experimental::optional<User> from;
46 
48  uint_fast32_t date;
49 
52 
54  std::experimental::optional<User> forward_from;
55 
58  std::experimental::optional<Chat> forward_from_chat;
59 
62  std::experimental::optional<uint_fast32_t> forward_date;
63 
66  std::shared_ptr<Message> reply_to_message;
67 
69  std::experimental::optional<uint_fast32_t> edit_date;
70 
73  std::experimental::optional<std::string> text;
74 
76  std::vector<MessageEntity> entities;
77 
79  std::experimental::optional<Audio> audio;
80 
82  std::experimental::optional<Document> document;
83 
84  std::experimental::optional<Game> game;
85 
87  std::vector<PhotoSize> photo;
88 
90  std::experimental::optional<Sticker> sticker;
91 
93  std::experimental::optional<Video> video;
94 
96  std::experimental::optional<Voice> voice;
97 
99  std::experimental::optional<Contact> contact;
100 
102  std::experimental::optional<Location> location;
103 
105  std::experimental::optional<Venue> venue;
106 
108  std::experimental::optional<std::string> caption;
109 
112  std::experimental::optional<User> new_chat_member;
113 
116  std::experimental::optional<User> left_chat_member;
117 
119  std::experimental::optional<std::string> new_chat_title;
120 
122  std::vector<PhotoSize> new_chat_photo;
123 
126 
131  std::experimental::optional<uint_fast64_t> migrate_to_chat_id,
132 
133  /*** \brief <i>Optional</i>. The supergroup has been migrated from a group with the
134  specified identifier. This number may be greater than 32 bits and some programming
135  languages may have difficulty/silent defects in interpreting it. But it is smaller
136  than 52 bits, so a signed 64 bit integer or double-precision float type are safe for
137  storing this identifier. */
138  migrate_from_chat_id;
139 
142  std::shared_ptr<Message> pinned_message;
143 
144  Message( Json::Value &json_message )
145  : message_id( json_message["message_id"].asUInt() )
146  , date( json_message["date"].asUInt() )
147  , chat( json_message["chat"] )
148  {
149  if ( !json_message["from"].isNull() )
150  {
151  from.emplace( User( json_message["from"] ) );
152  }
153 
154  if ( !json_message["forward_from"].isNull() )
155  {
156  forward_from.emplace( User( json_message["forward_from"] ) );
157  forward_from_chat.emplace( Chat( json_message["forward_from_chat"] ) );
158  forward_date.emplace( json_message["forward_date"].asUInt() );
159  }
160 
161  if ( !json_message["reply_to_message"].isNull() )
162  {
163  reply_to_message
164  = std::make_shared<Message>( Message( json_message["reply_to_message"] ) );
165  }
166  else
167  {
168  reply_to_message = nullptr;
169  }
170 
171  if ( !json_message["edit_date"].isNull() )
172  {
173  edit_date.emplace( json_message["edit_date"].asUInt() );
174  }
175 
176  if ( !json_message["text"].isNull() )
177  {
178  text.emplace( json_message["text"].asString() );
179  }
180 
181  entities.reserve( json_message["entities"].size() );
182  if ( !json_message["entities"].isNull() )
183  {
184  for ( Json::Value &json_entity : json_message["entities"] )
185  {
186  entities.push_back( MessageEntity( json_entity ) );
187  }
188  }
189 
190  if ( !json_message["audio"].isNull() )
191  {
192  audio.emplace( Audio( json_message["audio"] ) );
193  }
194  else if ( !json_message["document"].isNull() )
195  {
196  document.emplace( Document( json_message["document"] ) );
197  }
198  else if ( !json_message["game"].isNull() )
199  {
200  game.emplace( Game( json_message["game"] ) );
201  }
202  else if ( !json_message["photo"].isNull() )
203  {
204  photo.reserve( json_message["photo"].size() );
205  for ( Json::Value &json_photo : json_message["photo"] )
206  {
207  photo.emplace_back( PhotoSize( json_photo ) );
208  }
209  }
210  else if ( !json_message["sticker"].isNull() )
211  {
212  sticker.emplace( Sticker( json_message["sticker"] ) );
213  }
214  else if ( !json_message["video"].isNull() )
215  {
216  video.emplace( Video( json_message["video"] ) );
217  }
218  else if ( !json_message["voice"].isNull() )
219  {
220  voice.emplace( Voice( json_message["voice"] ) );
221  }
222  else if ( !json_message["contact"].isNull() )
223  {
224  contact.emplace( Contact( json_message["contact"] ) );
225  }
226  else if ( !json_message["location"].isNull() )
227  {
228  location.emplace( Location( json_message["location"] ) );
229  }
230  else if ( !json_message["venue"].isNull() )
231  {
232  venue.emplace( Venue( json_message["venue"] ) );
233  }
234 
235  if ( !json_message["caption"].isNull() )
236  {
237  caption = json_message["caption"].asString();
238  }
239 
240  if ( !json_message["new_chat_member"].isNull() )
241  {
242  new_chat_member.emplace( User( json_message["new_chat_member"] ) );
243  ;
244  }
245 
246  if ( !json_message["left_chat_member"].isNull() )
247  {
248  left_chat_member.emplace( User( json_message["left_chat_member"] ) );
249  }
250 
251  if ( !json_message["new_chat_photo"].empty() )
252  {
253  new_chat_photo.reserve( json_message["new_chat_photo"].size() );
254  for ( Json::Value &json_photo : json_message["new_chat_photo"] )
255  {
256  new_chat_photo.push_back( PhotoSize( json_photo ) );
257  }
258  }
259 
260  if ( !json_message["delete_chat_photo"].isNull() )
261  {
262  service_message = EServiceMessage::delete_chat_photo;
263  }
264  else if ( !json_message["group_chat_created"].isNull() )
265 
266  {
267  service_message = EServiceMessage::group_chat_created;
268  }
269  else if ( !json_message["supergroup_chat_created"].isNull() )
270  {
272  }
273  else if ( !json_message["channel_chat_created"].isNull() )
274  {
275  service_message = EServiceMessage::channel_chat_created;
276  }
277 
278  if ( !json_message["migrate_to_chat_id"].isNull() )
279  {
280  migrate_to_chat_id.emplace( json_message["migrate_to_chat_id"].asInt64() );
281  }
282 
283  if ( !json_message["migrate_from_chat_id"].isNull() )
284  {
285  migrate_from_chat_id.emplace( json_message["migrate_from_chat_id"].asInt64() );
286  }
287 
288  if ( !json_message["pinned_message"].isNull() )
289  {
290  pinned_message = std::make_shared<Message>( Message( json_message["pinned_message"] ) );
291  }
292  }
293 
294  Message() {}
295 };
296 }
297 }
298 
299 #endif
std::experimental::optional< Contact > contact
Optional. Message is a shared contact, information about the contact
Definition: message.hpp:99
EServiceMessage
Service message received.
Definition: enums.hpp:101
std::experimental::optional< std::string > caption
Optional. Caption for the document, photo or video, 0-200 characters
Definition: message.hpp:108
uint_fast32_t date
Date the message was sent in Unix time.
Definition: message.hpp:48
Definition: enums.hpp:110
std::experimental::optional< User > forward_from
Optional. For forwarded messages, sender of the original message
Definition: message.hpp:54
std::vector< PhotoSize > new_chat_photo
Optional. A chat photo was change to this value.
Definition: message.hpp:122
std::shared_ptr< Message > reply_to_message
Optional. For replies, the original message. Note that the Message object in this field will not cont...
Definition: message.hpp:66
uint_fast32_t message_id
Unique message identifier.
Definition: message.hpp:42
A message send by user.
Definition: message.hpp:36
std::experimental::optional< Location > location
Optional. Message is a shared location, information about the location
Definition: message.hpp:102
This object represents a venue.
Definition: venue.hpp:21
std::experimental::optional< Sticker > sticker
Optional. Message is a sticker, information about the sticker
Definition: message.hpp:90
std::experimental::optional< Document > document
Optional. Message is a general file, information about the file
Definition: message.hpp:82
std::experimental::optional< std::string > new_chat_title
Optional. A chat title was changed to this value.
Definition: message.hpp:119
std::experimental::optional< Video > video
Optional. Message is a video, information about the video
Definition: message.hpp:93
std::experimental::optional< User > left_chat_member
Optional. A member was removed from the group, information about them (this member may be the bot its...
Definition: message.hpp:116
std::experimental::optional< uint_fast64_t > migrate_to_chat_id
Optional. The group has been migrated to a supergroup with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
Definition: message.hpp:131
std::experimental::optional< User > new_chat_member
Optional. A new member was added to the group, information about them (this member may be the bot its...
Definition: message.hpp:112
std::experimental::optional< Venue > venue
Optional. Message is a venue, information about the venue
Definition: message.hpp:105
std::experimental::optional< std::string > text
Optional. For text messages, the actual UTF-8 text of the message, 0-4096 characters.
Definition: message.hpp:73
A photo or thumbnail.
Definition: photo_size.hpp:20
Definition: voice.hpp:18
A video sent by user.
Definition: video.hpp:21
Entity in the message.
Definition: message_entity.hpp:24
std::vector< MessageEntity > entities
Optional. For text messages, an array of messageEntinty
Definition: message.hpp:76
A sticker.
Definition: sticker.hpp:21
std::experimental::optional< uint_fast32_t > forward_date
Optional. For forwarded messages, date the original message was sent in Unix time ...
Definition: message.hpp:62
std::experimental::optional< Audio > audio
Optional. Message is an audio file, information about the file
Definition: message.hpp:79
Definition: enums.hpp:107
Contact send by a user.
Definition: contact.hpp:20
std::experimental::optional< uint_fast32_t > edit_date
Optional. Date the message was last edited in Unix time
Definition: message.hpp:69
Definition: enums.hpp:116
This object represents a game.
Definition: game.hpp:18
std::experimental::optional< Chat > forward_from_chat
Optional. For messages forwarded from a channel, information about the original channel ...
Definition: message.hpp:58
std::experimental::optional< User > from
Optional. Sender, can be empty for messages sent to channels
Definition: message.hpp:45
EServiceMessage service_message
Service message.
Definition: message.hpp:125
Definition: enums.hpp:121
A location send by a user.
Definition: location.hpp:19
A document send by a user.
Definition: document.hpp:22
std::shared_ptr< Message > pinned_message
Optional. Specified message was pinned. Note that the Message object in this field will not contain f...
Definition: message.hpp:142
main namespace for Cppgram
Chat chat
Conversation the message belongs to.
Definition: message.hpp:51
Chat object.
Definition: chat.hpp:21
std::vector< PhotoSize > photo
Optional. Message is a photo, available sizes of the photo
Definition: message.hpp:87
Audio message sent by a user.
Definition: audio.hpp:20
std::experimental::optional< Voice > voice
Optional. Message is a voice message, information about the file
Definition: message.hpp:96
User object.
Definition: user.hpp:20