2022-04-06 19:24:54 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
|
|
|
//
|
|
|
|
// 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/BotMenuButton.h"
|
|
|
|
|
2022-04-07 17:29:59 +02:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2022-04-07 16:20:26 +02:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2022-04-07 17:15:58 +02:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/LinkManager.h"
|
|
|
|
#include "td/telegram/misc.h"
|
2022-04-07 16:20:26 +02:00
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2022-04-07 19:38:22 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/SliceBuilder.h"
|
|
|
|
#include "td/utils/Status.h"
|
2022-04-07 16:20:26 +02:00
|
|
|
|
2022-04-06 19:24:54 +02:00
|
|
|
namespace td {
|
|
|
|
|
2022-04-07 17:15:58 +02:00
|
|
|
class SetBotMenuButtonQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetBotMenuButtonQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(UserId user_id, telegram_api::object_ptr<telegram_api::BotMenuButton> input_bot_menu_button) {
|
|
|
|
auto input_user = user_id.is_valid() ? td_->contacts_manager_->get_input_user(user_id).move_as_ok()
|
2022-04-07 19:38:22 +02:00
|
|
|
: make_tl_object<telegram_api::inputUserEmpty>();
|
2022-04-07 17:15:58 +02:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::bots_setBotMenuButton(std::move(input_user), std::move(input_bot_menu_button))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::bots_setBotMenuButton>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result_ptr.ok()) {
|
|
|
|
LOG(ERROR) << "Receive false as result of SetBotMenuButtonQuery";
|
|
|
|
}
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-07 16:20:26 +02:00
|
|
|
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()
|
2022-04-07 19:38:22 +02:00
|
|
|
: make_tl_object<telegram_api::inputUserEmpty>();
|
2022-04-07 16:20:26 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2022-04-08 03:02:25 +02:00
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetBotMenuButtonQuery: " << to_string(ptr);
|
|
|
|
auto bot_menu_button = get_bot_menu_button(std::move(ptr));
|
2022-04-07 16:20:26 +02:00
|
|
|
promise_.set_value(bot_menu_button == nullptr ? td_api::make_object<td_api::botMenuButton>()
|
2022-04-07 17:29:59 +02:00
|
|
|
: bot_menu_button->get_bot_menu_button_object(td_));
|
2022-04-07 16:20:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-04-19 16:43:24 +02:00
|
|
|
if (bot_menu_button == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-06 19:24:54 +02:00
|
|
|
switch (bot_menu_button->get_id()) {
|
|
|
|
case telegram_api::botMenuButtonCommands::ID:
|
|
|
|
return nullptr;
|
|
|
|
case telegram_api::botMenuButtonDefault::ID:
|
|
|
|
return td::make_unique<BotMenuButton>(string(), "default");
|
|
|
|
case telegram_api::botMenuButton::ID: {
|
|
|
|
auto button = telegram_api::move_object_as<telegram_api::botMenuButton>(bot_menu_button);
|
|
|
|
if (button->text_.empty()) {
|
|
|
|
LOG(ERROR) << "Receive bot menu button with empty text: " << to_string(button);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return td::make_unique<BotMenuButton>(std::move(button->text_), std::move(button->url_));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:29:59 +02:00
|
|
|
td_api::object_ptr<td_api::botMenuButton> BotMenuButton::get_bot_menu_button_object(Td *td) const {
|
|
|
|
bool is_bot = td->auth_manager_->is_bot();
|
|
|
|
return td_api::make_object<td_api::botMenuButton>(text_, is_bot ? url_ : "menu://" + url_);
|
2022-04-06 19:24:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const BotMenuButton &lhs, const BotMenuButton &rhs) {
|
|
|
|
return lhs.text_ == rhs.text_ && lhs.url_ == rhs.url_;
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:29:59 +02:00
|
|
|
td_api::object_ptr<td_api::botMenuButton> get_bot_menu_button_object(Td *td, const BotMenuButton *bot_menu_button) {
|
2022-04-07 16:20:26 +02:00
|
|
|
if (bot_menu_button == nullptr) {
|
|
|
|
return nullptr;
|
2022-04-06 19:24:54 +02:00
|
|
|
}
|
2022-04-07 17:29:59 +02:00
|
|
|
return bot_menu_button->get_bot_menu_button_object(td);
|
2022-04-07 16:20:26 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 17:15:58 +02:00
|
|
|
void set_menu_button(Td *td, UserId user_id, td_api::object_ptr<td_api::botMenuButton> &&menu_button,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!user_id.is_valid() && user_id != UserId()) {
|
|
|
|
return promise.set_error(Status::Error(400, "User not found"));
|
|
|
|
}
|
|
|
|
|
|
|
|
telegram_api::object_ptr<telegram_api::BotMenuButton> input_bot_menu_button;
|
|
|
|
if (menu_button == nullptr) {
|
|
|
|
input_bot_menu_button = telegram_api::make_object<telegram_api::botMenuButtonCommands>();
|
|
|
|
} else if (menu_button->text_.empty()) {
|
|
|
|
if (menu_button->url_ != "default") {
|
2022-09-10 21:48:34 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Menu button text must be non-empty"));
|
2022-04-07 17:15:58 +02:00
|
|
|
}
|
|
|
|
input_bot_menu_button = telegram_api::make_object<telegram_api::botMenuButtonDefault>();
|
|
|
|
} else {
|
|
|
|
if (!clean_input_string(menu_button->text_)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Menu button text must be encoded in UTF-8"));
|
|
|
|
}
|
|
|
|
if (!clean_input_string(menu_button->url_)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Menu button URL must be encoded in UTF-8"));
|
|
|
|
}
|
|
|
|
auto r_url = LinkManager::check_link(menu_button->url_, true, !G()->is_test_dc());
|
|
|
|
if (r_url.is_error()) {
|
2022-05-31 00:41:01 +02:00
|
|
|
return promise.set_error(Status::Error(400, PSLICE() << "Menu button Web App " << r_url.error().message()));
|
2022-04-07 17:15:58 +02:00
|
|
|
}
|
|
|
|
input_bot_menu_button = telegram_api::make_object<telegram_api::botMenuButton>(menu_button->text_, r_url.ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
td->create_handler<SetBotMenuButtonQuery>(std::move(promise))->send(user_id, std::move(input_bot_menu_button));
|
|
|
|
}
|
|
|
|
|
2022-04-07 16:20:26 +02:00
|
|
|
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);
|
2022-04-06 19:24:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|