From c39cc1b0ca79bb2bcd63c9f42658482d963462ac Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 13 Nov 2023 21:19:35 +0300 Subject: [PATCH] Allow to specify link preview options for sent text messages. --- telegram-bot-api/Client.cpp | 66 ++++++++++++++++++++++++++++++++----- telegram-bot-api/Client.h | 13 +++++--- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index ea18d9b..c8cc11a 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -6267,10 +6267,18 @@ td::Result> Client::get_input_me TRY_RESULT(message_text, object.get_optional_string_field("message_text")); if (!message_text.empty()) { - TRY_RESULT(disable_web_page_preview, object.get_optional_bool_field("disable_web_page_preview")); + object_ptr link_preview_options; + if (object.has_field("link_preview_options")) { + TRY_RESULT(options, object.extract_required_field("link_preview_options", td::JsonValue::Type::Object)); + CHECK(options.type() == td::JsonValue::Type::Object); + TRY_RESULT_ASSIGN(link_preview_options, get_link_preview_options(std::move(options))); + } else { + TRY_RESULT(disable_web_page_preview, object.get_optional_bool_field("disable_web_page_preview")); + link_preview_options = get_link_preview_options(disable_web_page_preview); + } TRY_RESULT(parse_mode, object.get_optional_string_field("parse_mode")); auto entities = object.extract_field("entities"); - TRY_RESULT(input_message_text, get_input_message_text(std::move(message_text), disable_web_page_preview, + TRY_RESULT(input_message_text, get_input_message_text(std::move(message_text), std::move(link_preview_options), std::move(parse_mode), std::move(entities))); return std::move(input_message_text); } @@ -6502,6 +6510,7 @@ td::Result> Client::get_inlin TRY_RESULT(input_message_content_obj, object.extract_optional_field("input_message_content", td::JsonValue::Type::Object)); if (input_message_content_obj.type() == td::JsonValue::Type::Null) { + // legacy TRY_RESULT(message_text, is_input_message_content_required ? object.get_required_string_field("message_text") : object.get_optional_string_field("message_text")); TRY_RESULT(disable_web_page_preview, object.get_optional_bool_field("disable_web_page_preview")); @@ -6509,8 +6518,9 @@ td::Result> Client::get_inlin auto entities = object.extract_field("entities"); if (is_input_message_content_required || !message_text.empty()) { - TRY_RESULT(input_message_text, get_input_message_text(std::move(message_text), disable_web_page_preview, - std::move(parse_mode), std::move(entities))); + TRY_RESULT(input_message_text, + get_input_message_text(std::move(message_text), get_link_preview_options(disable_web_page_preview), + std::move(parse_mode), std::move(entities))); input_message_content = std::move(input_message_text); } } else { @@ -7506,22 +7516,60 @@ td::Result> Client::get_formatted_text return make_object(text, std::move(entities)); } +td_api::object_ptr Client::get_link_preview_options(bool disable_web_page_preview) { + // legacy + if (!disable_web_page_preview) { + return nullptr; + } + return make_object(true, td::string(), false, false, false); +} + +td::Result> Client::get_link_preview_options(const Query *query) { + auto link_preview_options = query->arg("link_preview_options"); + if (link_preview_options.empty()) { + return get_link_preview_options(to_bool(query->arg("disable_web_page_preview"))); + } + + LOG(INFO) << "Parsing JSON object: " << link_preview_options; + auto r_value = json_decode(link_preview_options); + if (r_value.is_error()) { + LOG(INFO) << "Can't parse JSON object: " << r_value.error(); + return td::Status::Error(400, "Can't parse link preview options JSON object"); + } + + return get_link_preview_options(r_value.move_as_ok()); +} + +td::Result> Client::get_link_preview_options(td::JsonValue &&value) { + if (value.type() != td::JsonValue::Type::Object) { + return td::Status::Error(400, "Object expected as link preview options"); + } + auto &object = value.get_object(); + TRY_RESULT(is_disabled, object.get_optional_bool_field("is_disabled")); + TRY_RESULT(url, object.get_optional_string_field("url")); + TRY_RESULT(prefer_small_media, object.get_optional_bool_field("prefer_small_media")); + TRY_RESULT(prefer_large_media, object.get_optional_bool_field("prefer_large_media")); + TRY_RESULT(show_above_text, object.get_optional_bool_field("show_above_text")); + return make_object(is_disabled, url, prefer_small_media, prefer_large_media, + show_above_text); +} + td::Result> Client::get_input_message_text(const Query *query) { - return get_input_message_text(query->arg("text").str(), to_bool(query->arg("disable_web_page_preview")), + TRY_RESULT(link_preview_options, get_link_preview_options(query)); + return get_input_message_text(query->arg("text").str(), std::move(link_preview_options), query->arg("parse_mode").str(), get_input_entities(query, "entities")); } td::Result> Client::get_input_message_text( - td::string text, bool disable_web_page_preview, td::string parse_mode, td::JsonValue &&input_entities) { + td::string text, object_ptr link_preview_options, td::string parse_mode, + td::JsonValue &&input_entities) { if (text.empty()) { return td::Status::Error(400, "Message text is empty"); } TRY_RESULT(formatted_text, get_formatted_text(std::move(text), std::move(parse_mode), std::move(input_entities))); - return make_object( - std::move(formatted_text), - make_object(disable_web_page_preview, td::string(), false, false, false), false); + return make_object(std::move(formatted_text), std::move(link_preview_options), false); } td::Result> Client::get_location(const Query *query) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 202870c..073c8d4 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -464,12 +464,17 @@ class Client final : public WebhookActor::Callback { static td::Result> get_formatted_text(td::string text, td::string parse_mode, td::JsonValue &&input_entities); + static object_ptr get_link_preview_options(bool disable_web_page_preview); + + static td::Result> get_link_preview_options(const Query *query); + + static td::Result> get_link_preview_options(td::JsonValue &&value); + static td::Result> get_input_message_text(const Query *query); - static td::Result> get_input_message_text(td::string text, - bool disable_web_page_preview, - td::string parse_mode, - td::JsonValue &&input_entities); + static td::Result> get_input_message_text( + td::string text, object_ptr link_preview_options, td::string parse_mode, + td::JsonValue &&input_entities); static td::Result> get_location(const Query *query);