Add td_api::getMainWebAppUrl.

This commit is contained in:
levlam 2024-07-19 17:08:34 +03:00
parent 853301ff1c
commit 6d3c92c779
6 changed files with 100 additions and 0 deletions

View File

@ -9140,6 +9140,14 @@ searchWebApp bot_user_id:int53 web_app_short_name:string = FoundWebApp;
//@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 to open the main Web App of a bot
//@chat_id Identifier of the chat in which the Web App is opened; pass 0 if none
//@bot_user_id Identifier of the target bot
//@start_parameter Start parameter from internalLinkTypeMainWebApp
//@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
getMainWebAppUrl chat_id:int53 bot_user_id:int53 start_parameter:string theme:themeParameters application_name:string = HttpUrl;
//@description Returns an HTTPS URL of a Web App to open from the side menu, a keyboardButtonTypeWebApp button, an inlineQueryResultsButtonTypeWebApp button, or an internalLinkTypeSideMenuBot link
//@bot_user_id Identifier of the target bot
//@url The URL from a keyboardButtonTypeWebApp button, inlineQueryResultsButtonTypeWebApp button, an internalLinkTypeSideMenuBot link, or an empty when the bot is opened from the side menu

View File

@ -157,6 +157,51 @@ class RequestAppWebViewQuery final : public Td::ResultHandler {
}
};
class RequestMainWebViewQuery final : public Td::ResultHandler {
Promise<string> promise_;
public:
explicit RequestMainWebViewQuery(Promise<string> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, telegram_api::object_ptr<telegram_api::InputUser> &&input_user,
const string &start_parameter, const td_api::object_ptr<td_api::themeParameters> &theme,
const string &platform) {
telegram_api::object_ptr<telegram_api::dataJSON> theme_parameters;
int32 flags = 0;
if (theme != nullptr) {
flags |= telegram_api::messages_requestMainWebView::THEME_PARAMS_MASK;
theme_parameters = make_tl_object<telegram_api::dataJSON>(string());
theme_parameters->data_ = ThemeManager::get_theme_parameters_json_string(theme);
}
if (!start_parameter.empty()) {
flags |= telegram_api::messages_requestMainWebView::START_PARAM_MASK;
}
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
CHECK(input_peer != nullptr);
send_query(G()->net_query_creator().create(telegram_api::messages_requestMainWebView(
flags, false /*ignored*/, std::move(input_peer), std::move(input_user), start_parameter,
std::move(theme_parameters), platform)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_requestMainWebView>(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 RequestMainWebViewQuery: " << to_string(ptr);
LOG_IF(ERROR, ptr->query_id_ != 0) << "Receive " << 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_;
@ -885,6 +930,23 @@ void AttachMenuManager::request_app_web_view(DialogId dialog_id, UserId bot_user
allow_write_access);
}
void AttachMenuManager::request_main_web_view(DialogId dialog_id, UserId bot_user_id, string &&start_parameter,
const td_api::object_ptr<td_api::themeParameters> &theme,
string &&platform, Promise<string> &&promise) {
if (!td_->dialog_manager_->have_input_peer(dialog_id, false, AccessRights::Read)) {
dialog_id = DialogId(bot_user_id);
}
TRY_RESULT_PROMISE(promise, input_user, td_->user_manager_->get_input_user(bot_user_id));
TRY_RESULT_PROMISE(promise, bot_data, td_->user_manager_->get_bot_data(bot_user_id));
if (!bot_data.has_main_app) {
return promise.set_error(Status::Error(400, "The bot has no main Mini App"));
}
on_dialog_used(TopDialogCategory::BotApp, DialogId(bot_user_id), G()->unix_time());
td_->create_handler<RequestMainWebViewQuery>(std::move(promise))
->send(dialog_id, std::move(input_user), start_parameter, theme, platform);
}
void AttachMenuManager::request_web_view(DialogId dialog_id, UserId bot_user_id, MessageId top_thread_message_id,
td_api::object_ptr<td_api::InputMessageReplyTo> &&reply_to, string &&url,
td_api::object_ptr<td_api::themeParameters> &&theme, string &&platform,

View File

@ -45,6 +45,10 @@ class AttachMenuManager final : public Actor {
string &&start_parameter, const td_api::object_ptr<td_api::themeParameters> &theme,
string &&platform, bool allow_write_access, Promise<string> &&promise);
void request_main_web_view(DialogId dialog_id, UserId bot_user_id, string &&start_parameter,
const td_api::object_ptr<td_api::themeParameters> &theme, string &&platform,
Promise<string> &&promise);
void request_web_view(DialogId dialog_id, UserId bot_user_id, MessageId top_thread_message_id,
td_api::object_ptr<td_api::InputMessageReplyTo> &&reply_to, string &&url,
td_api::object_ptr<td_api::themeParameters> &&theme, string &&platform,

View File

@ -9064,6 +9064,23 @@ void Td::on_request(uint64 id, td_api::getWebAppLinkUrl &request) {
request.allow_write_access_, std::move(query_promise));
}
void Td::on_request(uint64 id, td_api::getMainWebAppUrl &request) {
CHECK_IS_USER();
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_main_web_view(DialogId(request.chat_id_), UserId(request.bot_user_id_),
std::move(request.start_parameter_), std::move(request.theme_),
std::move(request.application_name_), std::move(query_promise));
}
void Td::on_request(uint64 id, td_api::getWebAppUrl &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.url_);

View File

@ -1777,6 +1777,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::getWebAppLinkUrl &request);
void on_request(uint64 id, td_api::getMainWebAppUrl &request);
void on_request(uint64 id, td_api::getWebAppUrl &request);
void on_request(uint64 id, td_api::sendWebAppData &request);

View File

@ -4869,6 +4869,13 @@ class CliClient final : public Actor {
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 == "gmwau") {
ChatId chat_id;
UserId bot_user_id;
string start_parameter;
get_args(args, chat_id, bot_user_id, start_parameter);
send_request(td_api::make_object<td_api::getMainWebAppUrl>(chat_id, bot_user_id, start_parameter,
as_theme_parameters(), "android"));
} else if (op == "gwau") {
UserId bot_user_id;
string url;