diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 60a554b73..711198653 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9405,9 +9405,12 @@ resendChangePhoneNumberCode = AuthenticationCodeInfo; checkChangePhoneNumberCode code:string = Ok; -//@description Returns the business bot connected to the current user account. Returns a 404 error if there is no connected bot +//@description Returns the business bot that is connected to the current user account. Returns a 404 error if there is no connected bot getBusinessConnectedBot = BusinessConnectedBot; +//@description Adds or changes business bot that is connected to the current user account @bot Connection settings for the bot +setBusinessConnectedBot bot:businessConnectedBot = Ok; + //@description Returns an HTTPS link, which can be used to get information about the current user getUserLink = UserLink; diff --git a/td/telegram/BusinessConnectedBot.h b/td/telegram/BusinessConnectedBot.h index 379cf1594..d62852678 100644 --- a/td/telegram/BusinessConnectedBot.h +++ b/td/telegram/BusinessConnectedBot.h @@ -32,6 +32,18 @@ class BusinessConnectedBot { return user_id_.is_valid(); } + UserId get_user_id() const { + return user_id_; + } + + const BusinessRecipients &get_recipients() const { + return recipients_; + } + + bool get_can_reply() const { + return can_reply_; + } + template void store(StorerT &storer) const; diff --git a/td/telegram/BusinessManager.cpp b/td/telegram/BusinessManager.cpp index e2984eabc..6241385d1 100644 --- a/td/telegram/BusinessManager.cpp +++ b/td/telegram/BusinessManager.cpp @@ -15,6 +15,7 @@ #include "td/telegram/Global.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" +#include "td/telegram/UpdatesManager.h" #include "td/utils/buffer.h" #include "td/utils/Status.h" @@ -61,6 +62,40 @@ class GetConnectedBotsQuery final : public Td::ResultHandler { } }; +class UpdateConnectedBotQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit UpdateConnectedBotQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(const BusinessConnectedBot &bot, telegram_api::object_ptr &&input_user) { + int32 flags = 0; + if (bot.get_can_reply()) { + flags |= telegram_api::account_updateConnectedBot::CAN_REPLY_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::account_updateConnectedBot(flags, false /*ignored*/, false /*ignored*/, std::move(input_user), + bot.get_recipients().get_input_business_recipients(td_)), + {{"me"}})); + } + + 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()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for UpdateConnectedBotQuery: " << to_string(ptr); + td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class UpdateBusinessLocationQuery final : public Td::ResultHandler { Promise promise_; DialogLocation location_; @@ -215,6 +250,16 @@ void BusinessManager::get_business_connected_bot(Promisecreate_handler(std::move(promise))->send(); } +void BusinessManager::set_business_connected_bot(td_api::object_ptr &&bot, + Promise &&promise) { + if (bot == nullptr) { + return promise.set_error(Status::Error(400, "Bot must be non-empty")); + } + BusinessConnectedBot connected_bot(std::move(bot)); + TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(connected_bot.get_user_id())); + td_->create_handler(std::move(promise))->send(connected_bot, std::move(input_user)); +} + 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 e7ebf4a41..ad1c1c65f 100644 --- a/td/telegram/BusinessManager.h +++ b/td/telegram/BusinessManager.h @@ -27,6 +27,8 @@ class BusinessManager final : public Actor { void get_business_connected_bot(Promise> &&promise); + void set_business_connected_bot(td_api::object_ptr &&bot, 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/Td.cpp b/td/telegram/Td.cpp index 83aca8d2c..31af943bc 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7868,6 +7868,12 @@ void Td::on_request(uint64 id, const td_api::getBusinessConnectedBot &request) { business_manager_->get_business_connected_bot(std::move(promise)); } +void Td::on_request(uint64 id, td_api::setBusinessConnectedBot &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + business_manager_->set_business_connected_bot(std::move(request.bot_), 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 73572f631..b0735bfd2 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1407,6 +1407,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getBusinessConnectedBot &request); + void on_request(uint64 id, td_api::setBusinessConnectedBot &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 f1eccc6c3..0e9208985 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6021,6 +6021,13 @@ class CliClient final : public Actor { } } else if (op == "gbcb") { send_request(td_api::make_object()); + } else if (op == "sbcb") { + UserId bot_user_id; + string chat_ids; + bool can_reply = false; + 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 == "sco") { SearchQuery query; get_args(args, query);