diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 36c1614fe..9ade5e120 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -6201,6 +6201,11 @@ getMessagePublicForwards chat_id:int53 message_id:int53 offset:string limit:int3 //@description Returns sponsored messages to be shown in a chat; for channel chats only @chat_id Identifier of the chat getChatSponsoredMessages chat_id:int53 = SponsoredMessages; +//@description Informs TDLib that the user opened the sponsored chat via the button, the name, the photo, or a mention in the sponsored message +//@chat_id Chat identifier of the sponsored message +//@message_id Identifier of the sponsored message +clickChatSponsoredMessage chat_id:int53 message_id:int53 = Ok; + //@description Removes an active notification from notification list. Needs to be called only if the notification is removed by the current user @notification_group_id Identifier of notification group to which the notification belongs @notification_id Identifier of removed notification removeNotification notification_group_id:int32 notification_id:int32 = Ok; diff --git a/td/telegram/SponsoredMessageManager.cpp b/td/telegram/SponsoredMessageManager.cpp index 6770d97fb..1dcbe307b 100644 --- a/td/telegram/SponsoredMessageManager.cpp +++ b/td/telegram/SponsoredMessageManager.cpp @@ -88,6 +88,38 @@ class ViewSponsoredMessageQuery final : public Td::ResultHandler { } }; +class ClickSponsoredMessageQuery final : public Td::ResultHandler { + Promise promise_; + ChannelId channel_id_; + + public: + explicit ClickSponsoredMessageQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(ChannelId channel_id, const string &message_id) { + channel_id_ = channel_id; + auto input_channel = td_->contacts_manager_->get_input_channel(channel_id); + if (input_channel == nullptr) { + return promise_.set_value(Unit()); + } + send_query(G()->net_query_creator().create( + telegram_api::channels_clickSponsoredMessage(std::move(input_channel), BufferSlice(message_id)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + td_->contacts_manager_->on_get_channel_error(channel_id_, status, "ClickSponsoredMessageQuery"); + promise_.set_error(std::move(status)); + } +}; + struct SponsoredMessageManager::SponsoredMessage { int64 local_id = 0; bool is_recommended = false; @@ -385,4 +417,23 @@ void SponsoredMessageManager::view_sponsored_message(DialogId dialog_id, Message td_->create_handler()->send(dialog_id.get_channel_id(), random_id); } +void SponsoredMessageManager::click_sponsored_message(DialogId dialog_id, MessageId sponsored_message_id, + Promise &&promise) { + if (!dialog_id.is_valid() || !sponsored_message_id.is_valid_sponsored()) { + return promise.set_error(Status::Error(400, "Invalid message specified")); + } + auto it = dialog_sponsored_messages_.find(dialog_id); + if (it == dialog_sponsored_messages_.end()) { + return promise.set_value(Unit()); + } + auto random_id_it = it->second->message_random_ids.find(sponsored_message_id.get()); + if (random_id_it == it->second->message_random_ids.end()) { + return promise.set_value(Unit()); + } + + auto random_id = std::move(random_id_it->second); + it->second->message_random_ids.erase(random_id_it); + td_->create_handler(std::move(promise))->send(dialog_id.get_channel_id(), random_id); +} + } // namespace td diff --git a/td/telegram/SponsoredMessageManager.h b/td/telegram/SponsoredMessageManager.h index ceda66e6e..c8f736c9f 100644 --- a/td/telegram/SponsoredMessageManager.h +++ b/td/telegram/SponsoredMessageManager.h @@ -37,6 +37,8 @@ class SponsoredMessageManager final : public Actor { void view_sponsored_message(DialogId dialog_id, MessageId sponsored_message_id); + void click_sponsored_message(DialogId dialog_id, MessageId sponsored_message_id, Promise &&promise); + private: struct SponsoredMessage; struct DialogSponsoredMessages; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index ed8c64c9c..d3592d240 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4664,6 +4664,13 @@ void Td::on_request(uint64 id, const td_api::getChatSponsoredMessages &request) sponsored_message_manager_->get_dialog_sponsored_messages(DialogId(request.chat_id_), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::clickChatSponsoredMessage &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + sponsored_message_manager_->click_sponsored_message(DialogId(request.chat_id_), MessageId(request.message_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_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index ae758739f..e76661253 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -570,6 +570,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getChatSponsoredMessages &request); + void on_request(uint64 id, const td_api::clickChatSponsoredMessage &request); + void on_request(uint64 id, const td_api::getMessageLink &request); void on_request(uint64 id, const td_api::getMessageEmbeddingCode &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 09412bc84..33c602cb6 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3239,6 +3239,11 @@ class CliClient final : public Actor { ChatId chat_id; get_args(args, chat_id); send_request(td_api::make_object(chat_id)); + } else if (op == "ccspm") { + ChatId chat_id; + MessageId message_id; + get_args(args, chat_id, message_id); + send_request(td_api::make_object(chat_id, message_id)); } else if (op == "gmlink") { ChatId chat_id; MessageId message_id;