Add td_api::internalLinkTypeMainWebApp.

This commit is contained in:
levlam 2024-07-19 16:43:35 +03:00
parent 5a00eaa158
commit 853301ff1c
4 changed files with 72 additions and 71 deletions

View File

@ -6498,6 +6498,16 @@ internalLinkTypeLanguagePack language_pack_id:string = InternalLinkType;
//@description The link is a link to the language section of the app settings
internalLinkTypeLanguageSettings = InternalLinkType;
//@description The link is a link to the main Web App of a bot. Call searchPublicChat with the given bot username, check that the user is a bot and has the main Web App.
//-If the bot can be added to attachment menu, then use getAttachmentMenuBot to receive information about the bot, then if the bot isn't added to side menu,
//-show a disclaimer about Mini Apps being a third-party apps, ask the user to accept their Terms of service and confirm adding the bot to side and attachment menu,
//-then if the user accepts the terms and confirms adding, use toggleBotIsAddedToAttachmentMenu to add the bot.
//-Then use getMainWebAppUrl with the given URL and open the returned URL as a Web App
//@bot_username Username of the bot
//@url URL to be passed to getMainWebAppUrl
//@is_compact True, if the Web App must be opened in a compact mode instead of a full-size mode
internalLinkTypeMainWebApp bot_username:string url:string is_compact:Bool = InternalLinkType;
//@description The link is a link to a Telegram message or a forum topic. Call getMessageLinkInfo with the given URL to process the link,
//-and then open received forum topic or chat and show the message there
//@url URL to be passed to getMessageLinkInfo
@ -6562,15 +6572,6 @@ internalLinkTypeRestorePurchases = InternalLinkType;
//@description The link is a link to application settings
internalLinkTypeSettings = InternalLinkType;
//@description The link is a link to a bot, which can be installed to the side menu. Call searchPublicChat with the given bot username, check that the user is a bot and can be added to attachment menu.
//-Then, use getAttachmentMenuBot to receive information about the bot. If the bot isn't added to side menu, then show a disclaimer about Mini Apps being a third-party apps,
//-ask the user to accept their Terms of service and confirm adding the bot to side and attachment menu. If the user accept the terms and confirms adding, then use toggleBotIsAddedToAttachmentMenu to add the bot.
//-If the bot is added to side menu, then use getWebAppUrl with the given URL and open the returned URL as a Web App
//@bot_username Username of the bot
//@url URL to be passed to getWebAppUrl
//@is_compact True, if the Web App must be opened in a compact mode instead of a full-size mode
internalLinkTypeSideMenuBot bot_username:string url:string is_compact:Bool = InternalLinkType;
//@description The link is a link to a sticker set. Call searchStickerSet with the given sticker set name to process the link and show the sticker set.
//-If the sticker set is found and the user wants to add it, then call changeStickerSet
//@sticker_set_name Name of the sticker set

View File

