From 4c1fa3b45d9e0ce922f189ab2ab8531096a9eca7 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 8 Apr 2024 03:10:33 +0300 Subject: [PATCH] Add td_api::removeBusinessConnectedBotFromChat. --- td/generate/scheme/td_api.tl | 3 ++ td/telegram/BusinessManager.cpp | 58 +++++++++++++++++++++++++++++++-- td/telegram/BusinessManager.h | 4 ++- td/telegram/MessagesManager.cpp | 9 +++++ td/telegram/MessagesManager.h | 2 ++ td/telegram/Td.cpp | 10 ++++-- td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 4 +++ 8 files changed, 86 insertions(+), 6 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 09905985c..a180853a4 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9728,6 +9728,9 @@ deleteBusinessConnectedBot bot_user_id:int53 = Ok; //@description Pauses or resumes the connected business bot in a specific chat @chat_id Chat identifier @is_paused Pass true to pause the connected bot in the chat; pass false to resume the bot toggleBusinessConnectedBotChatIsPaused chat_id:int53 is_paused:Bool = Ok; +//@description Removes the connected business bot from a specific chat by adding the chat to businessRecipients.excluded_chat_ids @chat_id Chat identifier +removeBusinessConnectedBotFromChat chat_id:int53 = Ok; + //@description Returns an HTTPS link, which can be used to get information about the current user getUserLink = UserLink; diff --git a/td/telegram/BusinessManager.cpp b/td/telegram/BusinessManager.cpp index c8891be73..4ef990b2f 100644 --- a/td/telegram/BusinessManager.cpp +++ b/td/telegram/BusinessManager.cpp @@ -113,12 +113,14 @@ class UpdateConnectedBotQuery final : public Td::ResultHandler { class ToggleConnectedBotPausedQuery final : public Td::ResultHandler { Promise promise_; + DialogId dialog_id_; public: explicit ToggleConnectedBotPausedQuery(Promise &&promise) : promise_(std::move(promise)) { } void send(DialogId dialog_id, bool is_paused) { + dialog_id_ = dialog_id; auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write); if (input_peer == nullptr) { return on_error(Status::Error(400, "Have no write access to the chat")); @@ -140,6 +142,45 @@ class ToggleConnectedBotPausedQuery final : public Td::ResultHandler { } void on_error(Status status) final { + td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "ToggleConnectedBotPausedQuery"); + promise_.set_error(std::move(status)); + } +}; + +class DisablePeerConnectedBotQuery final : public Td::ResultHandler { + Promise promise_; + DialogId dialog_id_; + + public: + explicit DisablePeerConnectedBotQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(DialogId dialog_id) { + dialog_id_ = dialog_id; + auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write); + if (input_peer == nullptr) { + return on_error(Status::Error(400, "Have no write access to the chat")); + } + send_query(G()->net_query_creator().create(telegram_api::account_disablePeerConnectedBot(std::move(input_peer)), + {{"me"}, {dialog_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()); + } + + if (!result_ptr.ok()) { + LOG(INFO) << "Failed to remove business bot"; + } else { + td_->messages_manager_->on_update_dialog_business_bot_removed(dialog_id_); + } + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "DisablePeerConnectedBotQuery"); promise_.set_error(std::move(status)); } }; @@ -347,9 +388,9 @@ void BusinessManager::delete_business_connected_bot(UserId bot_user_id, Promise< td_->create_handler(std::move(promise))->send(std::move(input_user)); } -void BusinessManager::toggle_business_connected_bot_chat_is_paused(DialogId dialog_id, bool is_paused, - Promise &&promise) { - if (!td_->messages_manager_->have_dialog_force(dialog_id, "toggle_business_connected_bot_chat_is_paused")) { +void BusinessManager::toggle_business_connected_bot_dialog_is_paused(DialogId dialog_id, bool is_paused, + Promise &&promise) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "toggle_business_connected_bot_dialog_is_paused")) { return promise.set_error(Status::Error(400, "Chat not found")); } if (dialog_id.get_type() != DialogType::User) { @@ -359,6 +400,17 @@ void BusinessManager::toggle_business_connected_bot_chat_is_paused(DialogId dial td_->create_handler(std::move(promise))->send(dialog_id, is_paused); } +void BusinessManager::remove_business_connected_bot_from_dialog(DialogId dialog_id, Promise &&promise) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "remove_business_connected_bot_from_dialog")) { + return promise.set_error(Status::Error(400, "Chat not found")); + } + if (dialog_id.get_type() != DialogType::User) { + return promise.set_error(Status::Error(400, "The chat has no connected bot")); + } + td_->messages_manager_->on_update_dialog_business_bot_removed(dialog_id); + td_->create_handler(std::move(promise))->send(dialog_id); +} + void BusinessManager::set_business_location(DialogLocation &&location, Promise &&promise) { td_->create_handler(std::move(promise))->send(std::move(location)); } diff --git a/td/telegram/BusinessManager.h b/td/telegram/BusinessManager.h index 945e766b0..41492edae 100644 --- a/td/telegram/BusinessManager.h +++ b/td/telegram/BusinessManager.h @@ -34,7 +34,9 @@ class BusinessManager final : public Actor { void delete_business_connected_bot(UserId bot_user_id, Promise &&promise); - void toggle_business_connected_bot_chat_is_paused(DialogId dialog_id, bool is_paused, Promise &&promise); + void toggle_business_connected_bot_dialog_is_paused(DialogId dialog_id, bool is_paused, Promise &&promise); + + void remove_business_connected_bot_from_dialog(DialogId dialog_id, Promise &&promise); void set_business_location(DialogLocation &&location, Promise &&promise); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 6ec12bc73..ecb279cf4 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -30322,6 +30322,15 @@ void MessagesManager::on_update_dialog_business_bot_is_paused(DialogId dialog_id } } +void MessagesManager::on_update_dialog_business_bot_removed(DialogId dialog_id) { + auto d = get_dialog_force(dialog_id, "on_update_dialog_business_bot_removed"); + CHECK(d != nullptr); + if (d->business_bot_manage_bar != nullptr) { + d->business_bot_manage_bar = nullptr; + send_update_chat_business_bot_manage_bar(d); + } +} + void MessagesManager::on_update_dialog_last_pinned_message_id(DialogId dialog_id, MessageId pinned_message_id) { if (!dialog_id.is_valid()) { LOG(ERROR) << "Receive pinned message in invalid " << dialog_id; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index f83778aab..62880f4a0 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -266,6 +266,8 @@ class MessagesManager final : public Actor { void on_update_dialog_business_bot_is_paused(DialogId dialog_id, bool is_paused); + void on_update_dialog_business_bot_removed(DialogId dialog_id); + void on_update_dialog_last_pinned_message_id(DialogId dialog_id, MessageId last_pinned_message_id); void on_update_dialog_background(DialogId dialog_id, telegram_api::object_ptr &&wallpaper); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 69fdcf3ea..8ce0acf5c 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7973,8 +7973,14 @@ void Td::on_request(uint64 id, const td_api::deleteBusinessConnectedBot &request void Td::on_request(uint64 id, const td_api::toggleBusinessConnectedBotChatIsPaused &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - business_manager_->toggle_business_connected_bot_chat_is_paused(DialogId(request.chat_id_), request.is_paused_, - std::move(promise)); + business_manager_->toggle_business_connected_bot_dialog_is_paused(DialogId(request.chat_id_), request.is_paused_, + std::move(promise)); +} + +void Td::on_request(uint64 id, const td_api::removeBusinessConnectedBotFromChat &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + business_manager_->remove_business_connected_bot_from_dialog(DialogId(request.chat_id_), std::move(promise)); } void Td::on_request(uint64 id, td_api::setSupergroupUsername &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 0174aa409..52e8be144 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1441,6 +1441,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::toggleBusinessConnectedBotChatIsPaused &request); + void on_request(uint64 id, const td_api::removeBusinessConnectedBotFromChat &request); + void on_request(uint64 id, td_api::setSupergroupUsername &request); void on_request(uint64 id, td_api::toggleSupergroupUsernameIsActive &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 73082d912..347df7664 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6161,6 +6161,10 @@ class CliClient final : public Actor { bool is_paused; get_args(args, chat_id, is_paused); send_request(td_api::make_object(chat_id, is_paused)); + } else if (op == "rbcbfc") { + ChatId chat_id; + get_args(args, chat_id); + send_request(td_api::make_object(chat_id)); } else if (op == "dbcb") { UserId bot_user_id; get_args(args, bot_user_id);