From 5a5a9a28a44b05d93fc9d56106c2541fe8525e21 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 18 Jul 2024 17:54:41 +0300 Subject: [PATCH] Add td_api::getBotMediaPreviews. --- td/generate/scheme/td_api.tl | 9 ++++++ td/telegram/BotInfoManager.cpp | 56 ++++++++++++++++++++++++++++++++++ td/telegram/BotInfoManager.h | 5 +++ td/telegram/Td.cpp | 5 +++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 4 +++ 6 files changed, 81 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 0d812bf2c..f2a29cc9e 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4387,6 +4387,10 @@ publicForwardStory story:story = PublicForward; publicForwards total_count:int32 forwards:vector next_offset:string = PublicForwards; +//@description Contains a list of media previews of a bot @previews List of media previews +botMediaPreviews previews:vector = BotMediaPreviews; + + //@description Contains a list of features available on a specific chat boost level //@level Target chat boost level //@story_per_day_count Number of stories that the chat can publish daily @@ -10616,6 +10620,11 @@ allowBotToSendMessages bot_user_id:int53 = Ok; sendWebAppCustomRequest bot_user_id:int53 method:string parameters:string = CustomRequestResult; +//@description Returns the list of media previews of a bot +//@bot_user_id Identifier of the target bot +getBotMediaPreviews bot_user_id:int53 = BotMediaPreviews; + + //@description Sets the name of a bot. Can be called only if userTypeBot.can_be_edited == true //@bot_user_id Identifier of the target bot //@language_code A two-letter ISO 639-1 language code. If empty, the name will be shown to all users for whose languages there is no dedicated name diff --git a/td/telegram/BotInfoManager.cpp b/td/telegram/BotInfoManager.cpp index b9553a0bf..74a71108b 100644 --- a/td/telegram/BotInfoManager.cpp +++ b/td/telegram/BotInfoManager.cpp @@ -10,6 +10,7 @@ #include "td/telegram/Global.h" #include "td/telegram/misc.h" #include "td/telegram/net/NetQueryCreator.h" +#include "td/telegram/StoryContent.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" #include "td/telegram/UpdatesManager.h" @@ -90,6 +91,46 @@ class SetBotBroadcastDefaultAdminRightsQuery final : public Td::ResultHandler { } }; +class GetPreviewMediasQuery final : public Td::ResultHandler { + Promise> promise_; + UserId bot_user_id_; + + public: + explicit GetPreviewMediasQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(UserId bot_user_id, telegram_api::object_ptr input_user) { + bot_user_id_ = bot_user_id; + send_query( + G()->net_query_creator().create(telegram_api::bots_getPreviewMedias(std::move(input_user)), {{bot_user_id}})); + } + + 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 ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetPreviewMediasQuery: " << to_string(ptr); + vector> contents; + for (auto &media_ptr : ptr) { + auto content = get_story_content(td_, std::move(media_ptr->media_), DialogId(bot_user_id_)); + if (content == nullptr) { + LOG(ERROR) << "Receive invalid preview media for " << bot_user_id_; + } else { + contents.push_back(get_story_content_object(td_, content.get())); + } + } + promise_.set_value(td_api::make_object(std::move(contents))); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class CanBotSendMessageQuery final : public Td::ResultHandler { Promise promise_; @@ -392,6 +433,21 @@ void BotInfoManager::allow_bot_to_send_messages(UserId bot_user_id, Promisecreate_handler(std::move(promise))->send(bot_user_id); } +Result> BotInfoManager::get_media_preview_bot_input_user( + UserId user_id, bool can_be_edited) { + TRY_RESULT(bot_data, td_->user_manager_->get_bot_data(user_id)); + if (can_be_edited && !bot_data.can_be_edited) { + return Status::Error(400, "Bot must be owned"); + } + return td_->user_manager_->get_input_user(user_id); +} + +void BotInfoManager::get_bot_media_previews(UserId bot_user_id, + Promise> &&promise) { + TRY_RESULT_PROMISE(promise, input_user, get_media_preview_bot_input_user(bot_user_id)); + td_->create_handler(std::move(promise))->send(bot_user_id, std::move(input_user)); +} + void BotInfoManager::add_pending_set_query(UserId bot_user_id, const string &language_code, int type, const string &value, Promise &&promise) { pending_set_bot_info_queries_.emplace_back(bot_user_id, language_code, type, value, std::move(promise)); diff --git a/td/telegram/BotInfoManager.h b/td/telegram/BotInfoManager.h index 5d9270a59..14b4a88d8 100644 --- a/td/telegram/BotInfoManager.h +++ b/td/telegram/BotInfoManager.h @@ -30,6 +30,8 @@ class BotInfoManager final : public Actor { void allow_bot_to_send_messages(UserId bot_user_id, Promise &&promise); + void get_bot_media_previews(UserId bot_user_id, Promise> &&promise); + void set_bot_name(UserId bot_user_id, const string &language_code, const string &name, Promise &&promise); void get_bot_name(UserId bot_user_id, const string &language_code, Promise &&promise); @@ -81,6 +83,9 @@ class BotInfoManager final : public Actor { void timeout_expired() final; + Result> get_media_preview_bot_input_user( + UserId user_id, bool can_be_edited = false); + void add_pending_set_query(UserId bot_user_id, const string &language_code, int type, const string &value, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index fe4000b93..c44a9f3f5 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7895,6 +7895,11 @@ void Td::on_request(uint64 id, td_api::sendWebAppCustomRequest &request) { request.parameters_, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getBotMediaPreviews &request) { + CREATE_REQUEST_PROMISE(); + bot_info_manager_->get_bot_media_previews(UserId(request.bot_user_id_), std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setBotName &request) { CLEAN_INPUT_STRING(request.name_); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 7f3482f43..565ec7843 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1457,6 +1457,8 @@ class Td final : public Actor { void on_request(uint64 id, td_api::sendWebAppCustomRequest &request); + void on_request(uint64 id, const td_api::getBotMediaPreviews &request); + void on_request(uint64 id, td_api::setBotName &request); void on_request(uint64 id, const td_api::getBotName &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index ed5b626e8..443a4ee0c 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6558,6 +6558,10 @@ class CliClient final : public Actor { string parameters; get_args(args, bot_user_id, method, parameters); send_request(td_api::make_object(bot_user_id, method, parameters)); + } else if (op == "gbmp") { + UserId bot_user_id; + get_args(args, bot_user_id); + send_request(td_api::make_object(bot_user_id)); } else if (op == "gbi") { UserId bot_user_id; string language_code;