#include "tgbot/TgTypeParser.h" namespace TgBot { Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->updateId = data.get("update_id", 0); result->message = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "message"); result->editedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "edited_message"); result->channelPost = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "channel_post"); result->editedChannelPost = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "edited_channel_post"); result->messageReaction = tryParseJson(&TgTypeParser::parseJsonAndGetMessageReactionUpdated, data, "message_reaction"); result->messageReactionCount = tryParseJson(&TgTypeParser::parseJsonAndGetMessageReactionCountUpdated, data, "message_reaction_count"); result->inlineQuery = tryParseJson(&TgTypeParser::parseJsonAndGetInlineQuery, data, "inline_query"); result->chosenInlineResult = tryParseJson(&TgTypeParser::parseJsonAndGetChosenInlineResult, data, "chosen_inline_result"); result->callbackQuery = tryParseJson(&TgTypeParser::parseJsonAndGetCallbackQuery, data, "callback_query"); result->shippingQuery = tryParseJson(&TgTypeParser::parseJsonAndGetShippingQuery, data, "shipping_query"); result->preCheckoutQuery = tryParseJson(&TgTypeParser::parseJsonAndGetPreCheckoutQuery, data, "pre_checkout_query"); result->poll = tryParseJson(&TgTypeParser::parseJsonAndGetPoll, data, "poll"); result->pollAnswer = tryParseJson(&TgTypeParser::parseJsonAndGetPollAnswer, data, "poll_answer"); result->myChatMember = tryParseJson(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "my_chat_member"); result->chatMember = tryParseJson(&TgTypeParser::parseJsonAndGetChatMemberUpdated, data, "chat_member"); result->chatJoinRequest = tryParseJson(&TgTypeParser::parseJsonAndGetChatJoinRequest, data, "chat_join_request"); result->chatBoost = tryParseJson(&TgTypeParser::parseJsonAndGetChatBoostUpdated, data, "chat_boost"); result->removedChatBoost = tryParseJson(&TgTypeParser::parseJsonAndGetChatBoostRemoved, data, "removed_chat_boost"); return result; } std::string TgTypeParser::parseUpdate(const Update::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; appendToJson(result, "update_id", object->updateId); appendToJson(result, "message", parseMessage(object->message)); appendToJson(result, "edited_message", parseMessage(object->editedMessage)); appendToJson(result, "channel_post", parseMessage(object->channelPost)); appendToJson(result, "edited_channel_post", parseMessage(object->editedChannelPost)); appendToJson(result, "message_reaction", parseMessageReactionUpdated(object->messageReaction)); appendToJson(result, "message_reaction_count", parseMessageReactionCountUpdated(object->messageReactionCount)); appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery)); appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)); appendToJson(result, "callback_query", parseCallbackQuery(object->callbackQuery)); appendToJson(result, "shipping_query", parseShippingQuery(object->shippingQuery)); appendToJson(result, "pre_checkout_query", parsePreCheckoutQuery(object->preCheckoutQuery)); appendToJson(result, "poll", parsePoll(object->poll)); appendToJson(result, "poll_answer", parsePollAnswer(object->pollAnswer)); appendToJson(result, "my_chat_member", parseChatMemberUpdated(object->myChatMember)); appendToJson(result, "chat_member", parseChatMemberUpdated(object->chatMember)); appendToJson(result, "chat_join_request", parseChatJoinRequest(object->chatJoinRequest)); appendToJson(result, "chat_boost", parseChatBoostUpdated(object->chatBoost)); appendToJson(result, "removed_chat_boost", parseChatBoostRemoved(object->removedChatBoost)); removeLastComma(result); result += '}'; return result; } WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->url = data.get("url", ""); result->hasCustomCertificate = data.get("has_custom_certificate", false); result->pendingUpdateCount = data.get("pending_update_count", 0); result->ipAddress = data.get("ip_address", ""); result->lastErrorDate = data.get("last_error_date", 0); result->lastErrorMessage = data.get("last_error_message", ""); result->lastSynchronizationErrorDate = data.get("last_synchronization_error_date", 0); result->maxConnections = data.get("max_connections", 0); result->allowedUpdates = parseJsonAndGetArray( [] (const boost::property_tree::ptree& innerData)->std::string { return innerData.get(""); } , data, "allowed_updates"); return result; } std::string TgTypeParser::parseWebhookInfo(const WebhookInfo::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; appendToJson(result, "url", object->url); appendToJson(result, "has_custom_certificate", object->hasCustomCertificate); appendToJson(result, "pending_update_count", object->pendingUpdateCount); appendToJson(result, "ip_address", object->ipAddress); appendToJson(result, "last_error_date", object->lastErrorDate); appendToJson(result, "last_error_message", object->lastErrorMessage); appendToJson(result, "last_synchronization_error_date", object->lastSynchronizationErrorDate); appendToJson(result, "max_connections", object->maxConnections); appendToJson(result, "allowed_updates", parseArray([] (const std::string& s)->std::string { return s; } , object->allowedUpdates)); removeLastComma(result); result += '}'; return result; } User::Ptr TgTypeParser::parseJsonAndGetUser(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->id = data.get("id", 0); result->isBot = data.get("is_bot", false); result->firstName = data.get("first_name", ""); result->lastName = data.get("last_name", ""); result->username = data.get("username", ""); result->languageCode = data.get("language_code", ""); result->isPremium = data.get("is_premium", false); result->addedToAttachmentMenu = data.get("added_to_attachment_menu", false); result->canJoinGroups = data.get("can_join_groups", false); result->canReadAllGroupMessages = data.get("can_read_all_group_messages", false); result->supportsInlineQueries = data.get("supports_inline_queries", false); return result; } std::string TgTypeParser::parseUser(const User::Ptr& object) const { if (!object) { return ""; } std::string result; result += '{'; appendToJson(result, "id", object->id); appendToJson(result, "is_bot", object->isBot); appendToJson(result, "first_name", object->firstName); appendToJson(result, "last_name", object->lastName); appendToJson(result, "username", object->username); appendToJson(result, "language_code", object->languageCode); appendToJson(result, "is_premium", object->isPremium); appendToJson(result, "added_to_attachment_menu", object->addedToAttachmentMenu); appendToJson(result, "can_join_groups", object->canJoinGroups); appendToJson(result, "can_read_all_group_messages", object->canReadAllGroupMessages); appendToJson(result, "supports_inline_queries", object->supportsInlineQueries); removeLastComma(result); result += '}'; return result; } Chat::Ptr TgTypeParser::parseJsonAndGetChat(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->id = data.get("id", 0); std::string type = data.get("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->isForum = data.get("is_forum", false); result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); result->activeUsernames = parseJsonAndGetArray( [] (const boost::property_tree::ptree& innerData)->std::string { return innerData.get(""); }, data, "active_usernames"); result->availableReactions = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetReactionType, data, "available_reactions"); result->accentColorId = data.get("accent_color_id", 0); result->backgroundCustomEmojiId = data.get("background_custom_emoji_id", ""); result->profileAccentColorId = data.get("profile_accent_color_id", 0); result->profileBackgroundCustomEmojiId = data.get("profile_background_custom_emoji_id", ""); result->emojiStatusCustomEmojiId = data.get("emoji_status_custom_emoji_id", ""); result->emojiStatusExpirationDate = data.get("emoji_status_expiration_date", 0); result->bio = data.get("bio", ""); result->hasPrivateForwards = data.get("has_private_forwards", false); result->hasRestrictedVoiceAndVideoMessages = data.get("has_restricted_voice_and_video_messages", false); result->joinToSendMessages = data.get("join_to_send_messages", false); result->joinByRequest = data.get("join_by_request", false); result->description = data.get("description", ""); result->inviteLink = data.get("invite_link", ""); result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); result->permissions = tryParseJson(&TgTypeParser::parseJsonAndGetChatPermissions, data, "permissions"); result->slowModeDelay = data.get("slow_mode_delay", 0); result->messageAutoDeleteTime = data.get("message_auto_delete_time", 0); result->hasAggressiveAntiSpamEnabled = data.get("has_aggressive_anti_spam_enabled", false); result->hasHiddenMembers = data.get("has_hidden_members", false); result->hasProtectedContent = data.get("has_protected_content", false); result->hasVisibleHistory = data.get("has_visible_history", false); result->stickerSetName = data.get("sticker_set_name", ""); result->canSetStickerSet = data.get("can_set_sticker_set", false); result->linkedChatId = data.get("linked_chat_id", 0); result->location = tryParseJson(&TgTypeParser::parseJsonAndGetChatLocation, data, "location"); return result; } std::string TgTypeParser::parseChat(const Chat::Ptr& object) const { if (!object) { return ""; } std::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); appendToJson(result, "is_forum", object->isForum); appendToJson(result, "photo", parseChatPhoto(object->photo)); appendToJson(result, "active_usernames", parseArray( [] (const std::string& s)->std::string { return s; }, object->activeUsernames)); appendToJson(result, "available_reactions", parseArray(&TgTypeParser::parseReactionType, object->availableReactions)); appendToJson(result, "accent_color_id", object->accentColorId); appendToJson(result, "background_custom_emoji_id", object->backgroundCustomEmojiId); appendToJson(result, "profile_accent_color_id", object->profileAccentColorId); appendToJson(result, "profile_background_custom_emoji_id", object->profileBackgroundCustomEmojiId); appendToJson(result, "emoji_status_custom_emoji_id", object->emojiStatusCustomEmojiId); appendToJson(result, "emoji_status_expiration_date", object->emojiStatusExpirationDate); appendToJson(result, "bio", object->bio); appendToJson(result, "has_private_forwards", object->hasPrivateForwards); appendToJson(result, "has_restricted_voice_and_video_messages", object->hasRestrictedVoiceAndVideoMessages); appendToJson(result, "join_to_send_messages", object->joinToSendMessages); appendToJson(result, "join_by_request", object->joinByRequest); appendToJson(result, "description", object->description); appendToJson(result, "invite_link", object->inviteLink); appendToJson(result, "pinned_message", parseMessage(object->pinnedMessage)); appendToJson(result, "permissions", parseChatPermissions(object->permissions)); appendToJson(result, "slow_mode_delay", object->slowModeDelay); appendToJson(result, "message_auto_delete_time", object->messageAutoDeleteTime); appendToJson(result, "has_aggressive_anti_spam_enabled", object->hasAggressiveAntiSpamEnabled); appendToJson(result, "has_hidden_members", object->hasHiddenMembers); appendToJson(result, "has_protected_content", object->hasProtectedContent); appendToJson(result, "has_visible_history", object->hasVisibleHistory); appendToJson(result, "sticker_set_name", object->stickerSetName); appendToJson(result, "can_set_sticker_set", object->canSetStickerSet); appendToJson(result, "linked_chat_id", object->linkedChatId); appendToJson(result, "location", parseChatLocation(object->location)); removeLastComma(result); result += '}'; return result; } Message::Ptr TgTypeParser::parseJsonAndGetMessage(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->messageId = data.get("message_id", 0); result->messageThreadId = data.get("message_thread_id", 0); result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); result->senderChat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "sender_chat"); result->chat = parseJsonAndGetChat(data.find("chat")->second); result->date = data.get("date", 0); result->forwardOrigin = tryParseJson(&TgTypeParser::parseJsonAndGetMessageOrigin, data, "forward_origin"); result->isTopicMessage = data.get("is_topic_message", false); result->isAutomaticForward = data.get("is_automatic_forward", false); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); result->externalReply = tryParseJson(&TgTypeParser::parseJsonAndGetExternalReplyInfo, data, "external_reply"); result->quote = tryParseJson(&TgTypeParser::parseJsonAndGetTextQuote, data, "quote"); result->viaBot = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "via_bot"); result->editDate = data.get("edit_date", 0); result->hasProtectedContent = data.get("has_protected_content", false); result->mediaGroupId = data.get("media_group_id", ""); result->authorSignature = data.get("author_signature", ""); result->text = data.get("text", ""); result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); result->linkPreviewOptions = tryParseJson(&TgTypeParser::parseJsonAndGetLinkPreviewOptions, data, "link_preview_options"); result->animation = tryParseJson(&TgTypeParser::parseJsonAndGetAnimation, data, "animation"); result->audio = tryParseJson