Support sending of paid media by business bots.

This commit is contained in:
levlam 2024-08-02 15:59:42 +03:00
parent e90d8621fb
commit d13d8a3176
5 changed files with 90 additions and 7 deletions

View File

@ -783,10 +783,8 @@ Result<InputMessageContent> BusinessConnectionManager::process_input_message_con
if (message_content_id == td_api::inputMessageForwarded::ID) {
return Status::Error(400, "Can't forward messages as business");
}
if (message_content_id == td_api::inputMessagePaidMedia::ID) {
return Status::Error(400, "Can't send paid media as business");
}
return get_input_message_content(DialogId(), std::move(input_message_content), td_, true);
return get_input_message_content(td_->dialog_manager_->get_my_dialog_id(), std::move(input_message_content), td_,
true);
}
unique_ptr<BusinessConnectionManager::PendingMessage> BusinessConnectionManager::create_business_message_to_send(
@ -848,6 +846,42 @@ void BusinessConnectionManager::do_send_message(unique_ptr<PendingMessage> &&mes
return;
}
if (content_type == MessageContentType::PaidMedia) {
auto message_contents = get_individual_message_contents(content);
auto request_id = ++current_media_group_send_request_id_;
auto &request = media_group_send_requests_[request_id];
request.upload_results_.resize(message_contents.size());
request.paid_media_promise_ = std::move(promise);
request.paid_media_message_ = std::move(message);
for (size_t media_pos = 0; media_pos < message_contents.size(); media_pos++) {
auto fake_message = make_unique<PendingMessage>();
fake_message->dialog_id_ = request.paid_media_message_->dialog_id_;
fake_message->business_connection_id_ = request.paid_media_message_->business_connection_id_;
fake_message->content_ = std::move(message_contents[media_pos]);
auto input_media = get_message_content_input_media(fake_message->content_.get(), td_, MessageSelfDestructType(),
string(), td_->auth_manager_->is_bot());
if (input_media != nullptr) {
auto file_id = get_message_file_id(fake_message);
CHECK(file_id.is_valid());
FileView file_view = td_->file_manager_->get_file_view(file_id);
if (file_view.has_remote_location()) {
UploadMediaResult result;
result.message_ = std::move(fake_message);
result.input_media_ = std::move(input_media);
on_upload_message_paid_media(request_id, media_pos, std::move(result));
continue;
}
}
upload_media(std::move(fake_message), PromiseCreator::lambda([actor_id = actor_id(this), request_id, media_pos](
Result<UploadMediaResult> &&result) mutable {
send_closure(actor_id, &BusinessConnectionManager::on_upload_message_paid_media, request_id,
media_pos, std::move(result));
}));
}
return;
}
auto input_media =
get_message_content_input_media(content, td_, message->ttl_, message->send_emoji_, td_->auth_manager_->is_bot());
if (input_media != nullptr) {
@ -1129,6 +1163,7 @@ void BusinessConnectionManager::on_upload_message_album_media(int64 request_id,
auto upload_results = std::move(request.upload_results_);
auto promise = std::move(request.promise_);
CHECK(request.paid_media_message_ == nullptr);
media_group_send_requests_.erase(it);
for (auto &r_upload_result : upload_results) {
@ -1183,6 +1218,43 @@ void BusinessConnectionManager::process_sent_business_message_album(
promise.set_value(std::move(messages));
}
void BusinessConnectionManager::on_upload_message_paid_media(int64 request_id, size_t media_pos,
Result<UploadMediaResult> &&result) {
G()->ignore_result_if_closing(result);
auto it = media_group_send_requests_.find(request_id);
CHECK(it != media_group_send_requests_.end());
auto &request = it->second;
request.upload_results_[media_pos] = std::move(result);
request.finished_count_++;
LOG(INFO) << "Receive uploaded paid media " << media_pos << " for request " << request_id;
if (request.finished_count_ != request.upload_results_.size()) {
return;
}
auto upload_results = std::move(request.upload_results_);
auto message = std::move(request.paid_media_message_);
auto promise = std::move(request.paid_media_promise_);
media_group_send_requests_.erase(it);
CHECK(message != nullptr);
for (auto &r_upload_result : upload_results) {
if (r_upload_result.is_error()) {
return promise.set_error(r_upload_result.move_as_error());
}
}
vector<telegram_api::object_ptr<telegram_api::InputMedia>> input_media;
for (auto &r_upload_result : upload_results) {
auto upload_result = r_upload_result.move_as_ok();
input_media.push_back(std::move(upload_result.input_media_));
}
auto input_media_paid_media = telegram_api::make_object<telegram_api::inputMediaPaidMedia>(
get_message_content_star_count(message->content_.get()), std::move(input_media));
td_->create_handler<SendBusinessMediaQuery>(std::move(promise))
->send(std::move(message), std::move(input_media_paid_media));
}
void BusinessConnectionManager::edit_business_message_text(
BusinessConnectionId business_connection_id, DialogId dialog_id, MessageId message_id,
td_api::object_ptr<td_api::ReplyMarkup> &&reply_markup,

View File

@ -132,6 +132,8 @@ class BusinessConnectionManager final : public Actor {
size_t finished_count_ = 0;
vector<Result<UploadMediaResult>> upload_results_;
Promise<td_api::object_ptr<td_api::businessMessages>> promise_;
unique_ptr<PendingMessage> paid_media_message_;
Promise<td_api::object_ptr<td_api::businessMessage>> paid_media_promise_;
};
void tear_down() final;
@ -192,6 +194,8 @@ class BusinessConnectionManager final : public Actor {
void process_sent_business_message_album(telegram_api::object_ptr<telegram_api::Updates> &&updates_ptr,
Promise<td_api::object_ptr<td_api::businessMessages>> &&promise);
void on_upload_message_paid_media(int64 request_id, size_t media_pos, Result<UploadMediaResult> &&result);
void do_edit_business_message_media(Result<UploadMediaResult> &&result,
Promise<td_api::object_ptr<td_api::businessMessage>> &&promise);

View File

@ -4052,8 +4052,8 @@ Status can_send_message_content(DialogId dialog_id, const MessageContent *conten
return Status::Error(400, "Paid media can't be sent to secret chats");
}
} else {
if (dialog_type != DialogType::Channel ||
!td->chat_manager_->is_broadcast_channel(dialog_id.get_channel_id())) {
if (!td->auth_manager_->is_bot() && (dialog_type != DialogType::Channel ||
!td->chat_manager_->is_broadcast_channel(dialog_id.get_channel_id()))) {
return Status::Error(400, "Paid media can be sent only in channel chats");
}
}
@ -8074,6 +8074,11 @@ unique_ptr<MessageContent> get_uploaded_message_content(
return content;
}
int64 get_message_content_star_count(const MessageContent *content) {
CHECK(content->get_type() == MessageContentType::PaidMedia);
return static_cast<const MessagePaidMedia *>(content)->star_count;
}
int32 get_message_content_duration(const MessageContent *content, const Td *td) {
CHECK(content != nullptr);
switch (content->get_type()) {

View File

@ -279,6 +279,8 @@ const FormattedText *get_message_content_text(const MessageContent *content);
const FormattedText *get_message_content_caption(const MessageContent *content);
int64 get_message_content_star_count(const MessageContent *content);
int32 get_message_content_duration(const MessageContent *content, const Td *td);
int32 get_message_content_media_duration(const MessageContent *content, const Td *td);

View File

@ -3335,7 +3335,7 @@ Result<InputMessageContent> QuickReplyManager::process_input_message_content(
return Status::Error(400, "Can't add poll as a quick reply");
}
if (message_content_id == td_api::inputMessagePaidMedia::ID) {
return Status::Error(400, "Can't send paid media as business");
return Status::Error(400, "Can't add paid media as a quick reply");
}
if (message_content_id == td_api::inputMessageLocation::ID &&
static_cast<const td_api::inputMessageLocation *>(input_message_content.get())->live_period_ != 0) {