From 0b54e2846762201a118559f3fc737538d863eb40 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 13 Jan 2023 17:03:35 +0300 Subject: [PATCH] Add td_api::sendChosenUser. --- td/generate/scheme/td_api.tl | 8 +++++ td/telegram/MessagesManager.cpp | 56 +++++++++++++++++++++++++++++++++ td/telegram/MessagesManager.h | 2 ++ td/telegram/ReplyMarkup.cpp | 5 +++ td/telegram/ReplyMarkup.h | 2 ++ td/telegram/Td.cpp | 7 +++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 7 +++++ 8 files changed, 89 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 849abc253..562ef6f76 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -6288,6 +6288,14 @@ getLoginUrlInfo chat_id:int53 message_id:int53 button_id:int53 = LoginUrlInfo; getLoginUrl chat_id:int53 message_id:int53 button_id:int53 allow_write_access:Bool = HttpUrl; +//@description Sends a user chosen after pressing a keyboardButtonTypeRequestUser button to the bot +//@chat_id Identifier of the chat with the bot +//@message_id Identifier of the message with the button +//@button_id Identifier of the button +//@user_id Identifier of the chosen user +sendChosenUser chat_id:int53 message_id:int53 button_id:int32 user_id:int53 = Ok; + + //@description Sends an inline query to a bot and returns its results. Returns an error with code 502 if the bot fails to answer the query before the query timeout expires //@bot_user_id The identifier of the target bot //@chat_id Identifier of the chat where the query was sent diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 8a901b09f..9313e8b04 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4219,6 +4219,47 @@ class SendScreenshotNotificationQuery final : public Td::ResultHandler { } }; +class SendBotRequestedPeer final : public Td::ResultHandler { + Promise promise_; + + public: + explicit SendBotRequestedPeer(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(FullMessageId full_message_id, int32 button_id, DialogId requested_dialog_id) { + auto dialog_id = full_message_id.get_dialog_id(); + auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Write); + if (input_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chat")); + } + auto requested_peer = td_->messages_manager_->get_input_peer(requested_dialog_id, AccessRights::Read); + if (requested_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chosen chat")); + } + + send_query(G()->net_query_creator().create( + telegram_api::messages_sendBotRequestedPeer(std::move(input_peer), + full_message_id.get_message_id().get_server_message_id().get(), + button_id, std::move(requested_peer)), + {{dialog_id, MessageContentType::Text}})); + } + + 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()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for SendBotRequestedPeer: " << to_string(ptr); + td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_)); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class SetTypingQuery final : public Td::ResultHandler { Promise promise_; DialogId dialog_id_; @@ -29706,6 +29747,21 @@ void MessagesManager::do_send_screenshot_taken_notification_message(DialogId dia ->send(dialog_id, random_id); } +void MessagesManager::send_chosen_user(FullMessageId full_message_id, int32 button_id, UserId user_id, + Promise &&promise) { + const Message *m = get_message_force(full_message_id, "send_chosen_user"); + if (m == nullptr) { + return promise.set_error(Status::Error(400, "Message not found")); + } + if (m->reply_markup == nullptr) { + return promise.set_error(Status::Error(400, "Message has no buttons")); + } + CHECK(m->message_id.is_valid() && m->message_id.is_server()); + TRY_STATUS_PROMISE(promise, m->reply_markup->check_chosen_user(td_, button_id, user_id)); + + td_->create_handler(std::move(promise))->send(full_message_id, button_id, DialogId(user_id)); +} + Result MessagesManager::add_local_message( DialogId dialog_id, td_api::object_ptr &&sender, MessageId reply_to_message_id, bool disable_notification, tl_object_ptr &&input_message_content) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 26d380a15..1bfe6bfa1 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -462,6 +462,8 @@ class MessagesManager final : public Actor { Status send_screenshot_taken_notification_message(DialogId dialog_id); + void send_chosen_user(FullMessageId full_message_id, int32 button_id, UserId user_id, Promise &&promise); + Result add_local_message(DialogId dialog_id, td_api::object_ptr &&sender, MessageId reply_to_message_id, bool disable_notification, tl_object_ptr &&input_message_content) diff --git a/td/telegram/ReplyMarkup.cpp b/td/telegram/ReplyMarkup.cpp index ea42649a6..4be5036f8 100644 --- a/td/telegram/ReplyMarkup.cpp +++ b/td/telegram/ReplyMarkup.cpp @@ -1047,6 +1047,11 @@ tl_object_ptr ReplyMarkup::get_reply_markup_object(Contacts } } +Status ReplyMarkup::check_chosen_user(Td *td, int32 button_id, UserId user_id) const { + // TODO + return Status::OK(); +} + tl_object_ptr get_input_reply_markup(ContactsManager *contacts_manager, const unique_ptr &reply_markup) { if (reply_markup == nullptr) { diff --git a/td/telegram/ReplyMarkup.h b/td/telegram/ReplyMarkup.h index 560195243..761ce6447 100644 --- a/td/telegram/ReplyMarkup.h +++ b/td/telegram/ReplyMarkup.h @@ -80,6 +80,8 @@ struct ReplyMarkup { tl_object_ptr get_input_reply_markup(ContactsManager *contacts_manager) const; tl_object_ptr get_reply_markup_object(ContactsManager *contacts_manager) const; + + Status check_chosen_user(Td *td, int32 button_id, UserId user_id) const; }; bool operator==(const ReplyMarkup &lhs, const ReplyMarkup &rhs); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index abd5c6fa1..05400934f 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7631,6 +7631,13 @@ void Td::on_request(uint64 id, const td_api::getLoginUrl &request) { request.allow_write_access_, std::move(promise)); } +void Td::on_request(uint64 id, const td_api::sendChosenUser &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + messages_manager_->send_chosen_user({DialogId(request.chat_id_), MessageId(request.message_id_)}, request.button_id_, + UserId(request.user_id_), std::move(promise)); +} + void Td::on_request(uint64 id, td_api::getInlineQueryResults &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.query_); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index aa62c9f48..e6a08563a 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1290,6 +1290,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::getLoginUrl &request); + void on_request(uint64 id, const td_api::sendChosenUser &request); + void on_request(uint64 id, td_api::getInlineQueryResults &request); void on_request(uint64 id, td_api::answerInlineQuery &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index f438d3f93..b2e6e815c 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5102,6 +5102,13 @@ class CliClient final : public Actor { send_request( td_api::make_object(chat_id, message_id, as_button_id(button_id), op == "glua")); } + } else if (op == "scu") { + ChatId chat_id; + MessageId message_id; + int32 button_id; + UserId user_id; + get_args(args, chat_id, message_id, button_id, user_id); + send_request(td_api::make_object(chat_id, message_id, button_id, user_id)); } else if (op == "rsgs") { string supergroup_id; string message_ids;