diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 923a38d06..2a92fd0b4 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4396,6 +4396,11 @@ publicForwards total_count:int32 forwards:vector next_offset:stri //@description Contains a list of media previews of a bot @previews List of media previews botMediaPreviews previews:vector = BotMediaPreviews; +//@description Contains a list of media previews of a bot for the given language and the list of langauges for which the bot has dedicated previews +//@previews List of media previews +//@language_codes List of language codes for which the bot has dedicated previews +botMediaPreviewInfo previews:vector language_codes:vector = BotMediaPreviewInfo; + //@description Contains a list of features available on a specific chat boost level //@level Target chat boost level @@ -10635,14 +10640,18 @@ 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. The bot must have the main Web App +//@description Returns the list of media previews of a bot @bot_user_id Identifier of the target bot. The bot must have the main Web App getBotMediaPreviews bot_user_id:int53 = BotMediaPreviews; +//@description Returns the list of media previews for the given language and the list of langauges for which the bot has dedicated previews +//@bot_user_id Identifier of the target bot. The bot must be owned and must have the main Web App +//@language_code A two-letter ISO 639-1 language code for which to get previews. If empty, then default previews are returned +getBotMediaPreviewInfo bot_user_id:int53 language_code:string = BotMediaPreviewInfo; + //@description Adds a new media preview to the beginning of the list of media previews of a bot. Returns the added preview after addition is completed server-side. The total number of previews must not exceed getOption("bot_media_preview_count_max") for the given language //@bot_user_id Identifier of the target bot. The bot must be owned and must have the main Web App //@language_code A two-letter ISO 639-1 language code for which preview is added. If empty, then the preview will be shown to all users for whose languages there are no dedicated previews. -//-If non-empty, then there must be an official language pack with the same name as returned by getLocalizationTargetInfo +//-If non-empty, then there must be an official language pack of the same name, which is returned by getLocalizationTargetInfo //@content Content of the added preview addBotMediaPreview bot_user_id:int53 language_code:string content:InputStoryContent = StoryContent; diff --git a/td/telegram/BotInfoManager.cpp b/td/telegram/BotInfoManager.cpp index 905d3e377..1f5fafb19 100644 --- a/td/telegram/BotInfoManager.cpp +++ b/td/telegram/BotInfoManager.cpp @@ -143,6 +143,58 @@ class GetPreviewMediasQuery final : public Td::ResultHandler { } }; +class GetPreviewInfoQuery final : public Td::ResultHandler { + Promise> promise_; + UserId bot_user_id_; + string language_code_; + + public: + explicit GetPreviewInfoQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(UserId bot_user_id, telegram_api::object_ptr input_user, + const string &language_code) { + bot_user_id_ = bot_user_id; + language_code_ = language_code; + send_query(G()->net_query_creator().create(telegram_api::bots_getPreviewInfo(std::move(input_user), language_code), + {{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 GetPreviewInfoQuery: " << to_string(ptr); + vector> contents; + vector file_ids; + for (auto &media_ptr : ptr->media_) { + auto content = get_story_content(td_, std::move(media_ptr->media_), DialogId(bot_user_id_)); + if (content == nullptr) { + LOG(ERROR) << "Receive invalid media preview for " << bot_user_id_; + } else { + append(file_ids, get_story_content_file_ids(td_, content.get())); + contents.push_back(get_story_content_object(td_, content.get())); + } + } + if (!file_ids.empty()) { + // auto file_source_id = td_->bot_info_manager_->get_bot_media_preview_file_source_id(bot_user_id_); + for (auto file_id : file_ids) { + // td_->file_manager_->add_file_source(file_id, file_source_id); + } + } + promise_.set_value( + td_api::make_object(std::move(contents), std::move(ptr->lang_codes_))); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class BotInfoManager::AddPreviewMediaQuery final : public Td::ResultHandler { FileId file_id_; unique_ptr pending_preview_; @@ -641,6 +693,13 @@ void BotInfoManager::get_bot_media_previews(UserId bot_user_id, td_->create_handler(std::move(promise))->send(bot_user_id, std::move(input_user)); } +void BotInfoManager::get_bot_media_preview_info(UserId bot_user_id, const string &language_code, + Promise> &&promise) { + TRY_RESULT_PROMISE(promise, input_user, get_media_preview_bot_input_user(bot_user_id, true)); + TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code)); + td_->create_handler(std::move(promise))->send(bot_user_id, std::move(input_user), language_code); +} + void BotInfoManager::reload_bot_media_previews(UserId bot_user_id, Promise &&promise) { get_bot_media_previews( bot_user_id, PromiseCreator::lambda([promise = std::move(promise)]( diff --git a/td/telegram/BotInfoManager.h b/td/telegram/BotInfoManager.h index 4ff1187d1..85d99c720 100644 --- a/td/telegram/BotInfoManager.h +++ b/td/telegram/BotInfoManager.h @@ -45,6 +45,9 @@ class BotInfoManager final : public Actor { void get_bot_media_previews(UserId bot_user_id, Promise> &&promise); + void get_bot_media_preview_info(UserId bot_user_id, const string &language_code, + Promise> &&promise); + void reload_bot_media_previews(UserId bot_user_id, Promise &&promise); void add_bot_media_preview(UserId bot_user_id, const string &language_code, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index cdbeee079..0f2c8b09a 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7902,6 +7902,13 @@ void Td::on_request(uint64 id, const td_api::getBotMediaPreviews &request) { bot_info_manager_->get_bot_media_previews(UserId(request.bot_user_id_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::getBotMediaPreviewInfo &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + bot_info_manager_->get_bot_media_preview_info(UserId(request.bot_user_id_), request.language_code_, + std::move(promise)); +} + void Td::on_request(uint64 id, td_api::addBotMediaPreview &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index d4d26561c..5ddf2273e 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1459,6 +1459,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getBotMediaPreviews &request); + void on_request(uint64 id, const td_api::getBotMediaPreviewInfo &request); + void on_request(uint64 id, td_api::addBotMediaPreview &request); void on_request(uint64 id, td_api::editBotMediaPreview &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index d16f457f0..f5cfa6530 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6569,6 +6569,11 @@ class CliClient final : public Actor { UserId bot_user_id; get_args(args, bot_user_id); send_request(td_api::make_object(bot_user_id)); + } else if (op == "gbmpi") { + UserId bot_user_id; + string language_code; + get_args(args, bot_user_id, language_code); + send_request(td_api::make_object(bot_user_id, language_code)); } else if (op == "abmpp") { UserId bot_user_id; string language_code;