diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 053071aec..f73def823 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1885,7 +1885,7 @@ chatActionBarJoinRequest title:string is_channel:Bool request_date:int32 = ChatA //@description The chat is a private chat administered by a business bot //@bot_user_id User identifier of the bot //@manage_url URL to be opened to manage the bot -//@is_bot_paused True, if the bot is paused +//@is_bot_paused True, if the bot is paused. Use toggleBusinessConnectedBotChatIsPaused to change the value of the field //@can_bot_reply True, if the bot can reply chatActionBarManageBusinessBot bot_user_id:int53 manage_url:string is_bot_paused:Bool can_bot_reply:Bool = ChatActionBar; @@ -9720,6 +9720,9 @@ setBusinessConnectedBot bot:businessConnectedBot = Ok; //@description Deletes the business bot that is connected to the current user account @bot_user_id Unique user identifier for the bot 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 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 c31f875a1..027e53dd1 100644 --- a/td/telegram/BusinessManager.cpp +++ b/td/telegram/BusinessManager.cpp @@ -6,6 +6,7 @@ // #include "td/telegram/BusinessManager.h" +#include "td/telegram/AccessRights.h" #include "td/telegram/BusinessAwayMessage.h" #include "td/telegram/BusinessConnectedBot.h" #include "td/telegram/BusinessGreetingMessage.h" @@ -13,7 +14,9 @@ #include "td/telegram/BusinessRecipients.h" #include "td/telegram/BusinessWorkHours.h" #include "td/telegram/DialogLocation.h" +#include "td/telegram/DialogManager.h" #include "td/telegram/Global.h" +#include "td/telegram/MessagesManager.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" #include "td/telegram/UpdatesManager.h" @@ -107,6 +110,39 @@ class UpdateConnectedBotQuery final : public Td::ResultHandler { } }; +class ToggleConnectedBotPausedQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit ToggleConnectedBotPausedQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(DialogId dialog_id, bool is_paused) { + 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_toggleConnectedBotPaused(std::move(input_peer), is_paused), {{"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 toggle business bot is paused"; + } + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class UpdateBusinessLocationQuery final : public Td::ResultHandler { Promise promise_; DialogLocation location_; @@ -310,6 +346,18 @@ 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")) { + 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_is_paused(dialog_id, is_paused); + td_->create_handler(std::move(promise))->send(dialog_id, is_paused); +} + 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 614e46712..945e766b0 100644 --- a/td/telegram/BusinessManager.h +++ b/td/telegram/BusinessManager.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/DialogId.h" #include "td/telegram/td_api.h" #include "td/telegram/UserId.h" @@ -33,6 +34,8 @@ 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 set_business_location(DialogLocation &&location, Promise &&promise); void set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise); diff --git a/td/telegram/DialogActionBar.cpp b/td/telegram/DialogActionBar.cpp index a7fe34701..f65a2864b 100644 --- a/td/telegram/DialogActionBar.cpp +++ b/td/telegram/DialogActionBar.cpp @@ -310,6 +310,14 @@ bool DialogActionBar::on_outgoing_message() { return true; } +bool DialogActionBar::set_business_bot_is_paused(bool is_paused) { + if (!business_bot_user_id_.is_valid() || is_business_bot_paused_ == is_paused) { + return false; + } + is_business_bot_paused_ = is_paused; + return true; +} + void DialogActionBar::add_dependencies(Dependencies &dependencies) const { dependencies.add(business_bot_user_id_); } diff --git a/td/telegram/DialogActionBar.h b/td/telegram/DialogActionBar.h index 6c79e6a2f..6b126267b 100644 --- a/td/telegram/DialogActionBar.h +++ b/td/telegram/DialogActionBar.h @@ -69,6 +69,8 @@ class DialogActionBar { bool on_outgoing_message(); + bool set_business_bot_is_paused(bool is_paused); + void add_dependencies(Dependencies &dependencies) const; template diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index bfda6568b..54165392c 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -30251,6 +30251,14 @@ void MessagesManager::set_dialog_is_blocked(Dialog *d, bool is_blocked, bool is_ } } +void MessagesManager::on_update_dialog_business_bot_is_paused(DialogId dialog_id, bool is_paused) { + auto d = get_dialog_force(dialog_id, "on_update_dialog_is_blocked"); + CHECK(d != nullptr); + if (d->action_bar != nullptr && d->action_bar->set_business_bot_is_paused(is_paused)) { + send_update_chat_action_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 d1c147d43..f0bbd37b5 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -263,6 +263,8 @@ class MessagesManager final : public Actor { void on_update_dialog_is_blocked(DialogId dialog_id, bool is_blocked, bool is_blocked_for_stories); + void on_update_dialog_business_bot_is_paused(DialogId dialog_id, bool is_paused); + 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 0f41e93a9..69fdcf3ea 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7970,6 +7970,13 @@ void Td::on_request(uint64 id, const td_api::deleteBusinessConnectedBot &request business_manager_->delete_business_connected_bot(UserId(request.bot_user_id_), std::move(promise)); } +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)); +} + void Td::on_request(uint64 id, td_api::setSupergroupUsername &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.username_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index c60cd5be9..0174aa409 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1439,6 +1439,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::deleteBusinessConnectedBot &request); + void on_request(uint64 id, const td_api::toggleBusinessConnectedBotChatIsPaused &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 523f9df5c..73082d912 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6152,10 +6152,15 @@ class CliClient final : public Actor { } else if (op == "sbcb") { UserId bot_user_id; string chat_ids; - bool can_reply = false; + bool can_reply; get_args(args, bot_user_id, chat_ids, can_reply); send_request(td_api::make_object( td_api::make_object(bot_user_id, as_business_recipients(chat_ids), can_reply))); + } else if (op == "tbcbcip") { + ChatId chat_id; + bool is_paused; + get_args(args, chat_id, is_paused); + send_request(td_api::make_object(chat_id, is_paused)); } else if (op == "dbcb") { UserId bot_user_id; get_args(args, bot_user_id);