From e7da8e88e0717236972f4a1d5efd496e974975f5 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 24 Feb 2023 15:08:16 +0300 Subject: [PATCH] Add td_api::getWebAppLinkUrl. --- td/generate/scheme/td_api.tl | 12 +++++- td/telegram/AttachMenuManager.cpp | 66 +++++++++++++++++++++++++++++++ td/telegram/AttachMenuManager.h | 4 ++ td/telegram/Td.cpp | 19 +++++++++ td/telegram/Td.h | 2 + td/telegram/cli.cpp | 8 ++++ 6 files changed, 110 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 17ec7fd33..5da94370d 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4721,7 +4721,7 @@ internalLinkTypeUserToken token:string = InternalLinkType; internalLinkTypeVideoChat chat_username:string invite_hash:string is_live_stream:Bool = InternalLinkType; //@description The link is a link to a Web App. Call searchPublicChat with the given bot username, check that the user is a bot, then call searchWebApp with the received bot and the given web_app_short_name. -//-Process received foundWebApp by showing a confirmation dialog if needed, calling getWebAppLinkUrl and opening returned URL +//-Process received foundWebApp by showing a confirmation dialog if needed, then calling getWebAppLinkUrl and opening the returned URL //@bot_username Username of the bot that owns the Web App //@web_app_short_name Short name of the Web App //@start_parameter Start parameter to be passed to getWebAppLinkUrl @@ -6549,6 +6549,16 @@ answerInlineQuery inline_query_id:int64 is_personal:Bool button:inlineQueryResul //@web_app_short_name Short name of the Web App searchWebApp bot_user_id:int53 web_app_short_name:string = FoundWebApp; +//@description Returns an HTTPS URL of a Web App to open after a link of the type internalLinkTypeWebApp is clicked +//@chat_id Identifier of the chat in which the link was clicked; pass 0 if none +//@bot_user_id Identifier of the target bot +//@web_app_short_name Short name of the Web App +//@start_parameter Start parameter from internalLinkTypeWebApp +//@theme Preferred Web App theme; pass null to use the default theme +//@application_name Short name of the application; 0-64 English letters, digits, and underscores +//@allow_write_access Pass true if the current user allowed the bot to send them messages +getWebAppLinkUrl chat_id:int53 bot_user_id:int53 web_app_short_name:string start_parameter:string theme:themeParameters application_name:string allow_write_access:Bool = HttpUrl; + //@description Returns an HTTPS URL of a Web App to open after keyboardButtonTypeWebApp or inlineQueryResultsButtonTypeWebApp button is pressed //@bot_user_id Identifier of the target bot //@url The URL from the keyboardButtonTypeWebApp or inlineQueryResultsButtonTypeWebApp button diff --git a/td/telegram/AttachMenuManager.cpp b/td/telegram/AttachMenuManager.cpp index 063cb3030..64405ba9d 100644 --- a/td/telegram/AttachMenuManager.cpp +++ b/td/telegram/AttachMenuManager.cpp @@ -66,6 +66,55 @@ class GetBotAppQuery final : public Td::ResultHandler { } }; +class RequestAppWebViewQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit RequestAppWebViewQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(DialogId dialog_id, telegram_api::object_ptr &&input_user, + const string &web_app_short_name, const string &start_parameter, + const td_api::object_ptr &theme, const string &platform, bool allow_write_access) { + telegram_api::object_ptr theme_parameters; + int32 flags = 0; + if (theme != nullptr) { + flags |= telegram_api::messages_requestAppWebView::THEME_PARAMS_MASK; + + theme_parameters = make_tl_object(string()); + theme_parameters->data_ = ThemeManager::get_theme_parameters_json_string(theme, false); + } + if (allow_write_access) { + flags |= telegram_api::messages_requestAppWebView::WRITE_ALLOWED_MASK; + } + if (!start_parameter.empty()) { + flags |= telegram_api::messages_requestAppWebView::START_PARAM_MASK; + } + auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Read); + CHECK(input_peer != nullptr); + auto input_bot_app = + telegram_api::make_object(std::move(input_user), web_app_short_name); + send_query(G()->net_query_creator().create(telegram_api::messages_requestAppWebView( + flags, false /*ignored*/, std::move(input_peer), std::move(input_bot_app), start_parameter, + std::move(theme_parameters), platform))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for RequestAppWebViewQuery: " << to_string(ptr); + promise_.set_value(std::move(ptr->url_)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class RequestWebViewQuery final : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; @@ -648,6 +697,7 @@ void AttachMenuManager::schedule_ping_web_view() { void AttachMenuManager::get_web_app(UserId bot_user_id, string &&web_app_short_name, Promise> &&promise) { TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(bot_user_id)); + TRY_RESULT_PROMISE(promise, bot_data, td_->contacts_manager_->get_bot_data(bot_user_id)); auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), bot_user_id, web_app_short_name, promise = std::move(promise)]( Result> result) mutable { @@ -676,12 +726,28 @@ void AttachMenuManager::on_get_web_app(UserId bot_user_id, string web_app_short_ bot_app->request_write_access_, !bot_app->inactive_)); } +void AttachMenuManager::request_app_web_view(DialogId dialog_id, UserId bot_user_id, string &&web_app_short_name, + string &&start_parameter, + const td_api::object_ptr &theme, + string &&platform, bool allow_write_access, Promise &&promise) { + if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Read)) { + dialog_id = DialogId(bot_user_id); + } + TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(bot_user_id)); + TRY_RESULT_PROMISE(promise, bot_data, td_->contacts_manager_->get_bot_data(bot_user_id)); + + td_->create_handler(std::move(promise)) + ->send(dialog_id, std::move(input_user), web_app_short_name, start_parameter, theme, platform, + allow_write_access); +} + void AttachMenuManager::request_web_view(DialogId dialog_id, UserId bot_user_id, MessageId top_thread_message_id, MessageId reply_to_message_id, string &&url, td_api::object_ptr &&theme, string &&platform, Promise> &&promise) { TRY_STATUS_PROMISE(promise, td_->contacts_manager_->get_bot_data(bot_user_id)); TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(bot_user_id)); + TRY_RESULT_PROMISE(promise, bot_data, td_->contacts_manager_->get_bot_data(bot_user_id)); if (!td_->messages_manager_->have_dialog_force(dialog_id, "request_web_view")) { return promise.set_error(Status::Error(400, "Chat not found")); diff --git a/td/telegram/AttachMenuManager.h b/td/telegram/AttachMenuManager.h index 8ebc44f18..b298e48cd 100644 --- a/td/telegram/AttachMenuManager.h +++ b/td/telegram/AttachMenuManager.h @@ -35,6 +35,10 @@ class AttachMenuManager final : public Actor { void get_web_app(UserId bot_user_id, string &&web_app_short_name, Promise> &&promise); + void request_app_web_view(DialogId dialog_id, UserId bot_user_id, string &&web_app_short_name, + string &&start_parameter, const td_api::object_ptr &theme, + string &&platform, bool allow_write_access, Promise &&promise); + void request_web_view(DialogId dialog_id, UserId bot_user_id, MessageId top_thread_message_id, MessageId reply_to_message_id, string &&url, td_api::object_ptr &&theme, string &&platform, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index bb83efcd3..d2e588a58 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7778,6 +7778,25 @@ void Td::on_request(uint64 id, td_api::searchWebApp &request) { std::move(promise)); } +void Td::on_request(uint64 id, td_api::getWebAppLinkUrl &request) { + CHECK_IS_USER(); + CLEAN_INPUT_STRING(request.web_app_short_name_); + CLEAN_INPUT_STRING(request.start_parameter_); + CLEAN_INPUT_STRING(request.application_name_); + CREATE_REQUEST_PROMISE(); + auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + promise.set_value(td_api::make_object(result.move_as_ok())); + } + }); + attach_menu_manager_->request_app_web_view( + DialogId(request.chat_id_), UserId(request.bot_user_id_), std::move(request.web_app_short_name_), + std::move(request.start_parameter_), std::move(request.theme_), std::move(request.application_name_), + request.allow_write_access_, std::move(query_promise)); +} + void Td::on_request(uint64 id, td_api::getWebAppUrl &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.url_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index de79d05da..9880b2954 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1332,6 +1332,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::searchWebApp &request); + void on_request(uint64 id, td_api::getWebAppLinkUrl &request); + void on_request(uint64 id, td_api::getWebAppUrl &request); void on_request(uint64 id, td_api::sendWebAppData &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index ae1513541..f616e79cd 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3746,6 +3746,14 @@ class CliClient final : public Actor { string short_name; get_args(args, bot_user_id, short_name); send_request(td_api::make_object(bot_user_id, short_name)); + } else if (op == "gwalu") { + ChatId chat_id; + UserId bot_user_id; + string short_name; + string start_parameter; + get_args(args, chat_id, bot_user_id, short_name, start_parameter); + send_request(td_api::make_object(chat_id, bot_user_id, short_name, start_parameter, + as_theme_parameters(), "android", true)); } else if (op == "gwau") { UserId bot_user_id; string url;