@ -528,6 +528,24 @@ class LinkManager::InternalLinkLanguageSettings final : public InternalLink {
}
};
class LinkManager::InternalLinkMainWebApp final : public InternalLink {
string bot_username_;
string url_;
string mode_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeMainWebApp>(bot_username_, url_, mode_ == "compact");
}
public:
InternalLinkMainWebApp(string bot_username, string start_parameter, string mode)
: bot_username_(std::move(bot_username)), mode_(std::move(mode)) {
if (!start_parameter.empty()) {
url_ = PSTRING() << "start://" << start_parameter;
}
}
};
class LinkManager::InternalLinkMessage final : public InternalLink {
string url_;
@ -685,24 +703,6 @@ class LinkManager::InternalLinkSettings final : public InternalLink {
}
};
class LinkManager::InternalLinkSideMenuBot final : public InternalLink {
string bot_username_;
string url_;
string mode_;
td_api::object_ptr<td_api::InternalLinkType> get_internal_link_type_object() const final {
return td_api::make_object<td_api::internalLinkTypeSideMenuBot>(bot_username_, url_, mode_ == "compact");
}
public:
InternalLinkSideMenuBot(string bot_username, string start_parameter, string mode)
: bot_username_(std::move(bot_username)), mode_(std::move(mode)) {
if (!start_parameter.empty()) {
url_ = PSTRING() << "start://" << start_parameter;
}
}
};
class LinkManager::InternalLinkStickerSet final : public InternalLink {
string sticker_set_name_;
bool expect_custom_emoji_;
@ -1337,8 +1337,8 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_tg_link_query(Slice que
if (url_query.has_arg("startapp") && !url_query.has_arg("appname")) {
// resolve?domain=<bot_username>&startapp=
// resolve?domain=<bot_username>&startapp=<start_parameter>&mode=compact
return td::make_unique<InternalLinkSideMenuBot>(std::move(username), url_query.get_arg("startapp").str(),
url_query.get_arg("mode").str());
return td::make_unique<InternalLinkMainWebApp>(std::move(username), url_query.get_arg("startapp").str(),
url_query.get_arg("mode").str());
}
if (!url_query.get_arg("attach").empty()) {
// resolve?domain=<username>&attach=<bot_username>
@ -1794,8 +1794,8 @@ unique_ptr<LinkManager::InternalLink> LinkManager::parse_t_me_link_query(Slice q
if (arg.first == "startapp" && is_valid_start_parameter(arg.second)) {
// /<bot_username>?startapp
// /<bot_username>?startapp=<parameter>&mode=compact
return td::make_unique<InternalLinkSideMenuBot>(std::move(username), arg.second,
url_query.get_arg("mode").str());
return td::make_unique<InternalLinkMainWebApp>(std::move(username), arg.second,
url_query.get_arg("mode").str());
}
if (arg.first == "game" && is_valid_game_name(arg.second)) {
// /<bot_username>?game=<short_name>
@ -2208,6 +2208,29 @@ Result<string> LinkManager::get_internal_link_impl(const td_api::InternalLinkTyp
return Status::Error("HTTP link is unavailable for the link type");
}
return "tg://settings/language";
case td_api::internalLinkTypeMainWebApp::ID: {
auto link = static_cast<const td_api::internalLinkTypeMainWebApp *>(type_ptr);
if (!is_valid_username(link->bot_username_)) {
return Status::Error(400, "Invalid bot username specified");
}
string start_parameter;
if (!link->url_.empty()) {
if (!begins_with(link->url_, "start://")) {
return Status::Error(400, "Unsupported link URL specified");
}
auto start_parameter_slice = Slice(link->url_).substr(8);
if (start_parameter_slice.empty() || !is_valid_start_parameter(start_parameter_slice)) {
return Status::Error(400, "Invalid start parameter specified");
}
start_parameter = PSTRING() << '=' << start_parameter_slice;
}
string mode = link->is_compact_ ? "&mode=compact" : "";
if (is_internal) {
return PSTRING() << "tg://resolve?domain=" << link->bot_username_ << "&startapp" << start_parameter << mode;
} else {
return PSTRING() << get_t_me_url() << link->bot_username_ << "?startapp" << start_parameter << mode;
}
}
case td_api::internalLinkTypeMessage::ID: {
auto link = static_cast<const td_api::internalLinkTypeMessage *>(type_ptr);
auto parsed_link = parse_internal_link(link->url_);
@ -2321,29 +2344,6 @@ Result<string> LinkManager::get_internal_link_impl(const td_api::InternalLinkTyp
return Status::Error("HTTP link is unavailable for the link type");
}
return "tg://settings";
case td_api::internalLinkTypeSideMenuBot::ID: {
auto link = static_cast<const td_api::internalLinkTypeSideMenuBot *>(type_ptr);
if (!is_valid_username(link->bot_username_)) {
return Status::Error(400, "Invalid bot username specified");
}
string start_parameter;
if (!link->url_.empty()) {
if (!begins_with(link->url_, "start://")) {
return Status::Error(400, "Unsupported link URL specified");
}
auto start_parameter_slice = Slice(link->url_).substr(8);
if (start_parameter_slice.empty() || !is_valid_start_parameter(start_parameter_slice)) {
return Status::Error(400, "Invalid start parameter specified");
}
start_parameter = PSTRING() << '=' << start_parameter_slice;
}
string mode = link->is_compact_ ? "&mode=compact" : "";
if (is_internal) {
return PSTRING() << "tg://resolve?domain=" << link->bot_username_ << "&startapp" << start_parameter << mode;
} else {
return PSTRING() << get_t_me_url() << link->bot_username_ << "?startapp" << start_parameter << mode;
}
}
case td_api::internalLinkTypeStickerSet::ID: {
auto link = static_cast<const td_api::internalLinkTypeStickerSet *>(type_ptr);
if (link->sticker_set_name_.empty()) {

View File

@ -138,6 +138,7 @@ class LinkManager final : public Actor {
class InternalLinkInvoice;
class InternalLinkLanguage;
class InternalLinkLanguageSettings;
class InternalLinkMainWebApp;
class InternalLinkMessage;
class InternalLinkMessageDraft;
class InternalLinkPassportDataRequest;
@ -150,7 +151,6 @@ class LinkManager final : public Actor {
class InternalLinkQrCodeAuthentication;
class InternalLinkRestorePurchases;
class InternalLinkSettings;
class InternalLinkSideMenuBot;
class InternalLinkStickerSet;
class InternalLinkStory;
class InternalLinkTheme;

View File

@ -272,6 +272,11 @@ static auto language_settings() {
return td::td_api::make_object<td::td_api::internalLinkTypeLanguageSettings>();
}
static auto main_web_app(const td::string &bot_username, const td::string &start_parameter, bool is_compact) {
return td::td_api::make_object<td::td_api::internalLinkTypeMainWebApp>(
bot_username, start_parameter.empty() ? td::string() : "start://" + start_parameter, is_compact);
}
static auto message(const td::string &url) {
return td::td_api::make_object<td::td_api::internalLinkTypeMessage>(url);
}
@ -336,11 +341,6 @@ static auto settings() {
return td::td_api::make_object<td::td_api::internalLinkTypeSettings>();
}
static auto side_menu_bot(const td::string &bot_username, const td::string &start_parameter, bool is_compact) {
return td::td_api::make_object<td::td_api::internalLinkTypeSideMenuBot>(
bot_username, start_parameter.empty() ? td::string() : "start://" + start_parameter, is_compact);
}
static auto sticker_set(const td::string &sticker_set_name, bool expect_custom_emoji) {
return td::td_api::make_object<td::td_api::internalLinkTypeStickerSet>(sticker_set_name, expect_custom_emoji);
}
@ -1228,25 +1228,25 @@ TEST(Link, parse_internal_link_part4) {
parse_internal_link("https://telegram.dog/tele%63ram/t%63st", web_app("telecram", "tcst", "", false));
parse_internal_link("https://telegram.dog/tele%63ram/t%63st?mode=compact", web_app("telecram", "tcst", "", true));
parse_internal_link("tg:resolve?domain=username&startapp=aasdasd", side_menu_bot("username", "aasdasd", false));
parse_internal_link("TG://resolve?domain=username&startapp=&startapp=123asd", side_menu_bot("username", "", false));
parse_internal_link("tg:resolve?domain=username&startapp=aasdasd", main_web_app("username", "aasdasd", false));
parse_internal_link("TG://resolve?domain=username&startapp=&startapp=123asd", main_web_app("username", "", false));
parse_internal_link("TG://test@resolve?domain=username&startapp=asd", nullptr);
parse_internal_link("tg:resolve:80?domain=username&startapp=asd", nullptr);
parse_internal_link("tg:http://resolve?domain=username&startapp=asd", nullptr);
parse_internal_link("tg:https://resolve?domain=username&startapp=asd", nullptr);
parse_internal_link("tg:resolve?domain=&startapp=asd", unknown_deep_link("tg://resolve?domain=&startapp=asd"));
parse_internal_link("tg:resolve?domain=telegram&&&&&&&startapp=%41", side_menu_bot("telegram", "A", false));
parse_internal_link("tg:resolve?domain=telegram&&&&&&&startapp=%41b", side_menu_bot("telegram", "Ab", false));
parse_internal_link("tg:resolve?domain=telegram&&&&&&&startapp=%41bc", side_menu_bot("telegram", "Abc", false));
parse_internal_link("tg:resolve?domain=telegram&&&&&&&startapp=%41", main_web_app("telegram", "A", false));
parse_internal_link("tg:resolve?domain=telegram&&&&&&&startapp=%41b", main_web_app("telegram", "Ab", false));
parse_internal_link("tg:resolve?domain=telegram&&&&&&&startapp=%41bc", main_web_app("telegram", "Abc", false));
parse_internal_link("tg:resolve?domain=telegram&&mode=compact&&&&&&startapp=%41bc",
side_menu_bot("telegram", "Abc", true));
main_web_app("telegram", "Abc", true));
parse_internal_link("t.me/username?startapp=qwe", side_menu_bot("username", "qwe", false));
parse_internal_link("t.me/username?12312&startapp=qwe", side_menu_bot("username", "qwe", false));
parse_internal_link("t.me/username?startapp=0", side_menu_bot("username", "0", false));
parse_internal_link("https://telegram.dog/tele%63ram?startapp=t%63st", side_menu_bot("telecram", "tcst", false));
parse_internal_link("t.me/username?startapp=qwe", main_web_app("username", "qwe", false));
parse_internal_link("t.me/username?12312&startapp=qwe", main_web_app("username", "qwe", false));
parse_internal_link("t.me/username?startapp=0", main_web_app("username", "0", false));
parse_internal_link("https://telegram.dog/tele%63ram?startapp=t%63st", main_web_app("telecram", "tcst", false));
parse_internal_link("https://telegram.dog/tele%63ram?startapp=t%63st&mode=%63ompact",
side_menu_bot("telecram", "tcst", true));
main_web_app("telecram", "tcst", true));
parse_internal_link("https://telegram.dog?startapp=t%63st", nullptr);
parse_internal_link("tg:resolve?domain=username&Game=asd", public_chat("username"));