From 66cbca3b206567ce62af42cab950696ef6f80adb Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 27 May 2018 02:27:44 +0900 Subject: Bot API 3.0 (Maks Mazurov (fox.cpp)) --- src/TgTypeParser.cpp | 232 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 1b80d7f..c442600 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -85,6 +85,7 @@ User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { result->firstName = data.get("first_name"); result->lastName = data.get("last_name", ""); result->username = data.get("username", ""); + result->languageCode = data.get("language_code", ""); return result; } @@ -98,6 +99,7 @@ string TgTypeParser::parseUser(const User::Ptr& object) const { appendToJson(result, "first_name", object->firstName); appendToJson(result, "last_name", object->lastName); appendToJson(result, "username", object->username); + appendToJson(result, "language_code", object->languageCode); result.erase(result.length() - 1); result += '}'; return result; @@ -135,6 +137,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->contact = tryParseJson(&TgTypeParser::parseJsonAndGetContact, data, "contact"); result->location = tryParseJson(&TgTypeParser::parseJsonAndGetLocation, data, "location"); result->newChatMember = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_participant"); + result->newChatMembers = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_members"); result->leftChatMember = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "left_chat_participant"); result->newChatTitle = data.get("new_chat_title", ""); result->newChatPhoto = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetPhotoSize, data, "new_chat_photo"); @@ -173,6 +176,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const { 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)); @@ -322,6 +326,32 @@ string TgTypeParser::parseVideo(const Video::Ptr& object) const { return result; } +VideoNote::Ptr TgTypeParser::parseJsonAndGetVideoNote(const ptree& data) const { + VideoNote::Ptr result(new VideoNote); + result->fileId = data.get("file_id"); + result->length = data.get("length"); + result->duration = data.get("duration"); + result->thumb = tryParseJson(&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; +} + Contact::Ptr TgTypeParser::parseJsonAndGetContact(const ptree& data) const { auto result(make_shared()); result->phoneNumber = data.get("phone_number"); @@ -1178,6 +1208,7 @@ InlineQueryResultGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGif(cons result->gifUrl = data.get("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("thumb_url"); return result; } @@ -1191,6 +1222,7 @@ std::string TgTypeParser::parseInlineQueryResultGif(const InlineQueryResultGif:: 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; @@ -1202,6 +1234,7 @@ InlineQueryResultMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultMpe result->mpeg4Url = data.get("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("thumb_url"); return result; } @@ -1216,6 +1249,7 @@ std::string TgTypeParser::parseInlineQueryResultMpeg4Gif(const InlineQueryResult 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; @@ -1539,6 +1573,204 @@ std::string TgTypeParser::parseInputContactMessageContent(const InputContactMess return result; } +Invoice::Ptr TgTypeParser::parseJsonAndGetInvoice(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->title = data.get("title"); + result->description = data.get("description"); + result->startParameter = data.get("start_parameter"); + result->currency = data.get("currency"); + result->totalAmount = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +LabeledPrice::Ptr TgTypeParser::parseJsonAndGetLabeledPrice(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->label = data.get("label"); + result->amount = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +OrderInfo::Ptr TgTypeParser::parseJsonAndGetOrderInfo(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->name = data.get("name", ""); + result->phoneNumber = data.get("phone_number", ""); + result->email = data.get("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 += ","; + } + result.erase(result.length() - 1); + result += '}'; + return result; +} + +PreCheckoutQuery::Ptr TgTypeParser::parseJsonAndGetPreCheckoutQuery(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->id = data.get("id"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->currency = data.get("currency"); + result->totalAmount = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +ShippingAddress::Ptr TgTypeParser::parseJsonAndGetShippingAddress(const boost::property_tree::ptree& data) const { + ShippingAddress::Ptr result; + result->countryCode = data.get("country_code"); + result->state = data.get("state", ""); + result->city = data.get("city"); + result->streetLine1 = data.get("street_line1"); + result->streetLine2 = data.get("street_line2"); + result->postCode = data.get("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); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +ShippingOption::Ptr TgTypeParser::parseJsonAndGetShippingOption(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->id = data.get("id"); + result->title = data.get("title"); + result->prices = parseJsonAndGetArray(&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); + result.erase(result.length() - 1); + 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()); + result->id = data.get("id"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->invoicePayload = data.get("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 += ","; + result.erase(result.length() - 1); + result += '}'; + return result; +} + +SuccessfulPayment::Ptr TgTypeParser::parseJsonAndGetSucessfulPayment(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->currency = data.get("currency"); + result->totalAmount = data.get("total_amount"); + result->invoicePayload = data.get("invoice_payload"); + result->shippingOptionId = data.get("shipping_option_id"); + result->orderInfo = tryParseJson(&TgTypeParser::parseJsonAndGetOrderInfo, data, "order_info"); + return result; +} + +std::string TgTypeParser::parseSucessfulPayment(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 += ","; + result.erase(result.length() - 1); + result += '}'; + return result; +} + + void TgTypeParser::appendToJson(string& json, const string& varName, const string& value) const { if (value.empty()) { return; -- cgit v1.2.3 From 1deb50e9db6e1b668af8c993b449d59f001c8977 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 27 May 2018 16:35:52 +0900 Subject: Bot API 3.1 update --- src/TgTypeParser.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index c442600..9a48de5 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -50,6 +50,12 @@ Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { result->firstName = data.get("first_name", ""); result->lastName = data.get("last_name", ""); result->allMembersAreAdministrators = data.get("all_members_are_administrators", false); + result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); + result->description = data.get("description", ""); + result->inviteLink = data.get("invite_link", ""); + result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); + result->stickerSetName = data.get("sticker_set_name", ""); + result->canSetStickerSet = data.get("can_set_sticker_set", false); return result; } @@ -107,10 +113,10 @@ string TgTypeParser::parseUser(const User::Ptr& object) const { MessageEntity::Ptr TgTypeParser::parseJsonAndGetEntity(const ptree& data) const{ auto result(make_shared()); - result->type=data.get("type"); - result->offset=data.get("offset"); - result->length=data.get("length"); - result->url=data.get("url", ""); + result->type = data.get("type"); + result->offset = data.get("offset"); + result->length = data.get("length"); + result->url = data.get("url", ""); result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); return result; } @@ -571,7 +577,21 @@ std::string TgTypeParser::parseForceReply(const ForceReply::Ptr& object) const { ChatMember::Ptr TgTypeParser::parseJsonAndGetChatMember(const boost::property_tree::ptree& data) const { auto result(make_shared()); result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); - result->status = data.get("status"); + result->status = data.get("status", ""); + result->untilDate = data.get("until_date", 0); + result->canBeEdited = data.get("can_be_edited", false); + result->canChangeInfo = data.get("can_change_info", false); + result->canPostMessages = data.get("can_post_messages", false); + result->canEditMessages = data.get("can_edit_messages", false); + result->canDeleteMessages = data.get("can_delete_messages", false); + result->canInviteUsers = data.get("can_invite_users", false); + result->canRestrictMembers = data.get("can_restrict_members", false); + result->canPinMessages = data.get("can_pin_messages", false); + result->canPromoteMembers = data.get("can_promote_messages", false); + result->canSendMessages = data.get("can_send_messages", false); + result->canSendMediaMessages = data.get("can_send_media_messages", false); + result->canSendOtherMessages = data.get("can_send_other_messages", false); + result->canAddWebPagePreviews = data.get("can_add_web_page_previews", false); return result; } @@ -588,6 +608,13 @@ std::string TgTypeParser::parseChatMember(const ChatMember::Ptr& object) const { return result; } +ChatPhoto::Ptr TgTypeParser::parseJsonAndGetChatPhoto(const boost::property_tree::ptree& data) const { + auto result(make_shared()); + result->smallFileId = data.get("small_file_id", ""); + result->bigFileId = data.get("big_file_id", ""); + return result; +} + ResponseParameters::Ptr TgTypeParser::parseJsonAndGetResponseParameters(const boost::property_tree::ptree& data) const { auto result(make_shared()); result->migrateToChatId = data.get("migrate_to_chat_id", 0); -- cgit v1.2.3 From 8206df62f7f9540a2f13ce31c85f8a1f2054bd56 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 27 May 2018 17:30:39 +0900 Subject: Fix build error (cross-reference) --- src/TgTypeParser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 9a48de5..0facf93 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -53,7 +53,7 @@ Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); result->description = data.get("description", ""); result->inviteLink = data.get("invite_link", ""); - result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); + result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); result->stickerSetName = data.get("sticker_set_name", ""); result->canSetStickerSet = data.get("can_set_sticker_set", false); -- cgit v1.2.3 From c81f7d9d7a4fe9dc3b48b92fad633bd5c44b8e74 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sun, 27 May 2018 17:50:32 +0900 Subject: Bot API 3.3 update --- src/TgTypeParser.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/TgTypeParser.cpp') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 0facf93..fafe94f 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -47,7 +47,7 @@ Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { } result->title = data.get("title", ""); result->username = data.get("username", ""); - result->firstName = data.get("first_name", ""); + result->firstName = data.get("first_name", ""); result->lastName = data.get("last_name", ""); result->allMembersAreAdministrators = data.get("all_members_are_administrators", false); result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); @@ -88,6 +88,7 @@ string TgTypeParser::parseChat(const Chat::Ptr& object) const { User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { auto result(make_shared()); result->id = data.get("id"); + result->isBot = data.get("is_bot", false); result->firstName = data.get("first_name"); result->lastName = data.get("last_name", ""); result->username = data.get("username", ""); @@ -102,6 +103,7 @@ string TgTypeParser::parseUser(const User::Ptr& object) const { 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); @@ -130,9 +132,11 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->forwardFrom = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); result->forwardFromChat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat"); result->forwardFromMessageId = data.get("forward_from_message_id", 0); + result->forwardSignature = data.get("forward_signature", ""); result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); result->editDate = data.get("edit_date", 0); + result->authorSignature = data.get("author_signature", ""); result->text = data.get("text", ""); result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetEntity, data, "entities"); result->audio = tryParseJson