From c1aa1defbbe87e1b142acae2ac3c4b83d987c1d5 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 31 Mar 2022 21:45:35 +0300 Subject: [PATCH] Add td_api::closeWebApp. --- td/generate/scheme/td_api.tl | 3 +++ td/telegram/AttachMenuManager.cpp | 32 +++++++++++++++++++++++++++---- td/telegram/AttachMenuManager.h | 12 ++++++++++++ td/telegram/ReplyMarkup.cpp | 2 +- td/telegram/Td.cpp | 6 ++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 4 ++++ 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 19ecb22e6..75e2d1608 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4878,6 +4878,9 @@ sendWebAppData bot_user_id:int53 button_text:string data:string = Ok; //@reply_to_message_id Identifier of the replied message for the message sent by the web app; 0 if none openWebApp chat_id:int53 bot_user_id:int53 url:string from_bot_menu:Bool theme:themeParameters reply_to_message_id:int53 = WebAppInfo; +//@description Informs TDLib that a previously opened web app was closed @launch_id Identifier of web app launch, received from openWebApp +closeWebApp launch_id:int64 = Ok; + //@description Sets the result of interaction with a web app and sends corresponding message on behalf of the user to the chat from which the query originated; for bots only //@web_app_query_id Identifier of the web app query //@result The result of the query diff --git a/td/telegram/AttachMenuManager.cpp b/td/telegram/AttachMenuManager.cpp index 9a70f2fb0..476314851 100644 --- a/td/telegram/AttachMenuManager.cpp +++ b/td/telegram/AttachMenuManager.cpp @@ -31,6 +31,8 @@ namespace td { class RequestWebViewQuery final : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; + UserId bot_user_id_; + MessageId reply_to_message_id_; bool from_attach_menu_ = false; public: @@ -38,9 +40,12 @@ class RequestWebViewQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(DialogId dialog_id, tl_object_ptr &&input_user, string &&url, bool from_bot_menu, - td_api::object_ptr &&theme, MessageId reply_to_message_id, bool silent) { + void send(DialogId dialog_id, UserId bot_user_id, tl_object_ptr &&input_user, string &&url, + bool from_bot_menu, td_api::object_ptr &&theme, MessageId reply_to_message_id, + bool silent) { dialog_id_ = dialog_id; + bot_user_id_ = bot_user_id; + reply_to_message_id_ = reply_to_message_id; int32 flags = 0; @@ -89,6 +94,7 @@ class RequestWebViewQuery final : public Td::ResultHandler { } auto ptr = result_ptr.move_as_ok(); + td_->attach_menu_manager_->open_web_view(ptr->query_id_, dialog_id_, bot_user_id_, reply_to_message_id_); promise_.set_value(td_api::make_object(ptr->query_id_, ptr->url_)); } @@ -432,8 +438,26 @@ void AttachMenuManager::request_web_view(DialogId dialog_id, UserId bot_user_id, bool silent = td_->messages_manager_->get_dialog_silent_send_message(dialog_id); td_->create_handler(std::move(promise)) - ->send(dialog_id, std::move(input_user), std::move(url), from_bot_menu, std::move(theme), reply_to_message_id, - silent); + ->send(dialog_id, bot_user_id, std::move(input_user), std::move(url), from_bot_menu, std::move(theme), + reply_to_message_id, silent); +} + +void AttachMenuManager::open_web_view(int64 query_id, DialogId dialog_id, UserId bot_user_id, + MessageId reply_to_message_id) { + if (query_id == 0) { + LOG(ERROR) << "Receive web app query identifier == 0"; + return; + } + OpenedWebView opened_web_view; + opened_web_view.dialog_id_ = dialog_id; + opened_web_view.bot_user_id_ = bot_user_id; + opened_web_view.reply_to_message_id_ = reply_to_message_id; + opened_web_views_.emplace(query_id, std::move(opened_web_view)); +} + +void AttachMenuManager::close_web_view(int64 query_id, Promise &&promise) { + opened_web_views_.erase(query_id); + promise.set_value(Unit()); } Result AttachMenuManager::get_attach_menu_bot( diff --git a/td/telegram/AttachMenuManager.h b/td/telegram/AttachMenuManager.h index 015258b82..57fb7c504 100644 --- a/td/telegram/AttachMenuManager.h +++ b/td/telegram/AttachMenuManager.h @@ -17,6 +17,7 @@ #include "td/actor/PromiseFuture.h" #include "td/utils/common.h" +#include "td/utils/FlatHashSet.h" #include "td/utils/Status.h" namespace td { @@ -33,6 +34,10 @@ class AttachMenuManager final : public Actor { bool from_bot_menu, td_api::object_ptr &&theme, Promise> &&promise); + void open_web_view(int64 query_id, DialogId dialog_id, UserId bot_user_id, MessageId reply_to_message_id); + + void close_web_view(int64 query_id, Promise &&promise); + void reload_attach_menu_bots(Promise &&promise); void get_attach_menu_bot(UserId user_id, Promise> &&promise); @@ -117,6 +122,13 @@ class AttachMenuManager final : public Actor { bool is_inited_ = false; int64 hash_ = 0; vector attach_menu_bots_; + + struct OpenedWebView { + DialogId dialog_id_; + UserId bot_user_id_; + MessageId reply_to_message_id_; + }; + FlatHashMap opened_web_views_; }; } // namespace td diff --git a/td/telegram/ReplyMarkup.cpp b/td/telegram/ReplyMarkup.cpp index 5785d97b4..97cd65ba8 100644 --- a/td/telegram/ReplyMarkup.cpp +++ b/td/telegram/ReplyMarkup.cpp @@ -442,7 +442,7 @@ static Result get_keyboard_button(tl_object_ptr(button->type_); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index fc705f9ed..095d79947 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7369,6 +7369,12 @@ void Td::on_request(uint64 id, td_api::openWebApp &request) { request.from_bot_menu_, std::move(request.theme_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::closeWebApp &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + attach_menu_manager_->close_web_view(request.launch_id_, std::move(promise)); +} + void Td::on_request(uint64 id, td_api::answerWebAppQuery &request) { CHECK_IS_BOT(); CLEAN_INPUT_STRING(request.web_app_query_id_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 43440b56a..0c5edf876 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1161,6 +1161,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::openWebApp &request); + void on_request(uint64 id, const td_api::closeWebApp &request); + void on_request(uint64 id, td_api::answerWebAppQuery &request); void on_request(uint64 id, td_api::getCallbackQueryAnswer &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 6847f01f0..9d1a2e674 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3448,6 +3448,10 @@ class CliClient final : public Actor { get_args(args, chat_id, bot_user_id, url, from_bot_menu, reply_to_message_id); send_request(td_api::make_object(chat_id, bot_user_id, url, from_bot_menu, get_theme_parameters(), reply_to_message_id)); + } else if (op == "cwa") { + int64 launch_id; + get_args(args, launch_id); + send_request(td_api::make_object(launch_id)); } else if (op == "sca") { ChatId chat_id; string message_thread_id;