diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Api.cpp | 717 | ||||
-rw-r--r-- | src/EventHandler.cpp | 47 | ||||
-rw-r--r-- | src/TgTypeParser.cpp | 3185 | ||||
-rw-r--r-- | src/net/TgLongPoll.cpp | 8 | ||||
-rw-r--r-- | src/tools/StringTools.cpp | 18 |
5 files changed, 2069 insertions, 1906 deletions
diff --git a/src/Api.cpp b/src/Api.cpp index 4107a4e..9eefc09 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -1,53 +1,46 @@ #include "tgbot/Api.h" -#include "tgbot/tools/StringTools.h" - -#include "tgbot/TgException.h" -#include "tgbot/TgTypeParser.h" - -#include <cstdint> -#include <string> -#include <vector> -#include <utility> - -using namespace std; -using namespace boost::property_tree; - namespace TgBot { -Api::Api(string token, const HttpClient& httpClient, const std::string& url) +Api::Api(std::string token, const HttpClient& httpClient, const std::string& url) : _token(std::move(token)), _httpClient(httpClient), _tgTypeParser(), _url(url) { } -vector<Update::Ptr> Api::getUpdates(std::int32_t offset, std::int32_t limit, std::int32_t timeout, const StringArrayPtr& allowedUpdates) const { - vector<HttpReqArg> args; +std::vector<Update::Ptr> Api::getUpdates(std::int32_t offset, + std::int32_t limit, + std::int32_t timeout, + const StringArrayPtr& allowedUpdates) const { + std::vector<HttpReqArg> args; args.reserve(4); - if (offset) { + + if (offset != 0) { args.emplace_back("offset", offset); } - limit = max(1, min(100, limit)); - args.emplace_back("limit", limit); - if (timeout) { + if (limit != 100) { + args.emplace_back("limit", std::max(1, std::min(100, limit))); + } + if (timeout != 0) { args.emplace_back("timeout", timeout); } if (allowedUpdates != nullptr) { - string allowedUpdatesJson = _tgTypeParser.parseArray<string>( - [] (const string& s)->string { + std::string allowedUpdatesJson = _tgTypeParser.parseArray<std::string>( + [] (const std::string& s)->std::string { return s; }, *allowedUpdates); args.emplace_back("allowed_updates", allowedUpdatesJson); } + return _tgTypeParser.parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); } bool Api::setWebhook(const std::string& url, InputFile::Ptr certificate, - std::int32_t maxConnection, + std::int32_t maxConnections, const StringArrayPtr& allowedUpdates, const std::string& ipAddress, bool dropPendingUpdates, const std::string& secretToken) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(7); args.emplace_back("url", url); @@ -57,12 +50,12 @@ bool Api::setWebhook(const std::string& url, if (!ipAddress.empty()) { args.emplace_back("ip_address", ipAddress); } - if (maxConnection != 40) { - args.emplace_back("max_connections", maxConnection); + if (maxConnections != 40) { + args.emplace_back("max_connections", std::max(1, std::min(100, maxConnections))); } if (allowedUpdates != nullptr) { - auto allowedUpdatesJson = _tgTypeParser.parseArray<string>( - [] (const string& s)->string { + auto allowedUpdatesJson = _tgTypeParser.parseArray<std::string>( + [] (const std::string& s)->std::string { return s; }, *allowedUpdates); args.emplace_back("allowed_updates", allowedUpdatesJson); @@ -78,7 +71,7 @@ bool Api::setWebhook(const std::string& url, } bool Api::deleteWebhook(bool dropPendingUpdates) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(1); if (dropPendingUpdates) { @@ -89,12 +82,13 @@ bool Api::deleteWebhook(bool dropPendingUpdates) const { } WebhookInfo::Ptr Api::getWebhookInfo() const { - ptree p = sendRequest("getWebhookInfo"); + boost::property_tree::ptree p = sendRequest("getWebhookInfo"); if (!p.get_child_optional("url")) { return nullptr; } - if (p.get<string>("url", "") != string("")) { + + if (p.get<std::string>("url", "") != std::string("")) { return _tgTypeParser.parseJsonAndGetWebhookInfo(p); } else { return nullptr; @@ -105,7 +99,15 @@ User::Ptr Api::getMe() const { return _tgTypeParser.parseJsonAndGetUser(sendRequest("getMe")); } -Message::Ptr Api::sendMessage(boost::variant<std::int64_t, const std::string&> chatId, +bool Api::logOut() const { + return sendRequest("logOut").get<bool>("", false); +} + +bool Api::close() const { + return sendRequest("close").get<bool>("", false); +} + +Message::Ptr Api::sendMessage(boost::variant<std::int64_t, std::string> chatId, const std::string& text, bool disableWebPagePreview, std::int32_t replyToMessageId, @@ -115,7 +117,7 @@ Message::Ptr Api::sendMessage(boost::variant<std::int64_t, const std::string&> c const std::vector<MessageEntity::Ptr>& entities, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(10); args.emplace_back("chat_id", chatId); @@ -135,7 +137,7 @@ Message::Ptr Api::sendMessage(boost::variant<std::int64_t, const std::string&> c if (protectContent) { args.emplace_back("protect_content", protectContent); } - if (replyToMessageId) { + if (replyToMessageId != 0) { args.emplace_back("reply_to_message_id", replyToMessageId); } if (allowSendingWithoutReply) { @@ -148,12 +150,12 @@ Message::Ptr Api::sendMessage(boost::variant<std::int64_t, const std::string&> c return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendMessage", args)); } -Message::Ptr Api::forwardMessage(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<std::int64_t, const std::string&> fromChatId, +Message::Ptr Api::forwardMessage(boost::variant<std::int64_t, std::string> chatId, + boost::variant<std::int64_t, std::string> fromChatId, std::int32_t messageId, bool disableNotification, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(5); args.emplace_back("chat_id", chatId); @@ -169,8 +171,8 @@ Message::Ptr Api::forwardMessage(boost::variant<std::int64_t, const std::string& return _tgTypeParser.parseJsonAndGetMessage(sendRequest("forwardMessage", args)); } -MessageId::Ptr Api::copyMessage(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<std::int64_t, const std::string&> fromChatId, +MessageId::Ptr Api::copyMessage(boost::variant<std::int64_t, std::string> chatId, + boost::variant<std::int64_t, std::string> fromChatId, std::int32_t messageId, const std::string& caption, const std::string& parseMode, @@ -180,7 +182,7 @@ MessageId::Ptr Api::copyMessage(boost::variant<std::int64_t, const std::string&> bool allowSendingWithoutReply, GenericReply::Ptr replyMarkup, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -214,8 +216,8 @@ MessageId::Ptr Api::copyMessage(boost::variant<std::int64_t, const std::string&> return _tgTypeParser.parseJsonAndGetMessageId(sendRequest("copyMessage", args)); } -Message::Ptr Api::sendPhoto(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> photo, +Message::Ptr Api::sendPhoto(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> photo, const std::string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -224,15 +226,15 @@ Message::Ptr Api::sendPhoto(boost::variant<std::int64_t, const std::string&> cha const std::vector<MessageEntity::Ptr>& captionEntities, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(10); args.emplace_back("chat_id", chatId); if (photo.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(photo); args.emplace_back("photo", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("photo", boost::get<const std::string&>(photo)); + } else { // std::string + args.emplace_back("photo", boost::get<std::string>(photo)); } if (!caption.empty()) { args.emplace_back("caption", caption); @@ -262,13 +264,13 @@ Message::Ptr Api::sendPhoto(boost::variant<std::int64_t, const std::string&> cha return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } -Message::Ptr Api::sendAudio(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> audio, +Message::Ptr Api::sendAudio(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> audio, const std::string& caption, std::int32_t duration, const std::string& performer, const std::string& title, - boost::variant<InputFile::Ptr, const std::string&> thumb, + boost::variant<InputFile::Ptr, std::string> thumb, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, @@ -276,15 +278,15 @@ Message::Ptr Api::sendAudio(boost::variant<std::int64_t, const std::string&> cha const std::vector<MessageEntity::Ptr>& captionEntities, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(14); args.emplace_back("chat_id", chatId); if (audio.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(audio); args.emplace_back("audio", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("audio", boost::get<const std::string&>(audio)); + } else { // std::string + args.emplace_back("audio", boost::get<std::string>(audio)); } if (!caption.empty()) { args.emplace_back("caption", caption); @@ -307,8 +309,8 @@ Message::Ptr Api::sendAudio(boost::variant<std::int64_t, const std::string&> cha if (thumb.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(thumb); args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - auto thumbStr = boost::get<const std::string&>(thumb); + } else { // std::string + auto thumbStr = boost::get<std::string>(thumb); if (!thumbStr.empty()) { args.emplace_back("thumb", thumbStr); } @@ -332,9 +334,9 @@ Message::Ptr Api::sendAudio(boost::variant<std::int64_t, const std::string&> cha return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAudio", args)); } -Message::Ptr Api::sendDocument(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> document, - boost::variant<InputFile::Ptr, const std::string&> thumb, +Message::Ptr Api::sendDocument(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> document, + boost::variant<InputFile::Ptr, std::string> thumb, const std::string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -344,21 +346,21 @@ Message::Ptr Api::sendDocument(boost::variant<std::int64_t, const std::string&> bool disableContentTypeDetection, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(12); args.emplace_back("chat_id", chatId); if (document.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(document); args.emplace_back("document", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("document", boost::get<const std::string&>(document)); + } else { // std::string + args.emplace_back("document", boost::get<std::string>(document)); } if (thumb.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(thumb); args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - auto thumbStr = boost::get<const std::string&>(thumb); + } else { // std::string + auto thumbStr = boost::get<std::string>(thumb); if (!thumbStr.empty()) { args.emplace_back("thumb", thumbStr); } @@ -394,13 +396,13 @@ Message::Ptr Api::sendDocument(boost::variant<std::int64_t, const std::string&> return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDocument", args)); } -Message::Ptr Api::sendVideo(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> video, +Message::Ptr Api::sendVideo(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, - boost::variant<InputFile::Ptr, const std::string&> thumb, + boost::variant<InputFile::Ptr, std::string> thumb, const std::string& caption , std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -409,15 +411,15 @@ Message::Ptr Api::sendVideo(boost::variant<std::int64_t, const std::string&> cha const std::vector<MessageEntity::Ptr>& captionEntities, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(15); args.emplace_back("chat_id", chatId); if (video.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(video); args.emplace_back("video", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("video", boost::get<const std::string&>(video)); + } else { // std::string + args.emplace_back("video", boost::get<std::string>(video)); } if (duration) { args.emplace_back("duration", duration); @@ -431,8 +433,8 @@ Message::Ptr Api::sendVideo(boost::variant<std::int64_t, const std::string&> cha if (thumb.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(thumb); args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - auto thumbStr = boost::get<const std::string&>(thumb); + } else { // std::string + auto thumbStr = boost::get<std::string>(thumb); if (!thumbStr.empty()) { args.emplace_back("thumb", thumbStr); } @@ -468,12 +470,12 @@ Message::Ptr Api::sendVideo(boost::variant<std::int64_t, const std::string&> cha return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); } -Message::Ptr Api::sendAnimation(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> animation, +Message::Ptr Api::sendAnimation(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration, std::int32_t width, std::int32_t height, - boost::variant<InputFile::Ptr, const std::string&> thumb, + boost::variant<InputFile::Ptr, std::string> thumb, const std::string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -482,15 +484,15 @@ Message::Ptr Api::sendAnimation(boost::variant<std::int64_t, const std::string&> const std::vector<MessageEntity::Ptr>& captionEntities, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(14); args.emplace_back("chat_id", chatId); if (animation.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(animation); args.emplace_back("animation", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("animation", boost::get<const std::string&>(animation)); + } else { // std::string + args.emplace_back("animation", boost::get<std::string>(animation)); } if (duration) { args.emplace_back("duration", duration); @@ -504,8 +506,8 @@ Message::Ptr Api::sendAnimation(boost::variant<std::int64_t, const std::string&> if (thumb.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(thumb); args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - auto thumbStr = boost::get<const std::string&>(thumb); + } else { // std::string + auto thumbStr = boost::get<std::string>(thumb); if (!thumbStr.empty()) { args.emplace_back("thumb", thumbStr); } @@ -538,8 +540,8 @@ Message::Ptr Api::sendAnimation(boost::variant<std::int64_t, const std::string&> return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAnimation", args)); } -Message::Ptr Api::sendVoice(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> voice, +Message::Ptr Api::sendVoice(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> voice, const std::string& caption, std::int32_t duration, std::int32_t replyToMessageId, @@ -549,15 +551,15 @@ Message::Ptr Api::sendVoice(boost::variant<std::int64_t, const std::string&> cha const std::vector<MessageEntity::Ptr>& captionEntities, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); if (voice.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(voice); args.emplace_back("voice", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("voice", boost::get<const std::string&>(voice)); + } else { // std::string + args.emplace_back("voice", boost::get<std::string>(voice)); } if (!caption.empty()) { args.emplace_back("caption", caption); @@ -590,25 +592,25 @@ Message::Ptr Api::sendVoice(boost::variant<std::int64_t, const std::string&> cha return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoice", args)); } -Message::Ptr Api::sendVideoNote(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> videoNote, +Message::Ptr Api::sendVideoNote(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, - boost::variant<InputFile::Ptr, const std::string&> thumb, + boost::variant<InputFile::Ptr, std::string> thumb, GenericReply::Ptr replyMarkup, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(10); args.emplace_back("chat_id", chatId); if (videoNote.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(videoNote); args.emplace_back("video_note", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("video_note", boost::get<const std::string&>(videoNote)); + } else { // std::string + args.emplace_back("video_note", boost::get<std::string>(videoNote)); } if (duration) { args.emplace_back("duration", duration); @@ -619,8 +621,8 @@ Message::Ptr Api::sendVideoNote(boost::variant<std::int64_t, const std::string&> if (thumb.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(thumb); args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - auto thumbStr = boost::get<const std::string&>(thumb); + } else { // std::string + auto thumbStr = boost::get<std::string>(thumb); if (!thumbStr.empty()) { args.emplace_back("thumb", thumbStr); } @@ -644,13 +646,13 @@ Message::Ptr Api::sendVideoNote(boost::variant<std::int64_t, const std::string&> return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideoNote", args)); } -vector<Message::Ptr> Api::sendMediaGroup(boost::variant<std::int64_t, const std::string&> chatId, - const std::vector<InputMedia::Ptr>& media, - bool disableNotification, - std::int32_t replyToMessageId, - bool allowSendingWithoutReply, - bool protectContent) const { - vector<HttpReqArg> args; +std::vector<Message::Ptr> Api::sendMediaGroup(boost::variant<std::int64_t, std::string> chatId, + const std::vector<InputMedia::Ptr>& media, + bool disableNotification, + std::int32_t replyToMessageId, + bool allowSendingWithoutReply, + bool protectContent) const { + std::vector<HttpReqArg> args; args.reserve(6); args.emplace_back("chat_id", chatId); @@ -671,7 +673,7 @@ vector<Message::Ptr> Api::sendMediaGroup(boost::variant<std::int64_t, const std: return _tgTypeParser.parseJsonAndGetArray<Message>(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } -Message::Ptr Api::sendLocation(boost::variant<std::int64_t, const std::string&> chatId, +Message::Ptr Api::sendLocation(boost::variant<std::int64_t, std::string> chatId, float latitude, float longitude, std::int32_t livePeriod, @@ -683,7 +685,7 @@ Message::Ptr Api::sendLocation(boost::variant<std::int64_t, const std::string&> std::int32_t proximityAlertRadius, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(12); args.emplace_back("chat_id", chatId); @@ -693,13 +695,13 @@ Message::Ptr Api::sendLocation(boost::variant<std::int64_t, const std::string&> args.emplace_back("horizontal_accuracy", horizontalAccuracy); } if (livePeriod) { - args.emplace_back("live_period", livePeriod); + args.emplace_back("live_period", std::max(60, std::min(86400, livePeriod))); } if (heading) { - args.emplace_back("heading", heading); + args.emplace_back("heading", std::max(1, std::min(360, heading))); } if (proximityAlertRadius) { - args.emplace_back("proximity_alert_radius", proximityAlertRadius); + args.emplace_back("proximity_alert_radius", std::max(1, std::min(100000, proximityAlertRadius))); } if (disableNotification) { args.emplace_back("disable_notification", disableNotification); @@ -722,23 +724,23 @@ Message::Ptr Api::sendLocation(boost::variant<std::int64_t, const std::string&> Message::Ptr Api::editMessageLiveLocation(float latitude, float longitude, - std::int64_t chatId, + boost::variant<std::int64_t, std::string> chatId, std::int32_t messageId, - std::int32_t inlineMessageId, + const std::string& inlineMessageId, InlineKeyboardMarkup::Ptr replyMarkup, float horizontalAccuracy, std::int32_t heading, std::int32_t proximityAlertRadius) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(9); - if (chatId) { + if ((boost::get<std::int64_t>(chatId) != 0) || (boost::get<std::string>(chatId) != "")) { args.emplace_back("chat_id", chatId); } if (messageId) { args.emplace_back("message_id", messageId); } - if (inlineMessageId) { + if (!inlineMessageId.empty()) { args.emplace_back("inline_message_id", inlineMessageId); } args.emplace_back("latitude", latitude); @@ -759,25 +761,30 @@ Message::Ptr Api::editMessageLiveLocation(float latitude, return _tgTypeParser.parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); } -Message::Ptr Api::stopMessageLiveLocation(std::int64_t chatId, std::int32_t messageId, std::int32_t inlineMessageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { - vector<HttpReqArg> args; +Message::Ptr Api::stopMessageLiveLocation(boost::variant<std::int64_t, std::string> chatId, + std::int32_t messageId, + const std::string& inlineMessageId, + InlineKeyboardMarkup::Ptr replyMarkup) const { + std::vector<HttpReqArg> args; args.reserve(4); - if (chatId) { + + if ((boost::get<std::int64_t>(chatId) != 0) || (boost::get<std::string>(chatId) != "")) { args.emplace_back("chat_id", chatId); } if (messageId) { args.emplace_back("message_id", messageId); } - if (inlineMessageId) { + if (!inlineMessageId.empty()) { args.emplace_back("inline_message_id", inlineMessageId); } if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseInlineKeyboardMarkup(replyMarkup)); } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); + + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("stopMessageLiveLocation", args)); } -Message::Ptr Api::sendVenue(boost::variant<std::int64_t, const std::string&> chatId, +Message::Ptr Api::sendVenue(boost::variant<std::int64_t, std::string> chatId, float latitude, float longitude, const std::string& title, @@ -791,7 +798,7 @@ Message::Ptr Api::sendVenue(boost::variant<std::int64_t, const std::string&> cha const std::string& googlePlaceType, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(14); args.emplace_back("chat_id", chatId); @@ -830,7 +837,7 @@ Message::Ptr Api::sendVenue(boost::variant<std::int64_t, const std::string&> cha return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVenue", args)); } -Message::Ptr Api::sendContact(boost::variant<std::int64_t, const std::string&> chatId, +Message::Ptr Api::sendContact(boost::variant<std::int64_t, std::string> chatId, const std::string& phoneNumber, const std::string& firstName, const std::string& lastName , @@ -840,7 +847,7 @@ Message::Ptr Api::sendContact(boost::variant<std::int64_t, const std::string&> c GenericReply::Ptr replyMarkup, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(10); args.emplace_back("chat_id", chatId); @@ -871,7 +878,7 @@ Message::Ptr Api::sendContact(boost::variant<std::int64_t, const std::string&> c return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendContact", args)); } -Message::Ptr Api::sendPoll(boost::variant<std::int64_t, const std::string&> chatId, +Message::Ptr Api::sendPoll(boost::variant<std::int64_t, std::string> chatId, const std::string& question, const std::vector<std::string>& options, bool disableNotification, @@ -889,13 +896,14 @@ Message::Ptr Api::sendPoll(boost::variant<std::int64_t, const std::string&> chat bool isClosed, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(18); args.emplace_back("chat_id", chatId); args.emplace_back("question", question); - args.emplace_back("options", _tgTypeParser.parseArray<std::string>([] (const std::string& option) -> std::string { - return "\"" + StringTools::urlEncode(option) + "\""; + args.emplace_back("options", _tgTypeParser.parseArray<std::string>( + [](const std::string& option)->std::string { + return "\"" + StringTools::escapeJsonString(option) + "\""; }, options)); if (!isAnonymous) { args.emplace_back("is_anonymous", isAnonymous); @@ -906,7 +914,7 @@ Message::Ptr Api::sendPoll(boost::variant<std::int64_t, const std::string&> chat if (allowsMultipleAnswers) { args.emplace_back("allows_multiple_answers", allowsMultipleAnswers); } - if (correctOptionId != 0) { + if (correctOptionId != -1) { args.emplace_back("correct_option_id", correctOptionId); } if (!explanation.empty()) { @@ -946,14 +954,14 @@ Message::Ptr Api::sendPoll(boost::variant<std::int64_t, const std::string&> chat return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); } -Message::Ptr Api::sendDice(boost::variant<std::int64_t, const std::string&> chatId, +Message::Ptr Api::sendDice(boost::variant<std::int64_t, std::string> chatId, bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& emoji, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(7); args.emplace_back("chat_id", chatId); @@ -981,7 +989,7 @@ Message::Ptr Api::sendDice(boost::variant<std::int64_t, const std::string&> chat bool Api::sendChatAction(std::int64_t chatId, const std::string& action) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -990,67 +998,73 @@ bool Api::sendChatAction(std::int64_t chatId, return sendRequest("sendChatAction", args).get<bool>("", false); } -UserProfilePhotos::Ptr Api::getUserProfilePhotos(std::int64_t userId, std::int32_t offset, std::int32_t limit) const { - vector<HttpReqArg> args; +UserProfilePhotos::Ptr Api::getUserProfilePhotos(std::int64_t userId, + std::int32_t offset, + std::int32_t limit) const { + std::vector<HttpReqArg> args; args.reserve(3); + args.emplace_back("user_id", userId); if (offset) { args.emplace_back("offset", offset); } - limit = max(1, min(100, limit)); - args.emplace_back("limit", limit); + if (limit != 100) { + args.emplace_back("limit", std::max(1, std::min(100, limit))); + } + return _tgTypeParser.parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); } -File::Ptr Api::getFile(const string& fileId) const { - vector<HttpReqArg> args; +File::Ptr Api::getFile(const std::string& fileId) const { + std::vector<HttpReqArg> args; args.reserve(1); - args.emplace_back("file_id", fileId); - return _tgTypeParser.parseJsonAndGetFile(sendRequest("getFile", args)); -} -string Api::downloadFile(const string& filePath, const std::vector<HttpReqArg>& args) const { - string url(_url); - url += "/file/bot"; - url += _token; - url += "/"; - url += filePath; - - string serverResponse = _httpClient.makeRequest(url, args); + args.emplace_back("file_id", fileId); - return serverResponse; + return _tgTypeParser.parseJsonAndGetFile(sendRequest("getFile", args)); } -bool Api::banChatMember(std::int64_t chatId, +bool Api::banChatMember(boost::variant<std::int64_t, std::string> chatId, std::int64_t userId, - std::uint64_t untilDate, + std::int32_t untilDate, bool revokeMessages) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(4); args.emplace_back("chat_id", chatId); args.emplace_back("user_id", userId); - args.emplace_back("until_date", untilDate); - args.emplace_back("revoke_messages", revokeMessages); + if (untilDate != 0) { + args.emplace_back("until_date", untilDate); + } + if (revokeMessages) { + args.emplace_back("revoke_messages", revokeMessages); + } return sendRequest("banChatMember", args).get<bool>("", false); } -bool Api::unbanChatMember(std::int64_t chatId, std::int64_t userId, bool onlyIfBanned) const { - vector<HttpReqArg> args; +bool Api::unbanChatMember(boost::variant<std::int64_t, std::string> chatId, + std::int64_t userId, + bool onlyIfBanned) const { + std::vector<HttpReqArg> args; args.reserve(3); args.emplace_back("chat_id", chatId); args.emplace_back("user_id", userId); - args.emplace_back("only_if_banned", onlyIfBanned); + if (onlyIfBanned) { + args.emplace_back("only_if_banned", onlyIfBanned); + } return sendRequest("unbanChatMember", args).get<bool>("", false); } -bool Api::restrictChatMember(std::int64_t chatId, std::int64_t userId, TgBot::ChatPermissions::Ptr permissions, - std::uint64_t untilDate) const { - vector<HttpReqArg> args; +bool Api::restrictChatMember(boost::variant<std::int64_t, std::string> chatId, + std::int64_t userId, + TgBot::ChatPermissions::Ptr permissions, + std::int64_t untilDate) const { + std::vector<HttpReqArg> args; args.reserve(4); + args.emplace_back("chat_id", chatId); args.emplace_back("user_id", userId); args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); @@ -1061,7 +1075,7 @@ bool Api::restrictChatMember(std::int64_t chatId, std::int64_t userId, TgBot::Ch return sendRequest("restrictChatMember", args).get<bool>("", false); } -bool Api::promoteChatMember(boost::variant<std::int64_t, const std::string&> chatId, +bool Api::promoteChatMember(boost::variant<std::int64_t, std::string> chatId, std::int64_t userId, bool canChangeInfo, bool canPostMessages, @@ -1074,7 +1088,7 @@ bool Api::promoteChatMember(boost::variant<std::int64_t, const std::string&> cha bool canManageChat, bool canManageVideoChats, bool canRestrictMembers) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(13); args.emplace_back("chat_id", chatId); @@ -1116,8 +1130,10 @@ bool Api::promoteChatMember(boost::variant<std::int64_t, const std::string&> cha return sendRequest("promoteChatMember", args).get<bool>("", false); } -bool Api::setChatAdministratorCustomTitle(std::int64_t chatId, std::int64_t userId, const std::string& customTitle) const { - vector<HttpReqArg> args; +bool Api::setChatAdministratorCustomTitle(boost::variant<std::int64_t, std::string> chatId, + std::int64_t userId, + const std::string& customTitle) const { + std::vector<HttpReqArg> args; args.reserve(3); args.emplace_back("chat_id", chatId); @@ -1127,9 +1143,9 @@ bool Api::setChatAdministratorCustomTitle(std::int64_t chatId, std::int64_t user return sendRequest("setChatAdministratorCustomTitle", args).get<bool>("", false); } -bool Api::banChatSenderChat(std::int64_t chatId, - std::int64_t senderChatId) const { - vector<HttpReqArg> args; +bool Api::banChatSenderChat(boost::variant<std::int64_t, std::string> chatId, + std::int64_t senderChatId) const { + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -1138,9 +1154,9 @@ bool Api::banChatSenderChat(std::int64_t chatId, return sendRequest("banChatSenderChat", args).get<bool>("", false); } -bool Api::unbanChatSenderChat(std::int64_t chatId, +bool Api::unbanChatSenderChat(boost::variant<std::int64_t, std::string> chatId, std::int64_t senderChatId) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -1149,27 +1165,32 @@ bool Api::unbanChatSenderChat(std::int64_t chatId, return sendRequest("unbanChatSenderChat", args).get<bool>("", false); } -bool Api::setChatPermissions(std::int64_t chatId, ChatPermissions::Ptr permissions) const { - vector<HttpReqArg> args; +bool Api::setChatPermissions(boost::variant<std::int64_t, std::string> chatId, + ChatPermissions::Ptr permissions) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("chat_id", chatId); args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); + return sendRequest("setChatPermissions", args).get<bool>("", false); } -string Api::exportChatInviteLink(std::int64_t chatId) const { - vector<HttpReqArg> args; +std::string Api::exportChatInviteLink(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("exportChatInviteLink", args).get("", ""); } -ChatInviteLink::Ptr Api::createChatInviteLink(std::int64_t chatId, +ChatInviteLink::Ptr Api::createChatInviteLink(boost::variant<std::int64_t, std::string> chatId, std::int32_t expireDate, std::int32_t memberLimit, const std::string& name, bool createsJoinRequest) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(5); args.emplace_back("chat_id", chatId); @@ -1189,13 +1210,13 @@ ChatInviteLink::Ptr Api::createChatInviteLink(std::int64_t chatId, return _tgTypeParser.parseJsonAndGetChatInviteLink(sendRequest("createChatInviteLink", args)); } -ChatInviteLink::Ptr Api::editChatInviteLink(std::int64_t chatId, +ChatInviteLink::Ptr Api::editChatInviteLink(boost::variant<std::int64_t, std::string> chatId, const std::string& inviteLink, std::int32_t expireDate, std::int32_t memberLimit, const std::string& name, bool createsJoinRequest) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(6); args.emplace_back("chat_id", chatId); @@ -1216,9 +1237,9 @@ ChatInviteLink::Ptr Api::editChatInviteLink(std::int64_t chatId, return _tgTypeParser.parseJsonAndGetChatInviteLink(sendRequest("editChatInviteLink", args)); } -ChatInviteLink::Ptr Api::revokeChatInviteLink(std::int64_t chatId, +ChatInviteLink::Ptr Api::revokeChatInviteLink(boost::variant<std::int64_t, std::string> chatId, const std::string& inviteLink) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -1227,9 +1248,9 @@ ChatInviteLink::Ptr Api::revokeChatInviteLink(std::int64_t chatId, return _tgTypeParser.parseJsonAndGetChatInviteLink(sendRequest("revokeChatInviteLink", args)); } -bool Api::approveChatJoinRequest(std::int64_t chatId, +bool Api::approveChatJoinRequest(boost::variant<std::int64_t, std::string> chatId, std::int64_t userId) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -1238,9 +1259,9 @@ bool Api::approveChatJoinRequest(std::int64_t chatId, return sendRequest("approveChatJoinRequest", args).get<bool>("", false); } -bool Api::declineChatJoinRequest(std::int64_t chatId, +bool Api::declineChatJoinRequest(boost::variant<std::int64_t, std::string> chatId, std::int64_t userId) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -1249,60 +1270,80 @@ bool Api::declineChatJoinRequest(std::int64_t chatId, return sendRequest("declineChatJoinRequest", args).get<bool>("", false); } -bool Api::setChatPhoto(std::int64_t chatId, const InputFile::Ptr photo) const { - vector<HttpReqArg> args; +bool Api::setChatPhoto(boost::variant<std::int64_t, std::string> chatId, + const InputFile::Ptr photo) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("chat_id", chatId); args.emplace_back("photo", photo->data, true, photo->mimeType, photo->fileName); + return sendRequest("setChatPhoto", args).get<bool>("", false); } -bool Api::deleteChatPhoto(std::int64_t chatId) const { - vector<HttpReqArg> args; +bool Api::deleteChatPhoto(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatPhoto", args).get<bool>("", false); } -bool Api::setChatTitle(std::int64_t chatId, const string& title) const { - vector<HttpReqArg> args; +bool Api::setChatTitle(boost::variant<std::int64_t, std::string> chatId, + const std::string& title) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("chat_id", chatId); args.emplace_back("title", title); + return sendRequest("setChatTitle", args).get<bool>("", false); } -bool Api::setChatDescription(std::int64_t chatId, const string& description) const { - vector<HttpReqArg> args; +bool Api::setChatDescription(boost::variant<std::int64_t, std::string> chatId, + const std::string& description) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("chat_id", chatId); - args.emplace_back("description", description); + if (!description.empty()) { + args.emplace_back("description", description); + } + return sendRequest("setChatDescription", args).get<bool>("", false); } -bool Api::pinChatMessage(std::int64_t chatId, std::int32_t messageId, bool disableNotification) const { - vector<HttpReqArg> args; +bool Api::pinChatMessage(boost::variant<std::int64_t, std::string> chatId, + std::int32_t messageId, + bool disableNotification) const { + std::vector<HttpReqArg> args; args.reserve(3); + args.emplace_back("chat_id", chatId); args.emplace_back("message_id", messageId); if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } + return sendRequest("pinChatMessage", args).get<bool>("", false); } -bool Api::unpinChatMessage(std::int64_t chatId, std::int32_t messageId) const { - vector<HttpReqArg> args; +bool Api::unpinChatMessage(boost::variant<std::int64_t, std::string> chatId, + std::int32_t messageId) const { + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("chat_id", chatId); - args.emplace_back("message_id", messageId); + if (messageId != 0) { + args.emplace_back("message_id", messageId); + } return sendRequest("unpinChatMessage", args).get<bool>("", false); } -bool Api::unpinAllChatMessages(std::int64_t chatId) const { - vector<HttpReqArg> args; +bool Api::unpinAllChatMessages(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); args.emplace_back("chat_id", chatId); @@ -1310,15 +1351,17 @@ bool Api::unpinAllChatMessages(std::int64_t chatId) const { return sendRequest("unpinAllChatMessages", args).get<bool>("", false); } -bool Api::leaveChat(std::int64_t chatId) const { - vector<HttpReqArg> args; +bool Api::leaveChat(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("leaveChat", args).get<bool>("", false); } -Chat::Ptr Api::getChat(std::int64_t chatId) const { - vector<HttpReqArg> args; +Chat::Ptr Api::getChat(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); args.emplace_back("chat_id", chatId); @@ -1326,15 +1369,17 @@ Chat::Ptr Api::getChat(std::int64_t chatId) const { return _tgTypeParser.parseJsonAndGetChat(sendRequest("getChat", args)); } -vector<ChatMember::Ptr> Api::getChatAdministrators(std::int64_t chatId) const { - vector<HttpReqArg> args; +std::vector<ChatMember::Ptr> Api::getChatAdministrators(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); + args.emplace_back("chat_id", chatId); + return _tgTypeParser.parseJsonAndGetArray<ChatMember>(&TgTypeParser::parseJsonAndGetChatMember, sendRequest("getChatAdministrators", args)); } -int32_t Api::getChatMemberCount(std::int64_t chatId) const { - vector<HttpReqArg> args; +int32_t Api::getChatMemberCount(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); args.emplace_back("chat_id", chatId); @@ -1342,32 +1387,45 @@ int32_t Api::getChatMemberCount(std::int64_t chatId) const { return sendRequest("getChatMemberCount", args).get<int32_t>("", 0); } -ChatMember::Ptr Api::getChatMember(std::int64_t chatId, std::int64_t userId) const { - vector<HttpReqArg> args; +ChatMember::Ptr Api::getChatMember(boost::variant<std::int64_t, std::string> chatId, + std::int64_t userId) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("chat_id", chatId); args.emplace_back("user_id", userId); + return _tgTypeParser.parseJsonAndGetChatMember(sendRequest("getChatMember", args)); } -bool Api::setChatStickerSet(std::int64_t chatId, const string& stickerSetName) const { - vector<HttpReqArg> args; +bool Api::setChatStickerSet(boost::variant<std::int64_t, std::string> chatId, + const std::string& stickerSetName) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("chat_id", chatId); args.emplace_back("sticker_set_name ", stickerSetName); + return sendRequest("setChatStickerSet", args).get<bool>("", false); } -bool Api::deleteChatStickerSet(std::int64_t chatId) const { - vector<HttpReqArg> args; +bool Api::deleteChatStickerSet(boost::variant<std::int64_t, std::string> chatId) const { + std::vector<HttpReqArg> args; args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatStickerSet", args).get<bool>("", false); } -bool Api::answerCallbackQuery(const string& callbackQueryId, const string& text, bool showAlert, const string& url, std::int32_t cacheTime) const { - vector<HttpReqArg> args; +bool Api::answerCallbackQuery(const std::string& callbackQueryId, + const std::string& text, + bool showAlert, + const std::string& url, + std::int32_t cacheTime) const { + std::vector<HttpReqArg> args; args.reserve(5); + args.emplace_back("callback_query_id", callbackQueryId); if (!text.empty()) { args.emplace_back("text", text); @@ -1381,13 +1439,14 @@ bool Api::answerCallbackQuery(const string& callbackQueryId, const string& text, if (cacheTime) { args.emplace_back("cache_time", cacheTime); } + return sendRequest("answerCallbackQuery", args).get<bool>("", false); } bool Api::setMyCommands(const std::vector<BotCommand::Ptr>& commands, BotCommandScope::Ptr scope, const std::string& languageCode) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(3); args.emplace_back("commands", _tgTypeParser.parseArray<BotCommand>(&TgTypeParser::parseBotCommand, commands)); @@ -1403,7 +1462,7 @@ bool Api::setMyCommands(const std::vector<BotCommand::Ptr>& commands, bool Api::deleteMyCommands(BotCommandScope::Ptr scope, const std::string& languageCode) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); if (scope != nullptr) { @@ -1418,7 +1477,7 @@ bool Api::deleteMyCommands(BotCommandScope::Ptr scope, std::vector<BotCommand::Ptr> Api::getMyCommands(BotCommandScope::Ptr scope, const std::string& languageCode) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); ; if (scope != nullptr) { @@ -1433,7 +1492,7 @@ std::vector<BotCommand::Ptr> Api::getMyCommands(BotCommandScope::Ptr scope, bool Api::setChatMenuButton(std::int64_t chatId, MenuButton::Ptr menuButton) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); if (chatId != 0) { @@ -1447,7 +1506,7 @@ bool Api::setChatMenuButton(std::int64_t chatId, } MenuButton::Ptr Api::getChatMenuButton(std::int64_t chatId) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(1); if (chatId != 0) { @@ -1458,8 +1517,8 @@ MenuButton::Ptr Api::getChatMenuButton(std::int64_t chatId) const { } bool Api::setMyDefaultAdministratorRights(ChatAdministratorRights::Ptr rights, - bool forChannels) const { - vector<HttpReqArg> args; + bool forChannels) const { + std::vector<HttpReqArg> args; args.reserve(2); if (rights != nullptr) { @@ -1473,7 +1532,7 @@ bool Api::setMyDefaultAdministratorRights(ChatAdministratorRights::Ptr rights, } ChatAdministratorRights::Ptr Api::getMyDefaultAdministratorRights(bool forChannels) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(1); if (forChannels) { @@ -1484,17 +1543,17 @@ ChatAdministratorRights::Ptr Api::getMyDefaultAdministratorRights(bool forChanne } Message::Ptr Api::editMessageText(const std::string& text, - std::int64_t chatId, + boost::variant<std::int64_t, std::string> chatId, std::int32_t messageId, const std::string& inlineMessageId, const std::string& parseMode, bool disableWebPagePreview, GenericReply::Ptr replyMarkup, const std::vector<MessageEntity::Ptr>& entities) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(8); - if (chatId) { + if ((boost::get<std::int64_t>(chatId) != 0) || (boost::get<std::string>(chatId) != "")) { args.emplace_back("chat_id", chatId); } if (messageId) { @@ -1517,7 +1576,7 @@ Message::Ptr Api::editMessageText(const std::string& text, args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageText", args); + boost::property_tree::ptree p = sendRequest("editMessageText", args); if (p.get_child_optional("message_id")) { return _tgTypeParser.parseJsonAndGetMessage(p); } else { @@ -1525,17 +1584,17 @@ Message::Ptr Api::editMessageText(const std::string& text, } } -Message::Ptr Api::editMessageCaption(std::int64_t chatId, +Message::Ptr Api::editMessageCaption(boost::variant<std::int64_t, std::string> chatId, std::int32_t messageId, const std::string& caption, const std::string& inlineMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, const std::vector<MessageEntity::Ptr>& captionEntities) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(7); - if (chatId) { + if ((boost::get<std::int64_t>(chatId) != 0) || (boost::get<std::string>(chatId) != "")) { args.emplace_back("chat_id", chatId); } if (messageId) { @@ -1557,7 +1616,7 @@ Message::Ptr Api::editMessageCaption(std::int64_t chatId, args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageCaption", args); + boost::property_tree::ptree p = sendRequest("editMessageCaption", args); if (p.get_child_optional("message_id")) { return _tgTypeParser.parseJsonAndGetMessage(p); } else { @@ -1565,13 +1624,17 @@ Message::Ptr Api::editMessageCaption(std::int64_t chatId, } } -Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId, +Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, + boost::variant<std::int64_t, std::string> chatId, + std::int32_t messageId, + const std::string& inlineMessageId, GenericReply::Ptr replyMarkup) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(5); + args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); - if (chatId) { + if ((boost::get<std::int64_t>(chatId) != 0) || (boost::get<std::string>(chatId) != "")) { args.emplace_back("chat_id", chatId); } if (messageId) { @@ -1583,7 +1646,8 @@ Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, s if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageMedia", args); + + boost::property_tree::ptree p = sendRequest("editMessageMedia", args); if (p.get_child_optional("message_id")) { return _tgTypeParser.parseJsonAndGetMessage(p); } else { @@ -1591,12 +1655,15 @@ Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, s } } -Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, +Message::Ptr Api::editMessageReplyMarkup(boost::variant<std::int64_t, std::string> chatId, + std::int32_t messageId, + const std::string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(4); - if (chatId) { + + if ((boost::get<std::int64_t>(chatId) != 0) || (boost::get<std::string>(chatId) != "")) { args.emplace_back("chat_id", chatId); } if (messageId) { @@ -1608,7 +1675,8 @@ Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messa if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageReplyMarkup", args); + + boost::property_tree::ptree p = sendRequest("editMessageReplyMarkup", args); if (p.get_child_optional("message_id")) { return _tgTypeParser.parseJsonAndGetMessage(p); } else { @@ -1616,37 +1684,48 @@ Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messa } } -Poll::Ptr Api::stopPoll(std::int64_t chatId, std::int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { - vector<HttpReqArg> args; +Poll::Ptr Api::stopPoll(boost::variant<std::int64_t, std::string> chatId, + std::int64_t messageId, + const InlineKeyboardMarkup::Ptr replyMarkup) const { + std::vector<HttpReqArg> args; args.reserve(3); + args.emplace_back("chat_id", chatId); args.emplace_back("message_id", messageId); if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } + return _tgTypeParser.parseJsonAndGetPoll(sendRequest("stopPoll", args)); } -void Api::deleteMessage(std::int64_t chatId, std::int32_t messageId) const { - sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); +bool Api::deleteMessage(boost::variant<std::int64_t, std::string> chatId, + std::int32_t messageId) const { + std::vector<HttpReqArg> args; + args.reserve(2); + + args.emplace_back("chat_id", chatId); + args.emplace_back("message_id", messageId); + + return sendRequest("deleteMessage", args).get<bool>("", false); } -Message::Ptr Api::sendSticker(boost::variant<std::int64_t, const std::string&> chatId, - boost::variant<InputFile::Ptr, const std::string&> sticker, +Message::Ptr Api::sendSticker(boost::variant<std::int64_t, std::string> chatId, + boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, bool disableNotification, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(7); args.emplace_back("chat_id", chatId); if (sticker.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(sticker); args.emplace_back("sticker", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("sticker", boost::get<const std::string&>(sticker)); + } else { // std::string + args.emplace_back("sticker", boost::get<std::string>(sticker)); } if (disableNotification) { args.emplace_back("disable_notification", disableNotification); @@ -1667,8 +1746,8 @@ Message::Ptr Api::sendSticker(boost::variant<std::int64_t, const std::string&> c return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendSticker", args)); } -StickerSet::Ptr Api::getStickerSet(const string& name) const { - vector<HttpReqArg> args; +StickerSet::Ptr Api::getStickerSet(const std::string& name) const { + std::vector<HttpReqArg> args; args.reserve(1); args.emplace_back("name", name); @@ -1677,21 +1756,24 @@ StickerSet::Ptr Api::getStickerSet(const string& name) const { } std::vector<Sticker::Ptr> Api::getCustomEmojiStickers(const std::vector<std::string>& customEmojiIds) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(1); args.emplace_back("custom_emoji_ids", _tgTypeParser.parseArray<std::string>([] (const std::string& customEmojiId) -> std::string { - return "\"" + StringTools::urlEncode(customEmojiId) + "\""; + return "\"" + StringTools::escapeJsonString(customEmojiId) + "\""; }, customEmojiIds)); return _tgTypeParser.parseJsonAndGetArray<Sticker>(&TgTypeParser::parseJsonAndGetSticker, sendRequest("getCustomEmojiStickers", args)); } -File::Ptr Api::uploadStickerFile(std::int64_t userId, const InputFile::Ptr pngSticker) const { - vector<HttpReqArg> args; +File::Ptr Api::uploadStickerFile(std::int64_t userId, + const InputFile::Ptr pngSticker) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("user_id", userId); args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); + return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); } @@ -1700,11 +1782,11 @@ bool Api::createNewStickerSet(std::int64_t userId, const std::string& title, const std::string& emojis, MaskPosition::Ptr maskPosition, - boost::variant<InputFile::Ptr, const std::string&> pngSticker, + boost::variant<InputFile::Ptr, std::string> pngSticker, InputFile::Ptr tgsSticker, InputFile::Ptr webmSticker, const std::string& stickerType) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(10); args.emplace_back("user_id", userId); @@ -1713,8 +1795,8 @@ bool Api::createNewStickerSet(std::int64_t userId, if (pngSticker.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(pngSticker); args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("png_sticker", boost::get<const std::string&>(pngSticker)); + } else { // std::string + args.emplace_back("png_sticker", boost::get<std::string>(pngSticker)); } if (tgsSticker != nullptr) { args.emplace_back("tgs_sticker", tgsSticker->data, true, tgsSticker->mimeType, tgsSticker->fileName); @@ -1737,10 +1819,10 @@ bool Api::addStickerToSet(std::int64_t userId, const std::string& name, const std::string& emojis, MaskPosition::Ptr maskPosition, - boost::variant<InputFile::Ptr, const std::string&> pngSticker, + boost::variant<InputFile::Ptr, std::string> pngSticker, InputFile::Ptr tgsSticker, InputFile::Ptr webmSticker) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(7); args.emplace_back("user_id", userId); @@ -1749,8 +1831,8 @@ bool Api::addStickerToSet(std::int64_t userId, if (pngSticker.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(pngSticker); args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else { // const std::string& - args.emplace_back("png_sticker", boost::get<const std::string&>(pngSticker)); + } else { // std::string + args.emplace_back("png_sticker", boost::get<std::string>(pngSticker)); } if (tgsSticker != nullptr) { args.emplace_back("tgs_sticker", tgsSticker->data, true, tgsSticker->mimeType, tgsSticker->fileName); @@ -1766,44 +1848,56 @@ bool Api::addStickerToSet(std::int64_t userId, return sendRequest("addStickerToSet", args).get<bool>("", false); } -bool Api::setStickerPositionInSet(const string& sticker, std::uint32_t position) const { - vector<HttpReqArg> args; +bool Api::setStickerPositionInSet(const std::string& sticker, + std::int32_t position) const { + std::vector<HttpReqArg> args; args.reserve(2); + args.emplace_back("sticker", sticker); args.emplace_back("position", position); + return sendRequest("setStickerPositionInSet", args).get<bool>("", false); } -bool Api::deleteStickerFromSet(const string& sticker) const { - vector<HttpReqArg> args; +bool Api::deleteStickerFromSet(const std::string& sticker) const { + std::vector<HttpReqArg> args; args.reserve(1); + args.emplace_back("sticker", sticker); + return sendRequest("deleteStickerFromSet", args).get<bool>("", false); } -bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost::variant<InputFile::Ptr, std::string> thumb) const { - vector<HttpReqArg> args; +bool Api::setStickerSetThumb(const std::string& name, + std::int64_t userId, + boost::variant<InputFile::Ptr, std::string> thumb) const { + std::vector<HttpReqArg> args; args.reserve(3); args.emplace_back("name", name); args.emplace_back("user_id", userId); - if (thumb.which() == 0 /* InputFile::Ptr */) { + if (thumb.which() == 0) { // InputFile::Ptr auto file = boost::get<InputFile::Ptr>(thumb); args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { + } else { // std::string args.emplace_back("thumb", boost::get<std::string>(thumb)); } return sendRequest("setStickerSetThumb", args).get<bool>("", false); } -bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector<InlineQueryResult::Ptr>& results, - std::int32_t cacheTime, bool isPersonal, const string& nextOffset, const string& switchPmText, const string& switchPmParameter) const { - vector<HttpReqArg> args; +bool Api::answerInlineQuery(const std::string& inlineQueryId, + const std::vector<InlineQueryResult::Ptr>& results, + std::int32_t cacheTime, + bool isPersonal, + const std::string& nextOffset, + const std::string& switchPmText, + const std::string& switchPmParameter) const { + std::vector<HttpReqArg> args; args.reserve(7); + args.emplace_back("inline_query_id", inlineQueryId); - string resultsJson = _tgTypeParser.parseArray<InlineQueryResult>(&TgTypeParser::parseInlineQueryResult, results); - args.emplace_back("results", resultsJson); + args.emplace_back("results", _tgTypeParser.parseArray<InlineQueryResult>(&TgTypeParser::parseInlineQueryResult, results)); if (cacheTime) { args.emplace_back("cache_time", cacheTime); } @@ -1819,12 +1913,13 @@ bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector<Inlin if (!switchPmParameter.empty()) { args.emplace_back("switch_pm_parameter", switchPmParameter); } + return sendRequest("answerInlineQuery", args).get<bool>("", false); } SentWebAppMessage::Ptr Api::answerWebAppQuery(const std::string& webAppQueryId, InlineQueryResult::Ptr result) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("web_app_query_id", webAppQueryId); @@ -1833,7 +1928,7 @@ SentWebAppMessage::Ptr Api::answerWebAppQuery(const std::string& webAppQueryId, return _tgTypeParser.parseJsonAndGetSentWebAppMessage(sendRequest("answerWebAppQuery", args)); } -Message::Ptr Api::sendInvoice(boost::variant<std::int64_t, const std::string&> chatId, +Message::Ptr Api::sendInvoice(boost::variant<std::int64_t, std::string> chatId, const std::string& title, const std::string& description, const std::string& payload, @@ -1860,7 +1955,7 @@ Message::Ptr Api::sendInvoice(boost::variant<std::int64_t, const std::string&> c const std::vector<std::int32_t>& suggestedTipAmounts, const std::string& startParameter, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(27); args.emplace_back("chat_id", chatId); @@ -1954,7 +2049,7 @@ std::string Api::createInvoiceLink(const std::string& title, bool sendPhoneNumberToProvider, bool sendEmailToProvider, bool isFlexible) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(20); args.emplace_back("title", title); @@ -2009,9 +2104,13 @@ std::string Api::createInvoiceLink(const std::string& title, return sendRequest("createInvoiceLink", args).get<std::string>("", ""); } -bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector<ShippingOption::Ptr>& shippingOptions, const std::string& errorMessage) const { - vector<HttpReqArg> args; +bool Api::answerShippingQuery(const std::string& shippingQueryId, + bool ok, + const std::vector<ShippingOption::Ptr>& shippingOptions, + const std::string& errorMessage) const { + std::vector<HttpReqArg> args; args.reserve(4); + args.emplace_back("shipping_query_id", shippingQueryId); args.emplace_back("ok", ok); if (!shippingOptions.empty()) { @@ -2020,22 +2119,28 @@ bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const if (!errorMessage.empty()) { args.emplace_back("error_message", errorMessage); } + return sendRequest("answerShippingQuery", args).get<bool>("", false); } -bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, const std::string& errorMessage) const { - vector<HttpReqArg> args; +bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, + bool ok, + const std::string& errorMessage) const { + std::vector<HttpReqArg> args; args.reserve(3); + args.emplace_back("pre_checkout_query_id", preCheckoutQueryId); args.emplace_back("ok", ok); if (!errorMessage.empty()) { args.emplace_back("error_message", errorMessage); } + return sendRequest("answerPreCheckoutQuery", args).get<bool>("", false); } -bool Api::setPassportDataErrors(std::int64_t userId, const std::vector<PassportElementError::Ptr>& errors) const { - vector<HttpReqArg> args; +bool Api::setPassportDataErrors(std::int64_t userId, + const std::vector<PassportElementError::Ptr>& errors) const { + std::vector<HttpReqArg> args; args.reserve(2); args.emplace_back("user_id", userId); @@ -2051,7 +2156,7 @@ Message::Ptr Api::sendGame(std::int64_t chatId, bool disableNotification, bool allowSendingWithoutReply, bool protectContent) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(7); args.emplace_back("chat_id", chatId); @@ -2075,9 +2180,16 @@ Message::Ptr Api::sendGame(std::int64_t chatId, return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendGame", args)); } -Message::Ptr Api::setGameScore(std::int64_t userId, std::int32_t score, bool force, bool disableEditMessage, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId) const { - vector<HttpReqArg> args; +Message::Ptr Api::setGameScore(std::int64_t userId, + std::int32_t score, + bool force, + bool disableEditMessage, + std::int64_t chatId, + std::int32_t messageId, + const std::string& inlineMessageId) const { + std::vector<HttpReqArg> args; args.reserve(7); + args.emplace_back("user_id", userId); args.emplace_back("score", score); if (force) { @@ -2095,14 +2207,15 @@ Message::Ptr Api::setGameScore(std::int64_t userId, std::int32_t score, bool for if (!inlineMessageId.empty()) { args.emplace_back("inline_message_id", inlineMessageId); } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("setGameScore", args)); } -vector<GameHighScore::Ptr> Api::getGameHighScores(std::int64_t userId, +std::vector<GameHighScore::Ptr> Api::getGameHighScores(std::int64_t userId, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId) const { - vector<HttpReqArg> args; + std::vector<HttpReqArg> args; args.reserve(4); args.emplace_back("user_id", userId); @@ -2119,19 +2232,47 @@ vector<GameHighScore::Ptr> Api::getGameHighScores(std::int64_t userId, return _tgTypeParser.parseJsonAndGetArray<GameHighScore>(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args)); } -ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) const { - string url(_url); +std::string Api::downloadFile(const std::string& filePath, + const std::vector<HttpReqArg>& args) const { + std::string url(_url); + url += "/file/bot"; + url += _token; + url += "/"; + url += filePath; + + return _httpClient.makeRequest(url, args); +} + +bool Api::blockedByUser(std::int64_t chatId) const { + bool isBotBlocked = false; + + try { + sendChatAction(chatId, "typing"); + + } catch (std::exception& e) { + std::string error = e.what(); + + if (error.compare("Forbidden: bot was blocked by the user") == 0) { + isBotBlocked = true; + } + } + + return isBotBlocked; +} + +boost::property_tree::ptree Api::sendRequest(const std::string& method, const std::vector<HttpReqArg>& args) const { + std::string url(_url); url += "/bot"; url += _token; url += "/"; url += method; - string serverResponse = _httpClient.makeRequest(url, args); + std::string serverResponse = _httpClient.makeRequest(url, args); if (!serverResponse.compare(0, 6, "<html>")) { throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); } - ptree result = _tgTypeParser.parseJson(serverResponse); + boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse); try { if (result.get<bool>("ok", false)) { return result.get_child("result"); @@ -2139,7 +2280,7 @@ ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) con throw TgException(result.get("description", "")); } } catch (boost::property_tree::ptree_error& e) { - throw TgException("tgbot-cpp library can't parse json response. " + string(e.what())); + throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); } } } diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index edf4c13..1a6ed40 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -1,14 +1,20 @@ #include "tgbot/EventHandler.h" -#include <algorithm> -#include <cstddef> -#include <string> - -using namespace std; - namespace TgBot { void EventHandler::handleUpdate(const Update::Ptr& update) const { + if (update->message != nullptr) { + handleMessage(update->message); + } + if (update->editedMessage != nullptr) { + _broadcaster.broadcastEditedMessage(update->editedMessage); + } + if (update->channelPost != nullptr) { + handleMessage(update->channelPost); + } + if (update->editedChannelPost != nullptr) { + _broadcaster.broadcastEditedMessage(update->editedChannelPost); + } if (update->inlineQuery != nullptr) { _broadcaster.broadcastInlineQuery(update->inlineQuery); } @@ -18,11 +24,26 @@ void EventHandler::handleUpdate(const Update::Ptr& update) const { if (update->callbackQuery != nullptr) { _broadcaster.broadcastCallbackQuery(update->callbackQuery); } - if (update->message != nullptr) { - handleMessage(update->message); + if (update->shippingQuery != nullptr) { + _broadcaster.broadcastShippingQuery(update->shippingQuery); } - if (update->channelPost != nullptr) { - handleMessage(update->channelPost); + if (update->preCheckoutQuery != nullptr) { + _broadcaster.broadcastPreCheckoutQuery(update->preCheckoutQuery); + } + if (update->poll != nullptr) { + _broadcaster.broadcastPoll(update->poll); + } + if (update->pollAnswer != nullptr) { + _broadcaster.broadcastPollAnswer(update->pollAnswer); + } + if (update->myChatMember != nullptr) { + _broadcaster.broadcastMyChatMember(update->myChatMember); + } + if (update->chatMember != nullptr) { + _broadcaster.broadcastChatMember(update->chatMember); + } + if (update->chatJoinRequest != nullptr) { + _broadcaster.broadcastChatJoinRequest(update->chatJoinRequest); } } @@ -33,13 +54,13 @@ void EventHandler::handleMessage(const Message::Ptr& message) const { std::size_t splitPosition; std::size_t spacePosition = message->text.find(' '); std::size_t atSymbolPosition = message->text.find('@'); - if (spacePosition == string::npos) { - if (atSymbolPosition == string::npos) { + if (spacePosition == std::string::npos) { + if (atSymbolPosition == std::string::npos) { splitPosition = message->text.size(); } else { splitPosition = atSymbolPosition; } - } else if (atSymbolPosition == string::npos) { + } else if (atSymbolPosition == std::string::npos) { splitPosition = spacePosition; } else { splitPosition = std::min(spacePosition, atSymbolPosition); diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index d251da6..1f49489 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -1,10 +1,133 @@ #include "tgbot/TgTypeParser.h" -#include <memory> -#include <string> - namespace TgBot { +Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Update>()); + result->updateId = data.get<std::int32_t>("update_id", 0); + result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message"); + result->editedMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "edited_message"); + result->channelPost = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "channel_post"); + result->editedChannelPost = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "edited_channel_post"); + result->inlineQuery = tryParseJson<InlineQuery>(&TgTypeParser::parseJsonAndGetInlineQuery, data, "inline_query"); + result->chosenInlineResult = tryParseJson<ChosenInlineResult>(&TgTypeParser::parseJsonAndGetChosenInlineResult, data, "chosen_inline_result"); + result->callbackQuery = tryParseJson<CallbackQuery>(&TgTypeParser::parseJsonAndGetCallbackQuery, data, "callback_query"); + result->shippingQuery = tryParseJson<ShippingQuery>(&TgTypeParser::parseJsonAndGetShippingQuery, data, "shipping_query"); + result->preCheckoutQuery = tryParseJson<PreCheckoutQuery>(&TgTypeParser::parseJsonAndGetPreCheckoutQuery, data, "pre_checkout_query"); + result->poll = tryParseJson<Poll>(&TgTypeParser::parseJsonAndGetPoll, data, "poll"); + result->pollAnswer = tryParseJson<PollAnswer>(&TgTypeParser::parseJsonAndGetPollAnswer, data, "poll_answer"); + result->myChatMember = tryParseJson<ChatMemberUpdated>(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "my_chat_member"); + result->chatMember = tryParseJson<ChatMemberUpdated>(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "chat_member"); + result->chatJoinRequest = tryParseJson<ChatJoinRequest>(&TgTypeParser::parseJsonAndGetChatJoinRequest, data, "chat_join_request"); + return result; +} + +std::string TgTypeParser::parseUpdate(const Update::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "update_id", object->updateId); + appendToJson(result, "message", parseMessage(object->message)); + appendToJson(result, "edited_message", parseMessage(object->editedMessage)); + appendToJson(result, "channel_post", parseMessage(object->channelPost)); + appendToJson(result, "edited_channel_post", parseMessage(object->editedChannelPost)); + appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery)); + appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)); + appendToJson(result, "callback_query", parseCallbackQuery(object->callbackQuery)); + appendToJson(result, "shipping_query", parseShippingQuery(object->shippingQuery)); + appendToJson(result, "pre_checkout_query", parsePreCheckoutQuery(object->preCheckoutQuery)); + appendToJson(result, "poll", parsePoll(object->poll)); + appendToJson(result, "poll_answer", parsePollAnswer(object->pollAnswer)); + appendToJson(result, "my_chat_member", parseChatMemberUpdated(object->myChatMember)); + appendToJson(result, "chat_member", parseChatMemberUpdated(object->chatMember)); + appendToJson(result, "chat_join_request", parseChatJoinRequest(object->chatJoinRequest)); + removeLastComma(result); + result += '}'; + return result; +} + +WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<WebhookInfo>()); + result->url = data.get<std::string>("url", ""); + result->hasCustomCertificate = data.get<bool>("has_custom_certificate", false); + result->pendingUpdateCount = data.get<std::int32_t>("pending_update_count", 0); + result->ipAddress = data.get<std::string>("ip_address", ""); + result->lastErrorDate = data.get<std::int32_t>("last_error_date", 0); + result->lastErrorMessage = data.get<std::string>("last_error_message", ""); + result->lastSynchronizationErrorDate = data.get<std::int32_t>("last_synchronization_error_date", 0); + result->maxConnections = data.get<std::int32_t>("max_connections", 0); + result->allowedUpdates = parseJsonAndGetArray<std::string>( + [] (const boost::property_tree::ptree& innerData)->std::string { + return innerData.get<std::string>(""); + } + , data, "allowed_updates"); + return result; +} + +std::string TgTypeParser::parseWebhookInfo(const WebhookInfo::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "url", object->url); + appendToJson(result, "has_custom_certificate", object->hasCustomCertificate); + appendToJson(result, "pending_update_count", object->pendingUpdateCount); + appendToJson(result, "ip_address", object->ipAddress); + appendToJson(result, "last_error_date", object->lastErrorDate); + appendToJson(result, "last_error_message", object->lastErrorMessage); + appendToJson(result, "last_synchronization_error_date", object->lastSynchronizationErrorDate); + appendToJson(result, "max_connections", object->maxConnections); + appendToJson(result, "allowed_updates", + parseArray<std::string>([] (const std::string& s)->std::string { + return s; + } + , object->allowedUpdates)); + removeLastComma(result); + result += '}'; + return result; +} + +User::Ptr TgTypeParser::parseJsonAndGetUser(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<User>()); + result->id = data.get<std::int64_t>("id", 0); + result->isBot = data.get<bool>("is_bot", false); + result->firstName = data.get<std::string>("first_name", ""); + result->lastName = data.get<std::string>("last_name", ""); + result->username = data.get<std::string>("username", ""); + result->languageCode = data.get<std::string>("language_code", ""); + result->isPremium = data.get<bool>("is_premium", false); + result->addedToAttachmentMenu = data.get<bool>("added_to_attachment_menu", false); + result->canJoinGroups = data.get<bool>("can_join_groups", false); + result->canReadAllGroupMessages = data.get<bool>("can_read_all_group_messages", false); + result->supportsInlineQueries = data.get<bool>("supports_inline_queries", false); + return result; +} + +std::string TgTypeParser::parseUser(const User::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "is_bot", object->isBot); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + appendToJson(result, "username", object->username); + appendToJson(result, "language_code", object->languageCode); + appendToJson(result, "is_premium", object->isPremium); + appendToJson(result, "added_to_attachment_menu", object->addedToAttachmentMenu); + appendToJson(result, "can_join_groups", object->canJoinGroups); + appendToJson(result, "can_read_all_group_messages", object->canReadAllGroupMessages); + appendToJson(result, "supports_inline_queries", object->supportsInlineQueries); + removeLastComma(result); + result += '}'; + return result; +} + Chat::Ptr TgTypeParser::parseJsonAndGetChat(const boost::property_tree::ptree& data) const { auto result(std::make_shared<Chat>()); result->id = data.get<std::int64_t>("id", 0); @@ -84,143 +207,6 @@ std::string TgTypeParser::parseChat(const Chat::Ptr& object) const { return result; } -User::Ptr TgTypeParser::parseJsonAndGetUser(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<User>()); - result->id = data.get<std::int64_t>("id", 0); - result->isBot = data.get<bool>("is_bot", false); - result->firstName = data.get<std::string>("first_name", ""); - result->lastName = data.get<std::string>("last_name", ""); - result->username = data.get<std::string>("username", ""); - result->languageCode = data.get<std::string>("language_code", ""); - result->isPremium = data.get<bool>("is_premium", false); - result->addedToAttachmentMenu = data.get<bool>("added_to_attachment_menu", false); - result->canJoinGroups = data.get<bool>("can_join_groups", false); - result->canReadAllGroupMessages = data.get<bool>("can_read_all_group_messages", false); - result->supportsInlineQueries = data.get<bool>("supports_inline_queries", false); - return result; -} - -std::string TgTypeParser::parseUser(const User::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "is_bot", object->isBot); - appendToJson(result, "first_name", object->firstName); - appendToJson(result, "last_name", object->lastName); - appendToJson(result, "username", object->username); - appendToJson(result, "language_code", object->languageCode); - appendToJson(result, "is_premium", object->isPremium); - appendToJson(result, "added_to_attachment_menu", object->addedToAttachmentMenu); - appendToJson(result, "can_join_groups", object->canJoinGroups); - appendToJson(result, "can_read_all_group_messages", object->canReadAllGroupMessages); - appendToJson(result, "supports_inline_queries", object->supportsInlineQueries); - removeLastComma(result); - result += '}'; - return result; -} - -MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const boost::property_tree::ptree& data) const{ - auto result(std::make_shared<MessageEntity>()); - std::string type = data.get<std::string>("type", ""); - if (type == "mention") { - result->type = MessageEntity::Type::Mention; - } else if (type == "hashtag") { - result->type = MessageEntity::Type::Hashtag; - } else if (type == "cashtag") { - result->type = MessageEntity::Type::Cashtag; - } else if (type == "bot_command") { - result->type = MessageEntity::Type::BotCommand; - } else if (type == "url") { - result->type = MessageEntity::Type::Url; - } else if (type == "email") { - result->type = MessageEntity::Type::Email; - } else if (type == "phone_number") { - result->type = MessageEntity::Type::PhoneNumber; - } else if (type == "bold") { - result->type = MessageEntity::Type::Bold; - } else if (type == "italic") { - result->type = MessageEntity::Type::Italic; - } else if (type == "underline") { - result->type = MessageEntity::Type::Underline; - } else if (type == "strikethrough") { - result->type = MessageEntity::Type::Strikethrough; - } else if (type == "spoiler") { - result->type = MessageEntity::Type::Spoiler; - } else if (type == "code") { - result->type = MessageEntity::Type::Code; - } else if (type == "pre") { - result->type = MessageEntity::Type::Pre; - } else if (type == "text_link") { - result->type = MessageEntity::Type::TextLink; - } else if (type == "text_mention") { - result->type = MessageEntity::Type::TextMention; - } else if (type == "custom_emoji") { - result->type = MessageEntity::Type::CustomEmoji; - } - result->offset = data.get<std::int32_t>("offset", 0); - result->length = data.get<std::int32_t>("length", 0); - result->url = data.get<std::string>("url", ""); - result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->language = data.get<std::string>("language", ""); - result->customEmojiId = data.get<std::string>("custom_emoji_id", ""); - return result; -} - -std::string TgTypeParser::parseMessageEntity(const MessageEntity::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - if (object->type == MessageEntity::Type::Mention) { - appendToJson(result, "type", "mention"); - } else if (object->type == MessageEntity::Type::Hashtag) { - appendToJson(result, "type", "hashtag"); - } else if (object->type == MessageEntity::Type::Cashtag) { - appendToJson(result, "type", "cashtag"); - } else if (object->type == MessageEntity::Type::BotCommand) { - appendToJson(result, "type", "bot_command"); - } else if (object->type == MessageEntity::Type::Url) { - appendToJson(result, "type", "url"); - } else if (object->type == MessageEntity::Type::Email) { - appendToJson(result, "type", "email"); - } else if (object->type == MessageEntity::Type::PhoneNumber) { - appendToJson(result, "type", "phone_number"); - } else if (object->type == MessageEntity::Type::Bold) { - appendToJson(result, "type", "bold"); - } else if (object->type == MessageEntity::Type::Italic) { - appendToJson(result, "type", "italic"); - } else if (object->type == MessageEntity::Type::Underline) { - appendToJson(result, "type", "underline"); - } else if (object->type == MessageEntity::Type::Strikethrough) { - appendToJson(result, "type", "strikethrough"); - } else if (object->type == MessageEntity::Type::Spoiler) { - appendToJson(result, "type", "spoiler"); - } else if (object->type == MessageEntity::Type::Code) { - appendToJson(result, "type", "code"); - } else if (object->type == MessageEntity::Type::Pre) { - appendToJson(result, "type", "pre"); - } else if (object->type == MessageEntity::Type::TextLink) { - appendToJson(result, "type", "text_link"); - } else if (object->type == MessageEntity::Type::TextMention) { - appendToJson(result, "type", "text_mention"); - } else if (object->type == MessageEntity::Type::CustomEmoji) { - appendToJson(result, "type", "custom_emoji"); - } - appendToJson(result, "offset", object->offset); - appendToJson(result, "length", object->length); - appendToJson(result, "url", object->url); - appendToJson(result, "user", parseUser(object->user)); - appendToJson(result, "language", object->language); - appendToJson(result, "custom_emoji_id", object->customEmojiId); - removeLastComma(result); - result += '}'; - return result; -} - Message::Ptr TgTypeParser::parseJsonAndGetMessage(const boost::property_tree::ptree& data) const { auto result(std::make_shared<Message>()); result->messageId = data.get<std::int32_t>("message_id", 0); @@ -374,6 +360,105 @@ std::string TgTypeParser::parseMessageId(const MessageId::Ptr& object) const { return result; } +MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<MessageEntity>()); + std::string type = data.get<std::string>("type", ""); + if (type == "mention") { + result->type = MessageEntity::Type::Mention; + } else if (type == "hashtag") { + result->type = MessageEntity::Type::Hashtag; + } else if (type == "cashtag") { + result->type = MessageEntity::Type::Cashtag; + } else if (type == "bot_command") { + result->type = MessageEntity::Type::BotCommand; + } else if (type == "url") { + result->type = MessageEntity::Type::Url; + } else if (type == "email") { + result->type = MessageEntity::Type::Email; + } else if (type == "phone_number") { + result->type = MessageEntity::Type::PhoneNumber; + } else if (type == "bold") { + result->type = MessageEntity::Type::Bold; + } else if (type == "italic") { + result->type = MessageEntity::Type::Italic; + } else if (type == "underline") { + result->type = MessageEntity::Type::Underline; + } else if (type == "strikethrough") { + result->type = MessageEntity::Type::Strikethrough; + } else if (type == "spoiler") { + result->type = MessageEntity::Type::Spoiler; + } else if (type == "code") { + result->type = MessageEntity::Type::Code; + } else if (type == "pre") { + result->type = MessageEntity::Type::Pre; + } else if (type == "text_link") { + result->type = MessageEntity::Type::TextLink; + } else if (type == "text_mention") { + result->type = MessageEntity::Type::TextMention; + } else if (type == "custom_emoji") { + result->type = MessageEntity::Type::CustomEmoji; + } + result->offset = data.get<std::int32_t>("offset", 0); + result->length = data.get<std::int32_t>("length", 0); + result->url = data.get<std::string>("url", ""); + result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->language = data.get<std::string>("language", ""); + result->customEmojiId = data.get<std::string>("custom_emoji_id", ""); + return result; +} + +std::string TgTypeParser::parseMessageEntity(const MessageEntity::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + if (object->type == MessageEntity::Type::Mention) { + appendToJson(result, "type", "mention"); + } else if (object->type == MessageEntity::Type::Hashtag) { + appendToJson(result, "type", "hashtag"); + } else if (object->type == MessageEntity::Type::Cashtag) { + appendToJson(result, "type", "cashtag"); + } else if (object->type == MessageEntity::Type::BotCommand) { + appendToJson(result, "type", "bot_command"); + } else if (object->type == MessageEntity::Type::Url) { + appendToJson(result, "type", "url"); + } else if (object->type == MessageEntity::Type::Email) { + appendToJson(result, "type", "email"); + } else if (object->type == MessageEntity::Type::PhoneNumber) { + appendToJson(result, "type", "phone_number"); + } else if (object->type == MessageEntity::Type::Bold) { + appendToJson(result, "type", "bold"); + } else if (object->type == MessageEntity::Type::Italic) { + appendToJson(result, "type", "italic"); + } else if (object->type == MessageEntity::Type::Underline) { + appendToJson(result, "type", "underline"); + } else if (object->type == MessageEntity::Type::Strikethrough) { + appendToJson(result, "type", "strikethrough"); + } else if (object->type == MessageEntity::Type::Spoiler) { + appendToJson(result, "type", "spoiler"); + } else if (object->type == MessageEntity::Type::Code) { + appendToJson(result, "type", "code"); + } else if (object->type == MessageEntity::Type::Pre) { + appendToJson(result, "type", "pre"); + } else if (object->type == MessageEntity::Type::TextLink) { + appendToJson(result, "type", "text_link"); + } else if (object->type == MessageEntity::Type::TextMention) { + appendToJson(result, "type", "text_mention"); + } else if (object->type == MessageEntity::Type::CustomEmoji) { + appendToJson(result, "type", "custom_emoji"); + } + appendToJson(result, "offset", object->offset); + appendToJson(result, "length", object->length); + appendToJson(result, "url", object->url); + appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "language", object->language); + appendToJson(result, "custom_emoji_id", object->customEmojiId); + removeLastComma(result); + result += '}'; + return result; +} + PhotoSize::Ptr TgTypeParser::parseJsonAndGetPhotoSize(const boost::property_tree::ptree& data) const { auto result(std::make_shared<PhotoSize>()); result->fileId = data.get<std::string>("file_id", ""); @@ -400,21 +485,21 @@ std::string TgTypeParser::parsePhotoSize(const PhotoSize::Ptr& object) const { return result; } -Audio::Ptr TgTypeParser::parseJsonAndGetAudio(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Audio>()); +Animation::Ptr TgTypeParser::parseJsonAndGetAnimation(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Animation>()); result->fileId = data.get<std::string>("file_id", ""); result->fileUniqueId = data.get<std::string>("file_unique_id", ""); + result->width = data.get<std::int32_t>("width", 0); + result->height = data.get<std::int32_t>("height", 0); result->duration = data.get<std::int32_t>("duration", 0); - result->performer = data.get<std::string>("performer", ""); - result->title = data.get<std::string>("title", ""); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); result->fileName = data.get<std::string>("file_name", ""); result->mimeType = data.get<std::string>("mime_type", ""); result->fileSize = data.get<std::int64_t>("file_size", 0); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); return result; } -std::string TgTypeParser::parseAudio(const Audio::Ptr& object) const { +std::string TgTypeParser::parseAnimation(const Animation::Ptr& object) const { if (!object) { return ""; } @@ -422,30 +507,33 @@ std::string TgTypeParser::parseAudio(const Audio::Ptr& object) const { result += '{'; appendToJson(result, "file_id", object->fileId); appendToJson(result, "file_unique_id", object->fileUniqueId); + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); appendToJson(result, "duration", object->duration); - appendToJson(result, "performer", object->performer); - appendToJson(result, "title", object->title); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); appendToJson(result, "file_name", object->fileName); appendToJson(result, "mime_type", object->mimeType); appendToJson(result, "file_size", object->fileSize); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); removeLastComma(result); result += '}'; return result; } -Document::Ptr TgTypeParser::parseJsonAndGetDocument(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Document>()); +Audio::Ptr TgTypeParser::parseJsonAndGetAudio(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Audio>()); result->fileId = data.get<std::string>("file_id", ""); result->fileUniqueId = data.get<std::string>("file_unique_id", ""); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->duration = data.get<std::int32_t>("duration", 0); + result->performer = data.get<std::string>("performer", ""); + result->title = data.get<std::string>("title", ""); result->fileName = data.get<std::string>("file_name", ""); result->mimeType = data.get<std::string>("mime_type", ""); result->fileSize = data.get<std::int64_t>("file_size", 0); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); return result; } -std::string TgTypeParser::parseDocument(const Document::Ptr& object) const { +std::string TgTypeParser::parseAudio(const Audio::Ptr& object) const { if (!object) { return ""; } @@ -453,42 +541,30 @@ std::string TgTypeParser::parseDocument(const Document::Ptr& object) const { result += '{'; appendToJson(result, "file_id", object->fileId); appendToJson(result, "file_unique_id", object->fileUniqueId); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "duration", object->duration); + appendToJson(result, "performer", object->performer); + appendToJson(result, "title", object->title); appendToJson(result, "file_name", object->fileName); appendToJson(result, "mime_type", object->mimeType); appendToJson(result, "file_size", object->fileSize); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); removeLastComma(result); result += '}'; return result; } -Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Sticker>()); +Document::Ptr TgTypeParser::parseJsonAndGetDocument(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Document>()); result->fileId = data.get<std::string>("file_id", ""); result->fileUniqueId = data.get<std::string>("file_unique_id", ""); - std::string type = data.get<std::string>("type", ""); - if (type == "regular") { - result->type = Sticker::Type::Regular; - } else if (type == "mask") { - result->type = Sticker::Type::Mask; - } else if (type == "custom_emoji") { - result->type = Sticker::Type::CustomEmoji; - } - result->width = data.get<std::int32_t>("width", 0); - result->height = data.get<std::int32_t>("height", 0); - result->isAnimated = data.get<bool>("is_animated", false); - result->isVideo = data.get<bool>("is_video", false); result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->emoji = data.get<std::string>("emoji", ""); - result->setName = data.get<std::string>("set_name", ""); - result->premiumAnimation = tryParseJson<File>(&TgTypeParser::parseJsonAndGetFile, data, "premium_animation"); - result->maskPosition = tryParseJson<MaskPosition>(&TgTypeParser::parseJsonAndGetMaskPosition, data, "mask_position"); - result->customEmojiId = data.get<std::string>("custom_emoji_id", ""); - result->fileSize = data.get<std::int32_t>("file_size", 0); + result->fileName = data.get<std::string>("file_name", ""); + result->mimeType = data.get<std::string>("mime_type", ""); + result->fileSize = data.get<std::int64_t>("file_size", 0); return result; } -std::string TgTypeParser::parseSticker(const Sticker::Ptr& object) const { +std::string TgTypeParser::parseDocument(const Document::Ptr& object) const { if (!object) { return ""; } @@ -496,256 +572,15 @@ std::string TgTypeParser::parseSticker(const Sticker::Ptr& object) const { result += '{'; appendToJson(result, "file_id", object->fileId); appendToJson(result, "file_unique_id", object->fileUniqueId); - if (object->type == Sticker::Type::Regular) { - appendToJson(result, "type", "regular"); - } else if (object->type == Sticker::Type::Mask) { - appendToJson(result, "type", "mask"); - } else if (object->type == Sticker::Type::CustomEmoji) { - appendToJson(result, "type", "custom_emoji"); - } - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - appendToJson(result, "is_animated", object->isAnimated); - appendToJson(result, "is_video", object->isVideo); appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - appendToJson(result, "emoji", object->emoji); - appendToJson(result, "set_name", object->setName); - appendToJson(result, "premium_animation", parseFile(object->premiumAnimation)); - appendToJson(result, "mask_position", parseMaskPosition(object->maskPosition)); - appendToJson(result, "custom_emoji_id", object->customEmojiId); + appendToJson(result, "file_name", object->fileName); + appendToJson(result, "mime_type", object->mimeType); appendToJson(result, "file_size", object->fileSize); removeLastComma(result); result += '}'; return result; } -StickerSet::Ptr TgTypeParser::parseJsonAndGetStickerSet(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<StickerSet>()); - result->name = data.get<std::string>("name", ""); - result->title = data.get<std::string>("title", ""); - std::string type = data.get<std::string>("type", ""); - if (type == "regular") { - result->type = StickerSet::Type::Regular; - } else if (type == "mask") { - result->type = StickerSet::Type::Mask; - } else if (type == "custom_emoji") { - result->type = StickerSet::Type::CustomEmoji; - } - result->isAnimated = data.get<bool>("is_animated", false); - result->isVideo = data.get<bool>("is_video", false); - result->stickers = parseJsonAndGetArray<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "stickers"); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - return result; -} - -std::string TgTypeParser::parseStickerSet(const StickerSet::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "name", object->name); - appendToJson(result, "title", object->title); - if (object->type == StickerSet::Type::Regular) { - appendToJson(result, "type", "regular"); - } else if (object->type == StickerSet::Type::Mask) { - appendToJson(result, "type", "mask"); - } else if (object->type == StickerSet::Type::CustomEmoji) { - appendToJson(result, "type", "custom_emoji"); - } - appendToJson(result, "is_animated", object->isAnimated); - appendToJson(result, "is_video", object->isVideo); - appendToJson(result, "stickers", parseArray(&TgTypeParser::parseSticker, object->stickers)); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - removeLastComma(result); - result += '}'; - return result; -} - -MaskPosition::Ptr TgTypeParser::parseJsonAndGetMaskPosition(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<MaskPosition>()); - result->point = data.get("point", ""); - result->xShift = data.get<float>("x_shift", 0); - result->yShift = data.get<float>("y_shift", 0); - result->scale = data.get<float>("scale", 0); - return result; -} - -std::string TgTypeParser::parseMaskPosition(const MaskPosition::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "point", object->point); - appendToJson(result, "x_shift", object->xShift); - appendToJson(result, "y_shift", object->yShift); - appendToJson(result, "scale", object->scale); - removeLastComma(result); - result += '}'; - return result; -} - -Poll::Ptr TgTypeParser::parseJsonAndGetPoll(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Poll>()); - result->id = data.get<std::string>("id", ""); - result->question = data.get<std::string>("question", ""); - result->options = parseJsonAndGetArray<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); - result->totalVoterCount = data.get<std::int32_t>("total_voter_count", 0); - result->isClosed = data.get<bool>("is_closed", false); - result->isAnonymous = data.get<bool>("is_anonymous", true); - result->type = data.get<std::string>("type", ""); - result->allowsMultipleAnswers = data.get<bool>("allows_multiple_answers", false); - result->correctOptionId = data.get<std::int32_t>("correct_option_id", 0); - result->explanation = data.get<std::string>("explanation", ""); - result->explanationEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "explanation_entities"); - result->openPeriod = data.get<std::int32_t>("open_period", 0); - result->closeDate = data.get<std::int64_t>("close_date", 0); - return result; -} - -std::string TgTypeParser::parsePoll(const Poll::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "question", object->question); - appendToJson(result, "options", parseArray(&TgTypeParser::parsePollOption, object->options)); - appendToJson(result, "total_voter_count", object->totalVoterCount); - appendToJson(result, "is_closed", object->isClosed); - appendToJson(result, "is_anonymous", object->isAnonymous); - appendToJson(result, "type", object->type); - appendToJson(result, "allows_multiple_answers", object->allowsMultipleAnswers); - appendToJson(result, "correct_option_id", object->correctOptionId); - appendToJson(result, "explanation", object->correctOptionId); - appendToJson(result, "explanation_entities", parseArray(&TgTypeParser::parseMessageEntity, object->explanationEntities)); - appendToJson(result, "open_period", object->openPeriod); - appendToJson(result, "close_date", object->closeDate); - removeLastComma(result); - result += '}'; - return result; -} - -Dice::Ptr TgTypeParser::parseJsonAndGetDice(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Dice>()); - result->emoji = data.get<std::string>("emoji", ""); - result->value = data.get<std::int32_t>("value", 0); - return result; -} - -std::string TgTypeParser::parseDice(const Dice::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "emoji", object->emoji); - appendToJson(result, "value", object->value); - removeLastComma(result); - result += '}'; - return result; -} - -PollAnswer::Ptr TgTypeParser::parseJsonAndGetPollAnswer(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<PollAnswer>()); - result->pollId = data.get<std::string>("poll_id", ""); - result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->optionIds = parseJsonAndGetArray<std::int32_t>([] (const boost::property_tree::ptree& innerData)->std::int32_t { - return innerData.get<std::int32_t>(0); - }, data, "option_ids"); - return result; -} - -std::string TgTypeParser::parsePollAnswer(const PollAnswer::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "poll_id", object->pollId); - appendToJson(result, "user", parseUser(object->user)); - appendToJson(result, "option_ids", parseArray<std::int32_t>([] (std::int32_t i)->std::int32_t { - return i; - }, object->optionIds)); - removeLastComma(result); - result += '}'; - return result; -} - -PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<PollOption>()); - result->text = data.get("text", ""); - result->voterCount = data.get("voter_count", 0); - return result; -} - -std::string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "text", object->text); - appendToJson(result, "voter_count", object->voterCount); - removeLastComma(result); - result += '}'; - return result; -} - -ChatPermissions::Ptr TgTypeParser::parseJsonAndGetChatPermissions(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ChatPermissions>()); - result->canSendMessages = data.get<bool>("can_send_messages"); - result->canSendMediaMessages = data.get<bool>("can_send_media_messages"); - result->canSendPolls = data.get<bool>("can_send_polls"); - result->canSendOtherMessages = data.get<bool>("can_send_other_messages"); - result->canAddWebPagePreviews = data.get<bool>("can_add_web_page_previews"); - result->canChangeInfo = data.get<bool>("can_change_info"); - result->canInviteUsers = data.get<bool>("can_invite_users"); - result->canPinMessages = data.get<bool>("can_pin_messages"); - return result; -} - -std::string TgTypeParser::parseChatPermissions(const ChatPermissions::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "can_send_messages", object->canSendMessages); - appendToJson(result, "can_send_media_messages", object->canSendMediaMessages); - appendToJson(result, "can_send_polls", object->canSendPolls); - appendToJson(result, "can_send_other_messages", object->canSendOtherMessages); - appendToJson(result, "can_add_web_page_previews", object->canAddWebPagePreviews); - appendToJson(result, "can_change_info", object->canChangeInfo); - appendToJson(result, "can_invite_users", object->canInviteUsers); - appendToJson(result, "can_pin_messages", object->canPinMessages); - removeLastComma(result); - result += '}'; - return result; -} - -ChatLocation::Ptr TgTypeParser::parseJsonAndGetChatLocation(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ChatLocation>()); - result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); - result->address = data.get<std::string>("address", ""); - return result; -} - -std::string TgTypeParser::parseChatLocation(const ChatLocation::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "location", parseLocation(object->location)); - appendToJson(result, "address", object->address); - removeLastComma(result); - result += '}'; - return result; -} - Video::Ptr TgTypeParser::parseJsonAndGetVideo(const boost::property_tree::ptree& data) const { auto result(std::make_shared<Video>()); result->fileId = data.get<std::string>("file_id", ""); @@ -780,17 +615,18 @@ std::string TgTypeParser::parseVideo(const Video::Ptr& object) const { return result; } -Voice::Ptr TgTypeParser::parseJsonAndGetVoice(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Voice>()); +VideoNote::Ptr TgTypeParser::parseJsonAndGetVideoNote(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<VideoNote>()); result->fileId = data.get<std::string>("file_id", ""); result->fileUniqueId = data.get<std::string>("file_unique_id", ""); + result->length = data.get<std::int32_t>("length", 0); result->duration = data.get<std::int32_t>("duration", 0); - result->mimeType = data.get<std::string>("mime_type", ""); - result->fileSize = data.get<std::int64_t>("file_size", 0); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->fileSize = data.get("file_size", 0); return result; } -std::string TgTypeParser::parseVoice(const Voice::Ptr& object) const { +std::string TgTypeParser::parseVideoNote(const VideoNote::Ptr& object) const { if (!object) { return ""; } @@ -798,26 +634,26 @@ std::string TgTypeParser::parseVoice(const Voice::Ptr& object) const { result += '{'; appendToJson(result, "file_id", object->fileId); appendToJson(result, "file_unique_id", object->fileUniqueId); + appendToJson(result, "length", object->length); appendToJson(result, "duration", object->duration); - appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); appendToJson(result, "file_size", object->fileSize); removeLastComma(result); result += '}'; return result; } -VideoNote::Ptr TgTypeParser::parseJsonAndGetVideoNote(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<VideoNote>()); +Voice::Ptr TgTypeParser::parseJsonAndGetVoice(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Voice>()); result->fileId = data.get<std::string>("file_id", ""); result->fileUniqueId = data.get<std::string>("file_unique_id", ""); - result->length = data.get<std::int32_t>("length", 0); result->duration = data.get<std::int32_t>("duration", 0); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->fileSize = data.get("file_size", 0); + result->mimeType = data.get<std::string>("mime_type", ""); + result->fileSize = data.get<std::int64_t>("file_size", 0); return result; } -std::string TgTypeParser::parseVideoNote(const VideoNote::Ptr& object) const { +std::string TgTypeParser::parseVoice(const Voice::Ptr& object) const { if (!object) { return ""; } @@ -825,135 +661,144 @@ std::string TgTypeParser::parseVideoNote(const VideoNote::Ptr& object) const { result += '{'; appendToJson(result, "file_id", object->fileId); appendToJson(result, "file_unique_id", object->fileUniqueId); - appendToJson(result, "length", object->length); appendToJson(result, "duration", object->duration); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "mime_type", object->mimeType); appendToJson(result, "file_size", object->fileSize); removeLastComma(result); result += '}'; return result; } -Game::Ptr TgTypeParser::parseJsonAndGetGame(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Game>()); - result->title = data.get("title", ""); - result->description = data.get("description", ""); - result->photo = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photo"); - result->text = data.get("text", ""); - result->textEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "text_entities"); - result->animation = tryParseJson<Animation>(&TgTypeParser::parseJsonAndGetAnimation, data, "animation"); +Contact::Ptr TgTypeParser::parseJsonAndGetContact(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Contact>()); + result->phoneNumber = data.get<std::string>("phone_number"); + result->firstName = data.get<std::string>("first_name"); + result->lastName = data.get("last_name", ""); + result->userId = data.get("user_id", 0); + result->vcard = data.get("vcard", ""); return result; } -std::string TgTypeParser::parseGame(const Game::Ptr& object) const { +std::string TgTypeParser::parseContact(const Contact::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "title", object->title); - appendToJson(result, "description", object->description); - appendToJson(result, "photo", parseArray(&TgTypeParser::parsePhotoSize, object->photo)); - appendToJson(result, "text", object->text); - appendToJson(result, "text_entities", parseArray(&TgTypeParser::parseMessageEntity, object->textEntities)); - appendToJson(result, "animation", parseAnimation(object->animation)); + appendToJson(result, "phone_number", object->phoneNumber); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + appendToJson(result, "user_id", object->userId); + appendToJson(result, "vcard", object->vcard); removeLastComma(result); result += '}'; return result; } -CallbackGame::Ptr TgTypeParser::parseJsonAndGetCallbackGame(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<CallbackGame>()); +Dice::Ptr TgTypeParser::parseJsonAndGetDice(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Dice>()); + result->emoji = data.get<std::string>("emoji", ""); + result->value = data.get<std::int32_t>("value", 0); return result; } -std::string TgTypeParser::parseCallbackGame(const CallbackGame::Ptr& object) const { +std::string TgTypeParser::parseDice(const Dice::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; + appendToJson(result, "emoji", object->emoji); + appendToJson(result, "value", object->value); + removeLastComma(result); result += '}'; return result; } -GameHighScore::Ptr TgTypeParser::parseJsonAndGetGameHighScore(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<GameHighScore>()); - result->position = data.get("position", ""); - result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->score = data.get<std::int32_t>("score", 0); +PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<PollOption>()); + result->text = data.get("text", ""); + result->voterCount = data.get("voter_count", 0); return result; } -std::string TgTypeParser::parseGameHighScore(const GameHighScore::Ptr& object) const { +std::string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "position", object->position); - appendToJson(result, "user", parseUser(object->user)); - appendToJson(result, "score", object->score); + appendToJson(result, "text", object->text); + appendToJson(result, "voter_count", object->voterCount); removeLastComma(result); result += '}'; return result; } -Animation::Ptr TgTypeParser::parseJsonAndGetAnimation(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Animation>()); - result->fileId = data.get<std::string>("file_id", ""); - result->fileUniqueId = data.get<std::string>("file_unique_id", ""); - result->width = data.get<std::int32_t>("width", 0); - result->height = data.get<std::int32_t>("height", 0); - result->duration = data.get<std::int32_t>("duration", 0); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->fileName = data.get<std::string>("file_name", ""); - result->mimeType = data.get<std::string>("mime_type", ""); - result->fileSize = data.get<std::int64_t>("file_size", 0); +PollAnswer::Ptr TgTypeParser::parseJsonAndGetPollAnswer(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<PollAnswer>()); + result->pollId = data.get<std::string>("poll_id", ""); + result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->optionIds = parseJsonAndGetArray<std::int32_t>( + [] (const boost::property_tree::ptree& innerData)->std::int32_t { + return innerData.get<std::int32_t>(""); + }, data, "option_ids"); return result; } -std::string TgTypeParser::parseAnimation(const Animation::Ptr& object) const { +std::string TgTypeParser::parsePollAnswer(const PollAnswer::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "file_unique_id", object->fileUniqueId); - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - appendToJson(result, "duration", object->duration); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - appendToJson(result, "file_name", object->fileName); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "file_size", object->fileSize); + appendToJson(result, "poll_id", object->pollId); + appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "option_ids", parseArray<std::int32_t>([] (std::int32_t i)->std::int32_t { + return i; + }, object->optionIds)); removeLastComma(result); result += '}'; return result; } -Contact::Ptr TgTypeParser::parseJsonAndGetContact(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Contact>()); - result->phoneNumber = data.get<std::string>("phone_number"); - result->firstName = data.get<std::string>("first_name"); - result->lastName = data.get("last_name", ""); - result->userId = data.get("user_id", 0); - result->vcard = data.get("vcard", ""); +Poll::Ptr TgTypeParser::parseJsonAndGetPoll(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Poll>()); + result->id = data.get<std::string>("id", ""); + result->question = data.get<std::string>("question", ""); + result->options = parseJsonAndGetArray<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); + result->totalVoterCount = data.get<std::int32_t>("total_voter_count", 0); + result->isClosed = data.get<bool>("is_closed", false); + result->isAnonymous = data.get<bool>("is_anonymous", true); + result->type = data.get<std::string>("type", ""); + result->allowsMultipleAnswers = data.get<bool>("allows_multiple_answers", false); + result->correctOptionId = data.get<std::int32_t>("correct_option_id", 0); + result->explanation = data.get<std::string>("explanation", ""); + result->explanationEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "explanation_entities"); + result->openPeriod = data.get<std::int32_t>("open_period", 0); + result->closeDate = data.get<std::int64_t>("close_date", 0); return result; } -std::string TgTypeParser::parseContact(const Contact::Ptr& object) const { +std::string TgTypeParser::parsePoll(const Poll::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "phone_number", object->phoneNumber); - appendToJson(result, "first_name", object->firstName); - appendToJson(result, "last_name", object->lastName); - appendToJson(result, "user_id", object->userId); - appendToJson(result, "vcard", object->vcard); + appendToJson(result, "id", object->id); + appendToJson(result, "question", object->question); + appendToJson(result, "options", parseArray(&TgTypeParser::parsePollOption, object->options)); + appendToJson(result, "total_voter_count", object->totalVoterCount); + appendToJson(result, "is_closed", object->isClosed); + appendToJson(result, "is_anonymous", object->isAnonymous); + appendToJson(result, "type", object->type); + appendToJson(result, "allows_multiple_answers", object->allowsMultipleAnswers); + appendToJson(result, "correct_option_id", object->correctOptionId); + appendToJson(result, "explanation", object->correctOptionId); + appendToJson(result, "explanation_entities", parseArray(&TgTypeParser::parseMessageEntity, object->explanationEntities)); + appendToJson(result, "open_period", object->openPeriod); + appendToJson(result, "close_date", object->closeDate); removeLastComma(result); result += '}'; return result; @@ -1146,52 +991,6 @@ std::string TgTypeParser::parseVideoChatParticipantsInvited(const VideoChatParti return result; } -Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Update>()); - result->updateId = data.get<std::int32_t>("update_id", 0); - result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message"); - result->editedMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "edited_message"); - result->channelPost = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "channel_post"); - result->editedChannelPost = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "edited_channel_post"); - result->inlineQuery = tryParseJson<InlineQuery>(&TgTypeParser::parseJsonAndGetInlineQuery, data, "inline_query"); - result->chosenInlineResult = tryParseJson<ChosenInlineResult>(&TgTypeParser::parseJsonAndGetChosenInlineResult, data, "chosen_inline_result"); - result->callbackQuery = tryParseJson<CallbackQuery>(&TgTypeParser::parseJsonAndGetCallbackQuery, data, "callback_query"); - result->shippingQuery = tryParseJson<ShippingQuery>(&TgTypeParser::parseJsonAndGetShippingQuery, data, "shipping_query"); - result->preCheckoutQuery = tryParseJson<PreCheckoutQuery>(&TgTypeParser::parseJsonAndGetPreCheckoutQuery, data, "pre_checkout_query"); - result->poll = tryParseJson<Poll>(&TgTypeParser::parseJsonAndGetPoll, data, "poll"); - result->pollAnswer = tryParseJson<PollAnswer>(&TgTypeParser::parseJsonAndGetPollAnswer, data, "poll_answer"); - result->myChatMember = tryParseJson<ChatMemberUpdated>(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "my_chat_member"); - result->chatMember = tryParseJson<ChatMemberUpdated>(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "chat_member"); - result->chatJoinRequest = tryParseJson<ChatJoinRequest>(&TgTypeParser::parseJsonAndGetChatJoinRequest, data, "chat_join_request"); - return result; -} - -std::string TgTypeParser::parseUpdate(const Update::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "update_id", object->updateId); - appendToJson(result, "message", parseMessage(object->message)); - appendToJson(result, "edited_message", parseMessage(object->editedMessage)); - appendToJson(result, "channel_post", parseMessage(object->channelPost)); - appendToJson(result, "edited_channel_post", parseMessage(object->editedChannelPost)); - appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery)); - appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)); - appendToJson(result, "callback_query", parseCallbackQuery(object->callbackQuery)); - appendToJson(result, "shipping_query", parseShippingQuery(object->shippingQuery)); - appendToJson(result, "pre_checkout_query", parsePreCheckoutQuery(object->preCheckoutQuery)); - appendToJson(result, "poll", parsePoll(object->poll)); - appendToJson(result, "poll_answer", parsePollAnswer(object->pollAnswer)); - appendToJson(result, "my_chat_member", parseChatMemberUpdated(object->myChatMember)); - appendToJson(result, "chat_member", parseChatMemberUpdated(object->chatMember)); - appendToJson(result, "chat_join_request", parseChatJoinRequest(object->chatJoinRequest)); - removeLastComma(result); - result += '}'; - return result; -} - UserProfilePhotos::Ptr TgTypeParser::parseJsonAndGetUserProfilePhotos(const boost::property_tree::ptree& data) const { auto result(std::make_shared<UserProfilePhotos>()); result->totalCount = data.get<std::int32_t>("total_count"); @@ -1212,177 +1011,6 @@ std::string TgTypeParser::parseUserProfilePhotos(const UserProfilePhotos::Ptr& o return result; } -InputMedia::Ptr TgTypeParser::parseJsonAndGetInputMedia(const boost::property_tree::ptree& data) const { - std::string type = data.get<std::string>("type", ""); - InputMedia::Ptr result; - - if (type == InputMediaPhoto::TYPE) { - result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaPhoto(data)); - } else if (type == InputMediaVideo::TYPE) { - result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaVideo(data)); - } else if (type == InputMediaAnimation::TYPE) { - result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaAnimation(data)); - } else if (type == InputMediaAudio::TYPE) { - result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaAudio(data)); - } else if (type == InputMediaDocument::TYPE) { - result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaDocument(data)); - } else { - result = std::make_shared<InputMedia>(); - } - - result->type = data.get<std::string>("type", ""); - result->media = data.get<std::string>("media", ""); - result->caption = data.get<std::string>("caption", ""); - result->parseMode = data.get<std::string>("parse_mode", ""); - result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - - return result; -} - -std::string TgTypeParser::parseInputMedia(const InputMedia::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "type", object->type); - appendToJson(result, "media", object->media); - appendToJson(result, "caption", object->caption); - appendToJson(result, "parse_mode", object->parseMode); - appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); - - if (object->type == InputMediaPhoto::TYPE) { - result += parseInputMediaPhoto(std::static_pointer_cast<InputMediaPhoto>(object)); - } else if (object->type == InputMediaVideo::TYPE) { - result += parseInputMediaVideo(std::static_pointer_cast<InputMediaVideo>(object)); - } else if (object->type == InputMediaAnimation::TYPE) { - result += parseInputMediaAnimation(std::static_pointer_cast<InputMediaAnimation>(object)); - } else if (object->type == InputMediaAudio::TYPE) { - result += parseInputMediaAudio(std::static_pointer_cast<InputMediaAudio>(object)); - } else if (object->type == InputMediaDocument::TYPE) { - result += parseInputMediaDocument(std::static_pointer_cast<InputMediaDocument>(object)); - } - - removeLastComma(result); - result += '}'; - return result; -} - -InputMediaPhoto::Ptr TgTypeParser::parseJsonAndGetInputMediaPhoto(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMedia(). - auto result(std::make_shared<InputMediaPhoto>()); - return result; -} - -std::string TgTypeParser::parseInputMediaPhoto(const InputMediaPhoto::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseInputMedia(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseInputMedia(). - return result; -} - -InputMediaVideo::Ptr TgTypeParser::parseJsonAndGetInputMediaVideo(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMedia(). - auto result(std::make_shared<InputMediaVideo>()); - result->thumb = data.get<std::string>("thumb", ""); - result->width = data.get<std::int32_t>("width", 0); - result->height = data.get<std::int32_t>("height", 0); - result->duration = data.get<std::int32_t>("duration", 0); - result->supportsStreaming = data.get<bool>("supports_streaming", false); - return result; -} - -std::string TgTypeParser::parseInputMediaVideo(const InputMediaVideo::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseInputMedia(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "thumb", object->thumb); - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - appendToJson(result, "duration", object->duration); - appendToJson(result, "supports_streaming", object->supportsStreaming); - // The last comma will be erased by parseInputMedia(). - return result; -} - -InputMediaAnimation::Ptr TgTypeParser::parseJsonAndGetInputMediaAnimation(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMedia(). - auto result(std::make_shared<InputMediaAnimation>()); - result->thumb = data.get<std::string>("thumb", ""); - result->width = data.get<std::int32_t>("width", 0); - result->height = data.get<std::int32_t>("height", 0); - result->duration = data.get<std::int32_t>("duration", 0); - return result; -} - -std::string TgTypeParser::parseInputMediaAnimation(const InputMediaAnimation::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseInputMedia(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "thumb", object->thumb); - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - appendToJson(result, "duration", object->duration); - // The last comma will be erased by parseInputMedia(). - return result; -} - -InputMediaAudio::Ptr TgTypeParser::parseJsonAndGetInputMediaAudio(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMedia(). - auto result(std::make_shared<InputMediaAudio>()); - result->thumb = data.get<std::string>("thumb", ""); - result->duration = data.get<std::int32_t>("duration", 0); - result->performer = data.get<std::string>("performer", ""); - result->title = data.get<std::string>("title", ""); - return result; -} - -std::string TgTypeParser::parseInputMediaAudio(const InputMediaAudio::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseInputMedia(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "thumb", object->thumb); - appendToJson(result, "duration", object->duration); - appendToJson(result, "performer", object->performer); - appendToJson(result, "title", object->title); - // The last comma will be erased by parseInputMedia(). - return result; -} - -InputMediaDocument::Ptr TgTypeParser::parseJsonAndGetInputMediaDocument(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMedia(). - auto result(std::make_shared<InputMediaDocument>()); - result->thumb = data.get<std::string>("thumb", ""); - result->disableContentTypeDetection = data.get<bool>("disable_content_type_detection", false); - return result; -} - -std::string TgTypeParser::parseInputMediaDocument(const InputMediaDocument::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseInputMedia(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "thumb", object->thumb); - appendToJson(result, "disable_content_type_detection", object->disableContentTypeDetection); - // The last comma will be erased by parseInputMedia(). - return result; -} - File::Ptr TgTypeParser::parseJsonAndGetFile(const boost::property_tree::ptree& data) const { auto result(std::make_shared<File>()); result->fileId = data.get<std::string>("file_id", ""); @@ -1427,7 +1055,7 @@ std::string TgTypeParser::parseWebAppInfo(const WebAppInfo::Ptr& object) const { ReplyKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardMarkup(const boost::property_tree::ptree& data) const { auto result(std::make_shared<ReplyKeyboardMarkup>()); - for (const auto& item : data.find("keyboard")->second){ + for (const auto& item : data.find("keyboard")->second) { result->keyboard.push_back(parseJsonAndGetArray<KeyboardButton>(&TgTypeParser::parseJsonAndGetKeyboardButton, item.second)); } result->resizeKeyboard = data.get<bool>("resize_keyboard", false); @@ -1528,6 +1156,121 @@ std::string TgTypeParser::parseReplyKeyboardRemove(const ReplyKeyboardRemove::Pt return result; } +InlineKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardMarkup(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<InlineKeyboardMarkup>()); + for (const auto& item : data.find("inline_keyboard")->second) { + result->inlineKeyboard.push_back(parseJsonAndGetArray<InlineKeyboardButton>(&TgTypeParser::parseJsonAndGetInlineKeyboardButton, item.second)); + } + return result; +} + +std::string TgTypeParser::parseInlineKeyboardMarkup(const InlineKeyboardMarkup::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + result += R"("inline_keyboard":[)"; + for (const auto& item : object->inlineKeyboard) { + result += '['; + for (const auto& innerItem : item) { + result += parseInlineKeyboardButton(innerItem); + result += ','; + } + removeLastComma(result); + result += "],"; + } + if (!object->inlineKeyboard.empty()) + removeLastComma(result); + result += "]}"; + return result; +} + +InlineKeyboardButton::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardButton(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<InlineKeyboardButton>()); + result->text = data.get<std::string>("text", ""); + result->url = data.get<std::string>("url", ""); + result->callbackData = data.get<std::string>("callback_data", ""); + result->webApp = tryParseJson<WebAppInfo>(&TgTypeParser::parseJsonAndGetWebAppInfo, data, "web_app"); + result->loginUrl = tryParseJson<LoginUrl>(&TgTypeParser::parseJsonAndGetLoginUrl, data, "login_url"); + result->switchInlineQuery = data.get<std::string>("switch_inline_query", ""); + result->switchInlineQueryCurrentChat = data.get<std::string>("switch_inline_query_current_chat", ""); + result->callbackGame = tryParseJson<CallbackGame>(&TgTypeParser::parseJsonAndGetCallbackGame, data, "callback_game"); + result->pay = data.get<bool>("pay", false); + return result; +} +std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "pay", object->pay); + appendToJson(result, "text", object->text); + appendToJson(result, "url", object->url); + appendToJson(result, "callback_data", object->callbackData); + appendToJson(result, "switch_inline_query", object->switchInlineQuery); + appendToJson(result, "switch_inline_query_current_chat", object->switchInlineQueryCurrentChat); + removeLastComma(result); + result += '}'; + return result; +} + +LoginUrl::Ptr TgTypeParser::parseJsonAndGetLoginUrl(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<LoginUrl>()); + result->url = data.get<std::string>("url", ""); + result->forwardText = data.get<std::string>("forward_text", ""); + result->botUsername = data.get<std::string>("bot_username", ""); + result->requestWriteAccess = data.get<bool>("request_write_access", false); + return result; +} + +std::string TgTypeParser::parseLoginUrl(const LoginUrl::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "url", object->url); + appendToJson(result, "forward_text", object->forwardText); + appendToJson(result, "bot_username", object->botUsername); + appendToJson(result, "request_write_access", object->requestWriteAccess); + removeLastComma(result); + result += '}'; + return result; +} + +CallbackQuery::Ptr TgTypeParser::parseJsonAndGetCallbackQuery(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<CallbackQuery>()); + result->id = data.get<std::string>("id"); + result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message"); + result->inlineMessageId = data.get<std::string>("inline_message_id", ""); + result->chatInstance = data.get<std::string>("chat_instance"); + result->gameShortName = data.get<std::string>("game_short_name", ""); + result->data = data.get<std::string>("data", ""); + return result; +} + +std::string TgTypeParser::parseCallbackQuery(const CallbackQuery::Ptr& object) const { + if (!object) { + return ""; + } + + std::string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "from", parseUser(object->from)); + appendToJson(result, "message", parseMessage(object->message)); + appendToJson(result, "inline_message_id", object->inlineMessageId); + appendToJson(result, "chat_instance", object->chatInstance); + appendToJson(result, "game_short_name", object->gameShortName); + appendToJson(result, "data", object->data); + removeLastComma(result); + result += '}'; + return result; +} + ForceReply::Ptr TgTypeParser::parseJsonAndGetForceReply(const boost::property_tree::ptree& data) const { auto result(std::make_shared<ForceReply>()); result->inputFieldPlaceholder = data.get<std::string>("input_field_placeholder", ""); @@ -1549,6 +1292,102 @@ std::string TgTypeParser::parseForceReply(const ForceReply::Ptr& object) const { return result; } +ChatPhoto::Ptr TgTypeParser::parseJsonAndGetChatPhoto(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ChatPhoto>()); + result->smallFileId = data.get<std::string>("small_file_id", ""); + result->smallFileUniqueId = data.get<std::string>("small_file_unique_id", ""); + result->bigFileId = data.get<std::string>("big_file_id", ""); + result->bigFileUniqueId = data.get<std::string>("big_file_unique_id", ""); + return result; +} + +std::string TgTypeParser::parseChatPhoto(const ChatPhoto::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "small_file_id", object->smallFileId); + appendToJson(result, "small_file_unique_id", object->smallFileUniqueId); + appendToJson(result, "big_file_id", object->bigFileId); + appendToJson(result, "big_file_unique_id", object->bigFileUniqueId); + removeLastComma(result); + result += '}'; + return result; +} + +ChatInviteLink::Ptr TgTypeParser::parseJsonAndGetChatInviteLink(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ChatInviteLink>()); + result->inviteLink = data.get<std::string>("invite_link", ""); + result->creator = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "creator"); + result->createsJoinRequest = data.get<bool>("creates_join_request", false); + result->isPrimary = data.get<bool>("is_primary", false); + result->isRevoked = data.get<bool>("is_revoked", false); + result->name = data.get<std::string>("name", ""); + result->expireDate = data.get<std::uint32_t>("expire_date", 0); + result->memberLimit = data.get<std::uint32_t>("member_limit", 0); + result->pendingJoinRequestCount = data.get<std::uint32_t>("pending_join_request_count", 0); + return result; +} + +std::string TgTypeParser::parseChatInviteLink(const ChatInviteLink::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "invite_link", object->inviteLink); + appendToJson(result, "creator", parseUser(object->creator)); + appendToJson(result, "creates_join_request", object->createsJoinRequest); + appendToJson(result, "is_primary", object->isPrimary); + appendToJson(result, "is_revoked", object->isRevoked); + appendToJson(result, "name", object->name); + appendToJson(result, "expire_date", object->expireDate); + appendToJson(result, "member_limit", object->memberLimit); + appendToJson(result, "pending_join_request_count", object->pendingJoinRequestCount); + removeLastComma(result); + result += '}'; + return result; +} + +ChatAdministratorRights::Ptr TgTypeParser::parseJsonAndGetChatAdministratorRights(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ChatAdministratorRights>()); + result->isAnonymous = data.get<bool>("is_anonymous", false); + result->canManageChat = data.get<bool>("can_manage_chat", false); + result->canDeleteMessages = data.get<bool>("can_delete_messages", false); + result->canManageVideoChats = data.get<bool>("can_manage_video_chats", false); + result->canRestrictMembers = data.get<bool>("can_restrict_members", false); + result->canPromoteMembers = data.get<bool>("can_promote_members", false); + result->canChangeInfo = data.get<bool>("can_change_info", false); + result->canInviteUsers = data.get<bool>("can_invite_users", false); + result->canPostMessages = data.get<bool>("can_post_messages", false); + result->canEditMessages = data.get<bool>("can_edit_messages", false); + result->canPinMessages = data.get<bool>("can_pin_messages", false); + return result; +} + +std::string TgTypeParser::parseChatAdministratorRights(const ChatAdministratorRights::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "is_anonymous", object->isAnonymous); + appendToJson(result, "can_manage_chat", object->canManageChat); + appendToJson(result, "can_delete_messages", object->canDeleteMessages); + appendToJson(result, "can_manage_video_chats", object->canManageVideoChats); + appendToJson(result, "can_restrict_members", object->canRestrictMembers); + appendToJson(result, "can_promote_members", object->canPromoteMembers); + appendToJson(result, "can_change_info", object->canChangeInfo); + appendToJson(result, "can_invite_users", object->canInviteUsers); + appendToJson(result, "can_post_messages", object->canPostMessages); + appendToJson(result, "can_edit_messages", object->canEditMessages); + appendToJson(result, "can_pin_messages", object->canPinMessages); + removeLastComma(result); + result += '}'; + return result; +} + ChatMember::Ptr TgTypeParser::parseJsonAndGetChatMember(const boost::property_tree::ptree& data) const { std::string status = data.get<std::string>("status", ""); ChatMember::Ptr result; @@ -1811,102 +1650,353 @@ std::string TgTypeParser::parseChatJoinRequest(const ChatJoinRequest::Ptr& objec return result; } -ChatPhoto::Ptr TgTypeParser::parseJsonAndGetChatPhoto(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ChatPhoto>()); - result->smallFileId = data.get<std::string>("small_file_id", ""); - result->smallFileUniqueId = data.get<std::string>("small_file_unique_id", ""); - result->bigFileId = data.get<std::string>("big_file_id", ""); - result->bigFileUniqueId = data.get<std::string>("big_file_unique_id", ""); +ChatPermissions::Ptr TgTypeParser::parseJsonAndGetChatPermissions(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ChatPermissions>()); + result->canSendMessages = data.get<bool>("can_send_messages"); + result->canSendMediaMessages = data.get<bool>("can_send_media_messages"); + result->canSendPolls = data.get<bool>("can_send_polls"); + result->canSendOtherMessages = data.get<bool>("can_send_other_messages"); + result->canAddWebPagePreviews = data.get<bool>("can_add_web_page_previews"); + result->canChangeInfo = data.get<bool>("can_change_info"); + result->canInviteUsers = data.get<bool>("can_invite_users"); + result->canPinMessages = data.get<bool>("can_pin_messages"); return result; } -std::string TgTypeParser::parseChatPhoto(const ChatPhoto::Ptr& object) const { +std::string TgTypeParser::parseChatPermissions(const ChatPermissions::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "small_file_id", object->smallFileId); - appendToJson(result, "small_file_unique_id", object->smallFileUniqueId); - appendToJson(result, "big_file_id", object->bigFileId); - appendToJson(result, "big_file_unique_id", object->bigFileUniqueId); + appendToJson(result, "can_send_messages", object->canSendMessages); + appendToJson(result, "can_send_media_messages", object->canSendMediaMessages); + appendToJson(result, "can_send_polls", object->canSendPolls); + appendToJson(result, "can_send_other_messages", object->canSendOtherMessages); + appendToJson(result, "can_add_web_page_previews", object->canAddWebPagePreviews); + appendToJson(result, "can_change_info", object->canChangeInfo); + appendToJson(result, "can_invite_users", object->canInviteUsers); + appendToJson(result, "can_pin_messages", object->canPinMessages); removeLastComma(result); result += '}'; return result; } -ChatInviteLink::Ptr TgTypeParser::parseJsonAndGetChatInviteLink(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ChatInviteLink>()); - result->inviteLink = data.get<std::string>("invite_link", ""); - result->creator = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "creator"); - result->createsJoinRequest = data.get<bool>("creates_join_request", false); - result->isPrimary = data.get<bool>("is_primary", false); - result->isRevoked = data.get<bool>("is_revoked", false); - result->name = data.get<std::string>("name", ""); - result->expireDate = data.get<std::uint32_t>("expire_date", 0); - result->memberLimit = data.get<std::uint32_t>("member_limit", 0); - result->pendingJoinRequestCount = data.get<std::uint32_t>("pending_join_request_count", 0); +ChatLocation::Ptr TgTypeParser::parseJsonAndGetChatLocation(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ChatLocation>()); + result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); + result->address = data.get<std::string>("address", ""); return result; } -std::string TgTypeParser::parseChatInviteLink(const ChatInviteLink::Ptr& object) const { +std::string TgTypeParser::parseChatLocation(const ChatLocation::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "invite_link", object->inviteLink); - appendToJson(result, "creator", parseUser(object->creator)); - appendToJson(result, "creates_join_request", object->createsJoinRequest); - appendToJson(result, "is_primary", object->isPrimary); - appendToJson(result, "is_revoked", object->isRevoked); - appendToJson(result, "name", object->name); - appendToJson(result, "expire_date", object->expireDate); - appendToJson(result, "member_limit", object->memberLimit); - appendToJson(result, "pending_join_request_count", object->pendingJoinRequestCount); + appendToJson(result, "location", parseLocation(object->location)); + appendToJson(result, "address", object->address); removeLastComma(result); result += '}'; return result; } -ChatAdministratorRights::Ptr TgTypeParser::parseJsonAndGetChatAdministratorRights(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ChatAdministratorRights>()); - result->isAnonymous = data.get<bool>("is_anonymous", false); - result->canManageChat = data.get<bool>("can_manage_chat", false); - result->canDeleteMessages = data.get<bool>("can_delete_messages", false); - result->canManageVideoChats = data.get<bool>("can_manage_video_chats", false); - result->canRestrictMembers = data.get<bool>("can_restrict_members", false); - result->canPromoteMembers = data.get<bool>("can_promote_members", false); - result->canChangeInfo = data.get<bool>("can_change_info", false); - result->canInviteUsers = data.get<bool>("can_invite_users", false); - result->canPostMessages = data.get<bool>("can_post_messages", false); - result->canEditMessages = data.get<bool>("can_edit_messages", false); - result->canPinMessages = data.get<bool>("can_pin_messages", false); +BotCommand::Ptr TgTypeParser::parseJsonAndGetBotCommand(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<BotCommand>()); + result->command = data.get("command", ""); + result->description = data.get("description", ""); return result; } -std::string TgTypeParser::parseChatAdministratorRights(const ChatAdministratorRights::Ptr& object) const { +std::string TgTypeParser::parseBotCommand(const BotCommand::Ptr& object) const { + std::string result; + result += '{'; + appendToJson(result, "command", object->command); + appendToJson(result, "description", object->description); + removeLastComma(result); + result += '}'; + return result; +} + +BotCommandScope::Ptr TgTypeParser::parseJsonAndGetBotCommandScope(const boost::property_tree::ptree& data) const { + std::string type = data.get<std::string>("type", ""); + BotCommandScope::Ptr result; + + if (type == BotCommandScopeDefault::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeDefault(data)); + } else if (type == BotCommandScopeAllPrivateChats::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeAllPrivateChats(data)); + } else if (type == BotCommandScopeAllGroupChats::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeAllGroupChats(data)); + } else if (type == BotCommandScopeAllChatAdministrators::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeAllChatAdministrators(data)); + } else if (type == BotCommandScopeChat::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeChat(data)); + } else if (type == BotCommandScopeChatAdministrators::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeChatAdministrators(data)); + } else if (type == BotCommandScopeChatMember::TYPE) { + result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeChatMember(data)); + } else { + result = std::make_shared<BotCommandScope>(); + } + + result->type = type; + + return result; +} + +std::string TgTypeParser::parseBotCommandScope(const BotCommandScope::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "is_anonymous", object->isAnonymous); - appendToJson(result, "can_manage_chat", object->canManageChat); - appendToJson(result, "can_delete_messages", object->canDeleteMessages); - appendToJson(result, "can_manage_video_chats", object->canManageVideoChats); - appendToJson(result, "can_restrict_members", object->canRestrictMembers); - appendToJson(result, "can_promote_members", object->canPromoteMembers); - appendToJson(result, "can_change_info", object->canChangeInfo); - appendToJson(result, "can_invite_users", object->canInviteUsers); - appendToJson(result, "can_post_messages", object->canPostMessages); - appendToJson(result, "can_edit_messages", object->canEditMessages); - appendToJson(result, "can_pin_messages", object->canPinMessages); + appendToJson(result, "type", object->type); + + if (object->type == BotCommandScopeDefault::TYPE) { + result += parseBotCommandScopeDefault(std::static_pointer_cast<BotCommandScopeDefault>(object)); + } else if (object->type == BotCommandScopeAllPrivateChats::TYPE) { + result += parseBotCommandScopeAllPrivateChats(std::static_pointer_cast<BotCommandScopeAllPrivateChats>(object)); + } else if (object->type == BotCommandScopeAllGroupChats::TYPE) { + result += parseBotCommandScopeAllGroupChats(std::static_pointer_cast<BotCommandScopeAllGroupChats>(object)); + } else if (object->type == BotCommandScopeAllChatAdministrators::TYPE) { + result += parseBotCommandScopeAllChatAdministrators(std::static_pointer_cast<BotCommandScopeAllChatAdministrators>(object)); + } else if (object->type == BotCommandScopeChat::TYPE) { + result += parseBotCommandScopeChat(std::static_pointer_cast<BotCommandScopeChat>(object)); + } else if (object->type == BotCommandScopeChatAdministrators::TYPE) { + result += parseBotCommandScopeChatAdministrators(std::static_pointer_cast<BotCommandScopeChatAdministrators>(object)); + } else if (object->type == BotCommandScopeChatMember::TYPE) { + result += parseBotCommandScopeChatMember(std::static_pointer_cast<BotCommandScopeChatMember>(object)); + } + removeLastComma(result); result += '}'; return result; } +BotCommandScopeDefault::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeDefault(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeDefault>()); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeDefault(const BotCommandScopeDefault::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +BotCommandScopeAllPrivateChats::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeAllPrivateChats(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeAllPrivateChats>()); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeAllPrivateChats(const BotCommandScopeAllPrivateChats::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +BotCommandScopeAllGroupChats::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeAllGroupChats(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeAllGroupChats>()); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeAllGroupChats(const BotCommandScopeAllGroupChats::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +BotCommandScopeAllChatAdministrators::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeAllChatAdministrators(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeAllChatAdministrators>()); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeAllChatAdministrators(const BotCommandScopeAllChatAdministrators::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +BotCommandScopeChat::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeChat(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeChat>()); + result->chatId = data.get<std::int64_t>("chat_id", 0); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeChat(const BotCommandScopeChat::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "chat_id", object->chatId); + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +BotCommandScopeChatAdministrators::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeChatAdministrators(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeChatAdministrators>()); + result->chatId = data.get<std::int64_t>("chat_id", 0); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeChatAdministrators(const BotCommandScopeChatAdministrators::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "chat_id", object->chatId); + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +BotCommandScopeChatMember::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeChatMember(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). + auto result(std::make_shared<BotCommandScopeChatMember>()); + result->chatId = data.get<std::int64_t>("chat_id", 0); + result->userId = data.get<std::int64_t>("user_id", 0); + return result; +} + +std::string TgTypeParser::parseBotCommandScopeChatMember(const BotCommandScopeChatMember::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseBotCommandScope(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "chat_id", object->chatId); + appendToJson(result, "user_id", object->userId); + // The last comma will be erased by parseBotCommandScope(). + return result; +} + +MenuButton::Ptr TgTypeParser::parseJsonAndGetMenuButton(const boost::property_tree::ptree& data) const { + std::string type = data.get<std::string>("type", ""); + MenuButton::Ptr result; + + if (type == MenuButtonCommands::TYPE) { + result = std::static_pointer_cast<MenuButton>(parseJsonAndGetMenuButtonCommands(data)); + } else if (type == MenuButtonWebApp::TYPE) { + result = std::static_pointer_cast<MenuButton>(parseJsonAndGetMenuButtonWebApp(data)); + } else if (type == MenuButtonDefault::TYPE) { + result = std::static_pointer_cast<MenuButton>(parseJsonAndGetMenuButtonDefault(data)); + } else { + result = std::make_shared<MenuButton>(); + } + + result->type = type; + + return result; +} + +std::string TgTypeParser::parseMenuButton(const MenuButton::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "type", object->type); + + if (object->type == MenuButtonCommands::TYPE) { + result += parseMenuButtonCommands(std::static_pointer_cast<MenuButtonCommands>(object)); + } else if (object->type == MenuButtonWebApp::TYPE) { + result += parseMenuButtonWebApp(std::static_pointer_cast<MenuButtonWebApp>(object)); + } else if (object->type == MenuButtonDefault::TYPE) { + result += parseMenuButtonDefault(std::static_pointer_cast<MenuButtonDefault>(object)); + } + + removeLastComma(result); + result += '}'; + return result; +} + +MenuButtonCommands::Ptr TgTypeParser::parseJsonAndGetMenuButtonCommands(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetMenuButton(). + auto result(std::make_shared<MenuButtonCommands>()); + return result; +} + +std::string TgTypeParser::parseMenuButtonCommands(const MenuButtonCommands::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseMenuButton(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseMenuButton(). + return result; +} + +MenuButtonWebApp::Ptr TgTypeParser::parseJsonAndGetMenuButtonWebApp(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetMenuButton(). + auto result(std::make_shared<MenuButtonWebApp>()); + result->text = data.get<std::string>("text", ""); + result->webApp = tryParseJson<WebAppInfo>(&TgTypeParser::parseJsonAndGetWebAppInfo, data, "web_app"); + return result; +} + +std::string TgTypeParser::parseMenuButtonWebApp(const MenuButtonWebApp::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseMenuButton(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "text", object->text); + appendToJson(result, "web_app", parseWebAppInfo(object->webApp)); + // The last comma will be erased by parseMenuButton(). + return result; +} + +MenuButtonDefault::Ptr TgTypeParser::parseJsonAndGetMenuButtonDefault(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetMenuButton(). + auto result(std::make_shared<MenuButtonDefault>()); + return result; +} + +std::string TgTypeParser::parseMenuButtonDefault(const MenuButtonDefault::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseMenuButton(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseMenuButton(). + return result; +} + ResponseParameters::Ptr TgTypeParser::parseJsonAndGetResponseParameters(const boost::property_tree::ptree& data) const { auto result(std::make_shared<ResponseParameters>()); result->migrateToChatId = data.get<std::int64_t>("migrate_to_chat_id", 0); @@ -1927,33 +2017,299 @@ std::string TgTypeParser::parseResponseParameters(const ResponseParameters::Ptr& return result; } -GenericReply::Ptr TgTypeParser::parseJsonAndGetGenericReply(const boost::property_tree::ptree& data) const { - if (data.find("force_reply") != data.not_found()) { - return std::static_pointer_cast<GenericReply>(parseJsonAndGetForceReply(data)); - } else if (data.find("remove_keyboard") != data.not_found()) { - return std::static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardRemove(data)); - } else if (data.find("keyboard") != data.not_found()) { - return std::static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardMarkup(data)); - } else if (data.find("inline_keyboard") != data.not_found()) { - return std::static_pointer_cast<GenericReply>(parseJsonAndGetInlineKeyboardMarkup(data)); +InputMedia::Ptr TgTypeParser::parseJsonAndGetInputMedia(const boost::property_tree::ptree& data) const { + std::string type = data.get<std::string>("type", ""); + InputMedia::Ptr result; + + if (type == InputMediaPhoto::TYPE) { + result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaPhoto(data)); + } else if (type == InputMediaVideo::TYPE) { + result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaVideo(data)); + } else if (type == InputMediaAnimation::TYPE) { + result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaAnimation(data)); + } else if (type == InputMediaAudio::TYPE) { + result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaAudio(data)); + } else if (type == InputMediaDocument::TYPE) { + result = std::static_pointer_cast<InputMedia>(parseJsonAndGetInputMediaDocument(data)); + } else { + result = std::make_shared<InputMedia>(); } - return std::make_shared<GenericReply>(); + + result->type = data.get<std::string>("type", ""); + result->media = data.get<std::string>("media", ""); + result->caption = data.get<std::string>("caption", ""); + result->parseMode = data.get<std::string>("parse_mode", ""); + result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + + return result; } -std::string TgTypeParser::parseGenericReply(const GenericReply::Ptr& object) const { +std::string TgTypeParser::parseInputMedia(const InputMedia::Ptr& object) const { if (!object) { return ""; } - if (std::dynamic_pointer_cast<ForceReply>(object) != nullptr) { - return parseForceReply(std::static_pointer_cast<ForceReply>(object)); - } else if (std::dynamic_pointer_cast<ReplyKeyboardRemove>(object) != nullptr) { - return parseReplyKeyboardRemove(std::static_pointer_cast<ReplyKeyboardRemove>(object)); - } else if (std::dynamic_pointer_cast<ReplyKeyboardMarkup>(object) != nullptr){ - return parseReplyKeyboardMarkup(std::static_pointer_cast<ReplyKeyboardMarkup>(object)); - } else if (std::dynamic_pointer_cast<InlineKeyboardMarkup>(object) != nullptr){ - return parseInlineKeyboardMarkup(std::static_pointer_cast<InlineKeyboardMarkup>(object)); + std::string result; + result += '{'; + appendToJson(result, "type", object->type); + appendToJson(result, "media", object->media); + appendToJson(result, "caption", object->caption); + appendToJson(result, "parse_mode", object->parseMode); + appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); + + if (object->type == InputMediaPhoto::TYPE) { + result += parseInputMediaPhoto(std::static_pointer_cast<InputMediaPhoto>(object)); + } else if (object->type == InputMediaVideo::TYPE) { + result += parseInputMediaVideo(std::static_pointer_cast<InputMediaVideo>(object)); + } else if (object->type == InputMediaAnimation::TYPE) { + result += parseInputMediaAnimation(std::static_pointer_cast<InputMediaAnimation>(object)); + } else if (object->type == InputMediaAudio::TYPE) { + result += parseInputMediaAudio(std::static_pointer_cast<InputMediaAudio>(object)); + } else if (object->type == InputMediaDocument::TYPE) { + result += parseInputMediaDocument(std::static_pointer_cast<InputMediaDocument>(object)); } - return ""; + + removeLastComma(result); + result += '}'; + return result; +} + +InputMediaPhoto::Ptr TgTypeParser::parseJsonAndGetInputMediaPhoto(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetInputMedia(). + auto result(std::make_shared<InputMediaPhoto>()); + return result; +} + +std::string TgTypeParser::parseInputMediaPhoto(const InputMediaPhoto::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseInputMedia(), so I don't add + // curly brackets to the result std::string. + std::string result; + // The last comma will be erased by parseInputMedia(). + return result; +} + +InputMediaVideo::Ptr TgTypeParser::parseJsonAndGetInputMediaVideo(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetInputMedia(). + auto result(std::make_shared<InputMediaVideo>()); + result->thumb = data.get<std::string>("thumb", ""); + result->width = data.get<std::int32_t>("width", 0); + result->height = data.get<std::int32_t>("height", 0); + result->duration = data.get<std::int32_t>("duration", 0); + result->supportsStreaming = data.get<bool>("supports_streaming", false); + return result; +} + +std::string TgTypeParser::parseInputMediaVideo(const InputMediaVideo::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseInputMedia(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "thumb", object->thumb); + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); + appendToJson(result, "duration", object->duration); + appendToJson(result, "supports_streaming", object->supportsStreaming); + // The last comma will be erased by parseInputMedia(). + return result; +} + +InputMediaAnimation::Ptr TgTypeParser::parseJsonAndGetInputMediaAnimation(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetInputMedia(). + auto result(std::make_shared<InputMediaAnimation>()); + result->thumb = data.get<std::string>("thumb", ""); + result->width = data.get<std::int32_t>("width", 0); + result->height = data.get<std::int32_t>("height", 0); + result->duration = data.get<std::int32_t>("duration", 0); + return result; +} + +std::string TgTypeParser::parseInputMediaAnimation(const InputMediaAnimation::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseInputMedia(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "thumb", object->thumb); + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); + appendToJson(result, "duration", object->duration); + // The last comma will be erased by parseInputMedia(). + return result; +} + +InputMediaAudio::Ptr TgTypeParser::parseJsonAndGetInputMediaAudio(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetInputMedia(). + auto result(std::make_shared<InputMediaAudio>()); + result->thumb = data.get<std::string>("thumb", ""); + result->duration = data.get<std::int32_t>("duration", 0); + result->performer = data.get<std::string>("performer", ""); + result->title = data.get<std::string>("title", ""); + return result; +} + +std::string TgTypeParser::parseInputMediaAudio(const InputMediaAudio::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseInputMedia(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "thumb", object->thumb); + appendToJson(result, "duration", object->duration); + appendToJson(result, "performer", object->performer); + appendToJson(result, "title", object->title); + // The last comma will be erased by parseInputMedia(). + return result; +} + +InputMediaDocument::Ptr TgTypeParser::parseJsonAndGetInputMediaDocument(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetInputMedia(). + auto result(std::make_shared<InputMediaDocument>()); + result->thumb = data.get<std::string>("thumb", ""); + result->disableContentTypeDetection = data.get<bool>("disable_content_type_detection", false); + return result; +} + +std::string TgTypeParser::parseInputMediaDocument(const InputMediaDocument::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseInputMedia(), so I don't add + // curly brackets to the result std::string. + std::string result; + appendToJson(result, "thumb", object->thumb); + appendToJson(result, "disable_content_type_detection", object->disableContentTypeDetection); + // The last comma will be erased by parseInputMedia(). + return result; +} + +Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Sticker>()); + result->fileId = data.get<std::string>("file_id", ""); + result->fileUniqueId = data.get<std::string>("file_unique_id", ""); + std::string type = data.get<std::string>("type", ""); + if (type == "regular") { + result->type = Sticker::Type::Regular; + } else if (type == "mask") { + result->type = Sticker::Type::Mask; + } else if (type == "custom_emoji") { + result->type = Sticker::Type::CustomEmoji; + } + result->width = data.get<std::int32_t>("width", 0); + result->height = data.get<std::int32_t>("height", 0); + result->isAnimated = data.get<bool>("is_animated", false); + result->isVideo = data.get<bool>("is_video", false); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->emoji = data.get<std::string>("emoji", ""); + result->setName = data.get<std::string>("set_name", ""); + result->premiumAnimation = tryParseJson<File>(&TgTypeParser::parseJsonAndGetFile, data, "premium_animation"); + result->maskPosition = tryParseJson<MaskPosition>(&TgTypeParser::parseJsonAndGetMaskPosition, data, "mask_position"); + result->customEmojiId = data.get<std::string>("custom_emoji_id", ""); + result->fileSize = data.get<std::int32_t>("file_size", 0); + return result; +} + +std::string TgTypeParser::parseSticker(const Sticker::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "file_unique_id", object->fileUniqueId); + if (object->type == Sticker::Type::Regular) { + appendToJson(result, "type", "regular"); + } else if (object->type == Sticker::Type::Mask) { + appendToJson(result, "type", "mask"); + } else if (object->type == Sticker::Type::CustomEmoji) { + appendToJson(result, "type", "custom_emoji"); + } + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); + appendToJson(result, "is_animated", object->isAnimated); + appendToJson(result, "is_video", object->isVideo); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "emoji", object->emoji); + appendToJson(result, "set_name", object->setName); + appendToJson(result, "premium_animation", parseFile(object->premiumAnimation)); + appendToJson(result, "mask_position", parseMaskPosition(object->maskPosition)); + appendToJson(result, "custom_emoji_id", object->customEmojiId); + appendToJson(result, "file_size", object->fileSize); + removeLastComma(result); + result += '}'; + return result; +} + +StickerSet::Ptr TgTypeParser::parseJsonAndGetStickerSet(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<StickerSet>()); + result->name = data.get<std::string>("name", ""); + result->title = data.get<std::string>("title", ""); + std::string type = data.get<std::string>("type", ""); + if (type == "regular") { + result->type = StickerSet::Type::Regular; + } else if (type == "mask") { + result->type = StickerSet::Type::Mask; + } else if (type == "custom_emoji") { + result->type = StickerSet::Type::CustomEmoji; + } + result->isAnimated = data.get<bool>("is_animated", false); + result->isVideo = data.get<bool>("is_video", false); + result->stickers = parseJsonAndGetArray<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "stickers"); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + return result; +} + +std::string TgTypeParser::parseStickerSet(const StickerSet::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "name", object->name); + appendToJson(result, "title", object->title); + if (object->type == StickerSet::Type::Regular) { + appendToJson(result, "type", "regular"); + } else if (object->type == StickerSet::Type::Mask) { + appendToJson(result, "type", "mask"); + } else if (object->type == StickerSet::Type::CustomEmoji) { + appendToJson(result, "type", "custom_emoji"); + } + appendToJson(result, "is_animated", object->isAnimated); + appendToJson(result, "is_video", object->isVideo); + appendToJson(result, "stickers", parseArray(&TgTypeParser::parseSticker, object->stickers)); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + removeLastComma(result); + result += '}'; + return result; +} + +MaskPosition::Ptr TgTypeParser::parseJsonAndGetMaskPosition(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<MaskPosition>()); + result->point = data.get("point", ""); + result->xShift = data.get<float>("x_shift", 0); + result->yShift = data.get<float>("y_shift", 0); + result->scale = data.get<float>("scale", 0); + return result; +} + +std::string TgTypeParser::parseMaskPosition(const MaskPosition::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "point", object->point); + appendToJson(result, "x_shift", object->xShift); + appendToJson(result, "y_shift", object->yShift); + appendToJson(result, "scale", object->scale); + removeLastComma(result); + result += '}'; + return result; } InlineQuery::Ptr TgTypeParser::parseJsonAndGetInlineQuery(const boost::property_tree::ptree& data) const { @@ -1967,7 +2323,7 @@ InlineQuery::Ptr TgTypeParser::parseJsonAndGetInlineQuery(const boost::property_ return result; } -std::string TgTypeParser::parseInlineQuery(const InlineQuery::Ptr& object) const{ +std::string TgTypeParser::parseInlineQuery(const InlineQuery::Ptr& object) const { if (!object) { return ""; } @@ -2040,7 +2396,7 @@ InlineQueryResult::Ptr TgTypeParser::parseJsonAndGetInlineQueryResult(const boos } std::string TgTypeParser::parseInlineQueryResult(const InlineQueryResult::Ptr& object) const { - if (!object){ + if (!object) { return ""; } std::string result; @@ -2051,62 +2407,43 @@ std::string TgTypeParser::parseInlineQueryResult(const InlineQueryResult::Ptr& o if (object->type == InlineQueryResultCachedAudio::TYPE) { result += parseInlineQueryResultCachedAudio(std::static_pointer_cast<InlineQueryResultCachedAudio>(object)); - } - else if (object->type == InlineQueryResultCachedDocument::TYPE) { + } else if (object->type == InlineQueryResultCachedDocument::TYPE) { result += parseInlineQueryResultCachedDocument(std::static_pointer_cast<InlineQueryResultCachedDocument>(object)); - } - else if (object->type == InlineQueryResultCachedGif::TYPE) { + } else if (object->type == InlineQueryResultCachedGif::TYPE) { result += parseInlineQueryResultCachedGif(std::static_pointer_cast<InlineQueryResultCachedGif>(object)); - } - else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) { + } else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) { result += parseInlineQueryResultCachedMpeg4Gif(std::static_pointer_cast<InlineQueryResultCachedMpeg4Gif>(object)); - } - else if (object->type == InlineQueryResultCachedPhoto::TYPE) { + } else if (object->type == InlineQueryResultCachedPhoto::TYPE) { result += parseInlineQueryResultCachedPhoto(std::static_pointer_cast<InlineQueryResultCachedPhoto>(object)); - } - else if (object->type == InlineQueryResultCachedSticker::TYPE) { + } else if (object->type == InlineQueryResultCachedSticker::TYPE) { result += parseInlineQueryResultCachedSticker(std::static_pointer_cast<InlineQueryResultCachedSticker>(object)); - } - else if (object->type == InlineQueryResultCachedVideo::TYPE) { + } else if (object->type == InlineQueryResultCachedVideo::TYPE) { result += parseInlineQueryResultCachedVideo(std::static_pointer_cast<InlineQueryResultCachedVideo>(object)); - } - else if (object->type == InlineQueryResultCachedVoice::TYPE) { + } else if (object->type == InlineQueryResultCachedVoice::TYPE) { result += parseInlineQueryResultCachedVoice(std::static_pointer_cast<InlineQueryResultCachedVoice>(object)); - } - else if (object->type == InlineQueryResultArticle::TYPE) { + } else if (object->type == InlineQueryResultArticle::TYPE) { result += parseInlineQueryResultArticle(std::static_pointer_cast<InlineQueryResultArticle>(object)); - } - else if (object->type == InlineQueryResultAudio::TYPE) { + } else if (object->type == InlineQueryResultAudio::TYPE) { result += parseInlineQueryResultAudio(std::static_pointer_cast<InlineQueryResultAudio>(object)); - } - else if (object->type == InlineQueryResultContact::TYPE) { + } else if (object->type == InlineQueryResultContact::TYPE) { result += parseInlineQueryResultContact(std::static_pointer_cast<InlineQueryResultContact>(object)); - } - else if (object->type == InlineQueryResultGame::TYPE) { + } else if (object->type == InlineQueryResultGame::TYPE) { result += parseInlineQueryResultGame(std::static_pointer_cast<InlineQueryResultGame>(object)); - } - else if (object->type == InlineQueryResultDocument::TYPE) { + } else if (object->type == InlineQueryResultDocument::TYPE) { result += parseInlineQueryResultDocument(std::static_pointer_cast<InlineQueryResultDocument>(object)); - } - else if (object->type == InlineQueryResultLocation::TYPE) { + } else if (object->type == InlineQueryResultLocation::TYPE) { result += parseInlineQueryResultLocation(std::static_pointer_cast<InlineQueryResultLocation>(object)); - } - else if (object->type == InlineQueryResultVenue::TYPE) { + } else if (object->type == InlineQueryResultVenue::TYPE) { result += parseInlineQueryResultVenue(std::static_pointer_cast<InlineQueryResultVenue>(object)); - } - else if (object->type == InlineQueryResultVoice::TYPE) { + } else if (object->type == InlineQueryResultVoice::TYPE) { result += parseInlineQueryResultVoice(std::static_pointer_cast<InlineQueryResultVoice>(object)); - } - else if (object->type == InlineQueryResultPhoto::TYPE) { + } else if (object->type == InlineQueryResultPhoto::TYPE) { result += parseInlineQueryResultPhoto(std::static_pointer_cast<InlineQueryResultPhoto>(object)); - } - else if (object->type == InlineQueryResultGif::TYPE) { + } else if (object->type == InlineQueryResultGif::TYPE) { result += parseInlineQueryResultGif(std::static_pointer_cast<InlineQueryResultGif>(object)); - } - else if (object->type == InlineQueryResultMpeg4Gif::TYPE) { + } else if (object->type == InlineQueryResultMpeg4Gif::TYPE) { result += parseInlineQueryResultMpeg4Gif(std::static_pointer_cast<InlineQueryResultMpeg4Gif>(object)); - } - else if (object->type == InlineQueryResultVideo::TYPE) { + } else if (object->type == InlineQueryResultVideo::TYPE) { result += parseInlineQueryResultVideo(std::static_pointer_cast<InlineQueryResultVideo>(object)); } @@ -2115,38 +2452,47 @@ std::string TgTypeParser::parseInlineQueryResult(const InlineQueryResult::Ptr& o return result; } -InlineQueryResultCachedAudio::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedAudio(const boost::property_tree::ptree& data) const { +InlineQueryResultArticle::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultArticle(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedAudio>()); - result->audioFileId = data.get<std::string>("audio_file_id", ""); - result->caption = data.get<std::string>("caption", ""); - result->parseMode = data.get<std::string>("parse_mode", ""); - result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + auto result(std::make_shared<InlineQueryResultArticle>()); + result->title = data.get<std::string>("title", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); + result->url = data.get<std::string>("url", ""); + result->hideUrl = data.get<bool>("hide_url", false); + result->description = data.get<std::string>("description", ""); + result->thumbUrl = data.get<std::string>("thumb_url", ""); + result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); + result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultCachedAudio(const InlineQueryResultCachedAudio::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultArticle(const InlineQueryResultArticle::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "audio_file_id", object->audioFileId); - appendToJson(result, "caption", object->caption); - appendToJson(result, "parse_mode", object->parseMode); - appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); + appendToJson(result, "title", object->title); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); + appendToJson(result, "url", object->url); + appendToJson(result, "hide_url", object->hideUrl); + appendToJson(result, "description", object->description); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "thumb_width", object->thumbWidth); + appendToJson(result, "thumb_height", object->thumbHeight); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultCachedDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedDocument(const boost::property_tree::ptree& data) const { +InlineQueryResultPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultPhoto(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedDocument>()); + auto result(std::make_shared<InlineQueryResultPhoto>()); + result->photoUrl = data.get<std::string>("photo_url", ""); + result->thumbUrl = data.get<std::string>("thumb_url", ""); + result->photoWidth = data.get<std::int32_t>("photo_width", 0); + result->photoHeight = data.get<std::int32_t>("photo_height", 0); result->title = data.get<std::string>("title", ""); - result->documentFileId = data.get<std::string>("document_file_id", ""); result->description = data.get<std::string>("description", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); @@ -2155,15 +2501,18 @@ InlineQueryResultCachedDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryRes return result; } -std::string TgTypeParser::parseInlineQueryResultCachedDocument(const InlineQueryResultCachedDocument::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultPhoto(const InlineQueryResultPhoto::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; + appendToJson(result, "photo_url", object->photoUrl); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "photo_width", object->photoWidth); + appendToJson(result, "photo_height", object->photoHeight); appendToJson(result, "title", object->title); - appendToJson(result, "document_file_id", object->documentFileId); appendToJson(result, "description", object->description); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); @@ -2173,10 +2522,15 @@ std::string TgTypeParser::parseInlineQueryResultCachedDocument(const InlineQuery return result; } -InlineQueryResultCachedGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedGif(const boost::property_tree::ptree& data) const { +InlineQueryResultGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGif(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedGif>()); - result->gifFileId = data.get<std::string>("gif_file_id", ""); + auto result(std::make_shared<InlineQueryResultGif>()); + result->gifUrl = data.get<std::string>("gif_url", ""); + result->gifWidth = data.get<std::int32_t>("gif_width", 0); + result->gifHeight = data.get<std::int32_t>("gif_height", 0); + result->gifDuration = data.get<std::int32_t>("gif_duration", 0); + result->thumbUrl = data.get<std::string>("thumb_url", ""); + result->thumbMimeType = data.get<std::string>("thumb_mime_type", ""); result->title = data.get<std::string>("title", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); @@ -2185,14 +2539,19 @@ InlineQueryResultCachedGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCa return result; } -std::string TgTypeParser::parseInlineQueryResultCachedGif(const InlineQueryResultCachedGif::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultGif(const InlineQueryResultGif::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "gif_file_id", object->gifFileId); + appendToJson(result, "gif_url", object->gifUrl); + appendToJson(result, "gif_width", object->gifWidth); + appendToJson(result, "gif_height", object->gifHeight); + appendToJson(result, "gif_duration", object->gifDuration); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "thumb_mime_type", object->thumbMimeType); appendToJson(result, "title", object->title); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); @@ -2202,10 +2561,15 @@ std::string TgTypeParser::parseInlineQueryResultCachedGif(const InlineQueryResul return result; } -InlineQueryResultCachedMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedMpeg4Gif(const boost::property_tree::ptree& data) const { +InlineQueryResultMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultMpeg4Gif(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedMpeg4Gif>()); - result->mpeg4FileId = data.get<std::string>("mpeg4_file_id", ""); + auto result(std::make_shared<InlineQueryResultMpeg4Gif>()); + result->mpeg4Url = data.get<std::string>("mpeg4_url", ""); + result->mpeg4Width = data.get<std::int32_t>("mpeg4_width", 0); + result->mpeg4Height = data.get<std::int32_t>("mpeg4_height", 0); + result->mpeg4Duration = data.get<std::int32_t>("mpeg4_duration", 0); + result->thumbUrl = data.get<std::string>("thumb_url", ""); + result->thumbMimeType = data.get<std::string>("thumb_mime_type", ""); result->title = data.get<std::string>("title", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); @@ -2214,14 +2578,19 @@ InlineQueryResultCachedMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryRes return result; } -std::string TgTypeParser::parseInlineQueryResultCachedMpeg4Gif(const InlineQueryResultCachedMpeg4Gif::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultMpeg4Gif(const InlineQueryResultMpeg4Gif::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "mpeg4_file_id", object->mpeg4FileId); + appendToJson(result, "mpeg4_url", object->mpeg4Url); + appendToJson(result, "mpeg4_width", object->mpeg4Width); + appendToJson(result, "mpeg4_height", object->mpeg4Height); + appendToJson(result, "mpeg4_duration", object->mpeg4Duration); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "thumb_mime_type", object->thumbMimeType); appendToJson(result, "title", object->title); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); @@ -2231,144 +2600,182 @@ std::string TgTypeParser::parseInlineQueryResultCachedMpeg4Gif(const InlineQuery return result; } -InlineQueryResultCachedPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedPhoto(const boost::property_tree::ptree& data) const { +InlineQueryResultVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVideo(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedPhoto>()); - result->photoFileId = data.get<std::string>("photo_file_id", ""); + auto result(std::make_shared<InlineQueryResultVideo>()); + result->videoUrl = data.get<std::string>("video_url", ""); + result->mimeType = data.get<std::string>("mime_type", ""); + result->thumbUrl = data.get<std::string>("thumb_url", ""); result->title = data.get<std::string>("title", ""); - result->description = data.get<std::string>("description", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + result->videoWidth = data.get<std::int32_t>("video_width", 0); + result->videoHeight = data.get<std::int32_t>("video_height", 0); + result->videoDuration = data.get<std::int32_t>("video_duration", 0); + result->description = data.get<std::string>("description", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); return result; } -std::string TgTypeParser::parseInlineQueryResultCachedPhoto(const InlineQueryResultCachedPhoto::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultVideo(const InlineQueryResultVideo::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "photo_file_id", object->photoFileId); + appendToJson(result, "video_url", object->videoUrl); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "thumb_url", object->thumbUrl); appendToJson(result, "title", object->title); - appendToJson(result, "description", object->description); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); + appendToJson(result, "video_width", object->videoWidth); + appendToJson(result, "video_height", object->videoHeight); + appendToJson(result, "video_duration", object->videoDuration); + appendToJson(result, "description", object->description); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultCachedSticker::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedSticker(const boost::property_tree::ptree& data) const { +InlineQueryResultAudio::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultAudio(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedSticker>()); - result->stickerFileId = data.get<std::string>("sticker_file_id", ""); + auto result(std::make_shared<InlineQueryResultAudio>()); + result->audioUrl = data.get<std::string>("audio_url", ""); + result->title = data.get<std::string>("title", ""); + result->caption = data.get<std::string>("caption", ""); + result->parseMode = data.get<std::string>("parse_mode", ""); + result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + result->performer = data.get<std::string>("performer", ""); + result->audioDuration = data.get<std::int32_t>("audio_duration", 0); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); return result; } -std::string TgTypeParser::parseInlineQueryResultCachedSticker(const InlineQueryResultCachedSticker::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultAudio(const InlineQueryResultAudio::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "sticker_file_id", object->stickerFileId); + appendToJson(result, "audio_url", object->audioUrl); + appendToJson(result, "title", object->title); + appendToJson(result, "caption", object->caption); + appendToJson(result, "parse_mode", object->parseMode); + appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); + appendToJson(result, "performer", object->performer); + appendToJson(result, "audio_duration", object->audioDuration); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultCachedVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedVideo(const boost::property_tree::ptree& data) const { +InlineQueryResultVoice::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVoice(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedVideo>()); - result->videoFileId = data.get<std::string>("video_file_id", ""); + auto result(std::make_shared<InlineQueryResultVoice>()); + result->voiceUrl = data.get<std::string>("voice_url", ""); result->title = data.get<std::string>("title", ""); - result->description = data.get<std::string>("description", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + result->voiceDuration = data.get<std::int32_t>("voice_duration", 0); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); return result; } -std::string TgTypeParser::parseInlineQueryResultCachedVideo(const InlineQueryResultCachedVideo::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultVoice(const InlineQueryResultVoice::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "video_file_id", object->videoFileId); + appendToJson(result, "voice_url", object->voiceUrl); appendToJson(result, "title", object->title); - appendToJson(result, "description", object->description); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); + appendToJson(result, "voice_duration", object->voiceDuration); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultCachedVoice::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedVoice(const boost::property_tree::ptree& data) const { +InlineQueryResultDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultDocument(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultCachedVoice>()); - result->voiceFileId = data.get<std::string>("voice_file_id", ""); + auto result(std::make_shared<InlineQueryResultDocument>()); result->title = data.get<std::string>("title", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + result->documentUrl = data.get<std::string>("document_url", ""); + result->mimeType = data.get<std::string>("mime_type", ""); + result->description = data.get<std::string>("description", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); + result->thumbUrl = data.get<std::string>("thumb_url", ""); + result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); + result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultCachedVoice(const InlineQueryResultCachedVoice::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultDocument(const InlineQueryResultDocument::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "voice_file_id", object->voiceFileId); appendToJson(result, "title", object->title); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); + appendToJson(result, "document_url", object->documentUrl); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "description", object->description); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "thumb_width", object->thumbWidth); + appendToJson(result, "thumb_height", object->thumbHeight); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultArticle::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultArticle(const boost::property_tree::ptree& data) const { +InlineQueryResultLocation::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultLocation(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultArticle>()); + auto result(std::make_shared<InlineQueryResultLocation>()); + result->latitude = data.get<float>("latitude", 0); + result->longitude = data.get<float>("longitude", 0); result->title = data.get<std::string>("title", ""); + result->horizontalAccuracy = data.get<float>("horizontal_accuracy", 0); + result->livePeriod = data.get<std::int32_t>("live_period", 0); + result->heading = data.get<std::int32_t>("heading", 0); + result->proximityAlertRadius = data.get<std::int32_t>("proximity_alert_radius", 0); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); - result->url = data.get<std::string>("url", ""); - result->hideUrl = data.get<bool>("hide_url", false); - result->description = data.get<std::string>("description", ""); result->thumbUrl = data.get<std::string>("thumb_url", ""); result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultArticle(const InlineQueryResultArticle::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultLocation(const InlineQueryResultLocation::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; + appendToJson(result, "latitude", object->latitude); + appendToJson(result, "longitude", object->longitude); appendToJson(result, "title", object->title); + appendToJson(result, "horizontal_accuracy", object->horizontalAccuracy); + appendToJson(result, "live_period", object->livePeriod); + appendToJson(result, "heading", object->heading); + appendToJson(result, "proximity_alert_radius", object->proximityAlertRadius); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); - appendToJson(result, "url", object->url); - appendToJson(result, "hide_url", object->hideUrl); - appendToJson(result, "description", object->description); appendToJson(result, "thumb_url", object->thumbUrl); appendToJson(result, "thumb_width", object->thumbWidth); appendToJson(result, "thumb_height", object->thumbHeight); @@ -2376,35 +2783,43 @@ std::string TgTypeParser::parseInlineQueryResultArticle(const InlineQueryResultA return result; } -InlineQueryResultAudio::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultAudio(const boost::property_tree::ptree& data) const { +InlineQueryResultVenue::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVenue(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultAudio>()); - result->audioUrl = data.get<std::string>("audio_url", ""); + auto result(std::make_shared<InlineQueryResultVenue>()); + result->latitude = data.get<float>("latitude", 0); + result->longitude = data.get<float>("longitude", 0); result->title = data.get<std::string>("title", ""); - result->caption = data.get<std::string>("caption", ""); - result->parseMode = data.get<std::string>("parse_mode", ""); - result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - result->performer = data.get<std::string>("performer", ""); - result->audioDuration = data.get<std::int32_t>("audio_duration", 0); + result->address = data.get<std::string>("address", ""); + result->foursquareId = data.get<std::string>("foursquare_id", ""); + result->foursquareType = data.get<std::string>("foursquare_type", ""); + result->googlePlaceId = data.get<std::string>("google_place_id", ""); + result->googlePlaceType = data.get<std::string>("google_place_type", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); + result->thumbUrl = data.get<std::string>("thumb_url", ""); + result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); + result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultAudio(const InlineQueryResultAudio::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultVenue(const InlineQueryResultVenue::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "audio_url", object->audioUrl); + appendToJson(result, "latitude", object->latitude); + appendToJson(result, "longitude", object->longitude); appendToJson(result, "title", object->title); - appendToJson(result, "caption", object->caption); - appendToJson(result, "parse_mode", object->parseMode); - appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); - appendToJson(result, "performer", object->performer); - appendToJson(result, "audio_duration", object->audioDuration); + appendToJson(result, "address", object->address); + appendToJson(result, "foursquare_id", object->foursquareId); + appendToJson(result, "foursquare_type", object->foursquareType); + appendToJson(result, "google_place_id", object->googlePlaceId); + appendToJson(result, "google_place_type", object->googlePlaceType); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "thumb_width", object->thumbWidth); + appendToJson(result, "thumb_height", object->thumbHeight); // The last comma will be erased by parseInlineQueryResult(). return result; } @@ -2424,7 +2839,7 @@ InlineQueryResultContact::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCont } std::string TgTypeParser::parseInlineQueryResultContact(const InlineQueryResultContact::Ptr& object) const { - if (!object){ + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add @@ -2442,7 +2857,6 @@ std::string TgTypeParser::parseInlineQueryResultContact(const InlineQueryResultC return result; } - InlineQueryResultGame::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGame(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). auto result(std::make_shared<InlineQueryResultGame>()); @@ -2451,7 +2865,7 @@ InlineQueryResultGame::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGame(co } std::string TgTypeParser::parseInlineQueryResultGame(const InlineQueryResultGame::Ptr& object) const { - if (!object){ + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add @@ -2462,165 +2876,121 @@ std::string TgTypeParser::parseInlineQueryResultGame(const InlineQueryResultGame return result; } -InlineQueryResultDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultDocument(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedPhoto(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultDocument>()); + auto result(std::make_shared<InlineQueryResultCachedPhoto>()); + result->photoFileId = data.get<std::string>("photo_file_id", ""); result->title = data.get<std::string>("title", ""); + result->description = data.get<std::string>("description", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - result->documentUrl = data.get<std::string>("document_url", ""); - result->mimeType = data.get<std::string>("mime_type", ""); - result->description = data.get<std::string>("description", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); - result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultDocument(const InlineQueryResultDocument::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedPhoto(const InlineQueryResultCachedPhoto::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; + appendToJson(result, "photo_file_id", object->photoFileId); appendToJson(result, "title", object->title); + appendToJson(result, "description", object->description); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); - appendToJson(result, "document_url", object->documentUrl); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "description", object->description); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "thumb_width", object->thumbWidth); - appendToJson(result, "thumb_height", object->thumbHeight); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultLocation::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultLocation(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedGif(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultLocation>()); - result->latitude = data.get<float>("latitude", 0); - result->longitude = data.get<float>("longitude", 0); + auto result(std::make_shared<InlineQueryResultCachedGif>()); + result->gifFileId = data.get<std::string>("gif_file_id", ""); result->title = data.get<std::string>("title", ""); - result->horizontalAccuracy = data.get<float>("horizontal_accuracy", 0); - result->livePeriod = data.get<std::int32_t>("live_period", 0); - result->heading = data.get<std::int32_t>("heading", 0); - result->proximityAlertRadius = data.get<std::int32_t>("proximity_alert_radius", 0); + result->caption = data.get<std::string>("caption", ""); + result->parseMode = data.get<std::string>("parse_mode", ""); + result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); - result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultLocation(const InlineQueryResultLocation::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedGif(const InlineQueryResultCachedGif::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "latitude", object->latitude); - appendToJson(result, "longitude", object->longitude); + appendToJson(result, "gif_file_id", object->gifFileId); appendToJson(result, "title", object->title); - appendToJson(result, "horizontal_accuracy", object->horizontalAccuracy); - appendToJson(result, "live_period", object->livePeriod); - appendToJson(result, "heading", object->heading); - appendToJson(result, "proximity_alert_radius", object->proximityAlertRadius); + appendToJson(result, "caption", object->caption); + appendToJson(result, "parse_mode", object->parseMode); + appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "thumb_width", object->thumbWidth); - appendToJson(result, "thumb_height", object->thumbHeight); // The last comma will be erased by parseInlineQueryResult(). return result; } - -InlineQueryResultVenue::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVenue(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedMpeg4Gif(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultVenue>()); - result->latitude = data.get<float>("latitude", 0); - result->longitude = data.get<float>("longitude", 0); + auto result(std::make_shared<InlineQueryResultCachedMpeg4Gif>()); + result->mpeg4FileId = data.get<std::string>("mpeg4_file_id", ""); result->title = data.get<std::string>("title", ""); - result->address = data.get<std::string>("address", ""); - result->foursquareId = data.get<std::string>("foursquare_id", ""); - result->foursquareType = data.get<std::string>("foursquare_type", ""); - result->googlePlaceId = data.get<std::string>("google_place_id", ""); - result->googlePlaceType = data.get<std::string>("google_place_type", ""); + result->caption = data.get<std::string>("caption", ""); + result->parseMode = data.get<std::string>("parse_mode", ""); + result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->thumbWidth = data.get<std::int32_t>("thumb_width", 0); - result->thumbHeight = data.get<std::int32_t>("thumb_height", 0); return result; } -std::string TgTypeParser::parseInlineQueryResultVenue(const InlineQueryResultVenue::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedMpeg4Gif(const InlineQueryResultCachedMpeg4Gif::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "latitude", object->latitude); - appendToJson(result, "longitude", object->longitude); + appendToJson(result, "mpeg4_file_id", object->mpeg4FileId); appendToJson(result, "title", object->title); - appendToJson(result, "address", object->address); - appendToJson(result, "foursquare_id", object->foursquareId); - appendToJson(result, "foursquare_type", object->foursquareType); - appendToJson(result, "google_place_id", object->googlePlaceId); - appendToJson(result, "google_place_type", object->googlePlaceType); + appendToJson(result, "caption", object->caption); + appendToJson(result, "parse_mode", object->parseMode); + appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "thumb_width", object->thumbWidth); - appendToJson(result, "thumb_height", object->thumbHeight); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultVoice::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVoice(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedSticker::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedSticker(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultVoice>()); - result->voiceUrl = data.get<std::string>("voice_url", ""); - result->title = data.get<std::string>("title", ""); - result->caption = data.get<std::string>("caption", ""); - result->parseMode = data.get<std::string>("parse_mode", ""); - result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - result->voiceDuration = data.get<std::int32_t>("voice_duration", 0); + auto result(std::make_shared<InlineQueryResultCachedSticker>()); + result->stickerFileId = data.get<std::string>("sticker_file_id", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); return result; } -std::string TgTypeParser::parseInlineQueryResultVoice(const InlineQueryResultVoice::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedSticker(const InlineQueryResultCachedSticker::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "voice_url", object->voiceUrl); - appendToJson(result, "title", object->title); - appendToJson(result, "caption", object->caption); - appendToJson(result, "parse_mode", object->parseMode); - appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); - appendToJson(result, "voice_duration", object->voiceDuration); + appendToJson(result, "sticker_file_id", object->stickerFileId); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); // The last comma will be erased by parseInlineQueryResult(). return result; } -InlineQueryResultPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultPhoto(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedDocument(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultPhoto>()); - result->photoUrl = data.get<std::string>("photo_url", ""); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->photoWidth = data.get<std::int32_t>("photo_width", 0); - result->photoHeight = data.get<std::int32_t>("photo_height", 0); + auto result(std::make_shared<InlineQueryResultCachedDocument>()); result->title = data.get<std::string>("title", ""); + result->documentFileId = data.get<std::string>("document_file_id", ""); result->description = data.get<std::string>("description", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); @@ -2629,18 +2999,15 @@ InlineQueryResultPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultPhoto( return result; } -std::string TgTypeParser::parseInlineQueryResultPhoto(const InlineQueryResultPhoto::Ptr& object) const{ - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedDocument(const InlineQueryResultCachedDocument::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "photo_url", object->photoUrl); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "photo_width", object->photoWidth); - appendToJson(result, "photo_height", object->photoHeight); appendToJson(result, "title", object->title); + appendToJson(result, "document_file_id", object->documentFileId); appendToJson(result, "description", object->description); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); @@ -2650,16 +3017,12 @@ std::string TgTypeParser::parseInlineQueryResultPhoto(const InlineQueryResultPho return result; } -InlineQueryResultGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGif(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedVideo(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultGif>()); - result->gifUrl = data.get<std::string>("gif_url", ""); - result->gifWidth = data.get<std::int32_t>("gif_width", 0); - result->gifHeight = data.get<std::int32_t>("gif_height", 0); - result->gifDuration = data.get<std::int32_t>("gif_duration", 0); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->thumbMimeType = data.get<std::string>("thumb_mime_type", ""); + auto result(std::make_shared<InlineQueryResultCachedVideo>()); + result->videoFileId = data.get<std::string>("video_file_id", ""); result->title = data.get<std::string>("title", ""); + result->description = data.get<std::string>("description", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); @@ -2667,20 +3030,16 @@ InlineQueryResultGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGif(cons return result; } -std::string TgTypeParser::parseInlineQueryResultGif(const InlineQueryResultGif::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedVideo(const InlineQueryResultCachedVideo::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "gif_url", object->gifUrl); - appendToJson(result, "gif_width", object->gifWidth); - appendToJson(result, "gif_height", object->gifHeight); - appendToJson(result, "gif_duration", object->gifDuration); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "thumb_mime_type", object->thumbMimeType); + appendToJson(result, "video_file_id", object->videoFileId); appendToJson(result, "title", object->title); + appendToJson(result, "description", object->description); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); @@ -2689,15 +3048,10 @@ std::string TgTypeParser::parseInlineQueryResultGif(const InlineQueryResultGif:: return result; } -InlineQueryResultMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultMpeg4Gif(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedVoice::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedVoice(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultMpeg4Gif>()); - result->mpeg4Url = data.get<std::string>("mpeg4_url", ""); - result->mpeg4Width = data.get<std::int32_t>("mpeg4_width", 0); - result->mpeg4Height = data.get<std::int32_t>("mpeg4_height", 0); - result->mpeg4Duration = data.get<std::int32_t>("mpeg4_duration", 0); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->thumbMimeType = data.get<std::string>("thumb_mime_type", ""); + auto result(std::make_shared<InlineQueryResultCachedVoice>()); + result->voiceFileId = data.get<std::string>("voice_file_id", ""); result->title = data.get<std::string>("title", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); @@ -2706,19 +3060,14 @@ InlineQueryResultMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultMpe return result; } -std::string TgTypeParser::parseInlineQueryResultMpeg4Gif(const InlineQueryResultMpeg4Gif::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedVoice(const InlineQueryResultCachedVoice::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "mpeg4_url", object->mpeg4Url); - appendToJson(result, "mpeg4_width", object->mpeg4Width); - appendToJson(result, "mpeg4_height", object->mpeg4Height); - appendToJson(result, "mpeg4_duration", object->mpeg4Duration); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "thumb_mime_type", object->thumbMimeType); + appendToJson(result, "voice_file_id", object->voiceFileId); appendToJson(result, "title", object->title); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); @@ -2728,246 +3077,33 @@ std::string TgTypeParser::parseInlineQueryResultMpeg4Gif(const InlineQueryResult return result; } -InlineQueryResultVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVideo(const boost::property_tree::ptree& data) const { +InlineQueryResultCachedAudio::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedAudio(const boost::property_tree::ptree& data) const { // NOTE: This function will be called by parseJsonAndGetInlineQueryResult(). - auto result(std::make_shared<InlineQueryResultVideo>()); - result->videoUrl = data.get<std::string>("video_url", ""); - result->mimeType = data.get<std::string>("mime_type", ""); - result->thumbUrl = data.get<std::string>("thumb_url", ""); - result->title = data.get<std::string>("title", ""); + auto result(std::make_shared<InlineQueryResultCachedAudio>()); + result->audioFileId = data.get<std::string>("audio_file_id", ""); result->caption = data.get<std::string>("caption", ""); result->parseMode = data.get<std::string>("parse_mode", ""); result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - result->videoWidth = data.get<std::int32_t>("video_width", 0); - result->videoHeight = data.get<std::int32_t>("video_height", 0); - result->videoDuration = data.get<std::int32_t>("video_duration", 0); - result->description = data.get<std::string>("description", ""); result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); return result; } -std::string TgTypeParser::parseInlineQueryResultVideo(const InlineQueryResultVideo::Ptr& object) const { - if (!object){ +std::string TgTypeParser::parseInlineQueryResultCachedAudio(const InlineQueryResultCachedAudio::Ptr& object) const { + if (!object) { return ""; } // This function will be called by parseInlineQueryResult(), so I don't add // curly brackets to the result std::string. std::string result; - appendToJson(result, "video_url", object->videoUrl); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "title", object->title); + appendToJson(result, "audio_file_id", object->audioFileId); appendToJson(result, "caption", object->caption); appendToJson(result, "parse_mode", object->parseMode); appendToJson(result, "caption_entities", parseArray(&TgTypeParser::parseMessageEntity, object->captionEntities)); - appendToJson(result, "video_width", object->videoWidth); - appendToJson(result, "video_height", object->videoHeight); - appendToJson(result, "video_duration", object->videoDuration); - appendToJson(result, "description", object->description); appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); // The last comma will be erased by parseInlineQueryResult(). return result; } -ChosenInlineResult::Ptr TgTypeParser::parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ChosenInlineResult>()); - result->resultId = data.get<std::string>("result_id", ""); - result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); - result->inlineMessageId = data.get<std::string>("inline_message_id", ""); - result->query = data.get<std::string>("query", ""); - return result; -} - -std::string TgTypeParser::parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const { - if (!object){ - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "result_id", object->resultId); - appendToJson(result, "from", parseUser(object->from)); - appendToJson(result, "query", object->query); - removeLastComma(result); - result += '}'; - return result; -} - -SentWebAppMessage::Ptr TgTypeParser::parseJsonAndGetSentWebAppMessage(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<SentWebAppMessage>()); - result->inlineMessageId = data.get<std::string>("inline_message_id", ""); - return result; -} - -std::string TgTypeParser::parseSentWebAppMessage(const SentWebAppMessage::Ptr& object) const { - if (!object){ - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "inline_message_id", object->inlineMessageId); - removeLastComma(result); - result += '}'; - return result; -} - -CallbackQuery::Ptr TgTypeParser::parseJsonAndGetCallbackQuery(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<CallbackQuery>()); - result->id = data.get<std::string>("id"); - result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message"); - result->inlineMessageId = data.get<std::string>("inline_message_id", ""); - result->chatInstance = data.get<std::string>("chat_instance"); - result->gameShortName = data.get<std::string>("game_short_name", ""); - result->data = data.get<std::string>("data", ""); - return result; -} - -std::string TgTypeParser::parseCallbackQuery(const CallbackQuery::Ptr& object) const { - if (!object){ - return ""; - } - - std::string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "from", parseUser(object->from)); - appendToJson(result, "message", parseMessage(object->message)); - appendToJson(result, "inline_message_id", object->inlineMessageId); - appendToJson(result, "chat_instance", object->chatInstance); - appendToJson(result, "game_short_name", object->gameShortName); - appendToJson(result, "data", object->data); - removeLastComma(result); - result += '}'; - return result; -} - -InlineKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardMarkup(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<InlineKeyboardMarkup>()); - for (const auto& item : data.find("inline_keyboard")->second){ - result->inlineKeyboard.push_back(parseJsonAndGetArray<InlineKeyboardButton>(&TgTypeParser::parseJsonAndGetInlineKeyboardButton, item.second)); - } - return result; -} - -std::string TgTypeParser::parseInlineKeyboardMarkup(const InlineKeyboardMarkup::Ptr& object) const { - if (!object){ - return ""; - } - std::string result; - result += '{'; - result += R"("inline_keyboard":[)"; - for (const auto& item : object->inlineKeyboard){ - result += '['; - for (const auto& innerItem : item){ - result += parseInlineKeyboardButton(innerItem); - result += ','; - } - removeLastComma(result); - result += "],"; - } - if (!object->inlineKeyboard.empty()) - removeLastComma(result); - result += "]}"; - return result; -} - -InlineKeyboardButton::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardButton(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<InlineKeyboardButton>()); - result->text = data.get<std::string>("text", ""); - result->url = data.get<std::string>("url", ""); - result->callbackData = data.get<std::string>("callback_data", ""); - result->webApp = tryParseJson<WebAppInfo>(&TgTypeParser::parseJsonAndGetWebAppInfo, data, "web_app"); - result->loginUrl = tryParseJson<LoginUrl>(&TgTypeParser::parseJsonAndGetLoginUrl, data, "login_url"); - result->switchInlineQuery = data.get<std::string>("switch_inline_query", ""); - result->switchInlineQueryCurrentChat = data.get<std::string>("switch_inline_query_current_chat", ""); - result->callbackGame = tryParseJson<CallbackGame>(&TgTypeParser::parseJsonAndGetCallbackGame, data, "callback_game"); - result->pay = data.get<bool>("pay", false); - return result; -} -std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const { - if (!object){ - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "pay", object->pay); - appendToJson(result, "text", object->text); - appendToJson(result, "url", object->url); - appendToJson(result, "callback_data", object->callbackData); - appendToJson(result, "switch_inline_query", object->switchInlineQuery); - appendToJson(result, "switch_inline_query_current_chat", object->switchInlineQueryCurrentChat); - removeLastComma(result); - result += '}'; - return result; -} - -LoginUrl::Ptr TgTypeParser::parseJsonAndGetLoginUrl(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<LoginUrl>()); - result->url = data.get<std::string>("url", ""); - result->forwardText = data.get<std::string>("forward_text", ""); - result->botUsername = data.get<std::string>("bot_username", ""); - result->requestWriteAccess = data.get<bool>("request_write_access", false); - return result; -} - -std::string TgTypeParser::parseLoginUrl(const LoginUrl::Ptr& object) const { - if (!object){ - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "url", object->url); - appendToJson(result, "forward_text", object->forwardText); - appendToJson(result, "bot_username", object->botUsername); - appendToJson(result, "request_write_access", object->requestWriteAccess); - removeLastComma(result); - result += '}'; - return result; -} - -WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<WebhookInfo>()); - result->url = data.get<std::string>("url", ""); - result->hasCustomCertificate = data.get<bool>("has_custom_certificate", false); - result->pendingUpdateCount = data.get<std::int32_t>("pending_update_count", 0); - result->ipAddress = data.get<std::string>("ip_address", ""); - result->lastErrorDate = data.get<std::int32_t>("last_error_date", 0); - result->lastErrorMessage = data.get<std::string>("last_error_message", ""); - result->lastSynchronizationErrorDate = data.get<std::int32_t>("last_synchronization_error_date", 0); - result->maxConnections = data.get<std::int32_t>("max_connections", 0); - result->allowedUpdates = parseJsonAndGetArray<std::string>( - [](const boost::property_tree::ptree& innerData)->std::string { - return innerData.get<std::string>(""); - } - , data, "allowed_updates"); - return result; -} - -std::string TgTypeParser::parseWebhookInfo(const WebhookInfo::Ptr& object) const { - if (!object) { - return ""; - } - std::string result; - result += '{'; - appendToJson(result, "url", object->url); - appendToJson(result, "has_custom_certificate", object->hasCustomCertificate); - appendToJson(result, "pending_update_count", object->pendingUpdateCount); - appendToJson(result, "ip_address", object->ipAddress); - appendToJson(result, "last_error_date", object->lastErrorDate); - appendToJson(result, "last_error_message", object->lastErrorMessage); - appendToJson(result, "last_synchronization_error_date", object->lastSynchronizationErrorDate); - appendToJson(result, "max_connections", object->maxConnections); - appendToJson(result, "allowed_updates", - parseArray<std::string>([](const std::string &s)->std::string { - return s; - } - , object->allowedUpdates)); - removeLastComma(result); - result += '}'; - return result; -} - InputMessageContent::Ptr TgTypeParser::parseJsonAndGetInputMessageContent(const boost::property_tree::ptree& data) const { InputMessageContent::Ptr result; @@ -2995,7 +3131,7 @@ InputMessageContent::Ptr TgTypeParser::parseJsonAndGetInputMessageContent(const } std::string TgTypeParser::parseInputMessageContent(const InputMessageContent::Ptr& object) const { - if (!object){ + if (!object) { return ""; } std::string result; @@ -3029,7 +3165,7 @@ InputTextMessageContent::Ptr TgTypeParser::parseJsonAndGetInputTextMessageConten } std::string TgTypeParser::parseInputTextMessageContent(const InputTextMessageContent::Ptr& object) const { - if (!object){ + if (!object) { return ""; } // This function will be called by parseInputMessageContent() @@ -3055,7 +3191,7 @@ InputLocationMessageContent::Ptr TgTypeParser::parseJsonAndGetInputLocationMessa } std::string TgTypeParser::parseInputLocationMessageContent(const InputLocationMessageContent::Ptr& object) const { - if (!object){ + if (!object) { return ""; } // This function will be called by parseInputMessageContent() @@ -3085,7 +3221,7 @@ InputVenueMessageContent::Ptr TgTypeParser::parseJsonAndGetInputVenueMessageCont } std::string TgTypeParser::parseInputVenueMessageContent(const InputVenueMessageContent::Ptr& object) const { - if (!object){ + if (!object) { return ""; } // This function will be called by parseInputMessageContent() @@ -3113,7 +3249,7 @@ InputContactMessageContent::Ptr TgTypeParser::parseJsonAndGetInputContactMessage } std::string TgTypeParser::parseInputContactMessageContent(const InputContactMessageContent::Ptr& object) const { - if (!object){ + if (!object) { return ""; } // This function will be called by parseInputMessageContent() @@ -3136,8 +3272,9 @@ InputInvoiceMessageContent::Ptr TgTypeParser::parseJsonAndGetInputInvoiceMessage result->currency = data.get<std::string>("currency", ""); result->prices = parseJsonAndGetArray<LabeledPrice>(&TgTypeParser::parseJsonAndGetLabeledPrice, data, "prices"); result->maxTipAmount = data.get<std::int32_t>("max_tip_amount", 0); - result->suggestedTipAmounts = parseJsonAndGetArray<std::int32_t>([] (const boost::property_tree::ptree& innerData)->std::int32_t { - return innerData.get<std::int32_t>(0); + result->suggestedTipAmounts = parseJsonAndGetArray<std::int32_t>( + [] (const boost::property_tree::ptree& innerData)->std::int32_t { + return innerData.get<std::int32_t>(""); }, data, "suggested_tip_amounts"); result->providerData = data.get<std::string>("provider_data", ""); result->photoUrl = data.get<std::string>("photo_url", ""); @@ -3186,344 +3323,118 @@ std::string TgTypeParser::parseInputInvoiceMessageContent(const InputInvoiceMess return result; } -Invoice::Ptr TgTypeParser::parseJsonAndGetInvoice(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<Invoice>()); - result->title = data.get<std::string>("title"); - result->description = data.get<std::string>("description"); - result->startParameter = data.get<std::string>("start_parameter"); - result->currency = data.get<std::string>("currency"); - result->totalAmount = data.get<std::int32_t>("total_amount"); +ChosenInlineResult::Ptr TgTypeParser::parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ChosenInlineResult>()); + result->resultId = data.get<std::string>("result_id", ""); + result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); + result->inlineMessageId = data.get<std::string>("inline_message_id", ""); + result->query = data.get<std::string>("query", ""); return result; } -std::string TgTypeParser::parseInvoice(const Invoice::Ptr& object) const { +std::string TgTypeParser::parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "title", object->title); - appendToJson(result, "description", object->description); - appendToJson(result, "start_parameter", object->startParameter); - appendToJson(result, "currency", object->currency); - appendToJson(result, "total_amount", object->totalAmount); + appendToJson(result, "result_id", object->resultId); + appendToJson(result, "from", parseUser(object->from)); + appendToJson(result, "query", object->query); removeLastComma(result); result += '}'; return result; } -LabeledPrice::Ptr TgTypeParser::parseJsonAndGetLabeledPrice(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<LabeledPrice>()); - result->label = data.get<std::string>("label"); - result->amount = data.get<std::int32_t>("amount"); +SentWebAppMessage::Ptr TgTypeParser::parseJsonAndGetSentWebAppMessage(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<SentWebAppMessage>()); + result->inlineMessageId = data.get<std::string>("inline_message_id", ""); return result; } -std::string TgTypeParser::parseLabeledPrice(const LabeledPrice::Ptr& object) const { +std::string TgTypeParser::parseSentWebAppMessage(const SentWebAppMessage::Ptr& object) const { + if (!object) { + return ""; + } std::string result; result += '{'; - appendToJson(result, "label", object->label); - appendToJson(result, "amount", object->amount); + appendToJson(result, "inline_message_id", object->inlineMessageId); removeLastComma(result); result += '}'; return result; } -BotCommand::Ptr TgTypeParser::parseJsonAndGetBotCommand(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<BotCommand>()); - result->command = data.get("command", ""); - result->description = data.get("description",""); +LabeledPrice::Ptr TgTypeParser::parseJsonAndGetLabeledPrice(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<LabeledPrice>()); + result->label = data.get<std::string>("label"); + result->amount = data.get<std::int32_t>("amount"); return result; } -std::string TgTypeParser::parseBotCommand(const BotCommand::Ptr& object) const { +std::string TgTypeParser::parseLabeledPrice(const LabeledPrice::Ptr& object) const { std::string result; result += '{'; - appendToJson(result, "command", object->command); - appendToJson(result, "description", object->description); + appendToJson(result, "label", object->label); + appendToJson(result, "amount", object->amount); removeLastComma(result); result += '}'; return result; } -BotCommandScope::Ptr TgTypeParser::parseJsonAndGetBotCommandScope(const boost::property_tree::ptree& data) const { - std::string type = data.get<std::string>("type", ""); - BotCommandScope::Ptr result; - - if (type == BotCommandScopeDefault::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeDefault(data)); - } else if (type == BotCommandScopeAllPrivateChats::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeAllPrivateChats(data)); - } else if (type == BotCommandScopeAllGroupChats::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeAllGroupChats(data)); - } else if (type == BotCommandScopeAllChatAdministrators::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeAllChatAdministrators(data)); - } else if (type == BotCommandScopeChat::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeChat(data)); - } else if (type == BotCommandScopeChatAdministrators::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeChatAdministrators(data)); - } else if (type == BotCommandScopeChatMember::TYPE) { - result = std::static_pointer_cast<BotCommandScope>(parseJsonAndGetBotCommandScopeChatMember(data)); - } else { - result = std::make_shared<BotCommandScope>(); - } - - result->type = type; - +Invoice::Ptr TgTypeParser::parseJsonAndGetInvoice(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Invoice>()); + result->title = data.get<std::string>("title"); + result->description = data.get<std::string>("description"); + result->startParameter = data.get<std::string>("start_parameter"); + result->currency = data.get<std::string>("currency"); + result->totalAmount = data.get<std::int32_t>("total_amount"); return result; } -std::string TgTypeParser::parseBotCommandScope(const BotCommandScope::Ptr& object) const { +std::string TgTypeParser::parseInvoice(const Invoice::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; - appendToJson(result, "type", object->type); - - if (object->type == BotCommandScopeDefault::TYPE) { - result += parseBotCommandScopeDefault(std::static_pointer_cast<BotCommandScopeDefault>(object)); - } else if (object->type == BotCommandScopeAllPrivateChats::TYPE) { - result += parseBotCommandScopeAllPrivateChats(std::static_pointer_cast<BotCommandScopeAllPrivateChats>(object)); - } else if (object->type == BotCommandScopeAllGroupChats::TYPE) { - result += parseBotCommandScopeAllGroupChats(std::static_pointer_cast<BotCommandScopeAllGroupChats>(object)); - } else if (object->type == BotCommandScopeAllChatAdministrators::TYPE) { - result += parseBotCommandScopeAllChatAdministrators(std::static_pointer_cast<BotCommandScopeAllChatAdministrators>(object)); - } else if (object->type == BotCommandScopeChat::TYPE) { - result += parseBotCommandScopeChat(std::static_pointer_cast<BotCommandScopeChat>(object)); - } else if (object->type == BotCommandScopeChatAdministrators::TYPE) { - result += parseBotCommandScopeChatAdministrators(std::static_pointer_cast<BotCommandScopeChatAdministrators>(object)); - } else if (object->type == BotCommandScopeChatMember::TYPE) { - result += parseBotCommandScopeChatMember(std::static_pointer_cast<BotCommandScopeChatMember>(object)); - } - + appendToJson(result, "title", object->title); + appendToJson(result, "description", object->description); + appendToJson(result, "start_parameter", object->startParameter); + appendToJson(result, "currency", object->currency); + appendToJson(result, "total_amount", object->totalAmount); removeLastComma(result); result += '}'; return result; } -BotCommandScopeDefault::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeDefault(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeDefault>()); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeDefault(const BotCommandScopeDefault::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -BotCommandScopeAllPrivateChats::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeAllPrivateChats(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeAllPrivateChats>()); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeAllPrivateChats(const BotCommandScopeAllPrivateChats::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -BotCommandScopeAllGroupChats::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeAllGroupChats(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeAllGroupChats>()); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeAllGroupChats(const BotCommandScopeAllGroupChats::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -BotCommandScopeAllChatAdministrators::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeAllChatAdministrators(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeAllChatAdministrators>()); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeAllChatAdministrators(const BotCommandScopeAllChatAdministrators::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -BotCommandScopeChat::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeChat(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeChat>()); - result->chatId = data.get<std::int64_t>("chat_id", 0); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeChat(const BotCommandScopeChat::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "chat_id", object->chatId); - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -BotCommandScopeChatAdministrators::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeChatAdministrators(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeChatAdministrators>()); - result->chatId = data.get<std::int64_t>("chat_id", 0); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeChatAdministrators(const BotCommandScopeChatAdministrators::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "chat_id", object->chatId); - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -BotCommandScopeChatMember::Ptr TgTypeParser::parseJsonAndGetBotCommandScopeChatMember(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetBotCommandScope(). - auto result(std::make_shared<BotCommandScopeChatMember>()); - result->chatId = data.get<std::int64_t>("chat_id", 0); - result->userId = data.get<std::int64_t>("user_id", 0); - return result; -} - -std::string TgTypeParser::parseBotCommandScopeChatMember(const BotCommandScopeChatMember::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseBotCommandScope(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "chat_id", object->chatId); - appendToJson(result, "user_id", object->userId); - // The last comma will be erased by parseBotCommandScope(). - return result; -} - -MenuButton::Ptr TgTypeParser::parseJsonAndGetMenuButton(const boost::property_tree::ptree& data) const { - std::string type = data.get<std::string>("type", ""); - MenuButton::Ptr result; - - if (type == MenuButtonCommands::TYPE) { - result = std::static_pointer_cast<MenuButton>(parseJsonAndGetMenuButtonCommands(data)); - } else if (type == MenuButtonWebApp::TYPE) { - result = std::static_pointer_cast<MenuButton>(parseJsonAndGetMenuButtonWebApp(data)); - } else if (type == MenuButtonDefault::TYPE) { - result = std::static_pointer_cast<MenuButton>(parseJsonAndGetMenuButtonDefault(data)); - } else { - result = std::make_shared<MenuButton>(); - } - - result->type = type; - +ShippingAddress::Ptr TgTypeParser::parseJsonAndGetShippingAddress(const boost::property_tree::ptree& data) const { + ShippingAddress::Ptr result; + result->countryCode = data.get<std::string>("country_code"); + result->state = data.get<std::string>("state", ""); + result->city = data.get<std::string>("city"); + result->streetLine1 = data.get<std::string>("street_line1"); + result->streetLine2 = data.get<std::string>("street_line2"); + result->postCode = data.get<std::string>("post_code"); return result; } -std::string TgTypeParser::parseMenuButton(const MenuButton::Ptr& object) const { - if (!object) { - return ""; - } +std::string TgTypeParser::parseShippingAddress(const ShippingAddress::Ptr& object) const { std::string result; result += '{'; - appendToJson(result, "type", object->type); - - if (object->type == MenuButtonCommands::TYPE) { - result += parseMenuButtonCommands(std::static_pointer_cast<MenuButtonCommands>(object)); - } else if (object->type == MenuButtonWebApp::TYPE) { - result += parseMenuButtonWebApp(std::static_pointer_cast<MenuButtonWebApp>(object)); - } else if (object->type == MenuButtonDefault::TYPE) { - result += parseMenuButtonDefault(std::static_pointer_cast<MenuButtonDefault>(object)); + appendToJson(result, "country_code", object->countryCode); + if (!object->state.empty()) { + appendToJson(result, "state", object->state); } - + appendToJson(result, "city", object->city); + appendToJson(result, "street_line1", object->streetLine1); + appendToJson(result, "street_line2", object->streetLine2); + appendToJson(result, "post_code", object->postCode); removeLastComma(result); result += '}'; return result; } -MenuButtonCommands::Ptr TgTypeParser::parseJsonAndGetMenuButtonCommands(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetMenuButton(). - auto result(std::make_shared<MenuButtonCommands>()); - return result; -} - -std::string TgTypeParser::parseMenuButtonCommands(const MenuButtonCommands::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseMenuButton(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseMenuButton(). - return result; -} - -MenuButtonWebApp::Ptr TgTypeParser::parseJsonAndGetMenuButtonWebApp(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetMenuButton(). - auto result(std::make_shared<MenuButtonWebApp>()); - result->text = data.get<std::string>("text", ""); - result->webApp = tryParseJson<WebAppInfo>(&TgTypeParser::parseJsonAndGetWebAppInfo, data, "web_app"); - return result; -} - -std::string TgTypeParser::parseMenuButtonWebApp(const MenuButtonWebApp::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseMenuButton(), so I don't add - // curly brackets to the result std::string. - std::string result; - appendToJson(result, "text", object->text); - appendToJson(result, "web_app", parseWebAppInfo(object->webApp)); - // The last comma will be erased by parseMenuButton(). - return result; -} - -MenuButtonDefault::Ptr TgTypeParser::parseJsonAndGetMenuButtonDefault(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetMenuButton(). - auto result(std::make_shared<MenuButtonDefault>()); - return result; -} - -std::string TgTypeParser::parseMenuButtonDefault(const MenuButtonDefault::Ptr& object) const { - if (!object) { - return ""; - } - // This function will be called by parseMenuButton(), so I don't add - // curly brackets to the result std::string. - std::string result; - // The last comma will be erased by parseMenuButton(). - return result; -} - OrderInfo::Ptr TgTypeParser::parseJsonAndGetOrderInfo(const boost::property_tree::ptree& data) const { auto result(std::make_shared<OrderInfo>()); result->name = data.get<std::string>("name", ""); @@ -3558,72 +3469,47 @@ std::string TgTypeParser::parseOrderInfo(const OrderInfo::Ptr& object) const { return result; } -PreCheckoutQuery::Ptr TgTypeParser::parseJsonAndGetPreCheckoutQuery(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<PreCheckoutQuery>()); +ShippingOption::Ptr TgTypeParser::parseJsonAndGetShippingOption(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<ShippingOption>()); result->id = data.get<std::string>("id"); - result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->currency = data.get<std::string>("currency"); - result->totalAmount = data.get<std::int32_t>("total_amount"); + result->title = data.get<std::string>("title"); + result->prices = parseJsonAndGetArray<LabeledPrice>(&TgTypeParser::parseJsonAndGetLabeledPrice, data, "prices"); return result; } -std::string TgTypeParser::parsePreCheckoutQuery(const PreCheckoutQuery::Ptr& object) const { +std::string TgTypeParser::parseShippingOption(const ShippingOption::Ptr& object) const { std::string result; result += '{'; appendToJson(result, "id", object->id); - result += R"("user":)"; - result += parseUser(object->from); - result += ","; - appendToJson(result, "currency", object->currency); - appendToJson(result, "total_amount", object->totalAmount); - removeLastComma(result); - result += '}'; - return result; -} - -ShippingAddress::Ptr TgTypeParser::parseJsonAndGetShippingAddress(const boost::property_tree::ptree& data) const { - ShippingAddress::Ptr result; - result->countryCode = data.get<std::string>("country_code"); - result->state = data.get<std::string>("state", ""); - result->city = data.get<std::string>("city"); - result->streetLine1 = data.get<std::string>("street_line1"); - result->streetLine2 = data.get<std::string>("street_line2"); - result->postCode = data.get<std::string>("post_code"); - return result; -} - -std::string TgTypeParser::parseShippingAddress(const ShippingAddress::Ptr& object) const { - std::string result; - result += '{'; - appendToJson(result, "country_code", object->countryCode); - if (!object->state.empty()) { - appendToJson(result, "state", object->state); - } - appendToJson(result, "city", object->city); - appendToJson(result, "street_line1", object->streetLine1); - appendToJson(result, "street_line2", object->streetLine2); - appendToJson(result, "post_code", object->postCode); + appendToJson(result, "title", object->title); removeLastComma(result); + result += R"("prices":)"; + result += parseArray(&TgTypeParser::parseLabeledPrice, object->prices); result += '}'; return result; } -ShippingOption::Ptr TgTypeParser::parseJsonAndGetShippingOption(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<ShippingOption>()); - result->id = data.get<std::string>("id"); - result->title = data.get<std::string>("title"); - result->prices = parseJsonAndGetArray<LabeledPrice>(&TgTypeParser::parseJsonAndGetLabeledPrice, data, "prices"); +SuccessfulPayment::Ptr TgTypeParser::parseJsonAndGetSuccessfulPayment(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<SuccessfulPayment>()); + result->currency = data.get<std::string>("currency"); + result->totalAmount = data.get<std::int32_t>("total_amount"); + result->invoicePayload = data.get<std::string>("invoice_payload"); + result->shippingOptionId = data.get<std::string>("shipping_option_id"); + result->orderInfo = tryParseJson(&TgTypeParser::parseJsonAndGetOrderInfo, data, "order_info"); return result; } -std::string TgTypeParser::parseShippingOption(const ShippingOption::Ptr& object) const { +std::string TgTypeParser::parseSuccessfulPayment(const SuccessfulPayment::Ptr& object) const { std::string result; result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "title", object->title); + appendToJson(result, "currency", object->currency); + appendToJson(result, "total_amount", object->totalAmount); + appendToJson(result, "invoice_payload", object->invoicePayload); + appendToJson(result, "shipping_option_id", object->shippingOptionId); + result += R"("order_info":)"; + result += parseOrderInfo(object->orderInfo); + result += ","; removeLastComma(result); - result += R"("prices":)"; - result += parseArray(&TgTypeParser::parseLabeledPrice, object->prices); result += '}'; return result; } @@ -3653,26 +3539,24 @@ std::string TgTypeParser::parseShippingQuery(const ShippingQuery::Ptr& object) c return result; } -SuccessfulPayment::Ptr TgTypeParser::parseJsonAndGetSuccessfulPayment(const boost::property_tree::ptree& data) const { - auto result(std::make_shared<SuccessfulPayment>()); +PreCheckoutQuery::Ptr TgTypeParser::parseJsonAndGetPreCheckoutQuery(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<PreCheckoutQuery>()); + result->id = data.get<std::string>("id"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); result->currency = data.get<std::string>("currency"); result->totalAmount = data.get<std::int32_t>("total_amount"); - result->invoicePayload = data.get<std::string>("invoice_payload"); - result->shippingOptionId = data.get<std::string>("shipping_option_id"); - result->orderInfo = tryParseJson(&TgTypeParser::parseJsonAndGetOrderInfo, data, "order_info"); return result; } -std::string TgTypeParser::parseSuccessfulPayment(const SuccessfulPayment::Ptr& object) const { +std::string TgTypeParser::parsePreCheckoutQuery(const PreCheckoutQuery::Ptr& object) const { std::string result; result += '{'; + appendToJson(result, "id", object->id); + result += R"("user":)"; + result += parseUser(object->from); + result += ","; appendToJson(result, "currency", object->currency); appendToJson(result, "total_amount", object->totalAmount); - appendToJson(result, "invoice_payload", object->invoicePayload); - appendToJson(result, "shipping_option_id", object->shippingOptionId); - result += R"("order_info":)"; - result += parseOrderInfo(object->orderInfo); - result += ","; removeLastComma(result); result += '}'; return result; @@ -4033,6 +3917,99 @@ std::string TgTypeParser::parsePassportElementErrorUnspecified(const PassportEle return result; } +Game::Ptr TgTypeParser::parseJsonAndGetGame(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<Game>()); + result->title = data.get("title", ""); + result->description = data.get("description", ""); + result->photo = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photo"); + result->text = data.get("text", ""); + result->textEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "text_entities"); + result->animation = tryParseJson<Animation>(&TgTypeParser::parseJsonAndGetAnimation, data, "animation"); + return result; +} + +std::string TgTypeParser::parseGame(const Game::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "title", object->title); + appendToJson(result, "description", object->description); + appendToJson(result, "photo", parseArray(&TgTypeParser::parsePhotoSize, object->photo)); + appendToJson(result, "text", object->text); + appendToJson(result, "text_entities", parseArray(&TgTypeParser::parseMessageEntity, object->textEntities)); + appendToJson(result, "animation", parseAnimation(object->animation)); + removeLastComma(result); + result += '}'; + return result; +} + +CallbackGame::Ptr TgTypeParser::parseJsonAndGetCallbackGame(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<CallbackGame>()); + return result; +} + +std::string TgTypeParser::parseCallbackGame(const CallbackGame::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + result += '}'; + return result; +} + +GameHighScore::Ptr TgTypeParser::parseJsonAndGetGameHighScore(const boost::property_tree::ptree& data) const { + auto result(std::make_shared<GameHighScore>()); + result->position = data.get("position", ""); + result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->score = data.get<std::int32_t>("score", 0); + return result; +} + +std::string TgTypeParser::parseGameHighScore(const GameHighScore::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "position", object->position); + appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "score", object->score); + removeLastComma(result); + result += '}'; + return result; +} + +GenericReply::Ptr TgTypeParser::parseJsonAndGetGenericReply(const boost::property_tree::ptree& data) const { + if (data.find("force_reply") != data.not_found()) { + return std::static_pointer_cast<GenericReply>(parseJsonAndGetForceReply(data)); + } else if (data.find("remove_keyboard") != data.not_found()) { + return std::static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardRemove(data)); + } else if (data.find("keyboard") != data.not_found()) { + return std::static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardMarkup(data)); + } else if (data.find("inline_keyboard") != data.not_found()) { + return std::static_pointer_cast<GenericReply>(parseJsonAndGetInlineKeyboardMarkup(data)); + } + return std::make_shared<GenericReply>(); +} + +std::string TgTypeParser::parseGenericReply(const GenericReply::Ptr& object) const { + if (!object) { + return ""; + } + if (std::dynamic_pointer_cast<ForceReply>(object) != nullptr) { + return parseForceReply(std::static_pointer_cast<ForceReply>(object)); + } else if (std::dynamic_pointer_cast<ReplyKeyboardRemove>(object) != nullptr) { + return parseReplyKeyboardRemove(std::static_pointer_cast<ReplyKeyboardRemove>(object)); + } else if (std::dynamic_pointer_cast<ReplyKeyboardMarkup>(object) != nullptr) { + return parseReplyKeyboardMarkup(std::static_pointer_cast<ReplyKeyboardMarkup>(object)); + } else if (std::dynamic_pointer_cast<InlineKeyboardMarkup>(object) != nullptr) { + return parseInlineKeyboardMarkup(std::static_pointer_cast<InlineKeyboardMarkup>(object)); + } + return ""; +} void TgTypeParser::appendToJson(std::string& json, const std::string& varName, const std::string& value) const { if (value.empty()) { diff --git a/src/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp index a7b4fcb..6a224cb 100644 --- a/src/net/TgLongPoll.cpp +++ b/src/net/TgLongPoll.cpp @@ -21,13 +21,19 @@ TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, } void TgLongPoll::start() { - std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates); + // get all unconfirmed updates + std::vector<Update::Ptr> updates = _api->getUpdates(0, _limit, _timeout, _allowUpdates); + + // handle updates for (Update::Ptr& item : updates) { if (item->updateId >= _lastUpdateId) { _lastUpdateId = item->updateId + 1; } _eventHandler->handleUpdate(item); } + + // confirm handled updates + _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates); } } diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index 653047c..3e5e6c4 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -101,4 +101,22 @@ string urlDecode(const string& value) { return result; } +std::string escapeJsonString(const std::string& value) { + string result; + + for (const char& c : value) { + switch (c) { + case '"': + case '\\': + case '/': + result += '\\'; + break; + } + + result += c; + } + + return result; +} + } |