Support extended media in inputMessageInvoice.

This commit is contained in:
levlam 2022-09-23 16:16:56 +03:00
parent 3827bc92b0
commit 263b383530
9 changed files with 110 additions and 13 deletions

View File

@ -2241,7 +2241,8 @@ inputMessageGame bot_user_id:int53 game_short_name:string = InputMessageContent;
//@photo_url Product photo URL; optional @photo_size Product photo size @photo_width Product photo width @photo_height Product photo height
//@payload The invoice payload @provider_token Payment provider token @provider_data JSON-encoded data about the invoice, which will be shared with the payment provider
//@start_parameter Unique invoice bot deep link parameter for the generation of this invoice. If empty, it would be possible to pay directly from forwards of the invoice message
inputMessageInvoice invoice:invoice title:string description:string photo_url:string photo_size:int32 photo_width:int32 photo_height:int32 payload:bytes provider_token:string provider_data:string start_parameter:string = InputMessageContent;
//@extended_media_content The content of extended media attached to the invoice. The content of the message to be sent. Must be one of the following types: inputMessagePhoto, inputMessageVideo
inputMessageInvoice invoice:invoice title:string description:string photo_url:string photo_size:int32 photo_width:int32 photo_height:int32 payload:bytes provider_token:string provider_data:string start_parameter:string extended_media_content:InputMessageContent = InputMessageContent;
//@description A message with a poll. Polls can't be sent to secret chats. Polls can be sent only to a private chat with a bot @question Poll question; 1-255 characters (up to 300 characters for bots) @options List of poll answer options, 2-10 strings 1-100 characters each
//@is_anonymous True, if the poll voters are anonymous. Non-anonymous polls can't be sent or forwarded to channels @type Type of the poll

View File

