From 4155752cdf458d982ce7fe2bd8dca104f9510bab Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 7 Apr 2022 17:20:26 +0300 Subject: [PATCH] Add td_api::getMenuButton. --- td/generate/scheme/td_api.tl | 3 ++ td/telegram/BotMenuButton.cpp | 54 +++++++++++++++++++++++++++++---- td/telegram/BotMenuButton.h | 16 ++++++---- td/telegram/ContactsManager.cpp | 10 +++--- td/telegram/ContactsManager.h | 3 +- td/telegram/Td.cpp | 7 +++++ td/telegram/Td.h | 2 ++ 7 files changed, 75 insertions(+), 20 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 6e9889ad2..6b441a362 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5716,6 +5716,9 @@ deleteCommands scope:BotCommandScope language_code:string = Ok; //@language_code A two-letter ISO 639-1 language code or an empty string getCommands scope:BotCommandScope language_code:string = BotCommands; +//@description Returns menu button set by the bot for the given user; for bots only @user_id Identifier of the user +getMenuButton user_id:int53 = BotMenuButton; + //@description Sets default administrator rights for adding the bot to basic group and supergroup chats; for bots only @default_group_administrator_rights Default administrator rights for adding the bot to basic group and supergroup chats; may be null setDefaultGroupAdministratorRights default_group_administrator_rights:chatAdministratorRights = Ok; diff --git a/td/telegram/BotMenuButton.cpp b/td/telegram/BotMenuButton.cpp index ccc3413cb..5351daa9e 100644 --- a/td/telegram/BotMenuButton.cpp +++ b/td/telegram/BotMenuButton.cpp @@ -6,10 +6,44 @@ // #include "td/telegram/BotMenuButton.h" +#include "td/telegram/ContactsManager.h" +#include "td/telegram/Td.h" + +#include "td/utils/buffer.h" + namespace td { -unique_ptr BotMenuButton::get_bot_menu_button( - telegram_api::object_ptr &&bot_menu_button) { +class GetBotMenuButtonQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetBotMenuButtonQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(UserId user_id) { + auto input_user = user_id.is_valid() ? td_->contacts_manager_->get_input_user(user_id).move_as_ok() + : tl_object_ptr(); + send_query(G()->net_query_creator().create(telegram_api::bots_getBotMenuButton(std::move(input_user)))); + } + + 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 bot_menu_button = get_bot_menu_button(result_ptr.move_as_ok()); + promise_.set_value(bot_menu_button == nullptr ? td_api::make_object() + : bot_menu_button->get_bot_menu_button_object()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + +unique_ptr get_bot_menu_button(telegram_api::object_ptr &&bot_menu_button) { CHECK(bot_menu_button != nullptr); switch (bot_menu_button->get_id()) { case telegram_api::botMenuButtonCommands::ID: @@ -38,11 +72,19 @@ bool operator==(const BotMenuButton &lhs, const BotMenuButton &rhs) { return lhs.text_ == rhs.text_ && lhs.url_ == rhs.url_; } -bool operator==(const unique_ptr &lhs, const unique_ptr &rhs) { - if (lhs == nullptr) { - return rhs == nullptr; +td_api::object_ptr get_bot_menu_button_object(const BotMenuButton *bot_menu_button) { + if (bot_menu_button == nullptr) { + return nullptr; } - return rhs != nullptr && *lhs == *rhs; + return bot_menu_button->get_bot_menu_button_object(); +} + +void get_menu_button(Td *td, UserId user_id, Promise> &&promise) { + if (!user_id.is_valid() && user_id != UserId()) { + return promise.set_error(Status::Error(400, "User not found")); + } + + td->create_handler(std::move(promise))->send(user_id); } } // namespace td diff --git a/td/telegram/BotMenuButton.h b/td/telegram/BotMenuButton.h index cd1f19e9f..afcee2e56 100644 --- a/td/telegram/BotMenuButton.h +++ b/td/telegram/BotMenuButton.h @@ -8,6 +8,9 @@ #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" +#include "td/telegram/UserId.h" + +#include "td/actor/PromiseFuture.h" #include "td/utils/common.h" #include "td/utils/tl_helpers.h" @@ -28,9 +31,6 @@ class BotMenuButton { BotMenuButton(string &&text, string &&url) : text_(std::move(text)), url_(std::move(url)) { } - static unique_ptr get_bot_menu_button( - telegram_api::object_ptr &&bot_menu_button); - td_api::object_ptr get_bot_menu_button_object() const; template @@ -68,10 +68,14 @@ class BotMenuButton { bool operator==(const BotMenuButton &lhs, const BotMenuButton &rhs); -bool operator==(const unique_ptr &lhs, const unique_ptr &rhs); - -inline bool operator!=(const unique_ptr &lhs, const unique_ptr &rhs) { +inline bool operator!=(const BotMenuButton &lhs, const BotMenuButton &rhs) { return !(lhs == rhs); } +unique_ptr get_bot_menu_button(telegram_api::object_ptr &&bot_menu_button); + +td_api::object_ptr get_bot_menu_button_object(const BotMenuButton *bot_menu_button); + +void get_menu_button(Td *td, UserId user_id, Promise> &&promise); + } // namespace td diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 4488964f3..8ce51ef40 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -11567,8 +11567,9 @@ void ContactsManager::on_update_user_full_menu_button(UserFull *user_full, UserI tl_object_ptr &&bot_menu_button) { CHECK(user_full != nullptr); CHECK(bot_menu_button != nullptr); - auto new_button = BotMenuButton::get_bot_menu_button(std::move(bot_menu_button)); - if (user_full->menu_button != new_button) { + auto new_button = get_bot_menu_button(std::move(bot_menu_button)); + if (user_full->menu_button == nullptr ? new_button != nullptr + : new_button == nullptr || *user_full->menu_button != *new_button) { user_full->menu_button = std::move(new_button); user_full->is_changed = true; } @@ -16355,10 +16356,7 @@ tl_object_ptr ContactsManager::get_user_full_info_object(U td_api::object_ptr bot_info; bool is_bot = is_user_bot(user_id); if (is_bot) { - td_api::object_ptr menu_button; - if (user_full->menu_button != nullptr) { - menu_button = user_full->menu_button->get_bot_menu_button_object(); - } + auto menu_button = get_bot_menu_button_object(user_full->menu_button.get()); auto commands = transform(user_full->commands, [](const auto &command) { return command.get_bot_command_object(); }); bot_info = td_api::make_object( diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 8aef32e28..e70eb0e7f 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -8,6 +8,7 @@ #include "td/telegram/AccessRights.h" #include "td/telegram/BotCommand.h" +#include "td/telegram/BotMenuButton.h" #include "td/telegram/ChannelId.h" #include "td/telegram/ChannelType.h" #include "td/telegram/ChatId.h" @@ -57,8 +58,6 @@ namespace td { struct BinlogEvent; -class BotMenuButton; - class ChannelParticipantFilter; struct MinChannel; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 686a7c64f..33e7e8efe 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -16,6 +16,7 @@ #include "td/telegram/BackgroundManager.h" #include "td/telegram/BackgroundType.h" #include "td/telegram/BotCommand.h" +#include "td/telegram/BotMenuButton.h" #include "td/telegram/CallbackQueriesManager.h" #include "td/telegram/CallId.h" #include "td/telegram/CallManager.h" @@ -6798,6 +6799,12 @@ void Td::on_request(uint64 id, td_api::getCommands &request) { get_commands(this, std::move(request.scope_), std::move(request.language_code_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getMenuButton &request) { + CHECK_IS_BOT(); + CREATE_REQUEST_PROMISE(); + get_menu_button(this, UserId(request.user_id_), std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::setDefaultGroupAdministratorRights &request) { CHECK_IS_BOT(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 47fea94b9..48a61f304 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1002,6 +1002,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::getCommands &request); + void on_request(uint64 id, const td_api::getMenuButton &request); + void on_request(uint64 id, const td_api::setDefaultGroupAdministratorRights &request); void on_request(uint64 id, const td_api::setDefaultChannelAdministratorRights &request);