Add td_api::getMenuButton.

This commit is contained in:
levlam 2022-04-07 17:20:26 +03:00
parent 62612b9668
commit 4155752cdf
7 changed files with 75 additions and 20 deletions

View File

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

View File

@ -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> BotMenuButton::get_bot_menu_button(
telegram_api::object_ptr<telegram_api::BotMenuButton> &&bot_menu_button) {
class GetBotMenuButtonQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::botMenuButton>> promise_;
public:
explicit GetBotMenuButtonQuery(Promise<td_api::object_ptr<td_api::botMenuButton>> &&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<telegram_api::inputUserEmpty>();
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<telegram_api::bots_getBotMenuButton>(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<td_api::botMenuButton>()
: bot_menu_button->get_bot_menu_button_object());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
unique_ptr<BotMenuButton> get_bot_menu_button(telegram_api::object_ptr<telegram_api::BotMenuButton> &&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<BotMenuButton> &lhs, const unique_ptr<BotMenuButton> &rhs) {
if (lhs == nullptr) {
return rhs == nullptr;
td_api::object_ptr<td_api::botMenuButton> 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<td_api::object_ptr<td_api::botMenuButton>> &&promise) {
if (!user_id.is_valid() && user_id != UserId()) {
return promise.set_error(Status::Error(400, "User not found"));
}
td->create_handler<GetBotMenuButtonQuery>(std::move(promise))->send(user_id);
}
} // namespace td

View File

@ -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<BotMenuButton> get_bot_menu_button(
telegram_api::object_ptr<telegram_api::BotMenuButton> &&bot_menu_button);
td_api::object_ptr<td_api::botMenuButton> get_bot_menu_button_object() const;
template <class StorerT>
@ -68,10 +68,14 @@ class BotMenuButton {
bool operator==(const BotMenuButton &lhs, const BotMenuButton &rhs);
bool operator==(const unique_ptr<BotMenuButton> &lhs, const unique_ptr<BotMenuButton> &rhs);
inline bool operator!=(const unique_ptr<BotMenuButton> &lhs, const unique_ptr<BotMenuButton> &rhs) {
inline bool operator!=(const BotMenuButton &lhs, const BotMenuButton &rhs) {
return !(lhs == rhs);
}
unique_ptr<BotMenuButton> get_bot_menu_button(telegram_api::object_ptr<telegram_api::BotMenuButton> &&bot_menu_button);
td_api::object_ptr<td_api::botMenuButton> get_bot_menu_button_object(const BotMenuButton *bot_menu_button);
void get_menu_button(Td *td, UserId user_id, Promise<td_api::object_ptr<td_api::botMenuButton>> &&promise);
} // namespace td

View File

@ -11567,8 +11567,9 @@ void ContactsManager::on_update_user_full_menu_button(UserFull *user_full, UserI
tl_object_ptr<telegram_api::BotMenuButton> &&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<td_api::userFullInfo> ContactsManager::get_user_full_info_object(U
td_api::object_ptr<td_api::botInfo> bot_info;
bool is_bot = is_user_bot(user_id);
if (is_bot) {
td_api::object_ptr<td_api::botMenuButton> 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<td_api::botInfo>(

View File

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

View File

@ -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();

View File

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