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 --- src/TgTypeParser.cpp | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) (limited to 'src/TgTypeParser.cpp') 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