From 1720ba3e81f7302a4e12e1f0d18e8da68a7905c5 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 22 Feb 2023 21:01:05 +0300 Subject: [PATCH] Support "web_app" button in inline query results. --- telegram-bot-api/Client.cpp | 63 ++++++++++++++++++++++++++++++++----- telegram-bot-api/Client.h | 6 ++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index bc2af0b..01c8d46 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -6039,6 +6039,54 @@ td_api::object_ptr Client::get_message_send_options( return make_object(disable_notification, false, protect_content, false, nullptr, 0); } +td::Result> Client::get_inline_query_results_button( + td::JsonValue &&value) { + if (value.type() != td::JsonValue::Type::Object) { + return td::Status::Error(400, "InlineQueryResultsButton must be an Object"); + } + + auto &object = value.get_object(); + + TRY_RESULT(text, get_json_object_string_field(object, "text", false)); + + if (has_json_object_field(object, "start_parameter")) { + TRY_RESULT(start_parameter, get_json_object_string_field(object, "start_parameter", false)); + return make_object( + text, make_object(start_parameter)); + } + + if (has_json_object_field(object, "web_app")) { + TRY_RESULT(web_app, get_json_object_field(object, "web_app", td::JsonValue::Type::Object, false)); + auto &web_app_object = web_app.get_object(); + TRY_RESULT(url, get_json_object_string_field(web_app_object, "url", false)); + return make_object(text, + make_object(url)); + } + + return td::Status::Error(400, "InlineQueryResultsButton must have exactly one optional field"); +} + +td::Result> Client::get_inline_query_results_button( + td::MutableSlice button) { + if (button.empty()) { + return nullptr; + } + + LOG(INFO) << "Parsing JSON object: " << button; + auto r_value = json_decode(button); + if (r_value.is_error()) { + LOG(INFO) << "Can't parse JSON object: " << r_value.error(); + return td::Status::Error(400, "Can't parse inline query results button JSON object"); + } + + auto r_button = get_inline_query_results_button(r_value.move_as_ok()); + if (r_button.is_error()) { + return td::Status::Error(400, PSLICE() + << "Can't parse inline query results button: " << r_button.error().message()); + } + return r_button.move_as_ok(); +} + td::Result>> Client::get_inline_query_results( const Query *query) { auto results_encoded = query->arg("results"); @@ -8422,14 +8470,15 @@ td::Status Client::process_answer_inline_query_query(PromisedQueryPtr &query) { auto is_personal = to_bool(query->arg("is_personal")); int32 cache_time = get_integer_arg(query.get(), "cache_time", 300, 0, 24 * 60 * 60); auto next_offset = query->arg("next_offset"); - auto switch_pm_text = query->arg("switch_pm_text"); - object_ptr button; - if (!switch_pm_text.empty()) { - button = make_object( - switch_pm_text.str(), - make_object(query->arg("switch_pm_parameter").str())); + TRY_RESULT(button, get_inline_query_results_button(query->arg("button"))); + if (button == nullptr) { + auto switch_pm_text = query->arg("switch_pm_text"); + if (!switch_pm_text.empty()) { + button = make_object( + switch_pm_text.str(), + make_object(query->arg("switch_pm_parameter").str())); + } } - TRY_RESULT(results, get_inline_query_results(query.get())); resolve_inline_query_results_bot_usernames( diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index a0b4525..564c49e 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -358,6 +358,12 @@ class Client final : public WebhookActor::Callback { object_ptr get_input_thumbnail(const Query *query) const; + static td::Result> get_inline_query_results_button( + td::JsonValue &&value); + + static td::Result> get_inline_query_results_button( + td::MutableSlice value); + td::Result> get_inline_query_result(const Query *query); td::Result> get_inline_query_result(td::JsonValue &&value);