Add classes BotCommand and BotCommands.

This commit is contained in:
levlam 2021-06-21 02:08:11 +03:00
parent 37d10c0be4
commit f165429143
6 changed files with 111 additions and 6 deletions

View File

@ -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

View File

@ -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<telegram_api::botCommand> &&bot_command) {
CHECK(bot_command != nullptr);
command_ = std::move(bot_command->command_);
description_ = std::move(bot_command->description_);
}
td_api::object_ptr<td_api::botCommand> BotCommand::get_bot_command_object() const {
return td_api::make_object<td_api::botCommand>(command_, description_);
}
BotCommands::BotCommands(UserId bot_user_id, vector<telegram_api::object_ptr<telegram_api::botCommand>> &&bot_commands)
: bot_user_id_(bot_user_id) {
commands_ = transform(std::move(bot_commands), [](telegram_api::object_ptr<telegram_api::botCommand> &&bot_command) {
return BotCommand(std::move(bot_command));
});
}
td_api::object_ptr<td_api::botCommands> 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_api::botCommands>(
td->contacts_manager_->get_user_id_object(bot_user_id_, "get_bot_commands_object"), std::move(commands));
}
} // namespace td

66
td/telegram/BotCommand.h Normal file
View File

@ -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<telegram_api::botCommand> &&bot_command);
td_api::object_ptr<td_api::botCommand> get_bot_command_object() const;
template <class StorerT>
void store(StorerT &storer) const {
td::store(command_, storer);
td::store(description_, storer);
}
template <class ParserT>
void parse(ParserT &parser) {
td::parse(command_, parser);
td::parse(description_, parser);
}
};
class BotCommands {
UserId bot_user_id_;
vector<BotCommand> commands_;
public:
BotCommands() = default;
BotCommands(UserId bot_user_id, vector<telegram_api::object_ptr<telegram_api::botCommand>> &&bot_commands);
td_api::object_ptr<td_api::botCommands> get_bot_commands_object(Td *td) const;
template <class StorerT>
void store(StorerT &storer) const {
td::store(bot_user_id_, storer);
td::store(commands_, storer);
}
template <class ParserT>
void parse(ParserT &parser) {
td::parse(bot_user_id_, parser);
td::parse(commands_, parser);
}
};
} // namespace td

View File

@ -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"

View File

@ -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<td_api::botCommand>(command->command_, command->description_);
});
promise_.set_value(td_api::make_object<td_api::botCommands>(
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 {

View File

@ -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"