From 93c3b5cc074baeff674ed0ba9d90805554b604be Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 30 Mar 2023 15:34:27 +0300 Subject: [PATCH] Add toggleBotUsernameIsActive. --- td/generate/scheme/td_api.tl | 12 ++++-- td/telegram/ContactsManager.cpp | 70 +++++++++++++++++++++++++++++++++ td/telegram/ContactsManager.h | 4 ++ td/telegram/Td.cpp | 8 ++++ td/telegram/Td.h | 2 + 5 files changed, 93 insertions(+), 3 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 376df22ec..b58597701 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -673,9 +673,9 @@ emojiStatuses emoji_statuses:vector = EmojiStatuses; //@description Describes usernames assigned to a user, a supergroup, or a channel -//@active_usernames List of active usernames; the first one must be shown as the primary username. The order of active usernames can be changed with reorderActiveUsernames or reorderSupergroupActiveUsernames -//@disabled_usernames List of currently disabled usernames; the username can be activated with toggleUsernameIsActive/toggleSupergroupUsernameIsActive -//@editable_username The active username, which can be changed with setUsername/setSupergroupUsername +//@active_usernames List of active usernames; the first one must be shown as the primary username. The order of active usernames can be changed with reorderActiveUsernames, reorderBotActiveUsernames or reorderSupergroupActiveUsernames +//@disabled_usernames List of currently disabled usernames; the username can be activated with toggleUsernameIsActive, toggleBotUsernameIsActive, or toggleSupergroupUsernameIsActive +//@editable_username The active username, which can be changed with setUsername or setSupergroupUsername usernames active_usernames:vector disabled_usernames:vector editable_username:string = Usernames; @@ -7649,6 +7649,12 @@ getBotName bot_user_id:int53 language_code:string = Text; //@description Changes a profile photo for a bot @bot_user_id Identifier of the target bot @photo Profile photo to set; pass null to delete the chat photo setBotProfilePhoto bot_user_id:int53 photo:InputChatPhoto = Ok; +//@description Changes active state for a username of a bot. The editable username can't be disabled. May return an error with a message "USERNAMES_ACTIVE_TOO_MUCH" if the maximum number of active usernames has been reached +//@bot_user_id Identifier of the target bot +//@username The username to change +//@is_active Pass true to activate the username; pass false to disable it +toggleBotUsernameIsActive bot_user_id:int53 username:string is_active:Bool = Ok; + //@description Sets the text shown in the chat with a bot if the chat is empty. Can be called only if userTypeBot.can_be_edited == true //@bot_user_id Identifier of the target bot //@language_code A two-letter ISO 639-1 language code. If empty, the description will be shown to all users, for which language there are no dedicated description diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index dabe5ceec..de7d0f653 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -907,6 +907,50 @@ class ReorderUsernamesQuery final : public Td::ResultHandler { } }; +class ToggleBotUsernameQuery final : public Td::ResultHandler { + Promise promise_; + UserId bot_user_id_; + string username_; + bool is_active_; + + public: + explicit ToggleBotUsernameQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(UserId bot_user_id, string &&username, bool is_active) { + bot_user_id_ = bot_user_id; + username_ = std::move(username); + is_active_ = is_active; + auto r_input_user = td_->contacts_manager_->get_input_user(bot_user_id_); + if (r_input_user.is_error()) { + return on_error(r_input_user.move_as_error()); + } + send_query(G()->net_query_creator().create( + telegram_api::bots_toggleUsername(r_input_user.move_as_ok(), username_, is_active_), {{bot_user_id_}})); + } + + 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()); + } + + bool result = result_ptr.ok(); + LOG(DEBUG) << "Receive result for ToggleBotUsernameQuery: " << result; + td_->contacts_manager_->on_update_bot_username_is_active(bot_user_id_, std::move(username_), is_active_, + std::move(promise_)); + } + + void on_error(Status status) final { + if (status.message() == "USERNAME_NOT_MODIFIED") { + td_->contacts_manager_->on_update_bot_username_is_active(bot_user_id_, std::move(username_), is_active_, + std::move(promise_)); + return; + } + promise_.set_error(std::move(status)); + } +}; + class UpdateEmojiStatusQuery final : public Td::ResultHandler { Promise promise_; @@ -7546,6 +7590,32 @@ void ContactsManager::on_update_active_usernames_order(vector &&username promise.set_value(Unit()); } +void ContactsManager::toggle_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, + Promise &&promise) { + TRY_RESULT_PROMISE(promise, bot_data, get_bot_data(bot_user_id)); + if (!bot_data.can_be_edited) { + return promise.set_error(Status::Error(400, "The bot can't be edited")); + } + const User *u = get_user(bot_user_id); + CHECK(u != nullptr); + if (!u->usernames.can_toggle(username)) { + return promise.set_error(Status::Error(400, "Wrong username specified")); + } + td_->create_handler(std::move(promise))->send(bot_user_id, std::move(username), is_active); +} + +void ContactsManager::on_update_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, + Promise &&promise) { + User *u = get_user(bot_user_id); + CHECK(u != nullptr); + if (!u->usernames.can_toggle(username)) { + return reload_user(bot_user_id, std::move(promise)); + } + on_update_user_usernames(u, bot_user_id, u->usernames.toggle(username, is_active)); + update_user(u, bot_user_id); + promise.set_value(Unit()); +} + void ContactsManager::set_emoji_status(EmojiStatus emoji_status, Promise &&promise) { if (!td_->option_manager_->get_option_boolean("is_premium")) { return promise.set_error(Status::Error(400, "The method is available only to Telegram Premium users")); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 3ae0eccf6..df568a23f 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -308,6 +308,8 @@ class ContactsManager final : public Actor { void on_update_active_usernames_order(vector &&usernames, Promise &&promise); + void on_update_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise &&promise); + void on_update_channel_username_is_active(ChannelId channel_id, string &&username, bool is_active, Promise &&promise); @@ -399,6 +401,8 @@ class ContactsManager final : public Actor { void reorder_usernames(vector &&usernames, Promise &&promise); + void toggle_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise &&promise); + void set_emoji_status(EmojiStatus emoji_status, Promise &&promise); void set_chat_description(ChatId chat_id, const string &description, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 4d6ead618..495be3bc7 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7027,6 +7027,14 @@ void Td::on_request(uint64 id, td_api::setBotProfilePhoto &request) { contacts_manager_->set_bot_profile_photo(UserId(request.bot_user_id_), request.photo_, std::move(promise)); } +void Td::on_request(uint64 id, td_api::toggleBotUsernameIsActive &request) { + CHECK_IS_USER(); + CLEAN_INPUT_STRING(request.username_); + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->toggle_bot_username_is_active(UserId(request.bot_user_id_), std::move(request.username_), + request.is_active_, std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setBotInfoDescription &request) { CLEAN_INPUT_STRING(request.description_); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index c38a848c7..b09b2d9bc 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1143,6 +1143,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::setBotProfilePhoto &request); + void on_request(uint64 id, td_api::toggleBotUsernameIsActive &request); + void on_request(uint64 id, td_api::setBotInfoDescription &request); void on_request(uint64 id, const td_api::getBotInfoDescription &request);