Add td_api::getWebAppLinkUrl.

This commit is contained in:
levlam 2023-02-24 15:08:16 +03:00
parent c8ebe2aabe
commit e7da8e88e0
6 changed files with 110 additions and 1 deletions

View File

@ -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

View File

@ -66,6 +66,55 @@ class GetBotAppQuery final : public Td::ResultHandler {
}
};
class RequestAppWebViewQuery final : public Td::ResultHandler {
Promise<string> promise_;
public:
explicit RequestAppWebViewQuery(Promise<string> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, telegram_api::object_ptr<telegram_api::InputUser> &&input_user,
const string &web_app_short_name, const string &start_parameter,
const td_api::object_ptr<td_api::themeParameters> &theme, const string &platform, bool allow_write_access) {
telegram_api::object_ptr<telegram_api::dataJSON> theme_parameters;
int32 flags = 0;
if (theme != nullptr) {
flags |= telegram_api::messages_requestAppWebView::THEME_PARAMS_MASK;
theme_parameters = make_tl_object<telegram_api::dataJSON>(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<telegram_api::inputBotAppShortName>(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<telegram_api::messages_requestAppWebView>(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<td_api::object_ptr<td_api::webAppInfo>> 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<td_api::object_ptr<td_api::foundWebApp>> &&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<telegram_api::object_ptr<telegram_api::messages_botApp>> 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<td_api::themeParameters> &theme,
string &&platform, bool allow_write_access, Promise<string> &&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<RequestAppWebViewQuery>(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<td_api::themeParameters> &&theme, string &&platform,
Promise<td_api::object_ptr<td_api::webAppInfo>> &&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"));

View File

@ -35,6 +35,10 @@ class AttachMenuManager final : public Actor {
void get_web_app(UserId bot_user_id, string &&web_app_short_name,
Promise<td_api::object_ptr<td_api::foundWebApp>> &&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<td_api::themeParameters> &theme,
string &&platform, bool allow_write_access, Promise<string> &&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<td_api::themeParameters> &&theme, string &&platform,

View File

@ -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<string> result) mutable {
if (result.is_error()) {
promise.set_error(result.move_as_error());
} else {
promise.set_value(td_api::make_object<td_api::httpUrl>(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_);

View File

@ -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);

View File

@ -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<td_api::searchWebApp>(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<td_api::getWebAppLinkUrl>(chat_id, bot_user_id, short_name, start_parameter,
as_theme_parameters(), "android", true));
} else if (op == "gwau") {
UserId bot_user_id;
string url;