diff --git a/CMakeLists.txt b/CMakeLists.txt index bf9b75001..abc993f83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -278,6 +278,7 @@ set(TDLIB_SOURCE td/telegram/AutoDownloadSettings.cpp td/telegram/BackgroundManager.cpp td/telegram/BackgroundType.cpp + td/telegram/BotCommand.cpp td/telegram/BotCommandScope.cpp td/telegram/CallActor.cpp td/telegram/CallDiscardReason.cpp @@ -444,6 +445,7 @@ set(TDLIB_SOURCE td/telegram/BackgroundId.h td/telegram/BackgroundManager.h td/telegram/BackgroundType.h + td/telegram/BotCommand.h td/telegram/BotCommandScope.h td/telegram/CallActor.h td/telegram/CallDiscardReason.h diff --git a/td/telegram/BotCommand.cpp b/td/telegram/BotCommand.cpp new file mode 100644 index 000000000..800630c54 --- /dev/null +++ b/td/telegram/BotCommand.cpp @@ -0,0 +1,39 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/BotCommand.h" + +#include "td/telegram/ContactsManager.h" +#include "td/telegram/Td.h" + +#include "td/utils/algorithm.h" + +namespace td { + +BotCommand::BotCommand(telegram_api::object_ptr &&bot_command) { + CHECK(bot_command != nullptr); + command_ = std::move(bot_command->command_); + description_ = std::move(bot_command->description_); +} + +td_api::object_ptr BotCommand::get_bot_command_object() const { + return td_api::make_object(command_, description_); +} + +BotCommands::BotCommands(UserId bot_user_id, vector> &&bot_commands) + : bot_user_id_(bot_user_id) { + commands_ = transform(std::move(bot_commands), [](telegram_api::object_ptr &&bot_command) { + return BotCommand(std::move(bot_command)); + }); +} + +td_api::object_ptr BotCommands::get_bot_commands_object(Td *td) const { + auto commands = transform(commands_, [](const auto &command) { return command.get_bot_command_object(); }); + return td_api::make_object( + td->contacts_manager_->get_user_id_object(bot_user_id_, "get_bot_commands_object"), std::move(commands)); +} + +} // namespace td diff --git a/td/telegram/BotCommand.h b/td/telegram/BotCommand.h new file mode 100644 index 000000000..fd7a78fbc --- /dev/null +++ b/td/telegram/BotCommand.h @@ -0,0 +1,66 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" +#include "td/telegram/UserId.h" + +#include "td/utils/common.h" +#include "td/utils/tl_helpers.h" + +namespace td { + +class Td; + +class BotCommand { + string command_; + string description_; + + public: + BotCommand() = default; + BotCommand(telegram_api::object_ptr &&bot_command); + + td_api::object_ptr get_bot_command_object() const; + + template + void store(StorerT &storer) const { + td::store(command_, storer); + td::store(description_, storer); + } + + template + void parse(ParserT &parser) { + td::parse(command_, parser); + td::parse(description_, parser); + } +}; + +class BotCommands { + UserId bot_user_id_; + vector commands_; + + public: + BotCommands() = default; + BotCommands(UserId bot_user_id, vector> &&bot_commands); + + td_api::object_ptr get_bot_commands_object(Td *td) const; + + template + void store(StorerT &storer) const { + td::store(bot_user_id_, storer); + td::store(commands_, storer); + } + + template + void parse(ParserT &parser) { + td::parse(bot_user_id_, parser); + td::parse(commands_, parser); + } +}; + +} // namespace td diff --git a/td/telegram/BotCommandScope.h b/td/telegram/BotCommandScope.h index 28ef6f688..d16750835 100644 --- a/td/telegram/BotCommandScope.h +++ b/td/telegram/BotCommandScope.h @@ -8,6 +8,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" #include "td/telegram/UserId.h" #include "td/utils/common.h" diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index e81fd3997..2f4d65a0a 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1030,12 +1030,8 @@ class GetBotCommandsQuery : public Td::ResultHandler { return on_error(id, result_ptr.move_as_error()); } - auto commands = transform(result_ptr.move_as_ok(), [](auto &&command) { - return td_api::make_object(command->command_, command->description_); - }); - promise_.set_value(td_api::make_object( - td->contacts_manager_->get_user_id_object(td->contacts_manager_->get_my_id(), "GetBotCommandsQuery"), - std::move(commands))); + BotCommands commands(td->contacts_manager_->get_my_id(), result_ptr.move_as_ok()); + promise_.set_value(commands.get_bot_commands_object(td)); } void on_error(uint64 id, Status status) override { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 3064f016c..0cd9ee07b 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -10,6 +10,7 @@ #include "td/telegram/telegram_api.h" #include "td/telegram/AccessRights.h" +#include "td/telegram/BotCommand.h" #include "td/telegram/ChannelId.h" #include "td/telegram/ChatId.h" #include "td/telegram/Contact.h"