@ -376,7 +376,8 @@ Result<tl_object_ptr<telegram_api::InputBotInlineMessage>> InlineQueriesManager:
return contact.get_input_bot_inline_message_media_contact(std::move(input_reply_markup));
}
if (constructor_id == td_api::inputMessageInvoice::ID) {
TRY_RESULT(input_invoice, InputInvoice::process_input_message_invoice(std::move(input_message_content), td_));
TRY_RESULT(input_invoice,
InputInvoice::process_input_message_invoice(std::move(input_message_content), td_, DialogId(), false));
return input_invoice.get_input_bot_inline_message_media_invoice(std::move(input_reply_markup), td_);
}
if (constructor_id == td_api::inputMessageLocation::ID) {

View File

@ -94,7 +94,8 @@ InputInvoice::InputInvoice(tl_object_ptr<telegram_api::botInlineMessageMediaInvo
}
Result<InputInvoice> InputInvoice::process_input_message_invoice(
td_api::object_ptr<td_api::InputMessageContent> &&input_message_content, Td *td) {
td_api::object_ptr<td_api::InputMessageContent> &&input_message_content, Td *td, DialogId owner_dialog_id,
bool is_premium) {
CHECK(input_message_content != nullptr);
CHECK(input_message_content->get_id() == td_api::inputMessageInvoice::ID);
auto input_invoice = move_tl_object_as<td_api::inputMessageInvoice>(input_message_content);
@ -216,8 +217,9 @@ Result<InputInvoice> InputInvoice::process_input_message_invoice(
result.provider_token_ = std::move(input_invoice->provider_token_);
result.provider_data_ = std::move(input_invoice->provider_data_);
// TRY_RESULT(extended_media, MessageExtendedMedia::get_message_extended_media(td, std::move(input_invoice->extended_media_)));
// result.extended_media_ = std::move(extended_media);
TRY_RESULT(extended_media, MessageExtendedMedia::get_message_extended_media(
td, std::move(input_invoice->extended_media_content_), owner_dialog_id, is_premium));
result.extended_media_ = std::move(extended_media);
return result;
}
@ -299,7 +301,9 @@ static tl_object_ptr<telegram_api::inputWebDocument> get_input_web_document(cons
std::move(attributes));
}
tl_object_ptr<telegram_api::inputMediaInvoice> InputInvoice::get_input_media_invoice(Td *td) const {
tl_object_ptr<telegram_api::inputMediaInvoice> InputInvoice::get_input_media_invoice(
Td *td, tl_object_ptr<telegram_api::InputFile> input_file,
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const {
int32 flags = 0;
if (!start_parameter_.empty()) {
flags |= telegram_api::inputMediaInvoice::START_PARAM_MASK;
@ -308,12 +312,20 @@ tl_object_ptr<telegram_api::inputMediaInvoice> InputInvoice::get_input_media_inv
if (input_web_document != nullptr) {
flags |= telegram_api::inputMediaInvoice::PHOTO_MASK;
}
telegram_api::object_ptr<telegram_api::InputMedia> extended_media;
if (!extended_media_.is_empty()) {
flags |= telegram_api::inputMediaInvoice::EXTENDED_MEDIA_MASK;
extended_media = extended_media_.get_input_media(td, std::move(input_file), std::move(input_thumbnail));
if (extended_media == nullptr) {
return nullptr;
}
}
return make_tl_object<telegram_api::inputMediaInvoice>(
flags, title_, description_, std::move(input_web_document), get_input_invoice(invoice_), BufferSlice(payload_),
provider_token_,
telegram_api::make_object<telegram_api::dataJSON>(provider_data_.empty() ? "null" : provider_data_),
start_parameter_, nullptr);
start_parameter_, std::move(extended_media));
}
tl_object_ptr<telegram_api::inputBotInlineMessageMediaInvoice> InputInvoice::get_input_bot_inline_message_media_invoice(

View File

@ -66,12 +66,15 @@ struct InputInvoice {
DialogId owner_dialog_id);
static Result<InputInvoice> process_input_message_invoice(
td_api::object_ptr<td_api::InputMessageContent> &&input_message_content, Td *td);
td_api::object_ptr<td_api::InputMessageContent> &&input_message_content, Td *td, DialogId owner_dialog_id,
bool is_premium);
tl_object_ptr<td_api::messageInvoice> get_message_invoice_object(Td *td, bool skip_bot_commands,
int32 max_media_timestamp) const;
tl_object_ptr<telegram_api::inputMediaInvoice> get_input_media_invoice(Td *td) const;
tl_object_ptr<telegram_api::inputMediaInvoice> get_input_media_invoice(
Td *td, tl_object_ptr<telegram_api::InputFile> input_file,
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const;
tl_object_ptr<telegram_api::inputBotInlineMessageMediaInvoice> get_input_bot_inline_message_media_invoice(
tl_object_ptr<telegram_api::ReplyMarkup> &&reply_markup, Td *td) const;

View File

@ -1954,7 +1954,8 @@ static Result<InputMessageContent> create_input_message_content(
return Status::Error(400, "Invoices can be sent only by bots");
}
TRY_RESULT(input_invoice, InputInvoice::process_input_message_invoice(std::move(input_message_content), td));
TRY_RESULT(input_invoice, InputInvoice::process_input_message_invoice(std::move(input_message_content), td,
dialog_id, is_premium));
content = make_unique<MessageInvoice>(std::move(input_invoice));
break;
}
@ -2357,7 +2358,7 @@ static tl_object_ptr<telegram_api::InputMedia> get_input_media_impl(
}
case MessageContentType::Invoice: {
const auto *m = static_cast<const MessageInvoice *>(content);
return m->input_invoice.get_input_media_invoice(td);
return m->input_invoice.get_input_media_invoice(td, std::move(input_file), std::move(input_thumbnail));
}
case MessageContentType::LiveLocation: {
const auto *m = static_cast<const MessageLiveLocation *>(content);
@ -5440,6 +5441,16 @@ int32 get_message_content_media_duration(const MessageContent *content, const Td
}
}
const Photo *get_message_content_photo(const MessageContent *content) {
switch (content->get_type()) {
case MessageContentType::Photo:
return &static_cast<const MessagePhoto *>(content)->photo;
default:
break;
}
return nullptr;
}
FileId get_message_content_upload_file_id(const MessageContent *content) {
switch (content->get_type()) {
case MessageContentType::Animation:

View File

@ -216,6 +216,8 @@ int32 get_message_content_duration(const MessageContent *content, const Td *td);
int32 get_message_content_media_duration(const MessageContent *content, const Td *td);
const Photo *get_message_content_photo(const MessageContent *content);
FileId get_message_content_upload_file_id(const MessageContent *content);
FileId get_message_content_any_file_id(const MessageContent *content);

View File

@ -8,6 +8,7 @@
#include "td/telegram/Document.h"
#include "td/telegram/DocumentsManager.h"
#include "td/telegram/MessageContent.h"
#include "td/telegram/Td.h"
#include "td/telegram/VideosManager.h"
@ -92,6 +93,42 @@ MessageExtendedMedia::MessageExtendedMedia(
}
}
Result<MessageExtendedMedia> MessageExtendedMedia::get_message_extended_media(
Td *td, td_api::object_ptr<td_api::InputMessageContent> &&extended_media_content, DialogId owner_dialog_id,
bool is_premium) {
if (extended_media_content == nullptr) {
return MessageExtendedMedia();
}
if (!owner_dialog_id.is_valid()) {
return Status::Error(400, "Extended media can't be added to the invoice");
}
auto input_content_type = extended_media_content->get_id();
if (input_content_type != td_api::inputMessagePhoto::ID && input_content_type != td_api::inputMessageVideo::ID) {
return Status::Error("Invalid extended media content specified");
}
TRY_RESULT(input_message_content,
get_input_message_content(owner_dialog_id, std::move(extended_media_content), td, is_premium));
if (input_message_content.ttl != 0) {
return Status::Error("Can't use self-destructing extended media");
}
auto content = input_message_content.content.get();
auto content_type = content->get_type();
MessageExtendedMedia result;
CHECK(content_type == MessageContentType::Photo || content_type == MessageContentType::Video);
result.caption_ = *get_message_content_caption(content);
if (content_type == MessageContentType::Photo) {
result.type_ = Type::Photo;
result.photo_ = *get_message_content_photo(content);
} else {
CHECK(content_type == MessageContentType::Video);
result.type_ = Type::Video;
result.video_file_id_ = get_message_content_upload_file_id(content);
}
return result;
}
void MessageExtendedMedia::update_from(const MessageExtendedMedia &old_extended_media) {
if (!is_media() && old_extended_media.is_media()) {
*this = old_extended_media;
@ -234,6 +271,25 @@ FileId MessageExtendedMedia::get_thumbnail_file_id(const Td *td) const {
return FileId();
}
telegram_api::object_ptr<telegram_api::InputMedia> MessageExtendedMedia::get_input_media(
Td *td, tl_object_ptr<telegram_api::InputFile> input_file,
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const {
switch (type_) {
case Type::Empty:
case Type::Unsupported:
case Type::Preview:
break;
case Type::Photo:
return photo_get_input_media(td->file_manager_.get(), photo_, std::move(input_file), 0);
case Type::Video:
return td->videos_manager_->get_input_media(video_file_id_, std::move(input_file), std::move(input_thumbnail), 0);
default:
UNREACHABLE();
break;
}
return nullptr;
}
bool operator==(const MessageExtendedMedia &lhs, const MessageExtendedMedia &rhs) {
if (lhs.type_ != rhs.type_ || lhs.caption_ != rhs.caption_) {
return false;

View File

@ -50,6 +50,10 @@ class MessageExtendedMedia {
MessageExtendedMedia(Td *td, telegram_api::object_ptr<telegram_api::MessageExtendedMedia> &&extended_media,
FormattedText &&caption, DialogId owner_dialog_id);
static Result<MessageExtendedMedia> get_message_extended_media(
Td *td, td_api::object_ptr<td_api::InputMessageContent> &&extended_media_content, DialogId owner_dialog_id,
bool is_premium);
bool is_empty() const {
return type_ == Type::Empty;
}
@ -90,6 +94,10 @@ class MessageExtendedMedia {
FileId get_thumbnail_file_id(const Td *td) const;
telegram_api::object_ptr<telegram_api::InputMedia> get_input_media(
Td *td, tl_object_ptr<telegram_api::InputFile> input_file,
tl_object_ptr<telegram_api::InputFile> input_thumbnail) const;
template <class StorerT>
void store(StorerT &storer) const;

View File

@ -876,8 +876,11 @@ void export_invoice(Td *td, td_api::object_ptr<td_api::InputMessageContent> &&in
if (invoice == nullptr) {
return promise.set_error(Status::Error(400, "Invoice must be non-empty"));
}
TRY_RESULT_PROMISE(promise, input_invoice, InputInvoice::process_input_message_invoice(std::move(invoice), td));
td->create_handler<ExportInvoiceQuery>(std::move(promise))->send(input_invoice.get_input_media_invoice(td));
TRY_RESULT_PROMISE(promise, input_invoice,
InputInvoice::process_input_message_invoice(std::move(invoice), td, DialogId(), false));
auto input_media = input_invoice.get_input_media_invoice(td, nullptr, nullptr);
CHECK(input_media != nullptr);
td->create_handler<ExportInvoiceQuery>(std::move(promise))->send(std::move(input_media));
}
void get_bank_card_info(Td *td, const string &bank_card_number,