From b9e7c8d7da64e4680609f9b0068a31a579962aeb Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 16 Oct 2023 20:41:39 +0300 Subject: [PATCH] Use messages.sendMedia to send text messages with custom URLs. --- td/telegram/MessageContent.cpp | 25 +++++++++++++++++++++++++ td/telegram/MessageContent.h | 3 +++ td/telegram/MessagesManager.cpp | 23 ++++++++++++++++------- td/telegram/WebPagesManager.cpp | 8 ++++++++ td/telegram/WebPagesManager.h | 2 ++ 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index d1be61fbc..dba734f88 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3001,6 +3001,31 @@ tl_object_ptr get_fake_input_media(Td *td, tl_object_p } } +tl_object_ptr get_message_content_input_media_web_page(const Td *td, + const MessageContent *content) { + CHECK(content != nullptr); + if (content->get_type() != MessageContentType::Text) { + return nullptr; + } + auto *text = static_cast(content); + if (!text->is_manual && !text->force_small_media && !text->force_large_media) { + return nullptr; + } + int32 flags = 0; + if (text->force_small_media) { + flags |= telegram_api::inputMediaWebPage::FORCE_SMALL_MEDIA_MASK; + } + if (text->force_large_media) { + flags |= telegram_api::inputMediaWebPage::FORCE_LARGE_MEDIA_MASK; + } + if (!text->text.text.empty()) { + flags |= telegram_api::inputMediaWebPage::OPTIONAL_MASK; + } + return telegram_api::make_object( + flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, + td->web_pages_manager_->get_web_page_url(text->web_page_id)); +} + void delete_message_content_thumbnail(MessageContent *content, Td *td) { switch (content->get_type()) { case MessageContentType::Animation: { diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index 8fc2b0560..cfaf4ca5d 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -125,6 +125,9 @@ tl_object_ptr get_input_media(const MessageContent *co tl_object_ptr get_fake_input_media(Td *td, tl_object_ptr input_file, FileId file_id); +tl_object_ptr get_message_content_input_media_web_page(const Td *td, + const MessageContent *content); + void delete_message_content_thumbnail(MessageContent *content, Td *td); Status can_send_message_content(DialogId dialog_id, const MessageContent *content, bool is_forward, const Td *td); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index e646e8724..4e3a0e438 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -25743,14 +25743,23 @@ void MessagesManager::on_text_message_ready_to_send(DialogId dialog_id, MessageI } else { const FormattedText *message_text = get_message_content_text(content); CHECK(message_text != nullptr); - int64 random_id = begin_send_message(dialog_id, m); - td_->create_handler()->send( - get_message_flags(m), dialog_id, get_send_message_as_input_peer(m), get_message_input_reply_to(m), - m->top_thread_message_id, get_message_schedule_date(m), - get_input_reply_markup(td_->contacts_manager_.get(), m->reply_markup), - get_input_message_entities(td_->contacts_manager_.get(), message_text->entities, "do_send_message"), - message_text->text, m->is_copy, random_id, &m->send_query_ref); + auto input_media = get_message_content_input_media_web_page(td_, m->content.get()); + if (input_media == nullptr) { + td_->create_handler()->send( + get_message_flags(m), dialog_id, get_send_message_as_input_peer(m), get_message_input_reply_to(m), + m->top_thread_message_id, get_message_schedule_date(m), + get_input_reply_markup(td_->contacts_manager_.get(), m->reply_markup), + get_input_message_entities(td_->contacts_manager_.get(), message_text, "do_send_message"), message_text->text, + m->is_copy, random_id, &m->send_query_ref); + } else { + td_->create_handler()->send( + FileId(), FileId(), get_message_flags(m), dialog_id, get_send_message_as_input_peer(m), + get_message_input_reply_to(m), m->top_thread_message_id, get_message_schedule_date(m), + get_input_reply_markup(td_->contacts_manager_.get(), m->reply_markup), + get_input_message_entities(td_->contacts_manager_.get(), message_text, "do_send_message"), message_text->text, + std::move(input_media), MessageContentType::Text, m->is_copy, random_id, &m->send_query_ref); + } } } diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 701d82afb..7ae5b6cea 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -1097,6 +1097,14 @@ void WebPagesManager::update_web_page_instant_view_load_requests(WebPageId web_p } } +string WebPagesManager::get_web_page_url(WebPageId web_page_id) const { + const WebPage *web_page = get_web_page(web_page_id); + if (web_page != nullptr) { + return web_page->url_; + } + return string(); +} + WebPageId WebPagesManager::get_web_page_by_url(const string &url) const { if (url.empty()) { return WebPageId(); diff --git a/td/telegram/WebPagesManager.h b/td/telegram/WebPagesManager.h index b3bea4b37..2d1c7feb3 100644 --- a/td/telegram/WebPagesManager.h +++ b/td/telegram/WebPagesManager.h @@ -72,6 +72,8 @@ class WebPagesManager final : public Actor { void get_web_page_instant_view(const string &url, bool force_full, Promise &&promise); + string get_web_page_url(WebPageId web_page_id) const; + WebPageId get_web_page_by_url(const string &url) const; void get_web_page_by_url(const string &url, Promise &&promise);