yatbcpp  0.0.1
Yet another Telegram Bot CPP Library
Message_fromJson.cc
Go to the documentation of this file.
1 // From JSON Specialication //
4 #include <jsoncpp/json/json.h>
5 #include "types/telegram_type.h"
7 #include "types/Message.h"
8 
9 using namespace std;
10 
11 namespace yatbcpp {
17  template<> Message fromJson(Json::Value Data) {
18  if (!Data.isMember("message_id")) {
19  //message_id is missing
20  throw essential_key_missing("Message::message_id is missing");
21  }
22  if (!Data.isMember("date")) {
23  throw essential_key_missing("Message::date is missing");
24  }
25  if (!Data.isMember("chat")) {
26  throw essential_key_missing("Message::chat is missing");
27  }
28 
29  int message_id = Data["message_id"].asInt();
30  int date = Data["date"].asInt();
31  Chat chat = fromJson<Chat>(Data["chat"]);
32 
33  Message ret(message_id, date, chat);
34  //Additional Data parsing
35  if (Data.isMember("from")) {
36  User U = yatbcpp::fromJson<User>(Data["from"]);
37  ret.setFrom(U);
38  }
39  if (Data.isMember("forward_from")) {
40  User U = yatbcpp::fromJson<User>(Data["forward_from"]);
41  ret.setForward_from(U);
42  }
43  if (Data.isMember("forward_from_chat")) {
44  Chat C = fromJson<Chat>(Data["forward_from_chat"]);
45  ret.setForward_from_chat(C);
46  }
47  if (Data.isMember("forward_from_message_id")) {
48  ret.setForward_from_message_id(Data["forward_from_message_id"].asInt());
49  }
50  if (Data.isMember("forward_signature")) {
51  ret.setForward_signature(Data["forward_signature"].asString());
52  }
53  if (Data.isMember("forward_date")) {
54  ret.setForward_date(Data["forward_date"].asInt());
55  }
56  //todo reply to message
57  if (Data.isMember("edit_date")) {
58  ret.setEdit_date(Data["edit_date"].asInt());
59  }
60  if (Data.isMember("author_signature")) {
61  ret.setAuthor_signature(Data["author_signature"].asString());
62  }
63  if (Data.isMember("text")) {
64  ret.setText(Data["text"].asString());
65  }
66  if (Data.isMember("entities")) {
67  for (int i = 0; i < Data["entities"].size(); i++) {
68  Json::Value small_entity;
69  small_entity = Data["entities"][i];
70  MessageEntity messageEntity = fromJson<MessageEntity>(small_entity);
71  ret.addEntity(messageEntity);
72  }
73  }
74  if (Data.isMember("audio")) {
75  Audio A = yatbcpp::fromJson<Audio>(Data["audio"]);
76  ret.setAudio(A);
77  }
78  if (Data.isMember("document")) {
79  ret.setDocument(fromJson<Document>(Data["document"]));
80  }
81  if (Data.isMember("photo")) {
82  for (int i = 0; i < Data["photo"].size(); i++) {
83  ret.addPhoto(fromJson<PhotoSize>(Data["photo"][i]));
84  }
85  }
86  //todo missing sticker
87  if (Data.isMember("video")) {
88  ret.setVideo(fromJson<Video>(Data["video"]));
89  }
90  if (Data.isMember("voice")) {
91  ret.setVoice(fromJson<Voice>(Data["voice"]));
92  }
93  if (Data.isMember("video_note")) {
94  ret.setVideo_note(fromJson<VideoNote>(Data["video_note"]));
95  }
96  if (Data.isMember("new_chat_members")) {
97  for (int i = 0; i < Data["new_chat_members"].size(); i++) {
98  ret.addNew_chat_members(yatbcpp::fromJson<User>(Data["new_chat_members"][i]));
99  }
100  }
101  if (Data.isMember("caption")) {
102  ret.setCaption(Data["caption"].asString());
103  }
104  if (Data.isMember("contact")) {
105  ret.setContact(fromJson<Contact>(Data["contact"]));
106  }
107  if (Data.isMember("location")) {
108  ret.setLocation(fromJson<Location>(Data["location"]));
109  }
110  if (Data.isMember("venue")) {
111  ret.setVenue(fromJson<Venue>(Data["venue"]));
112  }
113  if (Data.isMember("new_chat_member")) {
114  ret.setNew_chat_member(yatbcpp::fromJson<User>(Data["new_chat_member"]));
115  }
116  if (Data.isMember("left_chat_member")) {
117  ret.setLeft_chat_member(yatbcpp::fromJson<User>(Data["left_chat_member"]));
118  }
119  if (Data.isMember("new_chat_title")) {
120  ret.setNew_chat_title(Data["new_chat_title"].asString());
121  }
122  if (Data.isMember("new_chat_photo")) {
123  for (int i = 0; i < Data["new_chat_photo"].size(); i++) {
124  ret.addNew_chat_photo(fromJson<PhotoSize>(Data["new_chat_photo"][i]));
125  }
126  }
127  if (Data.isMember("delete_chat_photo")) {
128  ret.setDelete_chat_photo(Data["delete_chat_photo"].asBool());
129  }
130  if (Data.isMember("group_chat_created")) {
131  ret.setGroup_chat_created(Data["group_chat_created"].asBool());
132  }
133  if (Data.isMember("supergroup_chat_created")) {
134  ret.setSupergroup_chat_created(Data["supergroup_chat_created"].asBool());
135  }
136  if (Data.isMember("channel_chat_created")) {
137  ret.setChannel_chat_created(Data["channel_chat_created"].asBool());
138  }
139  if (Data.isMember("migrate_to_chat_id")) {
140  ret.setMigrate_to_chat_id(Data["migrate_to_chat_id"].asLargestInt());
141  }
142  if (Data.isMember("migrate_from_chat_id")) {//not quite sure if sufficient from spave
143  ret.setMigrate_from_chat_id(Data["migrate_from_chat_id"].asLargestInt());
144  }
145  //pinned message, invoice, sucessfull payment
146 
147 
148  return ret;
149  }
150 }
Definition: Bot.h:27
User fromJson< User >(Json::Value Data)
Message fromJson(Json::Value Data)