From 5097989a4d3da2eb280030d8f6ff13d79549f42b Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 20 Feb 2024 15:55:17 +0300 Subject: [PATCH] Add td_api::setBusinessWorkHours. --- td/generate/scheme/td_api.tl | 3 +++ td/telegram/BusinessWorkHours.cpp | 25 ++++++++++++++++++++ td/telegram/BusinessWorkHours.h | 8 ++++++- td/telegram/ContactsManager.cpp | 38 +++++++++++++++++++++++++++++++ td/telegram/ContactsManager.h | 2 ++ td/telegram/Td.cpp | 7 ++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 23 +++++++++++++++++-- 8 files changed, 105 insertions(+), 3 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 80b9882d7..f2876b1df 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -9260,6 +9260,9 @@ setLocation location:location = Ok; //@description Changes the business location of the current user. Requires Telegram Business subscription @location The new location of the business; pass null to remove the location setBusinessLocation location:chatLocation = Ok; +//@description Changes the business work hours of the current user. Requires Telegram Business subscription @work_hours The new work hours of the business; pass null to remove the work hours +setBusinessWorkHours work_hours:businessWorkHours = Ok; + //@description Changes the phone number of the user and sends an authentication code to the user's new phone number; for official Android and iOS applications only. On success, returns information about the sent code //@phone_number The new phone number of the user in international format //@settings Settings for the authentication of the user's phone number; pass null to use default settings diff --git a/td/telegram/BusinessWorkHours.cpp b/td/telegram/BusinessWorkHours.cpp index 97ee42045..81aecc7cf 100644 --- a/td/telegram/BusinessWorkHours.cpp +++ b/td/telegram/BusinessWorkHours.cpp @@ -16,6 +16,11 @@ BusinessWorkHours::WorkHoursInterval::get_business_work_hours_interval_object() return td_api::make_object(start_minute_, end_minute_); } +telegram_api::object_ptr +BusinessWorkHours::WorkHoursInterval::get_input_business_weekly_open() const { + return telegram_api::make_object(start_minute_, end_minute_); +} + BusinessWorkHours::BusinessWorkHours(telegram_api::object_ptr &&work_hours) { if (work_hours != nullptr) { work_hours_ = transform(work_hours->weekly_open_, @@ -26,6 +31,16 @@ BusinessWorkHours::BusinessWorkHours(telegram_api::object_ptr &&work_hours) { + if (work_hours != nullptr) { + work_hours_ = + transform(work_hours->work_hours_, [](const td_api::object_ptr &interval) { + return WorkHoursInterval(interval->start_minute_, interval->end_minute_); + }); + time_zone_id_ = std::move(work_hours->time_zone_id_); + } +} + bool BusinessWorkHours::is_empty() const { return work_hours_.empty(); } @@ -40,6 +55,16 @@ td_api::object_ptr BusinessWorkHours::get_business_wo })); } +telegram_api::object_ptr BusinessWorkHours::get_input_business_work_hours() const { + if (is_empty()) { + return nullptr; + } + return telegram_api::make_object( + 0, false, time_zone_id_, transform(work_hours_, [](const WorkHoursInterval &interval) { + return interval.get_input_business_weekly_open(); + })); +} + bool operator==(const BusinessWorkHours::WorkHoursInterval &lhs, const BusinessWorkHours::WorkHoursInterval &rhs) { return lhs.start_minute_ == rhs.start_minute_ && lhs.end_minute_ == rhs.end_minute_; } diff --git a/td/telegram/BusinessWorkHours.h b/td/telegram/BusinessWorkHours.h index 811b98ad5..e9c1b6fd1 100644 --- a/td/telegram/BusinessWorkHours.h +++ b/td/telegram/BusinessWorkHours.h @@ -27,6 +27,8 @@ class BusinessWorkHours { td_api::object_ptr get_business_work_hours_interval_object() const; + telegram_api::object_ptr get_input_business_weekly_open() const; + template void store(StorerT &storer) const; @@ -49,12 +51,16 @@ class BusinessWorkHours { public: BusinessWorkHours() = default; - BusinessWorkHours(telegram_api::object_ptr &&work_hours); + explicit BusinessWorkHours(telegram_api::object_ptr &&work_hours); + + explicit BusinessWorkHours(td_api::object_ptr &&work_hours); bool is_empty() const; td_api::object_ptr get_business_work_hours_object() const; + telegram_api::object_ptr get_input_business_work_hours() const; + template void store(StorerT &storer) const; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 53288387f..e30e6351b 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1126,6 +1126,40 @@ class UpdateBusinessLocationQuery final : public Td::ResultHandler { } }; +class UpdateBusinessWorkHoursQuery final : public Td::ResultHandler { + Promise promise_; + BusinessWorkHours work_hours_; + + public: + explicit UpdateBusinessWorkHoursQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(BusinessWorkHours &&work_hours) { + work_hours_ = std::move(work_hours); + int32 flags = 0; + if (!work_hours_.is_empty()) { + flags |= telegram_api::account_updateBusinessWorkHours::BUSINESS_WORK_HOURS_MASK; + } + send_query(G()->net_query_creator().create( + telegram_api::account_updateBusinessWorkHours(flags, work_hours_.get_input_business_work_hours()), {{"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()); + } + + td_->contacts_manager_->on_update_user_work_hours(td_->contacts_manager_->get_my_id(), std::move(work_hours_)); + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class CreateChatQuery final : public Td::ResultHandler { Promise> promise_; @@ -6994,6 +7028,10 @@ void ContactsManager::set_business_location(DialogLocation &&location, Promisecreate_handler(std::move(promise))->send(std::move(location)); } +void ContactsManager::set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise) { + td_->create_handler(std::move(promise))->send(std::move(work_hours)); +} + void ContactsManager::set_chat_description(ChatId chat_id, const string &description, Promise &&promise) { auto new_description = strip_empty_characters(description, MAX_DESCRIPTION_LENGTH); auto c = get_chat(chat_id); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index a753664ad..14d5ca3aa 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -467,6 +467,8 @@ class ContactsManager final : public Actor { void set_business_location(DialogLocation &&location, Promise &&promise); + void set_business_work_hours(BusinessWorkHours &&work_hours, Promise &&promise); + void set_chat_description(ChatId chat_id, const string &description, Promise &&promise); void set_channel_username(ChannelId channel_id, const string &username, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 359c9b262..d73329ba7 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -22,6 +22,7 @@ #include "td/telegram/BotCommand.h" #include "td/telegram/BotInfoManager.h" #include "td/telegram/BotMenuButton.h" +#include "td/telegram/BusinessWorkHours.h" #include "td/telegram/CallbackQueriesManager.h" #include "td/telegram/CallId.h" #include "td/telegram/CallManager.h" @@ -7744,6 +7745,12 @@ void Td::on_request(uint64 id, td_api::setBusinessLocation &request) { contacts_manager_->set_business_location(DialogLocation(std::move(request.location_)), std::move(promise)); } +void Td::on_request(uint64 id, td_api::setBusinessWorkHours &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + contacts_manager_->set_business_work_hours(BusinessWorkHours(std::move(request.work_hours_)), std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setProfilePhoto &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index f8587afdc..e8852d4fb 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1369,6 +1369,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::setBusinessLocation &request); + void on_request(uint64 id, td_api::setBusinessWorkHours &request); + void on_request(uint64 id, td_api::setProfilePhoto &request); void on_request(uint64 id, const td_api::deleteProfilePhoto &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index e3c7d5ed1..d4866c0d9 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5907,9 +5907,28 @@ class CliClient final : public Actor { get_args(args, latitude, longitude); if (latitude.empty() || longitude.empty()) { send_request(td_api::make_object(nullptr)); + } else { + send_request(td_api::make_object( + td_api::make_object(as_location(latitude, longitude, string()), "business address"))); + } + } else if (op == "sbwh") { + string time_zone_id; + string work_hours; + get_args(args, time_zone_id, work_hours); + if (time_zone_id.empty()) { + send_request(td_api::make_object(nullptr)); + } else { + auto minutes = to_integers(work_hours); + if (minutes.size() % 2 == 1) { + minutes.push_back(8 * 24 * 60); + } + vector> intervals; + for (size_t i = 0; i < minutes.size(); i += 2) { + intervals.push_back(td_api::make_object(minutes[i], minutes[i + 1])); + } + send_request(td_api::make_object( + td_api::make_object(time_zone_id, std::move(intervals)))); } - send_request(td_api::make_object( - td_api::make_object(as_location(latitude, longitude, string()), "business address"))); } else if (op == "sco") { SearchQuery query; get_args(args, query);