From 314068fbcc815e35a5a3a28b7538f4a615dc5c96 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 3 Jul 2026 18:18:37 +0300 Subject: Support media in InputRichMessage. --- telegram-bot-api/Client.cpp | 38 ++++++++++++++++++++++---------------- telegram-bot-api/Client.h | 26 ++++++++++++++------------ 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index a60967f..ce97bba 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -10695,13 +10695,13 @@ td::Result> Client::get_input_thumbna } td::Result> Client::get_input_message_content( - td::JsonValue &input_message_content, bool is_input_message_content_required) { + const Query *query, td::JsonValue &input_message_content, bool is_input_message_content_required) const { CHECK(input_message_content.type() == td::JsonValue::Type::Object); auto &object = input_message_content.get_object(); if (object.has_field("rich_message")) { TRY_RESULT(rich_message, object.extract_required_field("rich_message", td::JsonValue::Type::Object)); - TRY_RESULT(input_rich_message, get_input_rich_message(std::move(rich_message))); + TRY_RESULT(input_rich_message, get_input_rich_message(query, std::move(rich_message))); return make_object(std::move(input_rich_message), false); } @@ -10855,7 +10855,7 @@ td::Result> Client::get_inl } td::Result>> Client::get_inline_query_results( - const Query *query, BotUserIds &bot_user_ids) { + const Query *query, BotUserIds &bot_user_ids) const { auto results_encoded = query->arg("results"); if (results_encoded.empty()) { return td::vector>(); @@ -10868,11 +10868,11 @@ td::Result>> Clien 400, PSLICE() << "Can't parse JSON encoded inline query results: " << r_values.error().message()); } - return get_inline_query_results(r_values.move_as_ok(), bot_user_ids); + return get_inline_query_results(query, r_values.move_as_ok(), bot_user_ids); } td::Result>> Client::get_inline_query_results( - td::JsonValue &&values, BotUserIds &bot_user_ids) { + const Query *query, td::JsonValue &&values, BotUserIds &bot_user_ids) const { if (values.type() == td::JsonValue::Type::Null) { return td::vector>(); } @@ -10886,7 +10886,7 @@ td::Result>> Clien td::vector> inline_query_results; for (auto &value : values.get_array()) { - auto r_inline_query_result = get_inline_query_result(std::move(value), bot_user_ids); + auto r_inline_query_result = get_inline_query_result(query, std::move(value), bot_user_ids); if (r_inline_query_result.is_error()) { return td::Status::Error( 400, PSLICE() << "Can't parse inline query result: " << r_inline_query_result.error().message()); @@ -10898,7 +10898,7 @@ td::Result>> Clien } td::Result> Client::get_inline_query_result( - const Query *query, BotUserIds &bot_user_ids) { + const Query *query, BotUserIds &bot_user_ids) const { auto result_encoded = query->arg("result"); if (result_encoded.empty()) { return td::Status::Error(400, "Result isn't specified"); @@ -10911,11 +10911,11 @@ td::Result> Client::get_inlin 400, PSLICE() << "Can't parse JSON encoded web view query results " << r_value.error().message()); } - return get_inline_query_result(r_value.move_as_ok(), bot_user_ids); + return get_inline_query_result(query, r_value.move_as_ok(), bot_user_ids); } td::Result> Client::get_inline_query_result( - td::JsonValue &&value, BotUserIds &bot_user_ids) { + const Query *query, td::JsonValue &&value, BotUserIds &bot_user_ids) const { if (value.type() != td::JsonValue::Type::Object) { return td::Status::Error(400, "Inline query result must be an object"); } @@ -10948,7 +10948,7 @@ td::Result> Client::get_inlin } } else { TRY_RESULT(input_message_content_result, - get_input_message_content(input_message_content_obj, is_input_message_content_required)); + get_input_message_content(query, input_message_content_obj, is_input_message_content_required)); input_message_content = std::move(input_message_content_result); } TRY_RESULT(input_caption, object.get_optional_string_field("caption")); @@ -11981,7 +11981,7 @@ td::Result> Client::get_input_messa return make_object(std::move(formatted_text), std::move(link_preview_options), false); } -td::Result> Client::get_input_rich_message(const Query *query) { +td::Result> Client::get_input_rich_message(const Query *query) const { auto rich_message = query->arg("rich_message"); if (rich_message.empty()) { return td::Status::Error(400, "Rich message must be non-empty"); @@ -11994,10 +11994,11 @@ td::Result> Client::get_input_rich_ return td::Status::Error(400, "Can't parse rich message JSON object"); } - return get_input_rich_message(r_value.move_as_ok()); + return get_input_rich_message(query, r_value.move_as_ok()); } -td::Result> Client::get_input_rich_message(td::JsonValue &&value) { +td::Result> Client::get_input_rich_message(const Query *query, + td::JsonValue &&value) const { if (value.type() != td::JsonValue::Type::Object) { return td::Status::Error(400, "Object expected as rich message"); } @@ -12007,10 +12008,12 @@ td::Result> Client::get_input_rich_ auto result = make_object(nullptr, is_rtl, !skip_entity_detection); if (object.has_field("markdown")) { TRY_RESULT(text, object.get_required_string_field("markdown")); - result->source_ = make_object(text, td::Auto()); + TRY_RESULT(media, get_input_rich_message_medias(query, object.extract_field("media"))); + result->source_ = make_object(text, std::move(media)); } else if (object.has_field("html")) { TRY_RESULT(text, object.get_required_string_field("html")); - result->source_ = make_object(text, td::Auto()); + TRY_RESULT(media, get_input_rich_message_medias(query, object.extract_field("media"))); + result->source_ = make_object(text, std::move(media)); } else { return td::Status::Error(400, "Rich message must be non-empty"); } @@ -12541,11 +12544,14 @@ td::Result> Client::get_input_ td::Result>> Client::get_input_rich_message_medias( const Query *query, td::JsonValue &&value) const { + td::vector> media; if (value.type() != td::JsonValue::Type::Array) { + if (value.type() == td::JsonValue::Type::Null) { + return std::move(media); + } return td::Status::Error(400, "Expected an Array of InputRichMessageMedia"); } - td::vector> media; for (auto &input_media : value.get_array()) { auto r_input_media = get_input_rich_message_media(query, std::move(input_media)); if (r_input_media.is_error()) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index a42a014..d95a03e 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -542,8 +542,8 @@ class Client final : public WebhookActor::Callback { static td::Result>> get_shipping_options(td::JsonValue &&value); - static td::Result> get_input_message_content( - td::JsonValue &input_message_content, bool is_input_message_content_required); + td::Result> get_input_message_content( + const Query *query, td::JsonValue &input_message_content, bool is_input_message_content_required) const; static object_ptr get_chat_action(const Query *query); @@ -565,17 +565,18 @@ class Client final : public WebhookActor::Callback { static td::Result> get_inline_query_results_button( td::MutableSlice value); - static td::Result> get_inline_query_result(const Query *query, - BotUserIds &bot_user_ids); + td::Result> get_inline_query_result(const Query *query, + BotUserIds &bot_user_ids) const; - static td::Result> get_inline_query_result(td::JsonValue &&value, - BotUserIds &bot_user_ids); + td::Result> get_inline_query_result(const Query *query, + td::JsonValue &&value, + BotUserIds &bot_user_ids) const; - static td::Result>> get_inline_query_results( - const Query *query, BotUserIds &bot_user_ids); + td::Result>> get_inline_query_results( + const Query *query, BotUserIds &bot_user_ids) const; - static td::Result>> get_inline_query_results( - td::JsonValue &&value, BotUserIds &bot_user_ids); + td::Result>> get_inline_query_results( + const Query *query, td::JsonValue &&value, BotUserIds &bot_user_ids) const; struct BotCommandScope { object_ptr scope_; @@ -663,9 +664,10 @@ class Client final : public WebhookActor::Callback { td::string text, object_ptr link_preview_options, td::string parse_mode, td::JsonValue &&input_entities); - static td::Result> get_input_rich_message(const Query *query); + td::Result> get_input_rich_message(const Query *query) const; - static td::Result> get_input_rich_message(td::JsonValue &&value); + td::Result> get_input_rich_message(const Query *query, + td::JsonValue &&value) const; static td::Result> get_location(const Query *query); -- cgit v1.2.3