From d1b0d86fa02ab56beae47bfab149c02a1f16662e Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 5 Jul 2026 23:11:19 +0300 Subject: Add get_input_page_blocks. --- telegram-bot-api/Client.cpp | 161 ++++++++++++++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 8 +++ 2 files changed, 169 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index a1823ef..c82bb87 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -12084,6 +12084,167 @@ td::Result> Client::get_rich_text(td::JsonV } } +td::Result> Client::get_input_page_block_list_item( + const Query *query, td::JsonValue &&value) const { + if (value.type() != td::JsonValue::Type::Object) { + return td::Status::Error(400, "Object expected as InputRichBlockListItem"); + } + auto &object = value.get_object(); + TRY_RESULT(blocks, get_input_page_blocks(query, object.extract_field("blocks"))); + TRY_RESULT(has_checkbox, object.get_optional_bool_field("has_checkbox")); + TRY_RESULT(is_checked, object.get_optional_bool_field("is_checked")); + TRY_RESULT(item_value, object.get_optional_int_field("value")); + TRY_RESULT(type, object.get_optional_string_field("type")); + return make_object(std::move(blocks), has_checkbox, is_checked, item_value, type); +} + +td::Result> Client::get_input_page_block(const Query *query, + td::JsonValue &&value) const { + if (value.type() != td::JsonValue::Type::Object) { + return td::Status::Error(400, "Object expected as InputRichMessageBlock"); + } + auto &object = value.get_object(); + TRY_RESULT(type, object.get_required_string_field("type")); + if (type == "heading") { + TRY_RESULT(text, get_rich_text(object.extract_field("text"))); + TRY_RESULT(size, object.get_required_int_field("size")); + return make_object(std::move(text), size); + } + if (type == "paragraph") { + TRY_RESULT(text, get_rich_text(object.extract_field("text"))); + return make_object(std::move(text)); + } + if (type == "pre") { + TRY_RESULT(text, get_rich_text(object.extract_field("text"))); + TRY_RESULT(language, object.get_optional_string_field("language")); + return make_object(std::move(text), language); + } + if (type == "footer") { + TRY_RESULT(text, get_rich_text(object.extract_field("text"))); + return make_object(std::move(text)); + } + if (type == "thinking") { + TRY_RESULT(text, get_rich_text(object.extract_field("text"))); + return make_object(std::move(text)); + } + if (type == "divider") { + return make_object(); + } + if (type == "mathematical_expression") { + TRY_RESULT(expression, object.get_required_string_field("expression")); + return make_object(expression); + } + if (type == "anchor") { + TRY_RESULT(name, object.get_required_string_field("name")); + return make_object(name); + } + if (type == "list") { + TRY_RESULT(items, get_array_generic( + object.extract_field("items"), "InputRichBlockListItem", [&](td::JsonValue &&item) { + return get_input_page_block_list_item(query, std::move(item)); + })); + return make_object(std::move(items)); + } + if (type == "blockquote") { + TRY_RESULT(blocks, get_input_page_blocks(query, object.extract_field("blocks"))); + TRY_RESULT(credit, get_rich_text(object.extract_field("credit"))); + return make_object(std::move(blocks), std::move(credit)); + } + if (type == "pullquote") { + TRY_RESULT(text, get_rich_text(object.extract_field("text"))); + TRY_RESULT(credit, get_rich_text(object.extract_field("credit"))); + return make_object(std::move(text), std::move(credit)); + } + if (type == "collage") { + TRY_RESULT(blocks, get_input_page_blocks(query, object.extract_field("blocks"))); + TRY_RESULT(caption, get_page_block_caption(object.extract_field("caption"))); + return make_object(std::move(blocks), std::move(caption)); + } + if (type == "slideshow") { + TRY_RESULT(blocks, get_input_page_blocks(query, object.extract_field("blocks"))); + TRY_RESULT(caption, get_page_block_caption(object.extract_field("caption"))); + return make_object(std::move(blocks), std::move(caption)); + } + if (type == "table") { + td::vector>> cells; + TRY_RESULT(cells_array, object.extract_required_field("cells", td::JsonValue::Type::Array)); + for (auto &row : cells_array.get_array()) { + TRY_RESULT(new_row, get_array(std::move(row), "PageBlockTableCell", get_page_block_table_cell)); + cells.push_back(std::move(new_row)); + } + TRY_RESULT(caption, get_rich_text(object.extract_field("caption"))); + TRY_RESULT(is_bordered, object.get_optional_bool_field("is_bordered")); + TRY_RESULT(is_striped, object.get_optional_bool_field("is_striped")); + return make_object(std::move(caption), std::move(cells), is_bordered, is_striped); + } + if (type == "details") { + TRY_RESULT(summary, get_rich_text(object.extract_field("summary"))); + TRY_RESULT(blocks, get_input_page_blocks(query, object.extract_field("blocks"))); + TRY_RESULT(is_open, object.get_optional_bool_field("is_open")); + return make_object(std::move(summary), std::move(blocks), is_open); + } + if (type == "map") { + TRY_RESULT(location_field, object.extract_required_field("location", td::JsonValue::Type::Object)); + auto &location_object = location_field.get_object(); + TRY_RESULT(location, get_location(location_object)); + TRY_RESULT(zoom, object.get_optional_int_field("zoom")); + TRY_RESULT(width, object.get_optional_int_field("width")); + TRY_RESULT(height, object.get_optional_int_field("height")); + TRY_RESULT(caption, get_page_block_caption(object.extract_field("caption"))); + return make_object(std::move(location), zoom, width, height, std::move(caption)); + } + if (type != "animation" && type != "audio" && type != "photo" && type != "video" && type != "voice_note") { + return td::Status::Error(400, PSLICE() << "type \"" << type << "\" is unsupported"); + } + TRY_RESULT(media_field, object.extract_required_field(type, td::JsonValue::Type::Object)); + auto &media_object = media_field.get_object(); + TRY_RESULT(media_type, media_object.get_required_string_field("type")); + if (media_type != type) { + return td::Status::Error(400, PSLICE() + << "Unexpected media type \"" << media_type << "\" for block \"" << type << "\""); + } + TRY_RESULT(input_message_content, get_input_media(query, media_object, type, nullptr, false, false, false, true)); + TRY_RESULT(caption, get_page_block_caption(object.extract_field("caption"))); + TRY_RESULT(has_spoiler, media_object.get_optional_bool_field("has_spoiler")); + if (type == "animation") { + CHECK(input_message_content->get_id() == td_api::inputMessageAnimation::ID); + return make_object( + std::move(static_cast(input_message_content.get())->animation_), + std::move(caption), has_spoiler); + } + if (type == "audio") { + CHECK(input_message_content->get_id() == td_api::inputMessageAudio::ID); + return make_object( + std::move(static_cast(input_message_content.get())->audio_), std::move(caption)); + } + if (type == "photo") { + CHECK(input_message_content->get_id() == td_api::inputMessagePhoto::ID); + return make_object( + std::move(static_cast(input_message_content.get())->photo_), std::move(caption), + has_spoiler); + } + if (type == "video") { + CHECK(input_message_content->get_id() == td_api::inputMessageVideo::ID); + return make_object( + std::move(static_cast(input_message_content.get())->video_), std::move(caption), + has_spoiler); + } + if (type == "voice_note") { + CHECK(input_message_content->get_id() == td_api::inputMessageVoiceNote::ID); + return make_object( + std::move(static_cast(input_message_content.get())->voice_note_), + std::move(caption)); + } + UNREACHABLE(); +} + +td::Result>> Client::get_input_page_blocks( + const Query *query, td::JsonValue &&value) const { + return get_array_generic(std::move(value), "InputRichBlock", [&](td::JsonValue &&block) { + return get_input_page_block(query, std::move(block)); + }); +} + td::Result> Client::get_input_rich_message(const Query *query) const { auto rich_message = query->arg("rich_message"); if (rich_message.empty()) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index c8b7aa1..6ce5973 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -677,6 +677,14 @@ class Client final : public WebhookActor::Callback { static td::Result> get_rich_text(td::JsonValue &&value); + td::Result> get_input_page_block_list_item( + const Query *query, td::JsonValue &&value) const; + + td::Result> get_input_page_block(const Query *query, td::JsonValue &&value) const; + + td::Result>> get_input_page_blocks(const Query *query, + td::JsonValue &&value) const; + td::Result> get_input_rich_message(const Query *query) const; td::Result> get_input_rich_message(const Query *query, -- cgit v1.2.3