Add td_api::getSponsoredMessages.

This commit is contained in:
levlam 2021-08-24 23:44:47 +03:00
parent 1e663dea46
commit 723b203c7d
8 changed files with 141 additions and 2 deletions

View File

@ -395,6 +395,7 @@ set(TDLIB_SOURCE
td/telegram/SendCodeHelper.cpp
td/telegram/SequenceDispatcher.cpp
td/telegram/SpecialStickerSetType.cpp
td/telegram/SponsoredMessages.cpp
td/telegram/StateManager.cpp
td/telegram/StickersManager.cpp
td/telegram/StorageManager.cpp
@ -607,6 +608,7 @@ set(TDLIB_SOURCE
td/telegram/ServerMessageId.h
td/telegram/SetWithPosition.h
td/telegram/SpecialStickerSetType.h
td/telegram/SponsoredMessages.h
td/telegram/StateManager.h
td/telegram/StickerSetId.h
td/telegram/StickersManager.h

View File

@ -787,6 +787,14 @@ messages total_count:int32 messages:vector<message> = Messages;
foundMessages total_count:int32 messages:vector<message> next_offset:string = FoundMessages;
//@description Describes a sponsored message @id Unique sponsored message identifier @sponsor_chat_id Chat identifier
//@start_parameter Parameter for the bot start message if the sponsored chat is a chat with a bot @content Content of the message
sponsoredMessage id:bytes sponsor_chat_id:int53 start_parameter:string content:MessageContent = SponsoredMessage;
//@description Contains a list of sponsored messages @messages List of sponsored messages
sponsoredMessages messages:vector<sponsoredMessage> = SponsoredMessages;
//@class NotificationSettingsScope @description Describes the types of chats to which notification settings are relevant
//@description Notification settings applied to all private and secret chats when the corresponding chat setting has a default value
@ -4044,6 +4052,9 @@ getCallbackQueryMessage chat_id:int53 message_id:int53 callback_query_id:int64 =
//@description Returns information about messages. If a message is not found, returns null on the corresponding position of the result @chat_id Identifier of the chat the messages belong to @message_ids Identifiers of the messages to get
getMessages chat_id:int53 message_ids:vector<int53> = Messages;
//@description Returns sponsored messages to be shown in a chat; for channel chats only @chat_id Identifier of the chat
getSponsoredMessages chat_id:int53 = SponsoredMessages;
//@description Returns information about a message thread. Can be used only if message.can_get_message_thread == true @chat_id Chat identifier @message_id Identifier of the message
getMessageThread chat_id:int53 message_id:int53 = MessageThreadInfo;

View File

@ -6,8 +6,6 @@
//
#pragma once
#include "td/actor/PromiseFuture.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/files/FileId.h"
#include "td/telegram/FullMessageId.h"
@ -16,6 +14,8 @@
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"

View File

@ -0,0 +1,91 @@
//
// 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/SponsoredMessages.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/MessageContent.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/Td.h"
namespace td {
class GetSponsoredMessagesQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::sponsoredMessages>> promise_;
ChannelId channel_id_;
public:
explicit GetSponsoredMessagesQuery(Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise)
: promise_(std::move(promise)) {
}
void send(ChannelId channel_id) {
channel_id_ = channel_id;
auto input_channel = td->contacts_manager_->get_input_channel(channel_id);
if (input_channel == nullptr) {
return promise_.set_error(Status::Error(3, "Chat info not found"));
}
send_query(G()->net_query_creator().create(telegram_api::channels_getSponsoredMessages(std::move(input_channel))));
}
void on_result(uint64 id, BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::channels_getSponsoredMessages>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto sponsored_messages = result_ptr.move_as_ok();
td->contacts_manager_->on_get_users(std::move(sponsored_messages->users_), "GetSponsoredMessagesQuery");
td->contacts_manager_->on_get_chats(std::move(sponsored_messages->chats_), "GetSponsoredMessagesQuery");
vector<td_api::object_ptr<td_api::sponsoredMessage>> messages;
for (auto &sponsored_message : sponsored_messages->messages_) {
DialogId dialog_id(sponsored_message->from_id_);
if (!dialog_id.is_valid() || !td->messages_manager_->have_dialog_info_force(dialog_id)) {
LOG(ERROR) << "Receive unknown sponsor " << dialog_id;
continue;
}
td->messages_manager_->force_create_dialog(dialog_id, "GetSponsoredMessagesQuery");
auto message_text =
get_message_text(td->contacts_manager_.get(), std::move(sponsored_message->message_),
std::move(sponsored_message->entities_), true, true, 0, false, "GetSponsoredMessagesQuery");
int32 ttl = 0;
auto content = get_message_content(td, std::move(message_text), nullptr, dialog_id, true, UserId(), &ttl);
if (ttl != 0) {
LOG(ERROR) << "Receive sponsored message with TTL " << ttl;
continue;
}
messages.push_back(td_api::make_object<td_api::sponsoredMessage>(
sponsored_message->random_id_.as_slice().str(), dialog_id.get(), sponsored_message->start_param_,
get_message_content_object(content.get(), td, DialogId(channel_id_), 0, false, true, -1)));
}
promise_.set_value(td_api::make_object<td_api::sponsoredMessages>(std::move(messages)));
}
void on_error(uint64 id, Status status) final {
td->contacts_manager_->on_get_channel_error(channel_id_, status, "GetSponsoredMessagesQuery");
promise_.set_error(std::move(status));
}
};
void get_sponsored_messages(Td *td, DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise) {
if (!td->messages_manager_->have_dialog_force(dialog_id, "get_sponsored_messages")) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
if (dialog_id.get_type() != DialogType::Channel ||
td->contacts_manager_->get_channel_type(dialog_id.get_channel_id()) != ContactsManager::ChannelType::Broadcast) {
return promise.set_value(td_api::make_object<td_api::sponsoredMessages>());
}
td->create_handler<GetSponsoredMessagesQuery>(std::move(promise))->send(dialog_id.get_channel_id());
}
} // namespace td

View File

@ -0,0 +1,23 @@
//
// 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/DialogId.h"
#include "td/telegram/td_api.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/common.h"
namespace td {
class Td;
void get_sponsored_messages(Td *td, DialogId dialog_id,
Promise<td_api::object_ptr<td_api::sponsoredMessages>> &&promise);
} // namespace td

View File

@ -90,6 +90,7 @@
#include "td/telegram/SecretChatsManager.h"
#include "td/telegram/SecureManager.h"
#include "td/telegram/SecureValue.h"
#include "td/telegram/SponsoredMessages.h"
#include "td/telegram/StateManager.h"
#include "td/telegram/StickerSetId.h"
#include "td/telegram/StickersManager.h"
@ -5065,6 +5066,12 @@ void Td::on_request(uint64 id, const td_api::getMessages &request) {
CREATE_REQUEST(GetMessagesRequest, request.chat_id_, request.message_ids_);
}
void Td::on_request(uint64 id, const td_api::getSponsoredMessages &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
get_sponsored_messages(this, DialogId(request.chat_id_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getMessageThread &request) {
CHECK_IS_USER();
CREATE_REQUEST(GetMessageThreadRequest, request.chat_id_, request.message_id_);

View File

@ -512,6 +512,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, const td_api::getMessages &request);
void on_request(uint64 id, const td_api::getSponsoredMessages &request);
void on_request(uint64 id, const td_api::getMessageLink &request);
void on_request(uint64 id, const td_api::getMessageEmbeddingCode &request);

View File

@ -2586,6 +2586,9 @@ class CliClient final : public Actor {
string message_ids;
get_args(args, chat_id, message_ids);
send_request(td_api::make_object<td_api::getMessages>(as_chat_id(chat_id), as_message_ids(message_ids)));
} else if (op == "gsm") {
string chat_id = args;
send_request(td_api::make_object<td_api::getSponsoredMessages>(as_chat_id(chat_id)));
} else if (op == "gmlink") {
string chat_id;
string message_id;