diff options
author | Oleg Morozenkov <m@oleg.rocks> | 2018-07-23 01:56:42 +0300 |
---|---|---|
committer | Oleg Morozenkov <m@oleg.rocks> | 2018-07-23 01:56:42 +0300 |
commit | d47ee877be5d1175bdc36f2d87881ddaf875a8e9 (patch) | |
tree | 7fd20cdc1236fe6b832ae980de12afd7071ebab9 /src | |
parent | cea20d4078f2088dea0dd589f1cc9dd7ee22461b (diff) |
Refactor http clients, fix webhook server, add more samples, change tabs to 4 spaces
Diffstat (limited to 'src')
-rw-r--r-- | src/Api.cpp | 1899 | ||||
-rw-r--r-- | src/EventHandler.cpp | 32 | ||||
-rw-r--r-- | src/TgTypeParser.cpp | 3183 | ||||
-rw-r--r-- | src/net/BoostHttpOnlySslClient.cpp | 92 | ||||
-rw-r--r-- | src/net/CurlHttpClient.cpp | 94 | ||||
-rw-r--r-- | src/net/HttpClient.cpp | 162 | ||||
-rw-r--r-- | src/net/HttpParser.cpp | 314 | ||||
-rw-r--r-- | src/net/TgLongPoll.cpp | 18 | ||||
-rw-r--r-- | src/net/Url.cpp | 88 | ||||
-rw-r--r-- | src/tools/FileTools.cpp | 30 | ||||
-rw-r--r-- | src/tools/StringTools.cpp | 139 | ||||
-rw-r--r-- | src/types/InputFile.cpp | 10 |
12 files changed, 3033 insertions, 3028 deletions
diff --git a/src/Api.cpp b/src/Api.cpp index abc99de..08f5fac 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -22,503 +22,504 @@ #include "tgbot/Api.h" +#include <utility> + #include "tgbot/TgTypeParser.h" #include "tgbot/TgException.h" -#include "tgbot/net/HttpClient.h" using namespace std; using namespace boost::property_tree; namespace TgBot { -Api::Api(const string& token, const HttpClient &httpClientDriver) - : _token(token), _httpClientDriver(httpClientDriver) { +Api::Api(string token, const HttpClient& httpClient) + : _token(std::move(token)), _httpClient(httpClient), _tgTypeParser() { } User::Ptr Api::getMe() const { - return TgTypeParser::getInstance().parseJsonAndGetUser(sendRequest("getMe")); + return _tgTypeParser.parseJsonAndGetUser(sendRequest("getMe")); } Message::Ptr Api::sendMessage(int64_t chatId, const string& text, bool disableWebPagePreview, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("text", text); - if (disableWebPagePreview) { - args.emplace_back("disable_web_page_preview", disableWebPagePreview); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendMessage", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("text", text); + if (disableWebPagePreview) { + args.emplace_back("disable_web_page_preview", disableWebPagePreview); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendMessage", args)); } Message::Ptr Api::forwardMessage(int64_t chatId, int64_t fromChatId, int32_t messageId, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(4); - args.emplace_back("chat_id", chatId); - args.emplace_back("from_chat_id", fromChatId); - args.emplace_back("message_id", messageId); - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("forwardMessage", args)); + vector<HttpReqArg> args; + args.reserve(4); + args.emplace_back("chat_id", chatId); + args.emplace_back("from_chat_id", fromChatId); + args.emplace_back("message_id", messageId); + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("forwardMessage", args)); } Message::Ptr Api::sendPhoto(int64_t chatId, const InputFile::Ptr photo, const string& caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("photo", photo->data, true, photo->mimeType, photo->fileName); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendPhoto", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("photo", photo->data, true, photo->mimeType, photo->fileName); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } Message::Ptr Api::sendPhoto(int64_t chatId, const string& photoId, const string& caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("photo", photoId); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendPhoto", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("photo", photoId); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } Message::Ptr Api::sendAudio(int64_t chatId, const InputFile::Ptr audio, const string &caption, int32_t duration, const string& performer, const string& title, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(10); - args.emplace_back("chat_id", chatId); - args.emplace_back("audio", audio->data, true, audio->mimeType, audio->fileName); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (duration) { - args.emplace_back("duration", duration); - } - if (!performer.empty()){ - args.emplace_back("performer", performer); - } - if (!title.empty()){ - args.emplace_back("title", title); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendAudio", args)); + vector<HttpReqArg> args; + args.reserve(10); + args.emplace_back("chat_id", chatId); + args.emplace_back("audio", audio->data, true, audio->mimeType, audio->fileName); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (duration) { + args.emplace_back("duration", duration); + } + if (!performer.empty()){ + args.emplace_back("performer", performer); + } + if (!title.empty()){ + args.emplace_back("title", title); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAudio", args)); } Message::Ptr Api::sendAudio(int64_t chatId, const string& audioId, const string &caption, int32_t duration, const string& performer, const string& title, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(10); - args.emplace_back("chat_id", chatId); - args.emplace_back("audio", audioId); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (duration) { - args.emplace_back("duration", duration); - } - if (!performer.empty()){ - args.emplace_back("performer", performer); - } - if (!title.empty()){ - args.emplace_back("title", title); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendAudio", args)); + vector<HttpReqArg> args; + args.reserve(10); + args.emplace_back("chat_id", chatId); + args.emplace_back("audio", audioId); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (duration) { + args.emplace_back("duration", duration); + } + if (!performer.empty()){ + args.emplace_back("performer", performer); + } + if (!title.empty()){ + args.emplace_back("title", title); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAudio", args)); } Message::Ptr Api::sendDocument(int64_t chatId, const InputFile::Ptr document, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("document", document->data, true, document->mimeType, document->fileName); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendDocument", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("document", document->data, true, document->mimeType, document->fileName); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDocument", args)); } Message::Ptr Api::sendDocument(int64_t chatId, const string& document, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("document", document); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendDocument", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("document", document); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDocument", args)); } Message::Ptr Api::sendInvoice(int64_t chatId, const std::string& title, const std::string& description, const std::string& payload, - const std::string& providerToken, const std::string& startParameter, const std::string& currency, const std::vector<LabeledPrice::Ptr>& prices, - const std::string& providerData, const std::string& photoUrl, int32_t photoSize, - int32_t photoWidth, int32_t photoHeight, bool needName, - bool needPhoneNumber, bool needEmail, bool needShippingAddress, - bool sendPhoneNumberToProvider, bool sendEmailToProvider, bool isFlexible, - int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(23); - args.emplace_back("chat_id", chatId); - args.emplace_back("title", title); - args.emplace_back("description", description); - args.emplace_back("payload", payload); - args.emplace_back("provider_token", providerToken); - args.emplace_back("start_parameter", startParameter); - args.emplace_back("currency", currency); - args.emplace_back("prices", TgTypeParser::getInstance().parseArray<LabeledPrice>(&TgTypeParser::parseLabeledPrice, prices)); - if (!providerData.empty()) { - args.emplace_back("provider_data", providerData); - } - if (!photoUrl.empty()) { - args.emplace_back("photo_url", photoUrl); - } - if (photoSize) { - args.emplace_back("photo_size", photoSize); - } - if (photoWidth) { - args.emplace_back("photo_width", photoWidth); - } - if (photoHeight) { - args.emplace_back("photo_height", photoHeight); - } - if (needName) { - args.emplace_back("need_name", needName); - } - if (needPhoneNumber) { - args.emplace_back("need_phone_number", needPhoneNumber); - } - if (needEmail) { - args.emplace_back("need_email", needEmail); - } - if (needShippingAddress) { - args.emplace_back("need_shipping_address", needShippingAddress); - } - if (sendPhoneNumberToProvider) { - args.emplace_back("send_phone_number_to_provider", sendPhoneNumberToProvider); - } - if (sendEmailToProvider) { - args.emplace_back("send_email_to_provider", sendEmailToProvider); - } - if (isFlexible) { - args.emplace_back("is_flexible", isFlexible); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendInvoice", args)); + const std::string& providerToken, const std::string& startParameter, const std::string& currency, const std::vector<LabeledPrice::Ptr>& prices, + const std::string& providerData, const std::string& photoUrl, int32_t photoSize, + int32_t photoWidth, int32_t photoHeight, bool needName, + bool needPhoneNumber, bool needEmail, bool needShippingAddress, + bool sendPhoneNumberToProvider, bool sendEmailToProvider, bool isFlexible, + int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { + vector<HttpReqArg> args; + args.reserve(23); + args.emplace_back("chat_id", chatId); + args.emplace_back("title", title); + args.emplace_back("description", description); + args.emplace_back("payload", payload); + args.emplace_back("provider_token", providerToken); + args.emplace_back("start_parameter", startParameter); + args.emplace_back("currency", currency); + args.emplace_back("prices", _tgTypeParser.parseArray<LabeledPrice>(&TgTypeParser::parseLabeledPrice, prices)); + if (!providerData.empty()) { + args.emplace_back("provider_data", providerData); + } + if (!photoUrl.empty()) { + args.emplace_back("photo_url", photoUrl); + } + if (photoSize) { + args.emplace_back("photo_size", photoSize); + } + if (photoWidth) { + args.emplace_back("photo_width", photoWidth); + } + if (photoHeight) { + args.emplace_back("photo_height", photoHeight); + } + if (needName) { + args.emplace_back("need_name", needName); + } + if (needPhoneNumber) { + args.emplace_back("need_phone_number", needPhoneNumber); + } + if (needEmail) { + args.emplace_back("need_email", needEmail); + } + if (needShippingAddress) { + args.emplace_back("need_shipping_address", needShippingAddress); + } + if (sendPhoneNumberToProvider) { + args.emplace_back("send_phone_number_to_provider", sendPhoneNumberToProvider); + } + if (sendEmailToProvider) { + args.emplace_back("send_email_to_provider", sendEmailToProvider); + } + if (isFlexible) { + args.emplace_back("is_flexible", isFlexible); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendInvoice", args)); } bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector<ShippingOption::Ptr>& shippingOptions, const std::string& errorMessage) const { - vector<HttpReqArg> args; - args.reserve(4); - args.emplace_back("shipping_query_id", shippingQueryId); - args.emplace_back("ok", ok); - if (!shippingOptions.empty()) { - args.emplace_back("shipping_options", TgTypeParser::getInstance().parseArray<ShippingOption>(&TgTypeParser::parseShippingOption, shippingOptions)); - } - if (!errorMessage.empty()) { - args.emplace_back("error_message", errorMessage); - } - return sendRequest("answerShippingQuery", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(4); + args.emplace_back("shipping_query_id", shippingQueryId); + args.emplace_back("ok", ok); + if (!shippingOptions.empty()) { + args.emplace_back("shipping_options", _tgTypeParser.parseArray<ShippingOption>(&TgTypeParser::parseShippingOption, shippingOptions)); + } + 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; - 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); + 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); } Message::Ptr Api::sendSticker(int64_t chatId, const InputFile::Ptr sticker, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(5); - args.emplace_back("chat_id", chatId); - args.emplace_back("sticker", sticker->data, true, sticker->mimeType, sticker->fileName); - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendSticker", args)); + vector<HttpReqArg> args; + args.reserve(5); + args.emplace_back("chat_id", chatId); + args.emplace_back("sticker", sticker->data, true, sticker->mimeType, sticker->fileName); + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendSticker", args)); } Message::Ptr Api::sendSticker(int64_t chatId, const string& stickerId, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(5); - args.emplace_back("chat_id", chatId); - args.emplace_back("sticker", stickerId); - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendSticker", args)); + vector<HttpReqArg> args; + args.reserve(5); + args.emplace_back("chat_id", chatId); + args.emplace_back("sticker", stickerId); + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendSticker", args)); } StickerSet::Ptr Api::getStickerSet(const string& name) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("name", name); - return TgTypeParser::getInstance().parseJsonAndGetStickerSet(sendRequest("getStickerSet", args)); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("name", name); + return _tgTypeParser.parseJsonAndGetStickerSet(sendRequest("getStickerSet", args)); } File::Ptr Api::uploadStickerFile(int32_t userId, const InputFile::Ptr pngSticker) const { - 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::getInstance().parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); + 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)); } bool Api::createNewStickerSet(int32_t userId, const string& name, const string& title, InputFile::Ptr pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("user_id", userId); - args.emplace_back("name", name); - args.emplace_back("title", title); - args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); - args.emplace_back("emojis", emojis); - if (containsMasks) { - args.emplace_back("contains_mask", containsMasks); - } - if (maskPosition != nullptr) { - args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition)); - } - return sendRequest("createNewStickerSet", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("user_id", userId); + args.emplace_back("name", name); + args.emplace_back("title", title); + args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); + args.emplace_back("emojis", emojis); + if (containsMasks) { + args.emplace_back("contains_mask", containsMasks); + } + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } + return sendRequest("createNewStickerSet", args).get<bool>("", false); } bool Api::createNewStickerSet(int32_t userId, const string& name, const string& title, const string& pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("user_id", userId); - args.emplace_back("name", name); - args.emplace_back("title", title); - args.emplace_back("png_sticker", pngSticker); - args.emplace_back("emojis", emojis); - if (containsMasks) { - args.emplace_back("contains_mask", containsMasks); - } - if (maskPosition != nullptr) { - args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition)); - } - return sendRequest("createNewStickerSet", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("user_id", userId); + args.emplace_back("name", name); + args.emplace_back("title", title); + args.emplace_back("png_sticker", pngSticker); + args.emplace_back("emojis", emojis); + if (containsMasks) { + args.emplace_back("contains_mask", containsMasks); + } + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } + return sendRequest("createNewStickerSet", args).get<bool>("", false); } bool Api::addStickerToSet(int32_t userId, const string& name, const string& title, InputFile::Ptr pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { - vector<HttpReqArg> args; - args.reserve(6); - args.emplace_back("user_id", userId); - args.emplace_back("name", name); - args.emplace_back("title", title); - args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); - args.emplace_back("emojis", emojis); - if (maskPosition != nullptr) { - args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition)); - } - return sendRequest("addStickerToSet", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(6); + args.emplace_back("user_id", userId); + args.emplace_back("name", name); + args.emplace_back("title", title); + args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); + args.emplace_back("emojis", emojis); + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } + return sendRequest("addStickerToSet", args).get<bool>("", false); } bool Api::addStickerToSet(int32_t userId, const string& name, const string& title, const string& pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { - vector<HttpReqArg> args; - args.reserve(6); - args.emplace_back("user_id", userId); - args.emplace_back("name", name); - args.emplace_back("title", title); - args.emplace_back("png_sticker", pngSticker); - args.emplace_back("emojis", emojis); - if (maskPosition != nullptr) { - args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition)); - } - return sendRequest("addStickerToSet", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(6); + args.emplace_back("user_id", userId); + args.emplace_back("name", name); + args.emplace_back("title", title); + args.emplace_back("png_sticker", pngSticker); + args.emplace_back("emojis", emojis); + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } + return sendRequest("addStickerToSet", args).get<bool>("", false); } bool Api::setStickerPositionInSet(const string& sticker, uint32_t position) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("sticker", sticker); - args.emplace_back("position", position); - return sendRequest("setStickerPositionInSet", args).get<bool>("", false); + 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::deleteStickerPositionInSet(const string& sticker) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("sticker", sticker); - return sendRequest("setStickerPositionInSet", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("sticker", sticker); + return sendRequest("setStickerPositionInSet", args).get<bool>("", false); } Message::Ptr Api::sendVideo(int64_t chatId, const InputFile::Ptr video, bool supportsStreaming, int32_t duration, int32_t width, int32_t height, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(11); - args.emplace_back("chat_id", chatId); - args.emplace_back("video", video->data, true, video->mimeType, video->fileName); - if (supportsStreaming) { - args.emplace_back("supports_streaming", supportsStreaming); - } - if (duration) { - args.emplace_back("duration", duration); - } - if (width) { - args.emplace_back("width", width); - } - if (height) { - args.emplace_back("height", height); - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVideo", args)); + vector<HttpReqArg> args; + args.reserve(11); + args.emplace_back("chat_id", chatId); + args.emplace_back("video", video->data, true, video->mimeType, video->fileName); + if (supportsStreaming) { + args.emplace_back("supports_streaming", supportsStreaming); + } + if (duration) { + args.emplace_back("duration", duration); + } + if (width) { + args.emplace_back("width", width); + } + if (height) { + args.emplace_back("height", height); + } + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); } Message::Ptr Api::sendVideo(int64_t chatId, const string& videoId, bool supportsStreaming, int32_t duration, int32_t width, int32_t height, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(11); - args.emplace_back("chat_id", chatId); - args.emplace_back("video", videoId); - if (supportsStreaming) { - args.emplace_back("supports_streaming", supportsStreaming); - } - if (duration) { - args.emplace_back("duration", duration); - } - if (width) { - args.emplace_back("width", width); - } - if (height) { - args.emplace_back("height", height); - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVideo", args)); + vector<HttpReqArg> args; + args.reserve(11); + args.emplace_back("chat_id", chatId); + args.emplace_back("video", videoId); + if (supportsStreaming) { + args.emplace_back("supports_streaming", supportsStreaming); + } + if (duration) { + args.emplace_back("duration", duration); + } + if (width) { + args.emplace_back("width", width); + } + if (height) { + args.emplace_back("height", height); + } + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); } Message::Ptr Api::sendVideoNote(int64_t chatId, const InputFile::Ptr videoNote, int64_t replyToMessageId, bool disableNotification, int32_t duration, int32_t length, const GenericReply::Ptr replyMarkup) { vector<HttpReqArg> args; - args.reserve(7); + args.reserve(7); args.emplace_back("chat_id", chatId); args.emplace_back("video_note", videoNote); if (disableNotification) { @@ -531,17 +532,17 @@ Message::Ptr Api::sendVideoNote(int64_t chatId, const InputFile::Ptr videoNote, args.emplace_back("length", length); } if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (replyToMessageId) { + if (replyToMessageId) { args.emplace_back("reply_to_message_id", replyToMessageId); } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoiceNote", args)); + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoiceNote", args)); } Message::Ptr Api::sendVideoNote(int64_t chatId, const string &videoNote, int64_t replyToMessageId, bool disableNotification, int32_t duration, int32_t length, const GenericReply::Ptr replyMarkup) { vector<HttpReqArg> args; - args.reserve(7); + args.reserve(7); args.emplace_back("chat_id", chatId); args.emplace_back("video_note", videoNote); if (disableNotification) { @@ -554,677 +555,675 @@ Message::Ptr Api::sendVideoNote(int64_t chatId, const string &videoNote, int64_t args.emplace_back("length", length); } if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (replyToMessageId) { + if (replyToMessageId) { args.emplace_back("reply_to_message_id", replyToMessageId); } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoiceNote", args)); + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoiceNote", args)); } vector<Message::Ptr> Api::sendMediaGroup(int64_t chatId, const vector<InputMedia::Ptr>& media, bool disableNotification, int32_t replyToMessageId) const { - vector<HttpReqArg> args; - args.reserve(4); + vector<HttpReqArg> args; + args.reserve(4); args.emplace_back("chat_id", chatId); - string mediaJson = TgTypeParser::getInstance().parseArray<InputMedia>(&TgTypeParser::parseInputMedia, media); - args.emplace_back("media", mediaJson); - args.emplace_back("disable_notification", disableNotification); - args.emplace_back("reply_to_message_id", replyToMessageId); - return TgTypeParser::getInstance().parseJsonAndGetArray<Message>(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); + string mediaJson = _tgTypeParser.parseArray<InputMedia>(&TgTypeParser::parseInputMedia, media); + args.emplace_back("media", mediaJson); + args.emplace_back("disable_notification", disableNotification); + args.emplace_back("reply_to_message_id", replyToMessageId); + return _tgTypeParser.parseJsonAndGetArray<Message>(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } Message::Ptr Api::sendVoice(int64_t chatId, const InputFile::Ptr voice, const string &caption, int duration, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(8); - args.emplace_back("chat_id", chatId); - args.emplace_back("voice", voice->data, true, voice->mimeType, voice->fileName); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (duration){ - args.emplace_back("duration", duration); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args)); + vector<HttpReqArg> args; + args.reserve(8); + args.emplace_back("chat_id", chatId); + args.emplace_back("voice", voice->data, true, voice->mimeType, voice->fileName); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (duration){ + args.emplace_back("duration", duration); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoice", args)); } Message::Ptr Api::sendVoice(int64_t chatId, const string& voiceId, const string &caption, int duration, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(8); - args.emplace_back("chat_id", chatId); - args.emplace_back("voice", voiceId); - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (duration){ - args.emplace_back("duration", duration); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args)); + vector<HttpReqArg> args; + args.reserve(8); + args.emplace_back("chat_id", chatId); + args.emplace_back("voice", voiceId); + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (duration){ + args.emplace_back("duration", duration); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoice", args)); } Message::Ptr Api::sendGame(int64_t chatId, const std::string& gameShortName, int32_t replyToMessageId, const InlineKeyboardMarkup::Ptr replyMarkup, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(5); - args.emplace_back("chat_id", chatId); - args.emplace_back("game_short_name", gameShortName); - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendGame", args)); + vector<HttpReqArg> args; + args.reserve(5); + args.emplace_back("chat_id", chatId); + args.emplace_back("game_short_name", gameShortName); + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendGame", args)); } Message::Ptr Api::sendLocation(int64_t chatId, float latitude, float longitude, uint32_t livePeriod, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("latitude", latitude); - args.emplace_back("longitude", longitude); - if (livePeriod) { - args.emplace_back("live_period", livePeriod); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendLocation", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("latitude", latitude); + args.emplace_back("longitude", longitude); + if (livePeriod) { + args.emplace_back("live_period", livePeriod); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendLocation", args)); } Message::Ptr Api::editMessageLiveLocation(float latitude, float longitude, int64_t chatId, int32_t messageId, int32_t inlineMessageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { - vector<HttpReqArg> args; - args.reserve(6); - args.emplace_back("latitude", latitude); - args.emplace_back("longitude", longitude); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (inlineMessageId) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseInlineKeyboardMarkup(replyMarkup)); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); + vector<HttpReqArg> args; + args.reserve(6); + args.emplace_back("latitude", latitude); + args.emplace_back("longitude", longitude); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (inlineMessageId) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseInlineKeyboardMarkup(replyMarkup)); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); } Message::Ptr Api::stopMessageLiveLocation(int64_t chatId, int32_t messageId, int32_t inlineMessageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { - vector<HttpReqArg> args; - args.reserve(4); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (inlineMessageId) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseInlineKeyboardMarkup(replyMarkup)); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); + vector<HttpReqArg> args; + args.reserve(4); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (inlineMessageId) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseInlineKeyboardMarkup(replyMarkup)); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); } bool Api::setChatStickerSet(int64_t chatId, const string& stickerSetName) const { - 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); + 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(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("deleteChatStickerSet", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatStickerSet", args).get<bool>("", false); } Message::Ptr Api::sendVenue(int64_t chatId, float latitude, float longitude, const string& title, const string& address, const string& foursquareId, bool disableNotification, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { - vector<HttpReqArg> args; - args.reserve(9); - args.emplace_back("chat_id", chatId); - args.emplace_back("latitude", latitude); - args.emplace_back("longitude", longitude); - args.emplace_back("title", title); - args.emplace_back("address", address); - if (!foursquareId.empty()) { - args.emplace_back("foursquare_id", foursquareId); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVenue", args)); + vector<HttpReqArg> args; + args.reserve(9); + args.emplace_back("chat_id", chatId); + args.emplace_back("latitude", latitude); + args.emplace_back("longitude", longitude); + args.emplace_back("title", title); + args.emplace_back("address", address); + if (!foursquareId.empty()) { + args.emplace_back("foursquare_id", foursquareId); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVenue", args)); } Message::Ptr Api::sendContact(int64_t chatId, const string& phoneNumber, const string& firstName, const string& lastName, bool disableNotification, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("phone_number", phoneNumber); - args.emplace_back("first_name", firstName); - args.emplace_back("last_name", lastName); - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendContact", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("phone_number", phoneNumber); + args.emplace_back("first_name", firstName); + args.emplace_back("last_name", lastName); + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendContact", args)); } void Api::sendChatAction(int64_t chatId, const string& action) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("action", action); - sendRequest("sendChatAction", args); + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("action", action); + sendRequest("sendChatAction", args); } UserProfilePhotos::Ptr Api::getUserProfilePhotos(int32_t userId, int32_t offset, int32_t limit) const { - 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); - return TgTypeParser::getInstance().parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); + 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); + return _tgTypeParser.parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); } File::Ptr Api::getFile(const string &fileId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("file_id", fileId); - return TgTypeParser::getInstance().parseJsonAndGetFile(sendRequest("getFile", args)); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("file_id", fileId); + return _tgTypeParser.parseJsonAndGetFile(sendRequest("getFile", args)); } bool Api::leaveChat(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("leaveChat", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("leaveChat", args).get<bool>("", false); } Chat::Ptr Api::getChat(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return TgTypeParser::getInstance().parseJsonAndGetChat(sendRequest("getChat", args)); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return _tgTypeParser.parseJsonAndGetChat(sendRequest("getChat", args)); } vector<ChatMember::Ptr> Api::getChatAdministrators(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return TgTypeParser::getInstance().parseJsonAndGetArray<ChatMember>(&TgTypeParser::parseJsonAndGetChatMember, sendRequest("getChatAdministrators", args)); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return _tgTypeParser.parseJsonAndGetArray<ChatMember>(&TgTypeParser::parseJsonAndGetChatMember, sendRequest("getChatAdministrators", args)); } int32_t Api::getChatMembersCount(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("getChatMembersCount", args).get<int32_t>("", 0); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("getChatMembersCount", args).get<int32_t>("", 0); } bool Api::answerCallbackQuery(const string & callbackQueryId, const string & text, bool showAlert, const string &url, int32_t cacheTime) const { - vector<HttpReqArg> args; - args.reserve(5); - args.emplace_back("callback_query_id", callbackQueryId); - if (!text.empty()) { - args.emplace_back("text", text); - } - if (showAlert) { - args.emplace_back("show_alert", showAlert); - } - if (!url.empty()) { - args.emplace_back("url", url); - } - if (cacheTime) { - args.emplace_back("cache_time", cacheTime); - } - return sendRequest("answerCallbackQuery", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(5); + args.emplace_back("callback_query_id", callbackQueryId); + if (!text.empty()) { + args.emplace_back("text", text); + } + if (showAlert) { + args.emplace_back("show_alert", showAlert); + } + if (!url.empty()) { + args.emplace_back("url", url); + } + if (cacheTime) { + args.emplace_back("cache_time", cacheTime); + } + return sendRequest("answerCallbackQuery", args).get<bool>("", false); } Message::Ptr Api::editMessageText(const string& text, int64_t chatId, int32_t messageId, const string& inlineMessageId, - const string& parseMode, bool disableWebPagePreview, const GenericReply::Ptr replyMarkup) const { - - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("text", text); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableWebPagePreview) { - args.emplace_back("disable_web_page_preview", disableWebPagePreview); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - ptree p = sendRequest("editMessageText", args); - if (p.get_child_optional("message_id")) { - return TgTypeParser::getInstance().parseJsonAndGetMessage(p); - } else { - return nullptr; - } + const string& parseMode, bool disableWebPagePreview, const GenericReply::Ptr replyMarkup) const { + + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("text", text); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableWebPagePreview) { + args.emplace_back("disable_web_page_preview", disableWebPagePreview); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageText", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } } Message::Ptr Api::editMessageCaption(int64_t chatId, int32_t messageId, const string& caption, - const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { - - vector<HttpReqArg> args; - args.reserve(5); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - ptree p = sendRequest("editMessageCaption", args); - if (p.get_child_optional("message_id")) { - return TgTypeParser::getInstance().parseJsonAndGetMessage(p); - } else { - return nullptr; - } + const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { + vector<HttpReqArg> args; + args.reserve(5); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageCaption", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } } Message::Ptr Api::editMessageReplyMarkup(int64_t chatId, int32_t messageId, const string& inlineMessageId, - const GenericReply::Ptr replyMarkup) const { - - vector<HttpReqArg> args; - args.reserve(4); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)); - } - ptree p = sendRequest("editMessageReplyMarkup", args); - if (p.get_child_optional("message_id")) { - return TgTypeParser::getInstance().parseJsonAndGetMessage(p); - } else { - return nullptr; - } -} - -ChatMember::Ptr Api::getChatMember(int64_t chatId, int32_t userId) const -{ - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - return TgTypeParser::getInstance().parseJsonAndGetChatMember(sendRequest("getChatMember", args)); + const GenericReply::Ptr replyMarkup) const { + + vector<HttpReqArg> args; + args.reserve(4); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageReplyMarkup", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } +} + +ChatMember::Ptr Api::getChatMember(int64_t chatId, int32_t userId) const { + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + return _tgTypeParser.parseJsonAndGetChatMember(sendRequest("getChatMember", args)); } vector<Update::Ptr> Api::getUpdates(int32_t offset, int32_t limit, int32_t timeout, const StringArrayPtr &allowedUpdates) const { - vector<HttpReqArg> args; - args.reserve(4); - if (offset) { - args.emplace_back("offset", offset); - } - limit = max(1, min(100, limit)); - args.emplace_back("limit", limit); - if (timeout) { - args.emplace_back("timeout", timeout); - } - if (allowedUpdates != nullptr) { - string allowedUpdatesJson = TgTypeParser::getInstance().parseArray<string>( - [](const string &s)->string { - return s; - }, *allowedUpdates); - args.emplace_back("allowed_updates", allowedUpdatesJson); - } - return TgTypeParser::getInstance().parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); + vector<HttpReqArg> args; + args.reserve(4); + if (offset) { + args.emplace_back("offset", offset); + } + limit = max(1, min(100, limit)); + args.emplace_back("limit", limit); + if (timeout) { + args.emplace_back("timeout", timeout); + } + if (allowedUpdates != nullptr) { + string allowedUpdatesJson = _tgTypeParser.parseArray<string>( + [](const string &s)->string { + return s; + }, *allowedUpdates); + args.emplace_back("allowed_updates", allowedUpdatesJson); + } + return _tgTypeParser.parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); } void Api::setWebhook(const string& url, const InputFile::Ptr certificate, int32_t maxConnection, const StringArrayPtr &allowedUpdates) const { - vector<HttpReqArg> args; - args.reserve(4); - if (!url.empty()) { - args.emplace_back("url", url); - } - if (certificate != nullptr) { - args.emplace_back("certificate", certificate->data, true, certificate->mimeType, certificate->fileName); - } - if (maxConnection != 40) { - args.emplace_back("max_connections", maxConnection); - } - if (allowedUpdates != nullptr) { - auto allowedUpdatesJson = TgTypeParser::getInstance().parseArray<string>( - [](const string &s)->string { - return s; - }, *allowedUpdates); - args.emplace_back("allowed_updates", allowedUpdatesJson); - } - - sendRequest("setWebhook", args); + vector<HttpReqArg> args; + args.reserve(4); + if (!url.empty()) { + args.emplace_back("url", url); + } + if (certificate != nullptr) { + args.emplace_back("certificate", certificate->data, true, certificate->mimeType, certificate->fileName); + } + if (maxConnection != 40) { + args.emplace_back("max_connections", maxConnection); + } + if (allowedUpdates != nullptr) { + auto allowedUpdatesJson = _tgTypeParser.parseArray<string>( + [](const string &s)->string { + return s; + }, *allowedUpdates); + args.emplace_back("allowed_updates", allowedUpdatesJson); + } + + sendRequest("setWebhook", args); } bool Api::deleteWebhook() const { - return sendRequest("deleteWebhook").get<bool>("", false); + return sendRequest("deleteWebhook").get<bool>("", false); } WebhookInfo::Ptr Api::getWebhookInfo() const { - ptree p = sendRequest("getWebhookInfo"); + ptree p = sendRequest("getWebhookInfo"); - if (!p.get_child_optional("url")) { - return nullptr; - } - if (p.get<string>("url","") != string("")) { - return TgTypeParser::getInstance().parseJsonAndGetWebhookInfo(p); - } - else { - return nullptr; - } + if (!p.get_child_optional("url")) { + return nullptr; + } + if (p.get<string>("url","") != string("")) { + return _tgTypeParser.parseJsonAndGetWebhookInfo(p); + } + else { + return nullptr; + } } bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector<InlineQueryResult::Ptr>& results, - int32_t cacheTime, bool isPersonal, const string& nextOffset, const string& switchPmText, const string& switchPmParameter) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("inline_query_id", inlineQueryId); - string resultsJson = TgTypeParser::getInstance().parseArray<InlineQueryResult>(&TgTypeParser::parseInlineQueryResult, results); - args.emplace_back("results", resultsJson); - if (cacheTime) { - args.emplace_back("cache_time", cacheTime); - } - if (isPersonal) { - args.emplace_back("is_personal", isPersonal); - } - if (!nextOffset.empty()) { - args.emplace_back("next_offset", nextOffset); - } - if (!switchPmText.empty()) { - args.emplace_back("switch_pm_text", switchPmText); - } - if (!switchPmParameter.empty()) { - args.emplace_back("switch_pm_parameter", switchPmParameter); - } - return sendRequest("answerInlineQuery", args).get<bool>("", false); + int32_t cacheTime, bool isPersonal, const string& nextOffset, const string& switchPmText, const string& switchPmParameter) const { + 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); + if (cacheTime) { + args.emplace_back("cache_time", cacheTime); + } + if (isPersonal) { + args.emplace_back("is_personal", isPersonal); + } + if (!nextOffset.empty()) { + args.emplace_back("next_offset", nextOffset); + } + if (!switchPmText.empty()) { + args.emplace_back("switch_pm_text", switchPmText); + } + if (!switchPmParameter.empty()) { + args.emplace_back("switch_pm_parameter", switchPmParameter); + } + return sendRequest("answerInlineQuery", args).get<bool>("", false); } bool Api::kickChatMember(int64_t chatId, int32_t userId, uint64_t untilDate) const { - vector<HttpReqArg> args; - args.reserve(3); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - if (untilDate) { - args.emplace_back("until_date", untilDate); - } - return sendRequest("kickChatMember", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(3); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + if (untilDate) { + args.emplace_back("until_date", untilDate); + } + return sendRequest("kickChatMember", args).get<bool>("", false); } bool Api::unbanChatMember(int64_t chatId, int32_t userId) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - return sendRequest("unbanChatMember", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + return sendRequest("unbanChatMember", args).get<bool>("", false); } bool Api::restrictChatMember(int64_t chatId, int32_t userId, uint64_t untilDate, bool canSendMessages, - bool canSendMediaMessages, bool canSendOtherMessages, bool canAddWebPagePreviews) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - if (untilDate) { - args.emplace_back("until_date", untilDate); - } - if (canSendMessages) { - args.emplace_back("can_send_messages", canSendMessages); - } - if (canSendMediaMessages) { - args.emplace_back("can_send_media_messages", canSendMediaMessages); - } - if (canSendOtherMessages) { - args.emplace_back("can_send_other_messages", canSendOtherMessages); - } - if (canAddWebPagePreviews) { - args.emplace_back("can_add_web_page_previews", canAddWebPagePreviews); - } - return sendRequest("restrictChatMember", args).get<bool>("", false); + bool canSendMediaMessages, bool canSendOtherMessages, bool canAddWebPagePreviews) const { + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + if (untilDate) { + args.emplace_back("until_date", untilDate); + } + if (canSendMessages) { + args.emplace_back("can_send_messages", canSendMessages); + } + if (canSendMediaMessages) { + args.emplace_back("can_send_media_messages", canSendMediaMessages); + } + if (canSendOtherMessages) { + args.emplace_back("can_send_other_messages", canSendOtherMessages); + } + if (canAddWebPagePreviews) { + args.emplace_back("can_add_web_page_previews", canAddWebPagePreviews); + } + return sendRequest("restrictChatMember", args).get<bool>("", false); } bool Api::promoteChatMember(int64_t chatId, int32_t userId, bool canChangeInfo, bool canPostMessages, - bool canEditMessages, bool canDeleteMessages, bool canInviteUsers, bool canPinMessages, bool canPromoteMembers) const { - vector<HttpReqArg> args; - args.reserve(9); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - if (canChangeInfo) { - args.emplace_back("can_change_info", canChangeInfo); - } - if (canPostMessages) { - args.emplace_back("can_post_messages", canPostMessages); - } - if (canEditMessages) { - args.emplace_back("can_edit_messages", canEditMessages); - } - if (canDeleteMessages) { - args.emplace_back("can_delete_messages", canDeleteMessages); - } - if (canInviteUsers) { - args.emplace_back("can_invite_users", canInviteUsers); - } - if (canPinMessages) { - args.emplace_back("can_pin_messages", canPinMessages); - } - if (canPromoteMembers) { - args.emplace_back("can_promote_members", canPromoteMembers); - } - return sendRequest("promoteChatMember", args).get<bool>("", false); + bool canEditMessages, bool canDeleteMessages, bool canInviteUsers, bool canPinMessages, bool canPromoteMembers) const { + vector<HttpReqArg> args; + args.reserve(9); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + if (canChangeInfo) { + args.emplace_back("can_change_info", canChangeInfo); + } + if (canPostMessages) { + args.emplace_back("can_post_messages", canPostMessages); + } + if (canEditMessages) { + args.emplace_back("can_edit_messages", canEditMessages); + } + if (canDeleteMessages) { + args.emplace_back("can_delete_messages", canDeleteMessages); + } + if (canInviteUsers) { + args.emplace_back("can_invite_users", canInviteUsers); + } + if (canPinMessages) { + args.emplace_back("can_pin_messages", canPinMessages); + } + if (canPromoteMembers) { + args.emplace_back("can_promote_members", canPromoteMembers); + } + return sendRequest("promoteChatMember", args).get<bool>("", false); } string Api::exportChatInviteLink(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("exportChatInviteLink", args).get("", ""); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("exportChatInviteLink", args).get("", ""); } bool Api::setChatPhoto(int64_t chatId, const InputFile::Ptr photo) const { - 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); + 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(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("deleteChatPhoto", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatPhoto", args).get<bool>("", false); } bool Api::setChatTitle(int64_t chatId, const string& title) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("title", title); - return sendRequest("setChatTitle", args).get<bool>("", false); + 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(int64_t chatId, const string& description) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("description", description); - return sendRequest("setChatDescription", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("description", description); + return sendRequest("setChatDescription", args).get<bool>("", false); } bool Api::pinChatMessage(int64_t chatId, int32_t messageId, bool disableNotification) const { - 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); + 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(int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("unpinChatMessage", args).get<bool>("", false); + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("unpinChatMessage", args).get<bool>("", false); } Message::Ptr Api::setGameScore(int32_t userId, int32_t score, bool force, bool disableEditMessage, int64_t chatId, int32_t messageId, const std::string& inlineMessageId) const { - vector<HttpReqArg> args; - args.reserve(7); - args.emplace_back("user_id", userId); - args.emplace_back("score", score); - if (force) { - args.emplace_back("force", force); - } - if (disableEditMessage) { - args.emplace_back("disable_edit_message", disableEditMessage); - } - if (chatId){ - args.emplace_back("chat_id", chatId); - } - if (messageId){ - args.emplace_back("message_id", messageId); - } - if (!inlineMessageId.empty()){ - args.emplace_back("inline_message_id", inlineMessageId); - } - return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("setGameScore", args)); + vector<HttpReqArg> args; + args.reserve(7); + args.emplace_back("user_id", userId); + args.emplace_back("score", score); + if (force) { + args.emplace_back("force", force); + } + if (disableEditMessage) { + args.emplace_back("disable_edit_message", disableEditMessage); + } + if (chatId){ + args.emplace_back("chat_id", chatId); + } + if (messageId){ + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()){ + args.emplace_back("inline_message_id", inlineMessageId); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("setGameScore", args)); } vector<GameHighScore::Ptr> Api::getGameHighScores(int32_t userId, int32_t score, bool force, bool disableEditMessage, int64_t chatId, int32_t messageId, const std::string& inlineMessageId) const { - vector<HttpReqArg> args; - args.reserve(7); + vector<HttpReqArg> args; + args.reserve(7); args.emplace_back("user_id", userId); - args.emplace_back("score", score); - if (force) { - args.emplace_back("force", force); - } - if (disableEditMessage) { - args.emplace_back("disable_edit_message", disableEditMessage); - } - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (!inlineMessageId.empty()){ - args.emplace_back("inline_message_id", inlineMessageId); - } - return TgTypeParser::getInstance().parseJsonAndGetArray<GameHighScore>(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args)); + args.emplace_back("score", score); + if (force) { + args.emplace_back("force", force); + } + if (disableEditMessage) { + args.emplace_back("disable_edit_message", disableEditMessage); + } + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()){ + args.emplace_back("inline_message_id", inlineMessageId); + } + return _tgTypeParser.parseJsonAndGetArray<GameHighScore>(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args)); } void Api::deleteMessage(int64_t chatId, int32_t messageId) const { - sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); + sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) const { - string url = "https://api.telegram.org/bot"; - url += _token; - url += "/"; - url += method; - - string serverResponse = _httpClientDriver.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::getInstance().parseJson(serverResponse); - try { - if (result.get<bool>("ok", false)) { - return result.get_child("result"); - } else { - 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())); - } + string url = "https://api.telegram.org/bot"; + url += _token; + url += "/"; + url += method; + + 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); + try { + if (result.get<bool>("ok", false)) { + return result.get_child("result"); + } else { + 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())); + } } string Api::downloadFile(const string& filePath, const std::vector<HttpReqArg>& args) const { - string url = "https://api.telegram.org/file/bot"; - url += _token; - url += "/"; - url += filePath; + string url = "https://api.telegram.org/file/bot"; + url += _token; + url += "/"; + url += filePath; - string serverResponse = _httpClientDriver.makeRequest(url, args); + string serverResponse = _httpClient.makeRequest(url, args); - return serverResponse; + return serverResponse; } } diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index ba0ce54..d85e0c3 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -5,47 +5,49 @@ #include "tgbot/EventHandler.h" #include <algorithm> +using namespace std; + namespace TgBot { -void EventHandler::handleUpdate(const Update::Ptr update) const { +void EventHandler::handleUpdate(Update::Ptr update) const { if (update->inlineQuery != nullptr) { - _broadcaster->broadcastInlineQuery(update->inlineQuery); + _broadcaster.broadcastInlineQuery(update->inlineQuery); } if (update->chosenInlineResult != nullptr) { - _broadcaster->broadcastChosenInlineResult(update->chosenInlineResult); + _broadcaster.broadcastChosenInlineResult(update->chosenInlineResult); } if (update->callbackQuery != nullptr) { - _broadcaster->broadcastCallbackQuery(update->callbackQuery); + _broadcaster.broadcastCallbackQuery(update->callbackQuery); } if (update->message != nullptr) { handleMessage(update->message); } } -void EventHandler::handleMessage(const Message::Ptr message) const { - _broadcaster->broadcastAnyMessage(message); +void EventHandler::handleMessage(Message::Ptr message) const { + _broadcaster.broadcastAnyMessage(message); if (StringTools::startsWith(message->text, "/")) { - uint16_t splitPosition; - uint16_t spacePosition = message->text.find(' '); - uint16_t atSymbolPosition = message->text.find('@'); - if (spacePosition == message->text.npos) { - if (atSymbolPosition == message->text.npos) { + size_t splitPosition; + size_t spacePosition = message->text.find(' '); + size_t atSymbolPosition = message->text.find('@'); + if (spacePosition == string::npos) { + if (atSymbolPosition == string::npos) { splitPosition = message->text.size(); } else { splitPosition = atSymbolPosition; } - } else if (atSymbolPosition == message->text.npos) { + } else if (atSymbolPosition == string::npos) { splitPosition = spacePosition; } else { splitPosition = std::min(spacePosition, atSymbolPosition); } std::string command = message->text.substr(1, splitPosition - 1); - if (!_broadcaster->broadcastCommand(command, message)) { - _broadcaster->broadcastUnknownCommand(message); + if (!_broadcaster.broadcastCommand(command, message)) { + _broadcaster.broadcastUnknownCommand(message); } } else { - _broadcaster->broadcastNonCommandMessage(message); + _broadcaster.broadcastNonCommandMessage(message); } } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 4eaeaea..dbfb266 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -27,2027 +27,2022 @@ using namespace boost::property_tree; namespace TgBot { -TgTypeParser& TgTypeParser::getInstance() { - static TgTypeParser result; - return result; -} - Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { - auto result(make_shared<Chat>()); - result->id = data.get<int64_t>("id"); - string type = data.get<string>("type"); - if (type == "private") { - result->type = Chat::Type::Private; - } else if (type == "group") { - result->type = Chat::Type::Group; - } else if (type == "supergroup") { - result->type = Chat::Type::Supergroup; - } else if (type == "channel") { - result->type = Chat::Type::Channel; - } - result->title = data.get("title", ""); - result->username = data.get("username", ""); - result->firstName = data.get("first_name", ""); - result->lastName = data.get("last_name", ""); - result->allMembersAreAdministrators = data.get<bool>("all_members_are_administrators", false); - result->photo = tryParseJson<ChatPhoto>(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); - result->description = data.get("description", ""); - result->inviteLink = data.get("invite_link", ""); - result->pinnedMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); - result->stickerSetName = data.get("sticker_set_name", ""); - result->canSetStickerSet = data.get<bool>("can_set_sticker_set", false); - - return result; + auto result(make_shared<Chat>()); + result->id = data.get<int64_t>("id"); + string type = data.get<string>("type"); + if (type == "private") { + result->type = Chat::Type::Private; + } else if (type == "group") { + result->type = Chat::Type::Group; + } else if (type == "supergroup") { + result->type = Chat::Type::Supergroup; + } else if (type == "channel") { + result->type = Chat::Type::Channel; + } + result->title = data.get("title", ""); + result->username = data.get("username", ""); + result->firstName = data.get("first_name", ""); + result->lastName = data.get("last_name", ""); + result->allMembersAreAdministrators = data.get<bool>("all_members_are_administrators", false); + result->photo = tryParseJson<ChatPhoto>(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); + result->description = data.get("description", ""); + result->inviteLink = data.get("invite_link", ""); + result->pinnedMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); + result->stickerSetName = data.get("sticker_set_name", ""); + result->canSetStickerSet = data.get<bool>("can_set_sticker_set", false); + + return result; } string TgTypeParser::parseChat(const Chat::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "id", object->id); - if (object->type == Chat::Type::Private) { - appendToJson(result, "type", "private"); - } else if (object->type == Chat::Type::Group) { - appendToJson(result, "type", "group"); - } else if (object->type == Chat::Type::Supergroup) { - appendToJson(result, "type", "supergroup"); - } else if (object->type == Chat::Type::Channel) { - appendToJson(result, "type", "channel"); - } - appendToJson(result, "title", object->title); - appendToJson(result, "username", object->username); - appendToJson(result, "first_name", object->firstName); - appendToJson(result, "last_name", object->lastName); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + if (object->type == Chat::Type::Private) { + appendToJson(result, "type", "private"); + } else if (object->type == Chat::Type::Group) { + appendToJson(result, "type", "group"); + } else if (object->type == Chat::Type::Supergroup) { + appendToJson(result, "type", "supergroup"); + } else if (object->type == Chat::Type::Channel) { + appendToJson(result, "type", "channel"); + } + appendToJson(result, "title", object->title); + appendToJson(result, "username", object->username); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + removeLastComma(result); + result += '}'; + return result; } User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { - auto result(make_shared<User>()); - result->id = data.get<int32_t>("id"); - result->isBot = data.get<bool>("is_bot", false); - result->firstName = data.get<string>("first_name"); - result->lastName = data.get("last_name", ""); - result->username = data.get("username", ""); - result->languageCode = data.get("language_code", ""); - return result; + auto result(make_shared<User>()); + result->id = data.get<int32_t>("id"); + result->isBot = data.get<bool>("is_bot", false); + result->firstName = data.get<string>("first_name"); + result->lastName = data.get("last_name", ""); + result->username = data.get("username", ""); + result->languageCode = data.get("language_code", ""); + return result; } string TgTypeParser::parseUser(const User::Ptr& object) const { - if (!object) { - return ""; - } - 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); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + 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); + removeLastComma(result); + result += '}'; + return result; } MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const ptree& data) const{ - auto result(make_shared<MessageEntity>()); - result->type = data.get<string>("type"); - result->offset = data.get<int32_t>("offset"); - result->length = data.get<int32_t>("length"); - result->url = data.get<string>("url", ""); - result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); - return result; + auto result(make_shared<MessageEntity>()); + result->type = data.get<string>("type"); + result->offset = data.get<int32_t>("offset"); + result->length = data.get<int32_t>("length"); + result->url = data.get<string>("url", ""); + result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); + return result; } string TgTypeParser::parseMessageEntity(const MessageEntity::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "type", object->type); - appendToJson(result, "offset", object->offset); - appendToJson(result, "length", object->length); - appendToJson(result, "url", object->url); - appendToJson(result, "user", parseUser(object->user)); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "type", object->type); + appendToJson(result, "offset", object->offset); + appendToJson(result, "length", object->length); + appendToJson(result, "url", object->url); + appendToJson(result, "user", parseUser(object->user)); + removeLastComma(result); + result += '}'; + return result; } Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { - auto result(make_shared<Message>()); - result->messageId = data.get<int32_t>("message_id"); - result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->date = data.get<int32_t>("date"); - result->chat = parseJsonAndGetChat(data.find("chat")->second); - result->forwardFrom = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); - result->forwardFromChat = tryParseJson<Chat>(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat"); - result->forwardFromMessageId = data.get<int32_t>("forward_from_message_id", 0); - result->forwardSignature = data.get("forward_signature", ""); - result->forwardDate = data.get("forward_date", 0); - result->replyToMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); - result->editDate = data.get<int32_t>("edit_date", 0); - result->authorSignature = data.get("author_signature", ""); - result->text = data.get("text", ""); - result->entities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); - result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); - result->audio = tryParseJson<Audio>(&TgTypeParser::parseJsonAndGetAudio, data, "audio"); - result->document = tryParseJson<Document>(&TgTypeParser::parseJsonAndGetDocument, data, "document"); - result->game = tryParseJson<Game>(&TgTypeParser::parseJsonAndGetGame, data, "game"); - result->photo = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photo"); - result->sticker = tryParseJson<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "sticker"); - result->video = tryParseJson<Video>(&TgTypeParser::parseJsonAndGetVideo, data, "video"); - result->contact = tryParseJson<Contact>(&TgTypeParser::parseJsonAndGetContact, data, "contact"); - result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); - result->newChatMember = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_participant"); - result->newChatMembers = parseJsonAndGetArray<User>(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_members"); - result->leftChatMember = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "left_chat_participant"); - result->newChatTitle = data.get("new_chat_title", ""); - result->newChatPhoto = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "new_chat_photo"); - result->deleteChatPhoto = data.get("delete_chat_photo", false); - result->groupChatCreated = data.get("group_chat_created", false); - result->caption = data.get("caption", ""); - result->supergroupChatCreated = data.get("supergroup_chat_created", false); - result->channelChatCreated = data.get("channel_chat_created", false); - result->migrateToChatId = data.get<int64_t>("migrate_to_chat_id", 0); - result->migrateFromChatId = data.get<int64_t>("migrate_from_chat_id", 0); - result->pinnedMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); - result->invoice = tryParseJson<Invoice>(&TgTypeParser::parseJsonAndGetInvoice, data, "invoice"); - result->successfulPayment = tryParseJson<SuccessfulPayment>(&TgTypeParser::parseJsonAndGetSuccessfulPayment, data, "successful_payment"); - result->connectedWebsite = data.get("connected_website", ""); - return result; + auto result(make_shared<Message>()); + result->messageId = data.get<int32_t>("message_id"); + result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->date = data.get<int32_t>("date"); + result->chat = parseJsonAndGetChat(data.find("chat")->second); + result->forwardFrom = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); + result->forwardFromChat = tryParseJson<Chat>(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat"); + result->forwardFromMessageId = data.get<int32_t>("forward_from_message_id", 0); + result->forwardSignature = data.get("forward_signature", ""); + result->forwardDate = data.get("forward_date", 0); + result->replyToMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); + result->editDate = data.get<int32_t>("edit_date", 0); + result->authorSignature = data.get("author_signature", ""); + result->text = data.get("text", ""); + result->entities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); + result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); + result->audio = tryParseJson<Audio>(&TgTypeParser::parseJsonAndGetAudio, data, "audio"); + result->document = tryParseJson<Document>(&TgTypeParser::parseJsonAndGetDocument, data, "document"); + result->game = tryParseJson<Game>(&TgTypeParser::parseJsonAndGetGame, data, "game"); + result->photo = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photo"); + result->sticker = tryParseJson<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "sticker"); + result->video = tryParseJson<Video>(&TgTypeParser::parseJsonAndGetVideo, data, "video"); + result->contact = tryParseJson<Contact>(&TgTypeParser::parseJsonAndGetContact, data, "contact"); + result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); + result->newChatMember = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_participant"); + result->newChatMembers = parseJsonAndGetArray<User>(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_members"); + result->leftChatMember = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "left_chat_participant"); + result->newChatTitle = data.get("new_chat_title", ""); + result->newChatPhoto = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "new_chat_photo"); + result->deleteChatPhoto = data.get("delete_chat_photo", false); + result->groupChatCreated = data.get("group_chat_created", false); + result->caption = data.get("caption", ""); + result->supergroupChatCreated = data.get("supergroup_chat_created", false); + result->channelChatCreated = data.get("channel_chat_created", false); + result->migrateToChatId = data.get<int64_t>("migrate_to_chat_id", 0); + result->migrateFromChatId = data.get<int64_t>("migrate_from_chat_id", 0); + result->pinnedMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); + result->invoice = tryParseJson<Invoice>(&TgTypeParser::parseJsonAndGetInvoice, data, "invoice"); + result->successfulPayment = tryParseJson<SuccessfulPayment>(&TgTypeParser::parseJsonAndGetSuccessfulPayment, data, "successful_payment"); + result->connectedWebsite = data.get("connected_website", ""); + return result; } string TgTypeParser::parseMessage(const Message::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "message_id", object->messageId); - appendToJson(result, "from", parseUser(object->from)); - appendToJson(result, "date", object->date); - appendToJson(result, "chat", parseChat(object->chat)); - appendToJson(result, "forward_from", parseUser(object->forwardFrom)); - appendToJson(result, "forward_from_chat", parseChat(object->forwardFromChat)); - appendToJson(result, "forward_from_message_id", object->forwardFromMessageId); - appendToJson(result, "forward_signature", object->forwardSignature); - appendToJson(result, "forward_date", object->forwardDate); - appendToJson(result, "reply_to_message", parseMessage(object->replyToMessage)); - appendToJson(result, "edit_date", object->editDate); - appendToJson(result, "author_signature", object->authorSignature); - appendToJson(result, "text", object->text); - appendToJson(result, "audio", parseAudio(object->audio)); - appendToJson(result, "document", parseDocument(object->document)); - appendToJson(result, "photo", parseArray(&TgTypeParser::parsePhotoSize, object->photo)); - appendToJson(result, "sticker", parseSticker(object->sticker)); - appendToJson(result, "video", parseVideo(object->video)); - appendToJson(result, "contact", parseContact(object->contact)); - appendToJson(result, "location", parseLocation(object->location)); - appendToJson(result, "new_chat_member", parseUser(object->newChatMember)); - appendToJson(result, "new_chat_members", parseArray(&TgTypeParser::parseUser, object->newChatMembers)); - appendToJson(result, "left_chat_member", parseUser(object->leftChatMember)); - appendToJson(result, "new_chat_title", object->newChatTitle); - appendToJson(result, "new_chat_photo", parseArray(&TgTypeParser::parsePhotoSize, object->newChatPhoto)); - appendToJson(result, "delete_chat_photo", object->deleteChatPhoto); - appendToJson(result, "group_chat_created", object->groupChatCreated); - appendToJson(result, "caption", object->caption); - appendToJson(result, "supergroup_chat_created", object->supergroupChatCreated); - appendToJson(result, "channel_chat_created", object->channelChatCreated); - appendToJson(result, "migrate_to_chat_id", object->migrateToChatId); - appendToJson(result, "migrate_from_chat_id", object->migrateFromChatId); - appendToJson(result, "pinned_message", parseMessage(object->pinnedMessage)); - appendToJson(result, "connected_website", object->connectedWebsite); - appendToJson(result, "invoice", parseInvoice(object->invoice)); - appendToJson(result, "successful_payment", parseSuccessfulPayment(object->successfulPayment)); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "message_id", object->messageId); + appendToJson(result, "from", parseUser(object->from)); + appendToJson(result, "date", object->date); + appendToJson(result, "chat", parseChat(object->chat)); + appendToJson(result, "forward_from", parseUser(object->forwardFrom)); + appendToJson(result, "forward_from_chat", parseChat(object->forwardFromChat)); + appendToJson(result, "forward_from_message_id", object->forwardFromMessageId); + appendToJson(result, "forward_signature", object->forwardSignature); + appendToJson(result, "forward_date", object->forwardDate); + appendToJson(result, "reply_to_message", parseMessage(object->replyToMessage)); + appendToJson(result, "edit_date", object->editDate); + appendToJson(result, "author_signature", object->authorSignature); + appendToJson(result, "text", object->text); + appendToJson(result, "audio", parseAudio(object->audio)); + appendToJson(result, "document", parseDocument(object->document)); + appendToJson(result, "photo", parseArray(&TgTypeParser::parsePhotoSize, object->photo)); + appendToJson(result, "sticker", parseSticker(object->sticker)); + appendToJson(result, "video", parseVideo(object->video)); + appendToJson(result, "contact", parseContact(object->contact)); + appendToJson(result, "location", parseLocation(object->location)); + appendToJson(result, "new_chat_member", parseUser(object->newChatMember)); + appendToJson(result, "new_chat_members", parseArray(&TgTypeParser::parseUser, object->newChatMembers)); + appendToJson(result, "left_chat_member", parseUser(object->leftChatMember)); + appendToJson(result, "new_chat_title", object->newChatTitle); + appendToJson(result, "new_chat_photo", parseArray(&TgTypeParser::parsePhotoSize, object->newChatPhoto)); + appendToJson(result, "delete_chat_photo", object->deleteChatPhoto); + appendToJson(result, "group_chat_created", object->groupChatCreated); + appendToJson(result, "caption", object->caption); + appendToJson(result, "supergroup_chat_created", object->supergroupChatCreated); + appendToJson(result, "channel_chat_created", object->channelChatCreated); + appendToJson(result, "migrate_to_chat_id", object->migrateToChatId); + appendToJson(result, "migrate_from_chat_id", object->migrateFromChatId); + appendToJson(result, "pinned_message", parseMessage(object->pinnedMessage)); + appendToJson(result, "connected_website", object->connectedWebsite); + appendToJson(result, "invoice", parseInvoice(object->invoice)); + appendToJson(result, "successful_payment", parseSuccessfulPayment(object->successfulPayment)); + removeLastComma(result); + result += '}'; + return result; } PhotoSize::Ptr TgTypeParser::parseJsonAndGetPhotoSize(const ptree& data) const { - auto result(make_shared<PhotoSize>()); - result->fileId = data.get<string>("file_id"); - result->width = data.get<int32_t>("width"); - result->height = data.get<int32_t>("height"); - result->fileSize = data.get("file_size", 0); - return result; + auto result(make_shared<PhotoSize>()); + result->fileId = data.get<string>("file_id"); + result->width = data.get<int32_t>("width"); + result->height = data.get<int32_t>("height"); + result->fileSize = data.get("file_size", 0); + return result; } string TgTypeParser::parsePhotoSize(const PhotoSize::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - appendToJson(result, "file_size", object->fileSize); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); + appendToJson(result, "file_size", object->fileSize); + removeLastComma(result); + result += '}'; + return result; } Audio::Ptr TgTypeParser::parseJsonAndGetAudio(const ptree& data) const { - auto result(make_shared<Audio>()); - result->fileId = data.get<string>("file_id"); - result->duration = data.get<int32_t>("duration"); - result->performer = data.get<string>("performer", ""); - result->title = data.get<string>("title", ""); - result->mimeType = data.get("mime_type", ""); - result->fileSize = data.get("file_size", 0); - return result; + auto result(make_shared<Audio>()); + result->fileId = data.get<string>("file_id"); + result->duration = data.get<int32_t>("duration"); + result->performer = data.get<string>("performer", ""); + result->title = data.get<string>("title", ""); + result->mimeType = data.get("mime_type", ""); + result->fileSize = data.get("file_size", 0); + return result; } string TgTypeParser::parseAudio(const Audio::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "duration", object->duration); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "file_size", object->fileSize); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "duration", object->duration); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "file_size", object->fileSize); + removeLastComma(result); + result += '}'; + return result; } Document::Ptr TgTypeParser::parseJsonAndGetDocument(const ptree& data) const { - auto result(make_shared<Document>()); - result->fileId = data.get<string>("file_id"); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->fileName = data.get("file_name", ""); - result->mimeType = data.get("mime_type", ""); - result->fileSize = data.get("file_size", 0); - return result; + auto result(make_shared<Document>()); + result->fileId = data.get<string>("file_id"); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->fileName = data.get("file_name", ""); + result->mimeType = data.get("mime_type", ""); + result->fileSize = data.get("file_size", 0); + return result; } string TgTypeParser::parseDocument(const Document::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - appendToJson(result, "file_name", object->fileName); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "file_size", object->fileSize); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "file_name", object->fileName); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "file_size", object->fileSize); + removeLastComma(result); + result += '}'; + return result; } Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const ptree& data) const { - auto result(make_shared<Sticker>()); - result->fileId = data.get<string>("file_id"); - result->width = data.get<int32_t>("width"); - result->height = data.get<int32_t>("height"); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->emoji = data.get("emoji", ""); - result->setName = data.get("set_name", ""); - result->maskPosition = tryParseJson<MaskPosition>(&TgTypeParser::parseJsonAndGetMaskPosition, data, "mask_position"); - result->fileSize = data.get("file_size", 0); - return result; + auto result(make_shared<Sticker>()); + result->fileId = data.get<string>("file_id"); + result->width = data.get<int32_t>("width"); + result->height = data.get<int32_t>("height"); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->emoji = data.get("emoji", ""); + result->setName = data.get("set_name", ""); + result->maskPosition = tryParseJson<MaskPosition>(&TgTypeParser::parseJsonAndGetMaskPosition, data, "mask_position"); + result->fileSize = data.get("file_size", 0); + return result; } string TgTypeParser::parseSticker(const Sticker::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - appendToJson(result, "emoji", object->emoji); - appendToJson(result, "file_size", object->fileSize); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "emoji", object->emoji); + appendToJson(result, "file_size", object->fileSize); + removeLastComma(result); + result += '}'; + return result; } StickerSet::Ptr TgTypeParser::parseJsonAndGetStickerSet(const ptree& data) const { - auto result(make_shared<StickerSet>()); - result->name = data.get("name", ""); - result->title = data.get("title", ""); - result->containsMasks = data.get<bool>("contains_masks", false); - result->stickers = parseJsonAndGetArray<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "stickers"); - return result; + auto result(make_shared<StickerSet>()); + result->name = data.get("name", ""); + result->title = data.get("title", ""); + result->containsMasks = data.get<bool>("contains_masks", false); + result->stickers = parseJsonAndGetArray<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "stickers"); + return result; } string TgTypeParser::parseStickerSet(const StickerSet::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "name", object->name); - appendToJson(result, "title", object->title); - appendToJson(result, "contains_masks", object->containsMasks); - appendToJson(result, "stickers", parseArray(&TgTypeParser::parseSticker, object->stickers)); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "name", object->name); + appendToJson(result, "title", object->title); + appendToJson(result, "contains_masks", object->containsMasks); + appendToJson(result, "stickers", parseArray(&TgTypeParser::parseSticker, object->stickers)); + removeLastComma(result); + result += '}'; + return result; } MaskPosition::Ptr TgTypeParser::parseJsonAndGetMaskPosition(const ptree& data) const { - auto result(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; + auto result(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; } string TgTypeParser::parseMaskPosition(const MaskPosition::Ptr& object) const { - if (!object) { - return ""; - } - 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; + if (!object) { + return ""; + } + 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; } Video::Ptr TgTypeParser::parseJsonAndGetVideo(const ptree& data) const { - auto result(make_shared<Video>()); - result->fileId = data.get<string>("file_id"); - result->width = data.get<int32_t>("width"); - result->height = data.get<int32_t>("height"); - result->duration = data.get<int32_t>("duration"); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->mimeType = data.get("mime_type", ""); - result->fileSize = data.get("file_size", 0); - return result; + auto result(make_shared<Video>()); + result->fileId = data.get<string>("file_id"); + result->width = data.get<int32_t>("width"); + result->height = data.get<int32_t>("height"); + result->duration = data.get<int32_t>("duration"); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->mimeType = data.get("mime_type", ""); + result->fileSize = data.get("file_size", 0); + return result; } string TgTypeParser::parseVideo(const Video::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "width", object->width); - appendToJson(result, "height", object->height); - 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; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "width", object->width); + appendToJson(result, "height", object->height); + 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; } VideoNote::Ptr TgTypeParser::parseJsonAndGetVideoNote(const ptree& data) const { - auto result(make_shared<VideoNote>()); - result->fileId = data.get<string>("file_id"); - result->length = data.get<int32_t>("length"); - result->duration = data.get<int32_t>("duration"); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->fileSize = data.get("file_size", 0); - return result; + auto result(make_shared<VideoNote>()); + result->fileId = data.get<string>("file_id"); + result->length = data.get<int32_t>("length"); + result->duration = data.get<int32_t>("duration"); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->fileSize = data.get("file_size", 0); + return result; } string TgTypeParser::parseVideoNote(const VideoNote::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "length", object->length); - appendToJson(result, "duration", object->duration); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - appendToJson(result, "file_size", object->fileSize); - result += '}'; - result.erase(); - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "length", object->length); + appendToJson(result, "duration", object->duration); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "file_size", object->fileSize); + result += '}'; + result.erase(); + return result; } Game::Ptr TgTypeParser::parseJsonAndGetGame(const ptree& data) const { - auto result(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; + auto result(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; } string TgTypeParser::parseGame(const Game::Ptr& object) const { - if (!object) { - return ""; - } - 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; + if (!object) { + return ""; + } + 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; } GameHighScore::Ptr TgTypeParser::parseJsonAndGetGameHighScore(const ptree& data) const { - auto result(make_shared<GameHighScore>()); - result->position = data.get("position", ""); - result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->score = data.get<int32_t>("score", 0); - return result; + auto result(make_shared<GameHighScore>()); + result->position = data.get("position", ""); + result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->score = data.get<int32_t>("score", 0); + return result; } string TgTypeParser::parseGameHighScore(const GameHighScore::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "position", object->position); - appendToJson(result, "user", parseUser(object->user)); - appendToJson(result, "score", object->score); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "position", object->position); + appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "score", object->score); + removeLastComma(result); + result += '}'; + return result; } Animation::Ptr TgTypeParser::parseJsonAndGetAnimation(const ptree& data) const { - auto result(make_shared<Animation>()); - result->fileId = data.get("file_id", ""); - result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); - result->fileName = data.get("file_name", ""); - result->mimeType = data.get("mime_type", ""); - result->fileSize = data.get<int32_t>("file_size", 0); - return result; + auto result(make_shared<Animation>()); + result->fileId = data.get("file_id", ""); + result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->fileName = data.get("file_name", ""); + result->mimeType = data.get("mime_type", ""); + result->fileSize = data.get<int32_t>("file_size", 0); + return result; } string TgTypeParser::parseAnimation(const Animation::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "thumb", parsePhotoSize(object->thumb)); - appendToJson(result, "file_name", object->fileName); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "file_size", object->fileSize); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "thumb", parsePhotoSize(object->thumb)); + appendToJson(result, "file_name", object->fileName); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "file_size", object->fileSize); + removeLastComma(result); + result += '}'; + return result; } Contact::Ptr TgTypeParser::parseJsonAndGetContact(const ptree& data) const { - auto result(make_shared<Contact>()); - result->phoneNumber = data.get<string>("phone_number"); - result->firstName = data.get<string>("first_name"); - result->lastName = data.get("last_name", ""); - result->userId = data.get("user_id", ""); - return result; + auto result(make_shared<Contact>()); + result->phoneNumber = data.get<string>("phone_number"); + result->firstName = data.get<string>("first_name"); + result->lastName = data.get("last_name", ""); + result->userId = data.get("user_id", ""); + return result; } string TgTypeParser::parseContact(const Contact::Ptr& object) const { - if (!object) { - return ""; - } - 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); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + 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); + removeLastComma(result); + result += '}'; + return result; } Location::Ptr TgTypeParser::parseJsonAndGetLocation(const ptree& data) const { - auto result(make_shared<Location>()); - result->longitude = data.get<float>("longitude", 0); - result->latitude = data.get<float>("latitude", 0); - return result; + auto result(make_shared<Location>()); + result->longitude = data.get<float>("longitude", 0); + result->latitude = data.get<float>("latitude", 0); + return result; } string TgTypeParser::parseLocation(const Location::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "longitude", object->longitude); - appendToJson(result, "latitude", object->latitude); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "longitude", object->longitude); + appendToJson(result, "latitude", object->latitude); + removeLastComma(result); + result += '}'; + return result; } Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const ptree& data) const { - auto result(make_shared<Update>()); - result->updateId = data.get<int32_t>("update_id"); - 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"); - return result; + auto result(make_shared<Update>()); + result->updateId = data.get<int32_t>("update_id"); + 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"); + return result; } string TgTypeParser::parseUpdate(const Update::Ptr& object) const { - if (!object) { - return ""; - } - 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)); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + 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)); + removeLastComma(result); + result += '}'; + return result; } UserProfilePhotos::Ptr TgTypeParser::parseJsonAndGetUserProfilePhotos(const ptree& data) const { - auto result(make_shared<UserProfilePhotos>()); - result->totalCount = data.get<int32_t>("total_count"); - result->photos = parseJsonAndGet2DArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photos"); - return result; + auto result(make_shared<UserProfilePhotos>()); + result->totalCount = data.get<int32_t>("total_count"); + result->photos = parseJsonAndGet2DArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photos"); + return result; } string TgTypeParser::parseUserProfilePhotos(const UserProfilePhotos::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "total_count", object->totalCount); - appendToJson(result, "photos", parse2DArray(&TgTypeParser::parsePhotoSize, object->photos)); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "total_count", object->totalCount); + appendToJson(result, "photos", parse2DArray(&TgTypeParser::parsePhotoSize, object->photos)); + removeLastComma(result); + result += '}'; + return result; } InputMedia::Ptr TgTypeParser::parseJsonAndGetInputMedia(const ptree& data) const { - string type = data.get("type", ""); - if (type == "photo") { - auto result(make_shared<InputMediaPhoto>()); - result->media = data.get("media", ""); - result->caption = data.get("caption", ""); - result->parseMode = data.get("parse_mode", ""); - return result; - } - else if (type == "video") { - auto result(make_shared<InputMediaVideo>()); - result->media = data.get("media", ""); - result->caption = data.get("caption", ""); - result->parseMode = data.get("parse_mode", ""); - result->width = data.get<int32_t>("width", 0); - result->height = data.get<int32_t>("height", 0); - result->duration = data.get<int32_t>("duration", 0); - result->supportsStreaming = data.get<bool>("supports_streaming", false); - return result; - } - else { - return nullptr; - } + string type = data.get("type", ""); + if (type == "photo") { + auto result(make_shared<InputMediaPhoto>()); + result->media = data.get("media", ""); + result->caption = data.get("caption", ""); + result->parseMode = data.get("parse_mode", ""); + return result; + } + else if (type == "video") { + auto result(make_shared<InputMediaVideo>()); + result->media = data.get("media", ""); + result->caption = data.get("caption", ""); + result->parseMode = data.get("parse_mode", ""); + result->width = data.get<int32_t>("width", 0); + result->height = data.get<int32_t>("height", 0); + result->duration = data.get<int32_t>("duration", 0); + result->supportsStreaming = data.get<bool>("supports_streaming", false); + return result; + } + else { + return nullptr; + } } string TgTypeParser::parseInputMedia(const InputMedia::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - if (object->type == InputMedia::TYPE::PHOTO) { - appendToJson(result, "type", "photo"); - } - else { - appendToJson(result, "type", "video"); - } - appendToJson(result, "media", object->media); - appendToJson(result, "caption", object->caption); - appendToJson(result, "parse_mode", object->parseMode); - if (object->width) { - appendToJson(result, "width", object->width); - } - if (object->height) { - appendToJson(result, "height", object->height); - } - if (object->duration) { - appendToJson(result, "duration", object->duration); - } - if (object->supportsStreaming) { - appendToJson(result, "supports_streaming", object->supportsStreaming); - } - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + if (object->type == InputMedia::TYPE::PHOTO) { + appendToJson(result, "type", "photo"); + } + else { + appendToJson(result, "type", "video"); + } + appendToJson(result, "media", object->media); + appendToJson(result, "caption", object->caption); + appendToJson(result, "parse_mode", object->parseMode); + if (object->width) { + appendToJson(result, "width", object->width); + } + if (object->height) { + appendToJson(result, "height", object->height); + } + if (object->duration) { + appendToJson(result, "duration", object->duration); + } + if (object->supportsStreaming) { + appendToJson(result, "supports_streaming", object->supportsStreaming); + } + removeLastComma(result); + result += '}'; + return result; } File::Ptr TgTypeParser::parseJsonAndGetFile(const boost::property_tree::ptree& data) const { - auto result(make_shared<File>()); - result->fileId = data.get<string>("file_id"); - result->fileSize = data.get<int32_t>("file_size", 0); - result->filePath = data.get<string>("file_path", ""); - return result; + auto result(make_shared<File>()); + result->fileId = data.get<string>("file_id"); + result->fileSize = data.get<int32_t>("file_size", 0); + result->filePath = data.get<string>("file_path", ""); + return result; } string TgTypeParser::parseFile(const File::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "file_id", object->fileId); - appendToJson(result, "file_size", object->fileSize); - appendToJson(result, "file_path", object->filePath); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "file_id", object->fileId); + appendToJson(result, "file_size", object->fileSize); + appendToJson(result, "file_path", object->filePath); + removeLastComma(result); + result += '}'; + return result; } ReplyKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardMarkup(const boost::property_tree::ptree& data) const { - auto result(make_shared<ReplyKeyboardMarkup>()); - 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); - result->oneTimeKeyboard = data.get<bool>("one_time_keyboard", false); - result->selective = data.get<bool>("selective", false); - return result; + auto result(make_shared<ReplyKeyboardMarkup>()); + 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); + result->oneTimeKeyboard = data.get<bool>("one_time_keyboard", false); + result->selective = data.get<bool>("selective", false); + return result; } std::string TgTypeParser::parseReplyKeyboardMarkup(const ReplyKeyboardMarkup::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - result += R"("keyboard":[)"; - for (const auto& item : object->keyboard) { - result += '['; - for (const auto& innerItem : item) { - result += parseKeyboardButton(innerItem); - result += ','; - } - removeLastComma(result); - result += "],"; - } - if (!object->keyboard.empty()) - removeLastComma(result); - result += "],"; - appendToJson(result, "resize_keyboard", object->resizeKeyboard); - appendToJson(result, "one_time_keyboard", object->oneTimeKeyboard); - appendToJson(result, "selective", object->selective); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + result += R"("keyboard":[)"; + for (const auto& item : object->keyboard) { + result += '['; + for (const auto& innerItem : item) { + result += parseKeyboardButton(innerItem); + result += ','; + } + removeLastComma(result); + result += "],"; + } + if (!object->keyboard.empty()) + removeLastComma(result); + result += "],"; + appendToJson(result, "resize_keyboard", object->resizeKeyboard); + appendToJson(result, "one_time_keyboard", object->oneTimeKeyboard); + appendToJson(result, "selective", object->selective); + removeLastComma(result); + result += '}'; + return result; } KeyboardButton::Ptr TgTypeParser::parseJsonAndGetKeyboardButton(const boost::property_tree::ptree& data) const { - auto result(make_shared<KeyboardButton>()); - result->text = data.get<string>("text"); - result->requestContact = data.get<bool>("request_contact", false); - result->requestLocation = data.get<bool>("request_location", false); - return result; + auto result(make_shared<KeyboardButton>()); + result->text = data.get<string>("text"); + result->requestContact = data.get<bool>("request_contact", false); + result->requestLocation = data.get<bool>("request_location", false); + return result; } std::string TgTypeParser::parseKeyboardButton(const KeyboardButton::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "text", object->text); - appendToJson(result, "request_contact", object->requestContact); - appendToJson(result, "request_location", object->requestLocation); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "text", object->text); + appendToJson(result, "request_contact", object->requestContact); + appendToJson(result, "request_location", object->requestLocation); + removeLastComma(result); + result += '}'; + return result; } ReplyKeyboardRemove::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardRemove(const boost::property_tree::ptree& data) const { - auto result(make_shared<ReplyKeyboardRemove>()); - result->selective = data.get<bool>("selective", false); - return result; + auto result(make_shared<ReplyKeyboardRemove>()); + result->selective = data.get<bool>("selective", false); + return result; } std::string TgTypeParser::parseReplyKeyboardRemove(const ReplyKeyboardRemove::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "remove_keyboard", object->removeKeyboard); - appendToJson(result, "selective", object->selective); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "remove_keyboard", object->removeKeyboard); + appendToJson(result, "selective", object->selective); + removeLastComma(result); + result += '}'; + return result; } ForceReply::Ptr TgTypeParser::parseJsonAndGetForceReply(const boost::property_tree::ptree& data) const { - auto result(make_shared<ForceReply>()); - result->selective = data.get<bool>("selective"); - return result; + auto result(make_shared<ForceReply>()); + result->selective = data.get<bool>("selective"); + return result; } std::string TgTypeParser::parseForceReply(const ForceReply::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "force_reply", object->forceReply); - appendToJson(result, "selective", object->selective); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "force_reply", object->forceReply); + appendToJson(result, "selective", object->selective); + removeLastComma(result); + result += '}'; + return result; } ChatMember::Ptr TgTypeParser::parseJsonAndGetChatMember(const boost::property_tree::ptree& data) const { - auto result(make_shared<ChatMember>()); - result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->status = data.get("status", ""); - result->untilDate = data.get<uint64_t>("until_date", 0); - result->canBeEdited = data.get<bool>("can_be_edited", false); - result->canChangeInfo = data.get<bool>("can_change_info", false); - result->canPostMessages = data.get<bool>("can_post_messages", false); - result->canEditMessages = data.get<bool>("can_edit_messages", false); - result->canDeleteMessages = data.get<bool>("can_delete_messages", false); - result->canInviteUsers = data.get<bool>("can_invite_users", false); - result->canRestrictMembers = data.get<bool>("can_restrict_members", false); - result->canPinMessages = data.get<bool>("can_pin_messages", false); - result->canPromoteMembers = data.get<bool>("can_promote_messages", false); - result->canSendMessages = data.get<bool>("can_send_messages", false); - result->canSendMediaMessages = data.get<bool>("can_send_media_messages", false); - result->canSendOtherMessages = data.get<bool>("can_send_other_messages", false); - result->canAddWebPagePreviews = data.get<bool>("can_add_web_page_previews", false); - return result; + auto result(make_shared<ChatMember>()); + result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->status = data.get("status", ""); + result->untilDate = data.get<uint64_t>("until_date", 0); + result->canBeEdited = data.get<bool>("can_be_edited", false); + result->canChangeInfo = data.get<bool>("can_change_info", false); + result->canPostMessages = data.get<bool>("can_post_messages", false); + result->canEditMessages = data.get<bool>("can_edit_messages", false); + result->canDeleteMessages = data.get<bool>("can_delete_messages", false); + result->canInviteUsers = data.get<bool>("can_invite_users", false); + result->canRestrictMembers = data.get<bool>("can_restrict_members", false); + result->canPinMessages = data.get<bool>("can_pin_messages", false); + result->canPromoteMembers = data.get<bool>("can_promote_messages", false); + result->canSendMessages = data.get<bool>("can_send_messages", false); + result->canSendMediaMessages = data.get<bool>("can_send_media_messages", false); + result->canSendOtherMessages = data.get<bool>("can_send_other_messages", false); + result->canAddWebPagePreviews = data.get<bool>("can_add_web_page_previews", false); + return result; } std::string TgTypeParser::parseChatMember(const ChatMember::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "user", parseUser(object->user)); - appendToJson(result, "status", object->status); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "status", object->status); + removeLastComma(result); + result += '}'; + return result; } ChatPhoto::Ptr TgTypeParser::parseJsonAndGetChatPhoto(const boost::property_tree::ptree& data) const { - auto result(make_shared<ChatPhoto>()); - result->smallFileId = data.get("small_file_id", ""); - result->bigFileId = data.get("big_file_id", ""); - return result; + auto result(make_shared<ChatPhoto>()); + result->smallFileId = data.get("small_file_id", ""); + result->bigFileId = data.get("big_file_id", ""); + return result; } std::string TgTypeParser::parseChatPhoto(const ChatPhoto::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "small_file_id", object->smallFileId); - appendToJson(result, "big_file_id", object->bigFileId); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "small_file_id", object->smallFileId); + appendToJson(result, "big_file_id", object->bigFileId); + removeLastComma(result); + result += '}'; + return result; } ResponseParameters::Ptr TgTypeParser::parseJsonAndGetResponseParameters(const boost::property_tree::ptree& data) const { - auto result(make_shared<ResponseParameters>()); - result->migrateToChatId = data.get<int32_t>("migrate_to_chat_id", 0); - result->retryAfter = data.get<int32_t>("retry_after", 0); - return result; + auto result(make_shared<ResponseParameters>()); + result->migrateToChatId = data.get<int32_t>("migrate_to_chat_id", 0); + result->retryAfter = data.get<int32_t>("retry_after", 0); + return result; } std::string TgTypeParser::parseResponseParameters(const ResponseParameters::Ptr& object) const { - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "migrate_to_chat_id", object->migrateToChatId); - appendToJson(result, "retry_after", object->retryAfter); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "migrate_to_chat_id", object->migrateToChatId); + appendToJson(result, "retry_after", object->retryAfter); + 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 static_pointer_cast<GenericReply>(parseJsonAndGetForceReply(data)); - } else if (data.find("remove_keyboard") != data.not_found()) { - return static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardRemove(data)); - } else if (data.find("keyboard") != data.not_found()) { - return static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardMarkup(data)); - } else if (data.find("inline_keyboard") != data.not_found()) { - return static_pointer_cast<GenericReply>(parseJsonAndGetInlineKeyboardMarkup(data)); - } - return make_shared<GenericReply>(); + if (data.find("force_reply") != data.not_found()) { + return static_pointer_cast<GenericReply>(parseJsonAndGetForceReply(data)); + } else if (data.find("remove_keyboard") != data.not_found()) { + return static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardRemove(data)); + } else if (data.find("keyboard") != data.not_found()) { + return static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardMarkup(data)); + } else if (data.find("inline_keyboard") != data.not_found()) { + return static_pointer_cast<GenericReply>(parseJsonAndGetInlineKeyboardMarkup(data)); + } + return make_shared<GenericReply>(); } std::string TgTypeParser::parseGenericReply(const GenericReply::Ptr& object) const { - if (!object) { - return ""; - } - if (dynamic_pointer_cast<ForceReply>(object) != nullptr) { - return parseForceReply(static_pointer_cast<ForceReply>(object)); - } else if (dynamic_pointer_cast<ReplyKeyboardRemove>(object) != nullptr) { - return parseReplyKeyboardRemove(static_pointer_cast<ReplyKeyboardRemove>(object)); - } else if (dynamic_pointer_cast<ReplyKeyboardMarkup>(object) != nullptr){ - return parseReplyKeyboardMarkup(static_pointer_cast<ReplyKeyboardMarkup>(object)); - } else if (dynamic_pointer_cast<InlineKeyboardMarkup>(object) != nullptr){ - return parseInlineKeyboardMarkup(static_pointer_cast<InlineKeyboardMarkup>(object)); - } - return ""; + if (!object) { + return ""; + } + if (dynamic_pointer_cast<ForceReply>(object) != nullptr) { + return parseForceReply(static_pointer_cast<ForceReply>(object)); + } else if (dynamic_pointer_cast<ReplyKeyboardRemove>(object) != nullptr) { + return parseReplyKeyboardRemove(static_pointer_cast<ReplyKeyboardRemove>(object)); + } else if (dynamic_pointer_cast<ReplyKeyboardMarkup>(object) != nullptr){ + return parseReplyKeyboardMarkup(static_pointer_cast<ReplyKeyboardMarkup>(object)); + } else if (dynamic_pointer_cast<InlineKeyboardMarkup>(object) != nullptr){ + return parseInlineKeyboardMarkup(static_pointer_cast<InlineKeyboardMarkup>(object)); + } + return ""; } InlineQuery::Ptr TgTypeParser::parseJsonAndGetInlineQuery(const boost::property_tree::ptree& data) const { - auto result(make_shared<InlineQuery>()); - result->id = data.get<string>("id"); - result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); - result->query = data.get<string>("query"); - result->offset = data.get<string>("offset"); + auto result(make_shared<InlineQuery>()); + result->id = data.get<string>("id"); + result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); + result->query = data.get<string>("query"); + result->offset = data.get<string>("offset"); - return result; + return result; } std::string TgTypeParser::parseInlineQuery(const InlineQuery::Ptr& object) const{ - if (!object) { - return ""; - } - string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "from", parseUser(object->from)); - appendToJson(result, "location", parseLocation(object->location)); - appendToJson(result, "query", object->query); - appendToJson(result, "offset", object->offset); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "from", parseUser(object->from)); + appendToJson(result, "location", parseLocation(object->location)); + appendToJson(result, "query", object->query); + appendToJson(result, "offset", object->offset); + removeLastComma(result); + result += '}'; + return result; } InlineQueryResult::Ptr TgTypeParser::parseJsonAndGetInlineQueryResult(const boost::property_tree::ptree& data) const { - string type = data.get<string>("type"); - InlineQueryResult::Ptr result; - - if (type == InlineQueryResultCachedAudio::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedAudio(data)); - } else if (type == InlineQueryResultCachedDocument::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedDocument(data)); - } else if (type == InlineQueryResultCachedGif::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedGif(data)); - } else if (type == InlineQueryResultCachedMpeg4Gif::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedMpeg4Gif(data)); - } else if (type == InlineQueryResultCachedPhoto::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedPhoto(data)); - } else if (type == InlineQueryResultCachedSticker::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedSticker(data)); - } else if (type == InlineQueryResultCachedVideo::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedVideo(data)); - } else if (type == InlineQueryResultCachedVoice::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedVoice(data)); - } else if (type == InlineQueryResultArticle::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultArticle(data)); - } else if (type == InlineQueryResultAudio::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultAudio(data)); - } else if (type == InlineQueryResultContact::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultContact(data)); - } else if (type == InlineQueryResultGame::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultGame(data)); - } else if (type == InlineQueryResultDocument::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultDocument(data)); - } else if (type == InlineQueryResultLocation::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultLocation(data)); - } else if (type == InlineQueryResultVenue::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultVenue(data)); - } else if (type == InlineQueryResultVoice::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultVoice(data)); - } else if (type == InlineQueryResultPhoto::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultPhoto(data)); - } else if (type == InlineQueryResultGif::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultGif(data)); - } else if (type == InlineQueryResultMpeg4Gif::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultMpeg4Gif(data)); - } else if (type == InlineQueryResultVideo::TYPE) { - result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultVideo(data)); - } else { - result = make_shared<InlineQueryResult>(); - } - - result->id = data.get<string>("id"); - result->title = data.get<string>("title", ""); - result->caption = data.get<string>("caption", ""); - result->replyMarkup = tryParseJson<InlineKeyboardMarkup>(&TgTypeParser::parseJsonAndGetInlineKeyboardMarkup, data, "reply_markup"); - result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); - - return result; + string type = data.get<string>("type"); + InlineQueryResult::Ptr result; + + if (type == InlineQueryResultCachedAudio::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedAudio(data)); + } else if (type == InlineQueryResultCachedDocument::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedDocument(data)); + } else if (type == InlineQueryResultCachedGif::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedGif(data)); + } else if (type == InlineQueryResultCachedMpeg4Gif::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedMpeg4Gif(data)); + } else if (type == InlineQueryResultCachedPhoto::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedPhoto(data)); + } else if (type == InlineQueryResultCachedSticker::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedSticker(data)); + } else if (type == InlineQueryResultCachedVideo::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedVideo(data)); + } else if (type == InlineQueryResultCachedVoice::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultCachedVoice(data)); + } else if (type == InlineQueryResultArticle::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultArticle(data)); + } else if (type == InlineQueryResultAudio::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultAudio(data)); + } else if (type == InlineQueryResultContact::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultContact(data)); + } else if (type == InlineQueryResultGame::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultGame(data)); + } else if (type == InlineQueryResultDocument::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultDocument(data)); + } else if (type == InlineQueryResultLocation::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultLocation(data)); + } else if (type == InlineQueryResultVenue::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultVenue(data)); + } else if (type == InlineQueryResultVoice::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultVoice(data)); + } else if (type == InlineQueryResultPhoto::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultPhoto(data)); + } else if (type == InlineQueryResultGif::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultGif(data)); + } else if (type == InlineQueryResultMpeg4Gif::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultMpeg4Gif(data)); + } else if (type == InlineQueryResultVideo::TYPE) { + result = static_pointer_cast<InlineQueryResult>(parseJsonAndGetInlineQueryResultVideo(data)); + } else { + result = make_shared<InlineQueryResult>(); + } + + result->id = data.get<string>("id"); + result->title = data.get<string>("title", ""); + result->caption = data.get<string>("caption", ""); + result->replyMarkup = tryParseJson<InlineKeyboardMarkup>(&TgTypeParser::parseJsonAndGetInlineKeyboardMarkup, data, "reply_markup"); + result->inputMessageContent = tryParseJson<InputMessageContent>(&TgTypeParser::parseJsonAndGetInputMessageContent, data, "input_message_content"); + + return result; } std::string TgTypeParser::parseInlineQueryResult(const InlineQueryResult::Ptr& object) const { - if (!object){ - return ""; - } - - string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "type", object->type); - appendToJson(result, "title", object->title); - appendToJson(result, "caption", object->caption); - appendToJson(result, "reply_markup", parseInlineKeyboardMarkup(object->replyMarkup)); - appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); - - if (object->type == InlineQueryResultCachedAudio::TYPE) { - result += parseInlineQueryResultCachedAudio(static_pointer_cast<InlineQueryResultCachedAudio>(object)); - } - else if (object->type == InlineQueryResultCachedDocument::TYPE) { - result += parseInlineQueryResultCachedDocument(static_pointer_cast<InlineQueryResultCachedDocument>(object)); - } - else if (object->type == InlineQueryResultCachedGif::TYPE) { - result += parseInlineQueryResultCachedGif(static_pointer_cast<InlineQueryResultCachedGif>(object)); - } - else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) { - result += parseInlineQueryResultCachedMpeg4Gif(static_pointer_cast<InlineQueryResultCachedMpeg4Gif>(object)); - } - else if (object->type == InlineQueryResultCachedPhoto::TYPE) { - result += parseInlineQueryResultCachedPhoto(static_pointer_cast<InlineQueryResultCachedPhoto>(object)); - } - else if (object->type == InlineQueryResultCachedSticker::TYPE) { - result += parseInlineQueryResultCachedSticker(static_pointer_cast<InlineQueryResultCachedSticker>(object)); - } - else if (object->type == InlineQueryResultCachedVideo::TYPE) { - result += parseInlineQueryResultCachedVideo(static_pointer_cast<InlineQueryResultCachedVideo>(object)); - } - else if (object->type == InlineQueryResultCachedVoice::TYPE) { - result += parseInlineQueryResultCachedVoice(static_pointer_cast<InlineQueryResultCachedVoice>(object)); - } - else if (object->type == InlineQueryResultArticle::TYPE) { - result += parseInlineQueryResultArticle(static_pointer_cast<InlineQueryResultArticle>(object)); - } - else if (object->type == InlineQueryResultAudio::TYPE) { - result += parseInlineQueryResultAudio(static_pointer_cast<InlineQueryResultAudio>(object)); - } - else if (object->type == InlineQueryResultContact::TYPE) { - result += parseInlineQueryResultContact(static_pointer_cast<InlineQueryResultContact>(object)); - } - else if (object->type == InlineQueryResultGame::TYPE) { - result += parseInlineQueryResultGame(static_pointer_cast<InlineQueryResultGame>(object)); - } - else if (object->type == InlineQueryResultDocument::TYPE) { - result += parseInlineQueryResultDocument(static_pointer_cast<InlineQueryResultDocument>(object)); - } - else if (object->type == InlineQueryResultLocation::TYPE) { - result += parseInlineQueryResultLocation(static_pointer_cast<InlineQueryResultLocation>(object)); - } - else if (object->type == InlineQueryResultVenue::TYPE) { - result += parseInlineQueryResultVenue(static_pointer_cast<InlineQueryResultVenue>(object)); - } - else if (object->type == InlineQueryResultVoice::TYPE) { - result += parseInlineQueryResultVoice(static_pointer_cast<InlineQueryResultVoice>(object)); - } - else if (object->type == InlineQueryResultPhoto::TYPE) { - result += parseInlineQueryResultPhoto(static_pointer_cast<InlineQueryResultPhoto>(object)); - } - else if (object->type == InlineQueryResultGif::TYPE) { - result += parseInlineQueryResultGif(static_pointer_cast<InlineQueryResultGif>(object)); - } - else if (object->type == InlineQueryResultMpeg4Gif::TYPE) { - result += parseInlineQueryResultMpeg4Gif(static_pointer_cast<InlineQueryResultMpeg4Gif>(object)); - } - else if (object->type == InlineQueryResultVideo::TYPE) { - result += parseInlineQueryResultVideo(static_pointer_cast<InlineQueryResultVideo>(object)); - } - - removeLastComma(result); - result += '}'; - return result; + if (!object){ + return ""; + } + + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "type", object->type); + appendToJson(result, "title", object->title); + appendToJson(result, "caption", object->caption); + appendToJson(result, "reply_markup", parseInlineKeyboardMarkup(object->replyMarkup)); + appendToJson(result, "input_message_content", parseInputMessageContent(object->inputMessageContent)); + + if (object->type == InlineQueryResultCachedAudio::TYPE) { + result += parseInlineQueryResultCachedAudio(static_pointer_cast<InlineQueryResultCachedAudio>(object)); + } + else if (object->type == InlineQueryResultCachedDocument::TYPE) { + result += parseInlineQueryResultCachedDocument(static_pointer_cast<InlineQueryResultCachedDocument>(object)); + } + else if (object->type == InlineQueryResultCachedGif::TYPE) { + result += parseInlineQueryResultCachedGif(static_pointer_cast<InlineQueryResultCachedGif>(object)); + } + else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) { + result += parseInlineQueryResultCachedMpeg4Gif(static_pointer_cast<InlineQueryResultCachedMpeg4Gif>(object)); + } + else if (object->type == InlineQueryResultCachedPhoto::TYPE) { + result += parseInlineQueryResultCachedPhoto(static_pointer_cast<InlineQueryResultCachedPhoto>(object)); + } + else if (object->type == InlineQueryResultCachedSticker::TYPE) { + result += parseInlineQueryResultCachedSticker(static_pointer_cast<InlineQueryResultCachedSticker>(object)); + } + else if (object->type == InlineQueryResultCachedVideo::TYPE) { + result += parseInlineQueryResultCachedVideo(static_pointer_cast<InlineQueryResultCachedVideo>(object)); + } + else if (object->type == InlineQueryResultCachedVoice::TYPE) { + result += parseInlineQueryResultCachedVoice(static_pointer_cast<InlineQueryResultCachedVoice>(object)); + } + else if (object->type == InlineQueryResultArticle::TYPE) { + result += parseInlineQueryResultArticle(static_pointer_cast<InlineQueryResultArticle>(object)); + } + else if (object->type == InlineQueryResultAudio::TYPE) { + result += parseInlineQueryResultAudio(static_pointer_cast<InlineQueryResultAudio>(object)); + } + else if (object->type == InlineQueryResultContact::TYPE) { + result += parseInlineQueryResultContact(static_pointer_cast<InlineQueryResultContact>(object)); + } + else if (object->type == InlineQueryResultGame::TYPE) { + result += parseInlineQueryResultGame(static_pointer_cast<InlineQueryResultGame>(object)); + } + else if (object->type == InlineQueryResultDocument::TYPE) { + result += parseInlineQueryResultDocument(static_pointer_cast<InlineQueryResultDocument>(object)); + } + else if (object->type == InlineQueryResultLocation::TYPE) { + result += parseInlineQueryResultLocation(static_pointer_cast<InlineQueryResultLocation>(object)); + } + else if (object->type == InlineQueryResultVenue::TYPE) { + result += parseInlineQueryResultVenue(static_pointer_cast<InlineQueryResultVenue>(object)); + } + else if (object->type == InlineQueryResultVoice::TYPE) { + result += parseInlineQueryResultVoice(static_pointer_cast<InlineQueryResultVoice>(object)); + } + else if (object->type == InlineQueryResultPhoto::TYPE) { + result += parseInlineQueryResultPhoto(static_pointer_cast<InlineQueryResultPhoto>(object)); + } + else if (object->type == InlineQueryResultGif::TYPE) { + result += parseInlineQueryResultGif(static_pointer_cast<InlineQueryResultGif>(object)); + } + else if (object->type == InlineQueryResultMpeg4Gif::TYPE) { + result += parseInlineQueryResultMpeg4Gif(static_pointer_cast<InlineQueryResultMpeg4Gif>(object)); + } + else if (object->type == InlineQueryResultVideo::TYPE) { + result += parseInlineQueryResultVideo(static_pointer_cast<InlineQueryResultVideo>(object)); + } + + removeLastComma(result); + result += '}'; + return result; } InlineQueryResultCachedAudio::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedAudio(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedAudio>()); - result->audioFileId = data.get<string>("audio_file_id"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedAudio>()); + result->audioFileId = data.get<string>("audio_file_id"); + return result; } 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 string. - string result; - appendToJson(result, "audio_file_id", object->audioFileId); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "audio_file_id", object->audioFileId); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedDocument(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedDocument>()); - result->documentFileId = data.get<string>("document_file_id"); - result->description = data.get<string>("description", ""); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedDocument>()); + result->documentFileId = data.get<string>("document_file_id"); + result->description = data.get<string>("description", ""); + return result; } 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 string. - string result; - appendToJson(result, "document_file_id", object->documentFileId); - appendToJson(result, "description", object->description); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "document_file_id", object->documentFileId); + appendToJson(result, "description", object->description); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedGif(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedGif>()); - result->gifFileId = data.get<string>("gif_file_id"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedGif>()); + result->gifFileId = data.get<string>("gif_file_id"); + return result; } 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 string. - string result; - appendToJson(result, "gif_file_id", object->gifFileId); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "gif_file_id", object->gifFileId); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedMpeg4Gif(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedMpeg4Gif>()); - result->mpeg4FileId = data.get<string>("mpeg4_file_id"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedMpeg4Gif>()); + result->mpeg4FileId = data.get<string>("mpeg4_file_id"); + return result; } 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 string. - string result; - appendToJson(result, "mpeg4_file_id", object->mpeg4FileId); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "mpeg4_file_id", object->mpeg4FileId); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedPhoto(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedPhoto>()); - result->photoFileId = data.get<string>("photo_file_id"); - result->description = data.get<string>("description", ""); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedPhoto>()); + result->photoFileId = data.get<string>("photo_file_id"); + result->description = data.get<string>("description", ""); + return result; } 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 string. - string result; - appendToJson(result, "photo_file_id", object->photoFileId); - appendToJson(result, "description", object->description); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "photo_file_id", object->photoFileId); + appendToJson(result, "description", object->description); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedSticker::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedSticker(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedSticker>()); - result->stickerFileId = data.get<string>("sticker_file_id"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedSticker>()); + result->stickerFileId = data.get<string>("sticker_file_id"); + return result; } 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 string. - string result; - appendToJson(result, "sticker_file_id", object->stickerFileId); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "sticker_file_id", object->stickerFileId); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedVideo(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedVideo>()); - result->videoFileId = data.get<string>("video_file_id"); - result->description = data.get<string>("description", ""); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedVideo>()); + result->videoFileId = data.get<string>("video_file_id"); + result->description = data.get<string>("description", ""); + return result; } 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 string. - string result; - appendToJson(result, "video_file_id", object->videoFileId); - appendToJson(result, "description", object->description); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "video_file_id", object->videoFileId); + appendToJson(result, "description", object->description); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultCachedVoice::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultCachedVoice(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultCachedVoice>()); - result->voiceFileId = data.get<string>("voice_file_id"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultCachedVoice>()); + result->voiceFileId = data.get<string>("voice_file_id"); + return result; } 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 string. - string result; - appendToJson(result, "voice_file_id", object->voiceFileId); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "voice_file_id", object->voiceFileId); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultArticle::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultArticle(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultArticle>()); - result->url = data.get<string>("url", ""); - result->hideUrl = data.get("hide_url", false); - result->description = data.get<string>("description", ""); - result->thumbUrl = data.get<string>("thumb_url", ""); - result->thumbWidth = data.get("thumb_width", 0); - result->thumbHeight = data.get("thumb_height", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultArticle>()); + result->url = data.get<string>("url", ""); + result->hideUrl = data.get("hide_url", false); + result->description = data.get<string>("description", ""); + result->thumbUrl = data.get<string>("thumb_url", ""); + result->thumbWidth = data.get("thumb_width", 0); + result->thumbHeight = data.get("thumb_height", 0); + return result; } 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 string. - string result; - 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; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + 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; } InlineQueryResultAudio::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultAudio(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultAudio>()); - result->audioUrl = data.get<string>("audio_url"); - result->performer = data.get<string>("performer", ""); - result->audioDuration = data.get<int32_t>("audio_duration", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultAudio>()); + result->audioUrl = data.get<string>("audio_url"); + result->performer = data.get<string>("performer", ""); + result->audioDuration = data.get<int32_t>("audio_duration", 0); + return result; } 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 string. - string result; - appendToJson(result, "audio_url", object->audioUrl); - appendToJson(result, "performer", object->performer); - appendToJson(result, "audio_duration", object->audioDuration); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "audio_url", object->audioUrl); + appendToJson(result, "performer", object->performer); + appendToJson(result, "audio_duration", object->audioDuration); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultContact::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultContact(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultContact>()); - result->phoneNumber = data.get<string>("phone_number"); - result->firstName = data.get<string>("first_name"); - result->lastName = data.get<string>("last_name", ""); - result->thumbUrl = data.get<string>("thumb_url", ""); - result->thumbWidth = data.get<int32_t>("thumb_width", 0); - result->thumbHeight = data.get<int32_t>("thumb_height", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultContact>()); + result->phoneNumber = data.get<string>("phone_number"); + result->firstName = data.get<string>("first_name"); + result->lastName = data.get<string>("last_name", ""); + result->thumbUrl = data.get<string>("thumb_url", ""); + result->thumbWidth = data.get<int32_t>("thumb_width", 0); + result->thumbHeight = data.get<int32_t>("thumb_height", 0); + return result; } std::string TgTypeParser::parseInlineQueryResultContact(const InlineQueryResultContact::Ptr& object) const { - if (!object){ - return " "; - } - // This function will be called by parseInlineQueryResult(), so I don't add - // curly brackets to the result string. - string result; - appendToJson(result, "phone_number", object->phoneNumber); - appendToJson(result, "first_name", object->firstName); - appendToJson(result, "last_name", object->lastName); - 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; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "phone_number", object->phoneNumber); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + 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; } InlineQueryResultGame::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGame(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultGame>()); - result->gameShortName = data.get<string>("game_short_name"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultGame>()); + result->gameShortName = data.get<string>("game_short_name"); + return result; } std::string TgTypeParser::parseInlineQueryResultGame(const InlineQueryResultGame::Ptr& object) const { - if (!object){ - return " "; - } - // This function will be called by parseInlineQueryResult(), so I don't add - // curly brackets to the result string. - string result; - appendToJson(result, "game_short_name", object->gameShortName); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "game_short_name", object->gameShortName); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultDocument::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultDocument(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultDocument>()); - result->documentUrl = data.get<string>("document_url"); - result->mimeType = data.get<string>("mime_type"); - result->description = data.get<string>("description", ""); - result->thumbUrl = data.get<string>("thumb_url", ""); - result->thumbWidth = data.get<int32_t>("thumb_width", 0); - result->thumbHeight = data.get<int32_t>("thumb_height", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultDocument>()); + result->documentUrl = data.get<string>("document_url"); + result->mimeType = data.get<string>("mime_type"); + result->description = data.get<string>("description", ""); + result->thumbUrl = data.get<string>("thumb_url", ""); + result->thumbWidth = data.get<int32_t>("thumb_width", 0); + result->thumbHeight = data.get<int32_t>("thumb_height", 0); + return result; } 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 string. - string result; - appendToJson(result, "document_url", object->documentUrl); - appendToJson(result, "mime_type", object->mimeType); - 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; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "document_url", object->documentUrl); + appendToJson(result, "mime_type", object->mimeType); + 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; } InlineQueryResultLocation::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultLocation(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultLocation>()); - result->latitude = data.get<float>("latitude"); - result->longitude = data.get<float>("longitude"); - result->thumbUrl = data.get<string>("thumb_url", ""); - result->thumbWidth = data.get<int32_t>("thumb_width", 0); - result->thumbHeight = data.get<int32_t>("thumb_height", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultLocation>()); + result->latitude = data.get<float>("latitude"); + result->longitude = data.get<float>("longitude"); + result->thumbUrl = data.get<string>("thumb_url", ""); + result->thumbWidth = data.get<int32_t>("thumb_width", 0); + result->thumbHeight = data.get<int32_t>("thumb_height", 0); + return result; } 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 string. - string result; - appendToJson(result, "latitude", object->latitude); - appendToJson(result, "longitude", object->longitude); - 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; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "latitude", object->latitude); + appendToJson(result, "longitude", object->longitude); + 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 { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultVenue>()); - result->latitude = data.get<float>("latitude"); - result->longitude = data.get<float>("longitude"); - result->address = data.get<string>("address"); - result->foursquareId = data.get<string>("foursquare_id", ""); - result->thumbUrl = data.get<string>("thumb_url", ""); - result->thumbWidth = data.get<int32_t>("thumb_width", 0); - result->thumbHeight = data.get<int32_t>("thumb_height", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultVenue>()); + result->latitude = data.get<float>("latitude"); + result->longitude = data.get<float>("longitude"); + result->address = data.get<string>("address"); + result->foursquareId = data.get<string>("foursquare_id", ""); + result->thumbUrl = data.get<string>("thumb_url", ""); + result->thumbWidth = data.get<int32_t>("thumb_width", 0); + result->thumbHeight = data.get<int32_t>("thumb_height", 0); + return result; } 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 string. - string result; - appendToJson(result, "latitude", object->latitude); - appendToJson(result, "longitude", object->longitude); - appendToJson(result, "address", object->address); - appendToJson(result, "foursquare_id", object->foursquareId); - 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; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "latitude", object->latitude); + appendToJson(result, "longitude", object->longitude); + appendToJson(result, "address", object->address); + appendToJson(result, "foursquare_id", object->foursquareId); + 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 { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultVoice>()); - result->voiceUrl = data.get<string>("voice_url"); - result->voiceDuration = data.get<int32_t>("voice_duration", 0); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultVoice>()); + result->voiceUrl = data.get<string>("voice_url"); + result->voiceDuration = data.get<int32_t>("voice_duration", 0); + return result; } 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 string. - string result; - appendToJson(result, "voice_url", object->voiceUrl); - appendToJson(result, "voice_duration", object->voiceDuration); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "voice_url", object->voiceUrl); + appendToJson(result, "voice_duration", object->voiceDuration); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultPhoto(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultPhoto>()); - result->photoUrl = data.get<string>("photo_url", ""); - result->thumbUrl = data.get<string>("thumb_url"); - result->photoWidth = data.get("photo_width", 0); - result->photoHeight = data.get("photo_height", 0); - result->description = data.get<string>("description", ""); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultPhoto>()); + result->photoUrl = data.get<string>("photo_url", ""); + result->thumbUrl = data.get<string>("thumb_url"); + result->photoWidth = data.get("photo_width", 0); + result->photoHeight = data.get("photo_height", 0); + result->description = data.get<string>("description", ""); + return result; } 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 string. - 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, "description", object->description); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + 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, "description", object->description); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGif(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultGif>()); - result->gifUrl = data.get<string>("gif_url", ""); - result->gifWidth = data.get("gif_width", 0); - result->gifHeight = data.get("gif_height", 0); - result->gifDuration = data.get("gif_duration", 0); - result->thumbUrl = data.get<string>("thumb_url"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultGif>()); + result->gifUrl = data.get<string>("gif_url", ""); + result->gifWidth = data.get("gif_width", 0); + result->gifHeight = data.get("gif_height", 0); + result->gifDuration = data.get("gif_duration", 0); + result->thumbUrl = data.get<string>("thumb_url"); + return result; } 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 string. - 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); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + 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); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultMpeg4Gif(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultMpeg4Gif>()); - result->mpeg4Url = data.get<string>("mpeg4_url"); - result->mpeg4Width = data.get("mpeg4_width", 0); - result->mpeg4Height = data.get("mpeg4_height", 0); - result->mpeg4Duration = data.get("mpeg4_duration", 0); - result->thumbUrl = data.get<string>("thumb_url"); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultMpeg4Gif>()); + result->mpeg4Url = data.get<string>("mpeg4_url"); + result->mpeg4Width = data.get("mpeg4_width", 0); + result->mpeg4Height = data.get("mpeg4_height", 0); + result->mpeg4Duration = data.get("mpeg4_duration", 0); + result->thumbUrl = data.get<string>("thumb_url"); + return result; } 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 string. - 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); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + 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); + // The last comma will be erased by parseInlineQueryResult(). + return result; } InlineQueryResultVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVideo(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). - auto result(make_shared<InlineQueryResultVideo>()); - result->videoUrl = data.get<string>("video_url"); - result->mimeType = data.get<string>("mime_type"); - result->thumbUrl = data.get<string>("thumb_url"); - result->videoWidth = data.get("video_width", 0); - result->videoHeight = data.get("video_height", 0); - result->videoDuration = data.get("video_duration", 0); - result->description = data.get<string>("description", ""); - return result; + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + auto result(make_shared<InlineQueryResultVideo>()); + result->videoUrl = data.get<string>("video_url"); + result->mimeType = data.get<string>("mime_type"); + result->thumbUrl = data.get<string>("thumb_url"); + result->videoWidth = data.get("video_width", 0); + result->videoHeight = data.get("video_height", 0); + result->videoDuration = data.get("video_duration", 0); + result->description = data.get<string>("description", ""); + return result; } 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 string. - string result; - appendToJson(result, "video_url", object->videoUrl); - appendToJson(result, "mime_type", object->mimeType); - appendToJson(result, "thumb_url", object->thumbUrl); - appendToJson(result, "video_width", object->videoWidth); - appendToJson(result, "video_height", object->videoHeight); - appendToJson(result, "video_duration", object->videoDuration); - appendToJson(result, "description", object->description); - // The last comma will be erased by parseInlineQueryResult(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "video_url", object->videoUrl); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "thumb_url", object->thumbUrl); + appendToJson(result, "video_width", object->videoWidth); + appendToJson(result, "video_height", object->videoHeight); + appendToJson(result, "video_duration", object->videoDuration); + appendToJson(result, "description", object->description); + // The last comma will be erased by parseInlineQueryResult(). + return result; } ChosenInlineResult::Ptr TgTypeParser::parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const { - auto result(make_shared<ChosenInlineResult>()); - result->resultId = data.get<string>("result_id"); - result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); - result->inlineMessageId = data.get<string>("inline_message_id", ""); - result->query = data.get<string>("query"); - return result; + auto result(make_shared<ChosenInlineResult>()); + result->resultId = data.get<string>("result_id"); + result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location"); + result->inlineMessageId = data.get<string>("inline_message_id", ""); + result->query = data.get<string>("query"); + return result; } std::string TgTypeParser::parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const { - if (!object){ - return ""; - } + if (!object){ + return ""; + } - 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; + 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; } CallbackQuery::Ptr TgTypeParser::parseJsonAndGetCallbackQuery(const boost::property_tree::ptree& data) const { - auto result(make_shared<CallbackQuery>()); - result->id = data.get<string>("id"); - result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message"); - result->inlineMessageId = data.get<string>("inline_message_id", ""); - result->chatInstance = data.get<string>("chat_instance"); - result->gameShortName = data.get<string>("game_short_name", ""); - result->data = data.get<string>("data", ""); - return result; + auto result(make_shared<CallbackQuery>()); + result->id = data.get<string>("id"); + result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message"); + result->inlineMessageId = data.get<string>("inline_message_id", ""); + result->chatInstance = data.get<string>("chat_instance"); + result->gameShortName = data.get<string>("game_short_name", ""); + result->data = data.get<string>("data", ""); + return result; } std::string TgTypeParser::parseCallbackQuery(const CallbackQuery::Ptr& object) const { - if (!object){ - return ""; - } - - 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; + if (!object){ + return ""; + } + + 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(make_shared<InlineKeyboardMarkup>()); - for (const auto& item : data.find("inline_keyboard")->second){ - result->inlineKeyboard.push_back(parseJsonAndGetArray<InlineKeyboardButton>(&TgTypeParser::parseJsonAndGetInlineKeyboardButton, item.second)); - } - return result; + auto result(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 ""; - } - 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; + if (!object){ + return ""; + } + 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(make_shared<InlineKeyboardButton>()); - result->text = data.get<string>("text"); - result->url = data.get<string>("url", ""); - result->callbackData = data.get<string>("callback_data", ""); - result->switchInlineQuery = data.get<string>("switch_inline_query", ""); - result->switchInlineQueryCurrentChat = data.get<string>("switch_inline_query_current_chat", ""); - result->callbackGame = make_shared<CallbackGame>(); - result->pay = data.get<bool>("pay", false); - return result; + auto result(make_shared<InlineKeyboardButton>()); + result->text = data.get<string>("text"); + result->url = data.get<string>("url", ""); + result->callbackData = data.get<string>("callback_data", ""); + result->switchInlineQuery = data.get<string>("switch_inline_query", ""); + result->switchInlineQueryCurrentChat = data.get<string>("switch_inline_query_current_chat", ""); + result->callbackGame = make_shared<CallbackGame>(); + result->pay = data.get<bool>("pay", false); + return result; } std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const { - if (!object){ - return ""; - } - string result; - result += '{'; - 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); - appendToJson(result, "pay", object->pay); - removeLastComma(result); - result += '}'; - return result; + if (!object){ + return ""; + } + string result; + result += '{'; + 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); + appendToJson(result, "pay", object->pay); + removeLastComma(result); + result += '}'; + return result; } WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const { - auto result(make_shared<WebhookInfo>()); - result->url = data.get<string>("url"); - result->hasCustomCertificate = data.get<bool>("has_custom_certificate"); - result->pendingUpdateCount = data.get<int32_t>("pending_update_count"); - result->lastErrorDate = data.get<int32_t>("last_error_date", 0); - result->lastErrorMessage = data.get<string>("last_error_message", ""); - result->maxConnections = data.get<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; + auto result(make_shared<WebhookInfo>()); + result->url = data.get<string>("url"); + result->hasCustomCertificate = data.get<bool>("has_custom_certificate"); + result->pendingUpdateCount = data.get<int32_t>("pending_update_count"); + result->lastErrorDate = data.get<int32_t>("last_error_date", 0); + result->lastErrorMessage = data.get<string>("last_error_message", ""); + result->maxConnections = data.get<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 ""; - } - string result; - result += '{'; - appendToJson(result, "url", object->url); - appendToJson(result, "has_custom_certificate", object->hasCustomCertificate); - appendToJson(result, "pending_update_count", object->pendingUpdateCount); - appendToJson(result, "last_error_date", object->lastErrorDate); - appendToJson(result, "last_error_message", object->lastErrorMessage); - 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; + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "url", object->url); + appendToJson(result, "has_custom_certificate", object->hasCustomCertificate); + appendToJson(result, "pending_update_count", object->pendingUpdateCount); + appendToJson(result, "last_error_date", object->lastErrorDate); + appendToJson(result, "last_error_message", object->lastErrorMessage); + 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; - // define InputMessageContent type + InputMessageContent::Ptr result; + // define InputMessageContent type - string tMessageText = data.get<string>("message_text", ""); - float tLatitude = data.get<float>("latitude", 1000); // latitude belong (-90,90) - string tTitle = data.get<string>("title", ""); - string tPnoneNumber = data.get<string>("phone_number", ""); + string tMessageText = data.get<string>("message_text", ""); + float tLatitude = data.get<float>("latitude", 1000); // latitude belong (-90,90) + string tTitle = data.get<string>("title", ""); + string tPnoneNumber = data.get<string>("phone_number", ""); - if (!tMessageText.empty()) { - result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputTextMessageContent(data)); - } else if (!tTitle.empty()) { - result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputVenueMessageContent(data)); - } else if (tLatitude != 1000) { - result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputLocationMessageContent(data)); - } else if (!tPnoneNumber.empty()) { - result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputContactMessageContent(data)); - } + if (!tMessageText.empty()) { + result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputTextMessageContent(data)); + } else if (!tTitle.empty()) { + result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputVenueMessageContent(data)); + } else if (tLatitude != 1000) { + result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputLocationMessageContent(data)); + } else if (!tPnoneNumber.empty()) { + result = static_pointer_cast<InputMessageContent>(parseJsonAndGetInputContactMessageContent(data)); + } - return result; + return result; } std::string TgTypeParser::parseInputMessageContent(const InputMessageContent::Ptr& object) const { - if (!object){ - return ""; - } - - string result; - result += '{'; - - if (object->type == std::string("InputTextMessageContent")) { - result += parseInputTextMessageContent(static_pointer_cast<InputTextMessageContent>(object)); - } - else if (object->type == std::string("InputLocationMessageContent")) { - result += parseInputLocationMessageContent(static_pointer_cast<InputLocationMessageContent>(object)); - } - else if (object->type == std::string("InputVenueMessageContent")) { - result += parseInputVenueMessageContent(static_pointer_cast<InputVenueMessageContent>(object)); - } - else if (object->type == std::string("InputContactMessageContent")) { - result += parseInputContactMessageContent(static_pointer_cast<InputContactMessageContent>(object)); - } - - removeLastComma(result); - result += '}'; - return result; + if (!object){ + return ""; + } + + string result; + result += '{'; + + if (object->type == std::string("InputTextMessageContent")) { + result += parseInputTextMessageContent(static_pointer_cast<InputTextMessageContent>(object)); + } + else if (object->type == std::string("InputLocationMessageContent")) { + result += parseInputLocationMessageContent(static_pointer_cast<InputLocationMessageContent>(object)); + } + else if (object->type == std::string("InputVenueMessageContent")) { + result += parseInputVenueMessageContent(static_pointer_cast<InputVenueMessageContent>(object)); + } + else if (object->type == std::string("InputContactMessageContent")) { + result += parseInputContactMessageContent(static_pointer_cast<InputContactMessageContent>(object)); + } + + removeLastComma(result); + result += '}'; + return result; } InputTextMessageContent::Ptr TgTypeParser::parseJsonAndGetInputTextMessageContent(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). - auto result(make_shared<InputTextMessageContent>()); - result->messageText = data.get<string>("message_text"); - result->parseMode = data.get<string>("parse_mode", ""); - result->disableWebPagePreview = data.get<bool>("disable_web_page_preview", false); - return result; + // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). + auto result(make_shared<InputTextMessageContent>()); + result->messageText = data.get<string>("message_text"); + result->parseMode = data.get<string>("parse_mode", ""); + result->disableWebPagePreview = data.get<bool>("disable_web_page_preview", false); + return result; } std::string TgTypeParser::parseInputTextMessageContent(const InputTextMessageContent::Ptr& object) const { - if (!object){ - return " "; - } - // This function will be called by parseInputMessageContent() - string result; - appendToJson(result, "message_text", object->messageText); - appendToJson(result, "parse_mode", object->parseMode); - appendToJson(result, "disable_web_page_preview", object->disableWebPagePreview); - // The last comma will be erased by parseInputMessageContent(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInputMessageContent() + string result; + appendToJson(result, "message_text", object->messageText); + appendToJson(result, "parse_mode", object->parseMode); + appendToJson(result, "disable_web_page_preview", object->disableWebPagePreview); + // The last comma will be erased by parseInputMessageContent(). + return result; } InputLocationMessageContent::Ptr TgTypeParser::parseJsonAndGetInputLocationMessageContent(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). - auto result(make_shared<InputLocationMessageContent>()); - result->latitude = data.get<float>("latitude"); - result->longitude = data.get<float>("longitude"); - return result; + // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). + auto result(make_shared<InputLocationMessageContent>()); + result->latitude = data.get<float>("latitude"); + result->longitude = data.get<float>("longitude"); + return result; } std::string TgTypeParser::parseInputLocationMessageContent(const InputLocationMessageContent::Ptr& object) const { - if (!object){ - return " "; - } - // This function will be called by parseInputMessageContent() - string result; - appendToJson(result, "latitude", object->latitude); - appendToJson(result, "longitude", object->longitude); - // The last comma will be erased by parseInputMessageContent(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInputMessageContent() + string result; + appendToJson(result, "latitude", object->latitude); + appendToJson(result, "longitude", object->longitude); + // The last comma will be erased by parseInputMessageContent(). + return result; } InputVenueMessageContent::Ptr TgTypeParser::parseJsonAndGetInputVenueMessageContent(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). - auto result(make_shared<InputVenueMessageContent>()); - result->latitude = data.get<float>("latitude"); - result->longitude = data.get<float>("longitude"); - result->title = data.get<string>("title"); - result->address = data.get<string>("address"); - result->foursquareId = data.get<string>("foursquare_id", ""); - return result; + // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). + auto result(make_shared<InputVenueMessageContent>()); + result->latitude = data.get<float>("latitude"); + result->longitude = data.get<float>("longitude"); + result->title = data.get<string>("title"); + result->address = data.get<string>("address"); + result->foursquareId = data.get<string>("foursquare_id", ""); + return result; } std::string TgTypeParser::parseInputVenueMessageContent(const InputVenueMessageContent::Ptr& object) const { - if (!object){ - return " "; - } - // This function will be called by parseInputMessageContent() - string result; - appendToJson(result, "latitude", object->latitude); - appendToJson(result, "longitude", object->longitude); - appendToJson(result, "title", object->title); - appendToJson(result, "address", object->address); - appendToJson(result, "foursquare_id", object->foursquareId); - // The last comma will be erased by parseInputMessageContent(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInputMessageContent() + string result; + appendToJson(result, "latitude", object->latitude); + appendToJson(result, "longitude", object->longitude); + appendToJson(result, "title", object->title); + appendToJson(result, "address", object->address); + appendToJson(result, "foursquare_id", object->foursquareId); + // The last comma will be erased by parseInputMessageContent(). + return result; } InputContactMessageContent::Ptr TgTypeParser::parseJsonAndGetInputContactMessageContent(const boost::property_tree::ptree& data) const { - // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). - auto result(make_shared<InputContactMessageContent>()); - result->phoneNumber = data.get<string>("phone_number"); - result->firstName = data.get<string>("first_name"); - result->lastName = data.get<string>("last_name", ""); - return result; + // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). + auto result(make_shared<InputContactMessageContent>()); + result->phoneNumber = data.get<string>("phone_number"); + result->firstName = data.get<string>("first_name"); + result->lastName = data.get<string>("last_name", ""); + return result; } std::string TgTypeParser::parseInputContactMessageContent(const InputContactMessageContent::Ptr& object) const { - if (!object){ - return " "; - } - // This function will be called by parseInputMessageContent() - string result; - appendToJson(result, "phone_number", object->phoneNumber); - appendToJson(result, "first_name", object->firstName); - appendToJson(result, "last_name", object->lastName); - // The last comma will be erased by parseInputMessageContent(). - return result; + if (!object){ + return " "; + } + // This function will be called by parseInputMessageContent() + string result; + appendToJson(result, "phone_number", object->phoneNumber); + appendToJson(result, "first_name", object->firstName); + appendToJson(result, "last_name", object->lastName); + // The last comma will be erased by parseInputMessageContent(). + return result; } Invoice::Ptr TgTypeParser::parseJsonAndGetInvoice(const boost::property_tree::ptree& data) const { - auto result(make_shared<Invoice>()); - result->title = data.get<string>("title"); - result->description = data.get<string>("description"); - result->startParameter = data.get<string>("start_parameter"); - result->currency = data.get<string>("currency"); - result->totalAmount = data.get<int32_t>("total_amount"); - return result; + auto result(make_shared<Invoice>()); + result->title = data.get<string>("title"); + result->description = data.get<string>("description"); + result->startParameter = data.get<string>("start_parameter"); + result->currency = data.get<string>("currency"); + result->totalAmount = data.get<int32_t>("total_amount"); + return result; } std::string TgTypeParser::parseInvoice(const Invoice::Ptr& object) const { - if (!object) { - return " "; - } - 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); - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return " "; + } + 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); + removeLastComma(result); + result += '}'; + return result; } LabeledPrice::Ptr TgTypeParser::parseJsonAndGetLabeledPrice(const boost::property_tree::ptree& data) const { - auto result(make_shared<LabeledPrice>()); - result->label = data.get<string>("label"); - result->amount = data.get<int32_t>("amount"); - return result; + auto result(make_shared<LabeledPrice>()); + result->label = data.get<string>("label"); + result->amount = data.get<int32_t>("amount"); + return result; } string TgTypeParser::parseLabeledPrice(const LabeledPrice::Ptr& object) const { - std::string result; - result += '{'; - appendToJson(result, "label", object->label); - appendToJson(result, "amount", object->amount); - removeLastComma(result); - result += '}'; - return result; + std::string result; + result += '{'; + appendToJson(result, "label", object->label); + appendToJson(result, "amount", object->amount); + removeLastComma(result); + result += '}'; + return result; } OrderInfo::Ptr TgTypeParser::parseJsonAndGetOrderInfo(const boost::property_tree::ptree& data) const { - auto result(make_shared<OrderInfo>()); - result->name = data.get<string>("name", ""); - result->phoneNumber = data.get<string>("phone_number", ""); - result->email = data.get<string>("email", ""); - result->shippingAddress = tryParseJson(&TgTypeParser::parseJsonAndGetShippingAddress, data, "shipping_address"); - return result; + auto result(make_shared<OrderInfo>()); + result->name = data.get<string>("name", ""); + result->phoneNumber = data.get<string>("phone_number", ""); + result->email = data.get<string>("email", ""); + result->shippingAddress = tryParseJson(&TgTypeParser::parseJsonAndGetShippingAddress, data, "shipping_address"); + return result; } string TgTypeParser::parseOrderInfo(const OrderInfo::Ptr& object) const { - if (!object) { - return " "; - } - std::string result; - result += '{'; - if (!object->name.empty()) { - appendToJson(result, "name", object->name); - } - if (!object->phoneNumber.empty()) { - appendToJson(result, "phone_number", object->phoneNumber); - } - if (!object->email.empty()) { - appendToJson(result, "email", object->email); - } - if (!object->shippingAddress) { - result += R"("shipping_address":)"; - result += parseShippingAddress(object->shippingAddress); - result += ","; - } - removeLastComma(result); - result += '}'; - return result; + if (!object) { + return " "; + } + std::string result; + result += '{'; + if (!object->name.empty()) { + appendToJson(result, "name", object->name); + } + if (!object->phoneNumber.empty()) { + appendToJson(result, "phone_number", object->phoneNumber); + } + if (!object->email.empty()) { + appendToJson(result, "email", object->email); + } + if (!object->shippingAddress) { + result += R"("shipping_address":)"; + result += parseShippingAddress(object->shippingAddress); + result += ","; + } + removeLastComma(result); + result += '}'; + return result; } PreCheckoutQuery::Ptr TgTypeParser::parseJsonAndGetPreCheckoutQuery(const boost::property_tree::ptree& data) const { - auto result(make_shared<PreCheckoutQuery>()); - result->id = data.get<string>("id"); - result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->currency = data.get<string>("currency"); - result->totalAmount = data.get<int32_t>("total_amount"); - return result; + auto result(make_shared<PreCheckoutQuery>()); + result->id = data.get<string>("id"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->currency = data.get<string>("currency"); + result->totalAmount = data.get<int32_t>("total_amount"); + return result; } 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); - removeLastComma(result); - result += '}'; - return result; + 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<string>("country_code"); - result->state = data.get<string>("state", ""); - result->city = data.get<string>("city"); - result->streetLine1 = data.get<string>("street_line1"); - result->streetLine2 = data.get<string>("street_line2"); - result->postCode = data.get<string>("post_code"); - return result; + ShippingAddress::Ptr result; + result->countryCode = data.get<string>("country_code"); + result->state = data.get<string>("state", ""); + result->city = data.get<string>("city"); + result->streetLine1 = data.get<string>("street_line1"); + result->streetLine2 = data.get<string>("street_line2"); + result->postCode = data.get<string>("post_code"); + return result; } 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); - removeLastComma(result); - result += '}'; - return result; + 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); + removeLastComma(result); + result += '}'; + return result; } ShippingOption::Ptr TgTypeParser::parseJsonAndGetShippingOption(const boost::property_tree::ptree& data) const { - auto result(make_shared<ShippingOption>()); - result->id = data.get<string>("id"); - result->title = data.get<string>("title"); - result->prices = parseJsonAndGetArray<LabeledPrice>(&TgTypeParser::parseJsonAndGetLabeledPrice, data, "prices"); - return result; + auto result(make_shared<ShippingOption>()); + result->id = data.get<string>("id"); + result->title = data.get<string>("title"); + result->prices = parseJsonAndGetArray<LabeledPrice>(&TgTypeParser::parseJsonAndGetLabeledPrice, data, "prices"); + return result; } string TgTypeParser::parseShippingOption(const ShippingOption::Ptr& object) const { - std::string result; - result += '{'; - appendToJson(result, "id", object->id); - appendToJson(result, "title", object->title); - removeLastComma(result); - result += R"("prices":)"; - result += parseArray(&TgTypeParser::parseLabeledPrice, object->prices); - result += '}'; - return result; + std::string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "title", object->title); + removeLastComma(result); + result += R"("prices":)"; + result += parseArray(&TgTypeParser::parseLabeledPrice, object->prices); + result += '}'; + return result; } ShippingQuery::Ptr TgTypeParser::parseJsonAndGetShippingQuery(const boost::property_tree::ptree& data) const { - auto result(make_shared<ShippingQuery>()); - result->id = data.get<string>("id"); - result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->invoicePayload = data.get<string>("invoice_payload"); - result->shippingAddress = tryParseJson(&TgTypeParser::parseJsonAndGetShippingAddress, data, "shipping_address"); - return result; + auto result(make_shared<ShippingQuery>()); + result->id = data.get<string>("id"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->invoicePayload = data.get<string>("invoice_payload"); + result->shippingAddress = tryParseJson(&TgTypeParser::parseJsonAndGetShippingAddress, data, "shipping_address"); + return result; } string TgTypeParser::parseShippingQuery(const ShippingQuery::Ptr& object) const { - string result; - result += '{'; - appendToJson(result, "id", object->id); - result += R"("from":)"; - result += parseUser(object->from); - result += ","; - appendToJson(result, "invoice_payload", object->invoicePayload); - result += R"("shipping_address":)"; - result += parseShippingAddress(object->shippingAddress); - result += ","; - removeLastComma(result); - result += '}'; - return result; + string result; + result += '{'; + appendToJson(result, "id", object->id); + result += R"("from":)"; + result += parseUser(object->from); + result += ","; + appendToJson(result, "invoice_payload", object->invoicePayload); + result += R"("shipping_address":)"; + result += parseShippingAddress(object->shippingAddress); + result += ","; + removeLastComma(result); + result += '}'; + return result; } SuccessfulPayment::Ptr TgTypeParser::parseJsonAndGetSuccessfulPayment(const boost::property_tree::ptree& data) const { - auto result(make_shared<SuccessfulPayment>()); - result->currency = data.get<string>("currency"); - result->totalAmount = data.get<int32_t>("total_amount"); - result->invoicePayload = data.get<string>("invoice_payload"); - result->shippingOptionId = data.get<string>("shipping_option_id"); - result->orderInfo = tryParseJson(&TgTypeParser::parseJsonAndGetOrderInfo, data, "order_info"); - return result; + auto result(make_shared<SuccessfulPayment>()); + result->currency = data.get<string>("currency"); + result->totalAmount = data.get<int32_t>("total_amount"); + result->invoicePayload = data.get<string>("invoice_payload"); + result->shippingOptionId = data.get<string>("shipping_option_id"); + result->orderInfo = tryParseJson(&TgTypeParser::parseJsonAndGetOrderInfo, data, "order_info"); + return result; } std::string TgTypeParser::parseSuccessfulPayment(const SuccessfulPayment::Ptr& object) const { - string result; - 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; + string result; + 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; } void TgTypeParser::appendToJson(string& json, const string& varName, const string& value) const { - if (value.empty()) { - return; - } - json += '"'; - json += varName; - json += R"(":)"; - if (value.front() != '{') { - json += '"'; - } - json += value; - if (value.back() != '}') { - json += '"'; - } - json += ','; + if (value.empty()) { + return; + } + json += '"'; + json += varName; + json += R"(":)"; + if (value.front() != '{') { + json += '"'; + } + json += value; + if (value.back() != '}') { + json += '"'; + } + json += ','; } } diff --git a/src/net/BoostHttpOnlySslClient.cpp b/src/net/BoostHttpOnlySslClient.cpp new file mode 100644 index 0000000..fd69986 --- /dev/null +++ b/src/net/BoostHttpOnlySslClient.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2015 Oleg Morozenkov + * Copyright (c) 2018 JellyBrick + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "tgbot/net/BoostHttpOnlySslClient.h" + +#include <boost/asio/ssl.hpp> + +using namespace std; +using namespace boost::asio; +using namespace boost::asio::ip; + +namespace TgBot { + +BoostHttpOnlySslClient::BoostHttpOnlySslClient() : _httpParser() { +} + +BoostHttpOnlySslClient::~BoostHttpOnlySslClient() { +} + +string BoostHttpOnlySslClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) const { + tcp::resolver resolver(_ioService); + tcp::resolver::query query(url.host, "443"); + + ssl::context context(ssl::context::tlsv12_client); + context.set_default_verify_paths(); + + ssl::stream<tcp::socket> socket(_ioService, context); + + connect(socket.lowest_layer(), resolver.resolve(query)); + + #ifdef TGBOT_DISABLE_NAGLES_ALGORITHM + socket.lowest_layer().set_option(tcp::no_delay(true)); + #endif //TGBOT_DISABLE_NAGLES_ALGORITHM + #ifdef TGBOT_CHANGE_SOCKET_BUFFER_SIZE + #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__ + socket.lowest_layer().set_option(socket_base::send_buffer_size(65536)); + socket.lowest_layer().set_option(socket_base::receive_buffer_size(65536)); + #else //for 32-bit + socket.lowest_layer().set_option(socket_base::send_buffer_size(32768)); + socket.lowest_layer().set_option(socket_base::receive_buffer_size(32768)); + #endif //Processor architecture + #endif //TGBOT_CHANGE_SOCKET_BUFFER_SIZE + socket.set_verify_mode(ssl::verify_none); + socket.set_verify_callback(ssl::rfc2818_verification(url.host)); + + socket.handshake(ssl::stream<tcp::socket>::client); + + string requestText = _httpParser.generateRequest(url, args, false); + write(socket, buffer(requestText.c_str(), requestText.length())); + + string response; + + #ifdef TGBOT_CHANGE_READ_BUFFER_SIZE + #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__ + char buff[65536]; + #else //for 32-bit + char buff[32768]; + #endif //Processor architecture + #else + char buff[1024]; + #endif //TGBOT_CHANGE_READ_BUFFER_SIZE + + boost::system::error_code error; + while (!error) { + size_t bytes = read(socket, buffer(buff), error); + response += string(buff, bytes); + } + + return _httpParser.extractBody(response); +} + +} diff --git a/src/net/CurlHttpClient.cpp b/src/net/CurlHttpClient.cpp new file mode 100644 index 0000000..932fd68 --- /dev/null +++ b/src/net/CurlHttpClient.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2018 Egor Pugin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifdef HAVE_CURL + +#include "tgbot/net/CurlHttpClient.h" + +#include <boost/asio/ssl.hpp> + +namespace TgBot { + +CurlHttpClient::CurlHttpClient() : _httpParser() { + curlSettings = curl_easy_init(); +} + +CurlHttpClient::~CurlHttpClient() { + curl_easy_cleanup(curlSettings); +} + +static size_t curlWriteString(char* ptr, size_t size, size_t nmemb, void* userdata) { + std::string &s = *(std::string *)userdata; + auto read = size * nmemb; + s.append(ptr, ptr + read); + return read; +}; + +std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const { + // Copy settings for each call because we change CURLOPT_URL and other stuff. + // This also protects multithreaded case. + auto curl = curl_easy_duphandle(curlSettings); + + std::string u = url.protocol + "://" + url.host + url.path; + curl_easy_setopt(curl, CURLOPT_URL, u.c_str()); + + // disable keep-alive + struct curl_slist* headers = nullptr; + headers = curl_slist_append(headers, "Connection: close"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + + std::string data; + std::vector<char*> escaped; + if (!args.empty()) { + for (const HttpReqArg& a : args) { + escaped.push_back(curl_easy_escape(curl, a.name.c_str(), a.name.size())); + data += escaped.back() + std::string("="); + escaped.push_back(curl_easy_escape(curl, a.value.c_str(), a.value.size())); + data += escaped.back() + std::string("&"); + } + data.resize(data.size() - 1); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data.size()); + } + + std::string response; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteString); + + auto res = curl_easy_perform(curl); + curl_slist_free_all(headers); + curl_easy_cleanup(curl); + + for (auto& e : escaped) { + curl_free(e); + } + + if (res != CURLE_OK) { + throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); + } + + return _httpParser.extractBody(response); +} + +} + +#endif diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp deleted file mode 100644 index 528d988..0000000 --- a/src/net/HttpClient.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2015 Oleg Morozenkov - * Copyright (c) 2018 Egor Pugin - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "tgbot/net/HttpClient.h" - -#include <boost/asio/ssl.hpp> - -using namespace std; -using namespace boost::asio; -using namespace boost::asio::ip; - -namespace TgBot { - -BoostHttpClient& BoostHttpClient::getInstance() { - static BoostHttpClient result; - return result; -} - -string BoostHttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) const { - ssl::context context(ssl::context::sslv23); - context.set_default_verify_paths(); - - ssl::stream<tcp::socket> socket(_ioService, context); - tcp::resolver resolver(_ioService); - tcp::resolver::query query(url.host, url.protocol); - - connect(socket.lowest_layer(), resolver.resolve(query)); - - #ifdef TGBOT_DISABLE_NAGLES_ALGORITHM - socket.lowest_layer().set_option(tcp::no_delay(true)); - #endif //TGBOT_DISABLE_NAGLES_ALGORITHM - - #ifdef TGBOT_CHANGE_SOCKET_BUFFER_SIZE - #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__ - socket.lowest_layer().set_option(socket_base::send_buffer_size(65536)); - socket.lowest_layer().set_option(socket_base::receive_buffer_size(65536)); - #else //for 32-bit - socket.lowest_layer().set_option(socket_base::send_buffer_size(32768)); - socket.lowest_layer().set_option(socket_base::receive_buffer_size(32768)); - #endif //Processor architecture - #endif //TGBOT_CHANGE_SOCKET_BUFFER_SIZE - - socket.set_verify_mode(ssl::verify_none); - socket.set_verify_callback(ssl::rfc2818_verification(url.host)); - socket.handshake(ssl::stream<tcp::socket>::client); - - string requestText = HttpParser::getInstance().generateRequest(url, args, false); - write(socket, buffer(requestText.c_str(), requestText.length())); - - string response; - - #ifdef TGBOT_CHANGE_READ_BUFFER_SIZE - #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__ - char buff[65536]; - #else //for 32-bit - char buff[32768]; - #endif //Processor architecture - #else - char buff[1024]; - #endif //TGBOT_CHANGE_READ_BUFFER_SIZE - - boost::system::error_code error; - while (!error) { - size_t bytes = read(socket, buffer(buff), error); - response += string(buff, bytes); - } - - return HttpParser::getInstance().parseResponse(response); -} - -#ifdef HAVE_CURL - -CurlHttpClient::CurlHttpClient() { - curlSettings = curl_easy_init(); -} - -CurlHttpClient::~CurlHttpClient() { - curl_easy_cleanup(curlSettings); -} - -CurlHttpClient& CurlHttpClient::getInstance() { - static CurlHttpClient result; - return result; -} - -static size_t curl_write_string(char *ptr, size_t size, size_t nmemb, void *userdata) -{ - std::string &s = *(std::string *)userdata; - auto read = size * nmemb; - s.append(ptr, ptr + read); - return read; -}; - -string CurlHttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) const { - // Copy settings for each call because we change CURLOPT_URL and other stuff. - // This also protects multithreaded case. - auto curl = curl_easy_duphandle(curlSettings); - - auto u = url.protocol + "://" + url.host + url.path; - curl_easy_setopt(curl, CURLOPT_URL, u.c_str()); - - // disable keep-alive - struct curl_slist *headers = NULL; - headers = curl_slist_append(headers, "Connection: close"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - - std::string data; - std::vector<char *> escaped; - if (!args.empty()) - { - for (auto &a : args) - { - escaped.push_back(curl_easy_escape(curl, a.name.c_str(), a.name.size())); - data += escaped.back() + std::string("="); - escaped.push_back(curl_easy_escape(curl, a.value.c_str(), a.value.size())); - data += escaped.back() + std::string("&"); - } - data.resize(data.size() - 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data.size()); - } - - std::string response; - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_string); - - auto res = curl_easy_perform(curl); - curl_slist_free_all(headers); - curl_easy_cleanup(curl); - - for (auto &e : escaped) - curl_free(e); - - if (res != CURLE_OK) - throw std::runtime_error(std::string("curl error: ") + curl_easy_strerror(res)); - - return HttpParser::getInstance().parseResponse(response); -} - -#endif - -} diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp index 3dd9215..41dec14 100644 --- a/src/net/HttpParser.cpp +++ b/src/net/HttpParser.cpp @@ -31,182 +31,168 @@ using namespace boost; namespace TgBot { -HttpParser& HttpParser::getInstance() { - static HttpParser result; - return result; +string HttpParser::generateRequest(const Url& url, const vector<HttpReqArg>& args, bool isKeepAlive) const { + string result; + if (args.empty()) { + result += "GET "; + } else { + result += "POST "; + } + result += url.path; + result += url.query.empty() ? "" : "?" + url.query; + result += " HTTP/1.1\r\n"; + result += "Host: "; + result += url.host; + result += "\r\nConnection: "; + if (isKeepAlive) { + result += "keep-alive"; + } else { + result += "close"; + } + result += "\r\n"; + if (args.empty()) { + result += "\r\n"; + } else { + string requestData; + + string bondary = generateMultipartBoundary(args); + if (bondary.empty()) { + result += "Content-Type: application/x-www-form-urlencoded\r\n"; + requestData = generateWwwFormUrlencoded(args); + } else { + result += "Content-Type: multipart/form-data; boundary="; + result += bondary; + result += "\r\n"; + requestData = generateMultipartFormData(args, bondary); + } + + result += "Content-Length: "; + result += std::to_string(requestData.length()); + result += "\r\n\r\n"; + result += requestData; + } + return result; } -string HttpParser::generateRequest(const Url& url, const vector<HttpReqArg>& args, bool isKeepAlive) { - string result; - if (args.empty()) { - result += "GET "; - } else { - result += "POST "; - } - result += url.path; - result += url.query.empty() ? "" : "?" + url.query; - result += " HTTP/1.1\r\n"; - result += "Host: "; - result += url.host; - result += "\r\nConnection: "; - if (isKeepAlive) { - result += "keep-alive"; - } else { - result += "close"; - } - result += "\r\n"; - if (args.empty()) { - result += "\r\n"; - } else { - string requestData; - - string bondary = generateMultipartBoundary(args); - if (bondary.empty()) { - result += "Content-Type: application/x-www-form-urlencoded\r\n"; - requestData = generateWwwFormUrlencoded(args); - } else { - result += "Content-Type: multipart/form-data; boundary="; - result += bondary; - result += "\r\n"; - requestData = generateMultipartFormData(args, bondary); - } - - result += "Content-Length: "; - result += lexical_cast<string>(requestData.length()); - result += "\r\n\r\n"; - result += requestData; - } - return result; +string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, const string& bondary) const { + string result; + for (const HttpReqArg& item : args) { + result += "--"; + result += bondary; + result += "\r\nContent-Disposition: form-data; name=\""; + result += item.name; + if (item.isFile) { + result += "\"; filename=\"" + item.fileName; + } + result += "\"\r\n"; + if (item.isFile) { + result += "Content-Type: "; + result += item.mimeType; + result += "\r\n"; + } + result += "\r\n"; + result += item.value; + result += "\r\n"; + } + result += "--" + bondary + "--\r\n"; + return result; } -string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, const string& bondary) { - string result; - for (const HttpReqArg& item : args) { - result += "--"; - result += bondary; - result += "\r\nContent-Disposition: form-data; name=\""; - result += item.name; - if (item.isFile) { - result += "\"; filename=\"" + item.fileName; - } - result += "\"\r\n"; - if (item.isFile) { - result += "Content-Type: "; - result += item.mimeType; - result += "\r\n"; - } - result += "\r\n"; - result += item.value; - result += "\r\n"; - } - result += "--" + bondary + "--\r\n"; - return result; +string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) const { + string result; + srand((unsigned int) time(nullptr)); + for (const HttpReqArg& item : args) { + if (item.isFile) { + while (result.empty() || item.value.find(result) != string::npos) { + result += StringTools::generateRandomString(4); + } + } + } + return result; } -string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) { - string result; - srand((unsigned int) time(nullptr)); - for (const HttpReqArg& item : args) { - if (item.isFile) { - while (result.empty() || item.value.find(result) != item.value.npos) { - result += StringTools::generateRandomString(4); - } - } - } - return result; +string HttpParser::generateWwwFormUrlencoded(const vector<HttpReqArg>& args) const { + string result; + + bool firstRun = true; + for (const HttpReqArg& item : args) { + if (firstRun) { + firstRun = false; + } else { + result += '&'; + } + result += StringTools::urlEncode(item.name); + result += '='; + result += StringTools::urlEncode(item.value); + } + + return result; } -string HttpParser::generateWwwFormUrlencoded(const vector<HttpReqArg>& args) { - string result; - - bool firstRun = true; - for (const HttpReqArg& item : args) { - if (firstRun) { - firstRun = false; - } else { - result += '&'; - } - result += StringTools::urlEncode(item.name); - result += '='; - result += StringTools::urlEncode(item.value); - } - - return result; +string HttpParser::generateResponse(const string& data, const string& mimeType, unsigned short statusCode, const string& statusStr, bool isKeepAlive) const { + string result; + result += "HTTP/1.1 "; + result += std::to_string(statusCode); + result += ' '; + result += statusStr; + result += "\r\nContent-Type: "; + result += mimeType; + result += "\r\nContent-Length: "; + result += std::to_string(data.length()); + result += "\r\nConnection: "; + if (isKeepAlive) { + result += "keep-alive"; + } else { + result += "close"; + } + result += "\r\n\r\n"; + result += data; + return result; } -string HttpParser::generateResponse(const string& data, const string& mimeType, unsigned short statusCode, const string& statusStr, bool isKeepAlive) { - string result; - result += "HTTP/1.1 "; - result += lexical_cast<string>(statusCode); - result += ' '; - result += statusStr; - result += "\r\nContent-Type: "; - result += mimeType; - result += "\r\nContent-Length: "; - result += lexical_cast<string>(data.length()); - result += "\r\n\r\n"; - result += data; - return result; +unordered_map<string, string> HttpParser::parseHeader(const string& data, bool isRequest) const { + unordered_map<string, string> headers; + + size_t lineStart = 0; + size_t lineEnd = 0; + size_t lineSepPos = 0; + size_t lastLineEnd = string::npos; + while (lastLineEnd != lineEnd) { + lastLineEnd = lineEnd; + bool isFirstLine = lineEnd == 0; + if (isFirstLine) { + if (isRequest) { + lineSepPos = data.find(' '); + lineEnd = data.find("\r\n"); + headers["_method"] = data.substr(0, lineSepPos); + headers["_path"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1); + } else { + lineSepPos = data.find(' '); + lineEnd = data.find("\r\n"); + headers["_status"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1); + } + } else { + lineStart = lineEnd; + lineStart += 2; + lineEnd = data.find("\r\n", lineStart); + lineSepPos = data.find(':', lineStart); + if (lastLineEnd == lineEnd || lineEnd == string::npos) { + break; + } + headers[data.substr(lineStart, lineSepPos - lineStart)] = trim_copy(data.substr(lineSepPos + 1, lineEnd - lineSepPos - 1)); + } + } + + return headers; } -string HttpParser::parseHttp(bool isRequest, const string& data, unordered_map<string, string>& headers) { - bool onlyNewLineChar = false; - size_t headerEnd = data.find("\r\n\r\n"); - if (headerEnd == data.npos) { - headerEnd = data.find("\n\n"); - if (headerEnd != data.npos) { - onlyNewLineChar = true; - headerEnd += 2; - } - } else { - headerEnd += 4; - } - - size_t lineStart = 0; - size_t lineEnd = 0; - size_t lineSepPos = 0; - size_t lastLineEnd = data.npos; - while (lastLineEnd != lineEnd) { - lastLineEnd = lineEnd; - if (lineEnd == 0) { - if (isRequest) { - lineSepPos = data.find(' '); - lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n"); - headers["method"] = data.substr(0, lineSepPos); - headers["path"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1); - } else { - lineSepPos = data.find(' '); - lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n"); - headers["status"] = data.substr(lineSepPos + 1, data.find(' ', lineSepPos + 1) - lineSepPos - 1); - } - } else { - lineStart = lineEnd; - lineStart += onlyNewLineChar ? 1 : 2; - lineEnd = data.find(onlyNewLineChar ? "\n" : "\r\n", lineStart); - lineSepPos = data.find(':', lineStart); - if (lineEnd >= headerEnd || lastLineEnd == lineEnd || lineSepPos >= headerEnd) { - break; - } - headers[to_lower_copy(data.substr(lineStart, lineSepPos - lineStart))] = trim_copy(data.substr(lineSepPos + 1, lineEnd - lineSepPos - 1)); - } - } - - return headerEnd == data.npos ? "" : data.substr(headerEnd); -} - -string HttpParser::parseHttp(bool isRequest, const string& data) { - size_t headerEnd = data.find("\r\n\r\n"); - if (headerEnd != data.npos) { - headerEnd += 4; - } else { - headerEnd = data.find("\n\n"); - if (headerEnd != data.npos) { - headerEnd += 2; - } else { - headerEnd = 0; - } - } - return data.substr(headerEnd); +string HttpParser::extractBody(const string& data) const { + size_t headerEnd = data.find("\r\n\r\n"); + if (headerEnd == string::npos) { + return data; + } + headerEnd += 4; + return data.substr(headerEnd); } } diff --git a/src/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp index 30ce815..6af8a62 100644 --- a/src/net/TgLongPoll.cpp +++ b/src/net/TgLongPoll.cpp @@ -25,21 +25,21 @@ namespace TgBot { TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, int32_t limit, int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowupdates) - : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout), _allowupdates(allowupdates) { + : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout), _allowupdates(allowupdates) { } TgLongPoll::TgLongPoll(const Bot& bot, int32_t limit, int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowupdates) : - TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowupdates) { + TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowupdates) { } void TgLongPoll::start() { - std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowupdates); - for (Update::Ptr& item : updates) { - if (item->updateId >= _lastUpdateId) { - _lastUpdateId = item->updateId + 1; - } - _eventHandler->handleUpdate(item); - } + std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowupdates); + for (Update::Ptr& item : updates) { + if (item->updateId >= _lastUpdateId) { + _lastUpdateId = item->updateId + 1; + } + _eventHandler->handleUpdate(item); + } } } diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 005506c..1c7aae6 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -29,52 +29,52 @@ using namespace std; namespace TgBot { Url::Url(const string& url) { - bool isProtocolParsed = false; - bool isHostParsed = false; - bool isPathParsed = false; - bool isQueryParsed = false; + bool isProtocolParsed = false; + bool isHostParsed = false; + bool isPathParsed = false; + bool isQueryParsed = false; - for (size_t i = 0, count = url.length(); i < count; ++i) { - char c = url[i]; + for (size_t i = 0, count = url.length(); i < count; ++i) { + char c = url[i]; - if (!isProtocolParsed) { - if (c == ':') { - isProtocolParsed = true; - i += 2; - } else { - protocol += c; - } - } else if (!isHostParsed) { - if (c == '/') { - isHostParsed = true; - path += '/'; - } else if (c == '?') { - isHostParsed = isPathParsed = true; - path += '/'; - } else if (c == '#') { - isHostParsed = isPathParsed = isQueryParsed = true; - path += '/'; - } else { - host += c; - } - } else if (!isPathParsed) { - if (c == '?') { - isPathParsed = true; - } else if (c == '#') { - isPathParsed = isQueryParsed = true; - } else { - path += c; - } - } else if (!isQueryParsed) { - if (c == '#') { - isQueryParsed = true; - } else { - query += c; - } - } else { - fragment += c; - } - } + if (!isProtocolParsed) { + if (c == ':') { + isProtocolParsed = true; + i += 2; + } else { + protocol += c; + } + } else if (!isHostParsed) { + if (c == '/') { + isHostParsed = true; + path += '/'; + } else if (c == '?') { + isHostParsed = isPathParsed = true; + path += '/'; + } else if (c == '#') { + isHostParsed = isPathParsed = isQueryParsed = true; + path += '/'; + } else { + host += c; + } + } else if (!isPathParsed) { + if (c == '?') { + isPathParsed = true; + } else if (c == '#') { + isPathParsed = isQueryParsed = true; + } else { + path += c; + } + } else if (!isQueryParsed) { + if (c == '#') { + isQueryParsed = true; + } else { + query += c; + } + } else { + fragment += c; + } + } } } diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp index 105d80f..279f152 100644 --- a/src/tools/FileTools.cpp +++ b/src/tools/FileTools.cpp @@ -12,24 +12,24 @@ using namespace std; namespace FileTools { string read(const string& filePath) { - ifstream in(filePath, ios::in | ios::binary); - if (!in) { - throw system_error(errno, system_category()); - } - ostringstream contents; - contents << in.rdbuf(); - in.close(); - return contents.str(); + ifstream in(filePath, ios::in | ios::binary); + if (!in) { + throw system_error(errno, system_category()); + } + ostringstream contents; + contents << in.rdbuf(); + in.close(); + return contents.str(); } bool write(const string& content, const string& filePath) { - ofstream out(filePath, ios::out | ios::binary); - if (!out) { - return false; - } - out << content; - out.close(); - return true; + ofstream out(filePath, ios::out | ios::binary); + if (!out) { + return false; + } + out << content; + out.close(); + return true; } }; diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index 0e68dcc..5055d8e 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -22,100 +22,99 @@ #include "tgbot/tools/StringTools.h" -#include <stdlib.h> +#include <cstdlib> #include <iomanip> -#include <stdio.h> +#include <cstdio> using namespace std; namespace StringTools { bool startsWith(const string& str1, const string& str2) { - if (str1.length() < str2.length()) { - return false; - } - string::const_iterator it1(str1.begin()); - string::const_iterator end1(str1.end()); - string::const_iterator it2(str2.begin()); - string::const_iterator end2(str2.end()); - while (it1 != end1 && it2 != end2) { - if (*it1 != *it2) { - return false; - } - ++it1; - ++it2; - } - return true; + if (str1.length() < str2.length()) { + return false; + } + string::const_iterator it1(str1.begin()); + string::const_iterator end1(str1.end()); + string::const_iterator it2(str2.begin()); + string::const_iterator end2(str2.end()); + while (it1 != end1 && it2 != end2) { + if (*it1 != *it2) { + return false; + } + ++it1; + ++it2; + } + return true; } bool endsWith(const string& str1, const string& str2) { - if (str1.length() < str2.length()) { - return false; - } - string::const_iterator it1(str1.end()); - string::const_iterator begin1(str1.begin()); - string::const_iterator it2(str2.end()); - string::const_iterator begin2(str2.begin()); - --begin1; - --begin2; - while (it1 != begin1 && it2 != begin2) { - if (*it1 != *it2) { - return false; - } - --it1; - --it2; - } - return true; + if (str1.length() < str2.length()) { + return false; + } + string::const_iterator it1(str1.end()); + string::const_iterator begin1(str1.begin()); + string::const_iterator it2(str2.end()); + string::const_iterator begin2(str2.begin()); + --begin1; + --begin2; + while (it1 != begin1 && it2 != begin2) { + if (*it1 != *it2) { + return false; + } + --it1; + --it2; + } + return true; } void split(const string& str, char delimiter, vector<string>& dest) { - istringstream stream(str); - string s; - while (getline(stream, s, delimiter)) { - dest.push_back(s); - } + istringstream stream(str); + string s; + while (getline(stream, s, delimiter)) { + dest.push_back(s); + } } string generateRandomString(size_t length) { - static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~"); - static const size_t charsLen = chars.length(); - string result; - for (size_t i = 0; i < length; ++i) { - result += chars[rand() % charsLen]; - } - return result; + static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~"); + static const size_t charsLen = chars.length(); + string result; + for (size_t i = 0; i < length; ++i) { + result += chars[rand() % charsLen]; + } + return result; } string urlEncode(const string& value, const std::string& additionalLegitChars) { - static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:"; - std::stringstream ss; - for (auto const &c : value) { - if ((legitPunctuation.find(c) == std::string::npos) - && (additionalLegitChars.find(c)==std::string::npos)) { - ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c; - } else { - ss << c; - } - } + static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:"; + std::stringstream ss; + for (auto const &c : value) { + if ((legitPunctuation.find(c) == std::string::npos) && (additionalLegitChars.find(c) == std::string::npos)) { + ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c; + } else { + ss << c; + } + } - return ss.str(); + return ss.str(); } string urlDecode(const string& value) { - string result; - for (size_t i = 0, count = value.length(); i < count; ++i) { - const char c = value[i]; - if (c == '%') { - int t = 0; - sscanf(value.substr(i + 1, 2).c_str(), "%x", &t); - result += (char) t; - i += 2; - } else { - result += c; - } - } - return result; + string result; + for (size_t i = 0, count = value.length(); i < count; ++i) { + const char c = value[i]; + if (c == '%') { + int t = 0; + sscanf(value.substr(i + 1, 2).c_str(), "%x", &t); + result += (char) t; + i += 2; + } else { + result += c; + } + } + return result; } } diff --git a/src/types/InputFile.cpp b/src/types/InputFile.cpp index 4884488..94a591b 100644 --- a/src/types/InputFile.cpp +++ b/src/types/InputFile.cpp @@ -14,11 +14,11 @@ using namespace std; namespace TgBot { InputFile::Ptr InputFile::fromFile(const string& filePath, const string& mimeType) { - auto result(make_shared<InputFile>()); - result->data = FileTools::read(filePath); - result->mimeType = mimeType; - result->fileName = StringTools::split(filePath, '/').back(); - return result; + auto result(make_shared<InputFile>()); + result->data = FileTools::read(filePath); + result->mimeType = mimeType; + result->fileName = StringTools::split(filePath, '/').back(); + return result; } }; |