From 9c60e1f40132b2cf2190de55e057c442a2934440 Mon Sep 17 00:00:00 2001 From: Konstantin Kukin Date: Tue, 27 Dec 2016 19:52:26 +0300 Subject: add parse types to JSON part1 --- include/tgbot/TgTypeParser.h | 101 ++++++++++++++++++- include/tgbot/types/InlineQueryResult.h | 2 +- src/TgTypeParser.cpp | 168 ++++++++++++++++++++++++++++++++ 3 files changed, 269 insertions(+), 2 deletions(-) diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 9e5a1d7..214480a 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -40,21 +40,42 @@ #include "tgbot/types/Location.h" #include "tgbot/types/Update.h" #include "tgbot/types/UserProfilePhotos.h" +#include "tgbot/types/File.h" #include "tgbot/types/ReplyKeyboardMarkup.h" +#include "tgbot/types/KeyboardButton.h" +#include "tgbot/types/ReplyKeyboardRemove.h" #include "tgbot/types/ReplyKeyboardHide.h" #include "tgbot/types/ForceReply.h" +#include "tgbot/types/ChatMember.h" +#include "tgbot/types/ResponseParameters.h" #include "tgbot/types/GenericReply.h" #include "tgbot/types/InlineQuery.h" #include "tgbot/types/InlineQueryResult.h" +#include "tgbot/types/InlineQueryResultCachedAudio.h" +#include "tgbot/types/InlineQueryResultCachedDocument.h" +#include "tgbot/types/InlineQueryResultCachedGif.h" +#include "tgbot/types/InlineQueryResultCachedMpeg4Gif.h" +#include "tgbot/types/InlineQueryResultCachedPhoto.h" +#include "tgbot/types/InlineQueryResultCachedSticker.h" +#include "tgbot/types/InlineQueryResultCachedVideo.h" +#include "tgbot/types/InlineQueryResultCachedVoice.h" #include "tgbot/types/InlineQueryResultArticle.h" -#include "tgbot/types/InlineQueryResultPhoto.h" +#include "tgbot/types/InlineQueryResultAudio.h" +#include "tgbot/types/InlineQueryResultContact.h" +#include "tgbot/types/InlineQueryResultGame.h" +#include "tgbot/types/InlineQueryResultDocument.h" #include "tgbot/types/InlineQueryResultGif.h" +#include "tgbot/types/InlineQueryResultLocation.h" #include "tgbot/types/InlineQueryResultMpeg4Gif.h" +#include "tgbot/types/InlineQueryResultPhoto.h" +#include "tgbot/types/InlineQueryResultVenue.h" #include "tgbot/types/InlineQueryResultVideo.h" +#include "tgbot/types/InlineQueryResultVoice.h" #include "tgbot/types/ChosenInlineResult.h" #include "tgbot/types/CallbackQuery.h" #include "tgbot/types/InlineKeyboardMarkup.h" #include "tgbot/types/InlineKeyboardButton.h" +#include "tgbot/types/WebhookInfo.h" namespace TgBot { @@ -94,20 +115,72 @@ public: std::string parseUpdate(const Update::Ptr& object) const; UserProfilePhotos::Ptr parseJsonAndGetUserProfilePhotos(const boost::property_tree::ptree& data) const; std::string parseUserProfilePhotos(const UserProfilePhotos::Ptr& object) const; + + File::Ptr parseJsonAndGetFile(const boost::property_tree::ptree& data) const; + std::string parseFile(const File::Ptr& object) const; + ReplyKeyboardMarkup::Ptr parseJsonAndGetReplyKeyboardMarkup(const boost::property_tree::ptree& data) const; std::string parseReplyKeyboardMarkup(const ReplyKeyboardMarkup::Ptr& object) const; + + KeyboardButton::Ptr parseJsonAndGetKeyboardButton(const boost::property_tree::ptree& data) const; + std::string parseKeyboardButton(const KeyboardButton::Ptr& object) const; + + ReplyKeyboardRemove::Ptr parseJsonAndGetReplyKeyboardRemove(const boost::property_tree::ptree& data) const; + std::string parseReplyKeyboardRemove(const ReplyKeyboardRemove::Ptr& object) const; + ReplyKeyboardHide::Ptr parseJsonAndGetReplyKeyboardHide(const boost::property_tree::ptree& data) const; std::string parseReplyKeyboardHide(const ReplyKeyboardHide::Ptr& object) const; ForceReply::Ptr parseJsonAndGetForceReply(const boost::property_tree::ptree& data) const; std::string parseForceReply(const ForceReply::Ptr& object) const; + + ChatMember::Ptr parseJsonAndGetChatMember(const boost::property_tree::ptree& data) const; + std::string parseChatMember(const ChatMember::Ptr& object) const; + + ResponseParameters::Ptr parseJsonAndGetResponseParameters(const boost::property_tree::ptree& data) const; + std::string parseResponseParameters(const ResponseParameters::Ptr& object) const; + GenericReply::Ptr parseJsonAndGetGenericReply(const boost::property_tree::ptree& data) const; std::string parseGenericReply(const GenericReply::Ptr& object) const; + InlineQuery::Ptr parseJsonAndGetInlineQuery(const boost::property_tree::ptree& data) const; std::string parseInlineQuery(const InlineQuery::Ptr& object) const; + InlineQueryResult::Ptr parseJsonAndGetInlineQueryResult(const boost::property_tree::ptree& data) const; std::string parseInlineQueryResult(const InlineQueryResult::Ptr& object) const; + + InlineQueryResultCachedAudio::Ptr parseJsonAndGetInlineQueryResultCachedAudio(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedAudio(const InlineQueryResultCachedAudio::Ptr& object) const; + + InlineQueryResultCachedDocument::Ptr parseJsonAndGetInlineQueryResultCachedDocument(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedDocument(const InlineQueryResultCachedDocument::Ptr& object) const; + + InlineQueryResultCachedGif::Ptr parseJsonAndGetInlineQueryResultCachedGif(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedGif(const InlineQueryResultCachedGif::Ptr& object) const; + + InlineQueryResultCachedMpeg4Gif::Ptr parseJsonAndGetInlineQueryResultCachedMpeg4Gif(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedMpeg4Gif(const InlineQueryResultCachedMpeg4Gif::Ptr& object) const; + + InlineQueryResultCachedPhoto::Ptr parseJsonAndGetInlineQueryResultCachedPhoto(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedPhoto(const InlineQueryResultCachedPhoto::Ptr& object) const; + + InlineQueryResultCachedSticker::Ptr parseJsonAndGetInlineQueryResultCachedSticker(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedSticker(const InlineQueryResultCachedSticker::Ptr& object) const; + + InlineQueryResultCachedVideo::Ptr parseJsonAndGetInlineQueryResultCachedVideo(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedVideo(const InlineQueryResultCachedVideo::Ptr& object) const; + + InlineQueryResultCachedVoice::Ptr parseJsonAndGetInlineQueryResultCachedVoice(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultCachedVoice(const InlineQueryResultCachedVoice::Ptr& object) const; + InlineQueryResultArticle::Ptr parseJsonAndGetInlineQueryResultArticle(const boost::property_tree::ptree& data) const; std::string parseInlineQueryResultArticle(const InlineQueryResultArticle::Ptr& object) const; + + InlineQueryResultAudio::Ptr parseJsonAndGetInlineQueryResultAudio(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultAudio(const InlineQueryResultAudio::Ptr& object) const; + + InlineQueryResultContact::Ptr parseJsonAndGetInlineQueryResultContact(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultContact(const InlineQueryResultContact::Ptr& object) const; + InlineQueryResultPhoto::Ptr parseJsonAndGetInlineQueryResultPhoto(const boost::property_tree::ptree& data) const; std::string parseInlineQueryResultPhoto(const InlineQueryResultPhoto::Ptr& object) const; InlineQueryResultGif::Ptr parseJsonAndGetInlineQueryResultGif(const boost::property_tree::ptree& data) const; @@ -118,6 +191,7 @@ public: std::string parseInlineQueryResultVideo(const InlineQueryResultVideo::Ptr& object) const; ChosenInlineResult::Ptr parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const; std::string parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const; + CallbackQuery::Ptr parseJsonAndGetCallbackQuery(const boost::property_tree::ptree& data) const; std::string parseCallbackQuery(const CallbackQuery::Ptr& object) const; InlineKeyboardMarkup::Ptr parseJsonAndGetInlineKeyboardMarkup(const boost::property_tree::ptree& data) const; @@ -125,6 +199,9 @@ public: InlineKeyboardButton::Ptr parseJsonAndGetInlineKeyboardButton(const boost::property_tree::ptree& data) const; std::string parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const; + WebhookInfo::Ptr parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const; + std::string parseWebhookInfo(const WebhookInfo::Ptr& object) const; + inline boost::property_tree::ptree parseJson(const std::string& json) const { boost::property_tree::ptree tree; std::istringstream input(json); @@ -150,6 +227,15 @@ public: return result; } + template + std::vector parseJsonAndGetArray(std::function parseFunc, const boost::property_tree::ptree& data) const { + std::vector result; + for (const std::pair& innerTreeItem : data) { + result.push_back(parseFunc(innerTreeItem.second)); + } + return result; + } + template std::vector> parseJsonAndGetArray(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { std::vector> result; @@ -193,6 +279,19 @@ public: return result; } + template + std::string parseArray(std::function parseFunc, const std::vector& objects) const { + std::string result; + result += '['; + for (const T& item : objects) { + result += parseFunc(item); + result += ','; + } + result.erase(result.length() - 1); + result += ']'; + return result; + } + template std::string parse2DArray(TgTypeToJsonFunc parseFunc, const std::vector>>& objects) const { std::string result; diff --git a/include/tgbot/types/InlineQueryResult.h b/include/tgbot/types/InlineQueryResult.h index 6dad81b..29e6f7a 100644 --- a/include/tgbot/types/InlineQueryResult.h +++ b/include/tgbot/types/InlineQueryResult.h @@ -22,7 +22,7 @@ public: typedef std::shared_ptr Ptr; InlineQueryResult() { - //this->disableWebPagePreview = false; + } virtual ~InlineQueryResult() { } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 6bd77a7..ea176b0 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -49,6 +49,7 @@ Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { result->username = data.get("username", ""); result->firstName = data.get("first_name", ""); result->lastName = data.get("last_name", ""); + result->allMembersAreAdministrators = data.get("all_members_are_administrators", false); return result; } @@ -108,6 +109,7 @@ MessageEntity::Ptr TgTypeParser::parseJsonAndGetEntity(const ptree& data) const{ result->offset=data.get("offset"); result->length=data.get("length"); result->url=data.get("url", ""); + result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); return result; } @@ -208,6 +210,8 @@ Audio::Ptr TgTypeParser::parseJsonAndGetAudio(const ptree& data) const { Audio::Ptr result(new Audio); result->fileId = data.get("file_id"); result->duration = data.get("duration"); + result->performer = data.get("performer", ""); + result->title = data.get("title", ""); result->mimeType = data.get("mime_type", ""); result->fileSize = data.get("file_size", 0); return result; @@ -260,6 +264,7 @@ Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const ptree& data) const { result->width = data.get("width"); result->height = data.get("height"); result->thumb = tryParseJson(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); + result->emoji = data.get("emoji"); result->fileSize = data.get("file_size", 0); return result; } @@ -274,6 +279,7 @@ string TgTypeParser::parseSticker(const Sticker::Ptr& object) const { 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); result.erase(result.length() - 1); result += '}'; @@ -400,6 +406,28 @@ string TgTypeParser::parseUserProfilePhotos(const UserProfilePhotos::Ptr& object return result; } +File::Ptr TgTypeParser::parseJsonAndGetFile(const boost::property_tree::ptree& data) const { + File::Ptr result(new File); + result->fileId = data.get("file_id"); + result->filePath = data.get("file_size", 0); + result->filePath = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + ReplyKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardMarkup(const boost::property_tree::ptree& data) const { ReplyKeyboardMarkup::Ptr result(new ReplyKeyboardMarkup); for (const pair& item : data.find("keyboard")->second) { @@ -442,6 +470,47 @@ std::string TgTypeParser::parseReplyKeyboardMarkup(const ReplyKeyboardMarkup::Pt return result; } +KeyboardButton::Ptr TgTypeParser::parseJsonAndGetKeyboardButton(const boost::property_tree::ptree& data) const { + KeyboardButton::Ptr result(new KeyboardButton); + result->text = data.get("text"); + result->requestContact = data.get("request_contact", false); + result->requestLocation = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +ReplyKeyboardRemove::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardRemove(const boost::property_tree::ptree& data) const { + ReplyKeyboardRemove::Ptr result(new ReplyKeyboardRemove); + result->selective = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + ReplyKeyboardHide::Ptr TgTypeParser::parseJsonAndGetReplyKeyboardHide(const boost::property_tree::ptree& data) const { ReplyKeyboardHide::Ptr result(new ReplyKeyboardHide); result->selective = data.get("selective"); @@ -480,6 +549,46 @@ std::string TgTypeParser::parseForceReply(const ForceReply::Ptr& object) const { return result; } +ChatMember::Ptr TgTypeParser::parseJsonAndGetChatMember(const boost::property_tree::ptree& data) const { + ChatMember::Ptr result(new ChatMember); + result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->status = data.get("status"); + 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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +ResponseParameters::Ptr TgTypeParser::parseJsonAndGetResponseParameters(const boost::property_tree::ptree& data) const { + ResponseParameters::Ptr result(new ResponseParameters); + result->migrateToChatId = data.get("migrate_to_chat_id", 0); + result->retryAfter = data.get("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); + result.erase(result.length() - 1); + 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(parseJsonAndGetForceReply(data)); @@ -511,6 +620,7 @@ InlineQuery::Ptr TgTypeParser::parseJsonAndGetInlineQuery(const boost::property_ InlineQuery::Ptr result(new InlineQuery); result->id = data.get("id"); result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->location = tryParseJson(&TgTypeParser::parseJsonAndGetLocation, data, "location"); result->query = data.get("query"); result->offset = data.get("offset"); @@ -525,6 +635,7 @@ std::string TgTypeParser::parseInlineQuery(const InlineQuery::Ptr& object) const 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); result.erase(result.length() - 1); @@ -536,6 +647,19 @@ InlineQueryResult::Ptr TgTypeParser::parseJsonAndGetInlineQueryResult(const boos string type = data.get("type"); InlineQueryResult::Ptr result; + /*if (type == InlineQueryResultCachedAudio::TYPE) { + result = static_pointer_cast(parseJsonAndGetInlineQueryResultArticle(data)); + } else if (type == InlineQueryResultCachedDocument::TYPE) { + + } else if (type == InlineQueryResultCachedDocument::TYPE) { + + } else if (type == InlineQueryResultCachedDocument::TYPE) { + + } else if (type == InlineQueryResultCachedDocument::TYPE) { + + } else if (type == InlineQueryResultCachedDocument::TYPE) { + + }*/ if (type == InlineQueryResultArticle::TYPE) { result = static_pointer_cast(parseJsonAndGetInlineQueryResultArticle(data)); } else if (type == InlineQueryResultPhoto::TYPE) { @@ -753,6 +877,8 @@ CallbackQuery::Ptr TgTypeParser::parseJsonAndGetCallbackQuery(const boost::prope result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); result->message = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "message"); result->inlineMessageId = data.get("inline_message_id", ""); + result->chatInstance = data.get("chat_instance"); + result->gameShortName = data.get("game_short_name", ""); result->data = data.get("data", ""); return result; } @@ -768,6 +894,8 @@ std::string TgTypeParser::parseCallbackQuery(const CallbackQuery::Ptr& object) c 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); result.erase(result.length() - 1); result += '}'; @@ -809,6 +937,7 @@ InlineKeyboardButton::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardButton(cons result->url = data.get("url", ""); result->callbackData = data.get("callback_data", ""); result->switchInlineQuery = data.get("switch_inline_query", ""); + result->switchInlineQueryCurrentChat = data.get("switch_inline_query_current_chat", ""); return result; } std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const { @@ -821,6 +950,45 @@ std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton:: 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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const { + WebhookInfo::Ptr result(new WebhookInfo); + result->url = data.get("url"); + result->hasCustomCertificate = data.get("has_custom_certificate"); + result->pendingUpdateCount = data.get("pending_update_count"); + result->lastErrorDate = data.get("last_error_date", 0); + result->lastErrorMessage = data.get("last_error_message", ""); + result->maxConnections = data.get("max_connections", 0); + result->allowedUpdates = parseJsonAndGetArray( + [](const boost::property_tree::ptree& innerData)->std::string { + return innerData.get(""); + } + , data); + 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([](const std::string &s)->std::string { + return s; + } + , object->allowedUpdates)); result.erase(result.length() - 1); result += '}'; return result; -- cgit v1.2.3