tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

td_api::messageSendOptions#random_id #2933

Closed faucct closed 4 months ago

faucct commented 4 months ago

Following-up https://github.com/tdlib/telegram-bot-api/issues/482#issuecomment-2165067053 If I've understood correctly, I will have to use the parameter in that repository like this and add some docs for it.

diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp
index 142dc26..1b363d4 100644
--- a/telegram-bot-api/Client.cpp
+++ b/telegram-bot-api/Client.cpp
@@ -7727,9 +7727,9 @@ td::Result<td_api::object_ptr<td_api::InputMessageContent>> Client::get_input_me
   return nullptr;
 }

-td_api::object_ptr<td_api::messageSendOptions> Client::get_message_send_options(bool disable_notification,
+td_api::object_ptr<td_api::messageSendOptions> Client::get_message_send_options(int32 random_id, bool disable_notification,
                                                                                 bool protect_content, int64 effect_id) {
-  return make_object<td_api::messageSendOptions>(disable_notification, false, protect_content, false, nullptr,
+  return make_object<td_api::messageSendOptions>(random_id, disable_notification, false, protect_content, false, nullptr,
                                                  effect_id, 0, false);
 }

@@ -10074,7 +10074,7 @@ td::Status Client::process_copy_messages_query(PromisedQueryPtr &query) {

           send_request(make_object<td_api::forwardMessages>(
                            chat_id, message_thread_id, from_chat_id, std::move(message_ids),
-                           get_message_send_options(disable_notification, protect_content, 0), true, remove_caption),
+                           get_message_send_options(0, disable_notification, protect_content, 0), true, remove_caption),
                        td::make_unique<TdOnForwardMessagesCallback>(this, chat_id, message_count, std::move(query)));
         });
   };
@@ -10128,7 +10128,7 @@ td::Status Client::process_forward_messages_query(PromisedQueryPtr &query) {

           send_request(make_object<td_api::forwardMessages>(
                            chat_id, message_thread_id, from_chat_id, std::move(message_ids),
-                           get_message_send_options(disable_notification, protect_content, 0), false, false),
+                           get_message_send_options(0, disable_notification, protect_content, 0), false, false),
                        td::make_unique<TdOnForwardMessagesCallback>(this, chat_id, message_count, std::move(query)));
         });
   };
@@ -10190,7 +10190,7 @@ td::Status Client::process_send_media_group_query(PromisedQueryPtr &query) {

           send_request(make_object<td_api::sendMessageAlbum>(
                            chat_id, message_thread_id, get_input_message_reply_to(std::move(reply_parameters)),
-                           get_message_send_options(disable_notification, protect_content, effect_id),
+                           get_message_send_options(0, disable_notification, protect_content, effect_id),
                            std::move(input_message_contents)),
                        td::make_unique<TdOnSendMessageAlbumCallback>(this, chat_id, message_count, std::move(query)));
         };
@@ -11994,6 +11994,7 @@ void Client::delete_last_send_message_time(td::int64 file_size, double max_delay

 void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query,
                              bool force) {
+  auto random_id = td::to_integer<int32>(query->arg("random_id"));
   auto chat_id = query->arg("chat_id");
   auto message_thread_id = get_message_id(query.get(), "message_thread_id");
   auto business_connection_id = query->arg("business_connection_id");
@@ -12051,7 +12052,7 @@ void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_messa

           send_request(make_object<td_api::sendMessage>(
                            chat_id, message_thread_id, get_input_message_reply_to(std::move(reply_parameters)),
-                           get_message_send_options(disable_notification, protect_content, effect_id),
+                           get_message_send_options(random_id, disable_notification, protect_content, effect_id),
                            std::move(reply_markup), std::move(input_message_content)),
                        td::make_unique<TdOnSendMessageCallback>(this, chat_id, std::move(query)));
         };
diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h
index 4e0bc74..791d621 100644
--- a/telegram-bot-api/Client.h
+++ b/telegram-bot-api/Client.h
@@ -560,7 +560,7 @@ class Client final : public WebhookActor::Callback {

   td::Result<object_ptr<td_api::inputMessageInvoice>> get_input_message_invoice(const Query *query) const;

-  static object_ptr<td_api::messageSendOptions> get_message_send_options(bool disable_notification,
+  static object_ptr<td_api::messageSendOptions> get_message_send_options(int32 random_id, bool disable_notification,
                                                                          bool protect_content, int64 effect_id);

   static td::Result<td::vector<object_ptr<td_api::formattedText>>> get_poll_options(const Query *query);
levlam commented 4 months ago

This will not be merged, because as I said "It is highly unlikely that they will be able to use random_id correctly". For example, the patch has an extremely important mistake in the type of "random_id", which must be int64 instead of int32. int32 is absolutely not enough for random_id to be unique. After this is fixed, the code is likely to work as intended for bots. If you are still sure that you understand all consequences of providing random_id externally, you can use the fixed patch for the local Bot API server to achieve uniqueness guaranties.