Add toggleBotUsernameIsActive.

This commit is contained in:
levlam 2023-03-30 15:34:27 +03:00
parent 3f9b43227c
commit 93c3b5cc07
5 changed files with 93 additions and 3 deletions

View File

@ -673,9 +673,9 @@ emojiStatuses emoji_statuses:vector<emojiStatus> = 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<string> disabled_usernames:vector<string> 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

View File

@ -907,6 +907,50 @@ class ReorderUsernamesQuery final : public Td::ResultHandler {
}
};
class ToggleBotUsernameQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
UserId bot_user_id_;
string username_;
bool is_active_;
public:
explicit ToggleBotUsernameQuery(Promise<Unit> &&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<telegram_api::bots_toggleUsername>(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<Unit> promise_;
@ -7546,6 +7590,32 @@ void ContactsManager::on_update_active_usernames_order(vector<string> &&username
promise.set_value(Unit());
}
void ContactsManager::toggle_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active,
Promise<Unit> &&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<ToggleBotUsernameQuery>(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<Unit> &&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<Unit> &&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"));

View File

@ -308,6 +308,8 @@ class ContactsManager final : public Actor {
void on_update_active_usernames_order(vector<string> &&usernames, Promise<Unit> &&promise);
void on_update_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise<Unit> &&promise);
void on_update_channel_username_is_active(ChannelId channel_id, string &&username, bool is_active,
Promise<Unit> &&promise);
@ -399,6 +401,8 @@ class ContactsManager final : public Actor {
void reorder_usernames(vector<string> &&usernames, Promise<Unit> &&promise);
void toggle_bot_username_is_active(UserId bot_user_id, string &&username, bool is_active, Promise<Unit> &&promise);
void set_emoji_status(EmojiStatus emoji_status, Promise<Unit> &&promise);
void set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise);

View File

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

View File

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