diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 9fe5402..ff797a1 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -4273,12 +4273,12 @@ class Client::TdOnCheckMessageCallback final : public TdQueryCallback { template class Client::TdOnCheckMessageThreadCallback final : public TdQueryCallback { public: - TdOnCheckMessageThreadCallback(Client *client, int64 chat_id, int64 message_thread_id, int64 reply_to_message_id, - PromisedQueryPtr query, OnSuccess on_success) + TdOnCheckMessageThreadCallback(Client *client, int64 chat_id, int64 message_thread_id, + CheckedReplyParameters reply_parameters, PromisedQueryPtr query, OnSuccess on_success) : client_(client) , chat_id_(chat_id) , message_thread_id_(message_thread_id) - , reply_to_message_id_(reply_to_message_id) + , reply_parameters_(std::move(reply_parameters)) , query_(std::move(query)) , on_success_(std::move(on_success)) { } @@ -4307,14 +4307,14 @@ class Client::TdOnCheckMessageThreadCallback final : public TdQueryCallback { "Message thread is not a forum topic thread"); } - on_success_(chat_id_, message_thread_id_, reply_to_message_id_, std::move(query_)); + on_success_(chat_id_, message_thread_id_, std::move(reply_parameters_), std::move(query_)); } private: Client *client_; int64 chat_id_; int64 message_thread_id_; - int64 reply_to_message_id_; + CheckedReplyParameters reply_parameters_; PromisedQueryPtr query_; OnSuccess on_success_; }; @@ -5840,14 +5840,18 @@ void Client::check_message(td::Slice chat_id_str, int64 message_id, bool allow_e } template -void Client::check_reply_parameters(td::Slice chat_id_str, int64 reply_to_message_id, bool allow_sending_without_reply, +void Client::check_reply_parameters(td::Slice chat_id_str, InputReplyParameters &&reply_parameters, int64 message_thread_id, PromisedQueryPtr query, OnSuccess on_success) { - check_message(chat_id_str, reply_to_message_id, reply_to_message_id <= 0 || allow_sending_without_reply, + check_message(chat_id_str, reply_parameters.reply_to_message_id, + reply_parameters.reply_to_message_id <= 0 || reply_parameters.allow_sending_without_reply, AccessRights::Write, "message to reply", std::move(query), [this, message_thread_id, on_success = std::move(on_success)](int64 chat_id, int64 reply_to_message_id, PromisedQueryPtr query) mutable { + CheckedReplyParameters reply_parameters; + reply_parameters.reply_to_message_id = reply_to_message_id; + if (message_thread_id <= 0) { - return on_success(chat_id, 0, reply_to_message_id, std::move(query)); + return on_success(chat_id, 0, std::move(reply_parameters), std::move(query)); } if (reply_to_message_id != 0) { @@ -5859,12 +5863,12 @@ void Client::check_reply_parameters(td::Slice chat_id_str, int64 reply_to_messag } } if (reply_to_message_id == message_thread_id) { - return on_success(chat_id, message_thread_id, reply_to_message_id, std::move(query)); + return on_success(chat_id, message_thread_id, std::move(reply_parameters), std::move(query)); } send_request(make_object(chat_id, message_thread_id), td::make_unique>( - this, chat_id, message_thread_id, reply_to_message_id, std::move(query), + this, chat_id, message_thread_id, std::move(reply_parameters), std::move(query), std::move(on_success))); }); } @@ -6682,13 +6686,51 @@ bool Client::to_bool(td::MutableSlice value) { return value == "true" || value == "yes" || value == "1"; } -td_api::object_ptr Client::get_input_message_reply_to(int64 reply_to_message_id) { - if (reply_to_message_id > 0) { - return make_object(0, reply_to_message_id, nullptr); +td_api::object_ptr Client::get_input_message_reply_to( + const CheckedReplyParameters &reply_parameters) { + if (reply_parameters.reply_to_message_id > 0) { + return make_object(0, reply_parameters.reply_to_message_id, nullptr); } return nullptr; } +td::Result Client::get_reply_parameters(const Query *query) { + if (!query->has_arg("reply_parameters")) { + InputReplyParameters result; + result.reply_to_message_id = get_message_id(query, "reply_to_message_id"); + result.allow_sending_without_reply = to_bool(query->arg("allow_sending_without_reply")); + return std::move(result); + } + + auto reply_parameters = query->arg("reply_parameters"); + if (reply_parameters.empty()) { + return InputReplyParameters(); + } + + LOG(INFO) << "Parsing JSON object: " << reply_parameters; + auto r_value = json_decode(reply_parameters); + if (r_value.is_error()) { + LOG(INFO) << "Can't parse JSON object: " << r_value.error(); + return td::Status::Error(400, "Can't parse reply parameters JSON object"); + } + + return get_reply_parameters(r_value.move_as_ok()); +} + +td::Result Client::get_reply_parameters(td::JsonValue &&value) { + if (value.type() != td::JsonValue::Type::Object) { + return td::Status::Error(400, "Object expected as reply parameters"); + } + auto &object = value.get_object(); + TRY_RESULT(message_id, object.get_required_int_field("message_id")); + TRY_RESULT(allow_sending_without_reply, object.get_optional_bool_field("allow_sending_without_reply")); + + InputReplyParameters result; + result.reply_to_message_id = as_tdlib_message_id(td::max(message_id, 0)); + result.allow_sending_without_reply = allow_sending_without_reply; + return std::move(result); +} + td::Result> Client::get_keyboard_button(td::JsonValue &button) { if (button.type() == td::JsonValue::Type::Object) { auto &object = button.get_object(); @@ -9685,8 +9727,7 @@ td::Status Client::process_forward_message_query(PromisedQueryPtr &query) { td::Status Client::process_send_media_group_query(PromisedQueryPtr &query) { auto chat_id = query->arg("chat_id"); auto message_thread_id = get_message_id(query.get(), "message_thread_id"); - auto reply_to_message_id = get_message_id(query.get(), "reply_to_message_id"); - auto allow_sending_without_reply = to_bool(query->arg("allow_sending_without_reply")); + TRY_RESULT(reply_parameters, get_reply_parameters(query.get())); auto disable_notification = to_bool(query->arg("disable_notification")); auto protect_content = to_bool(query->arg("protect_content")); // TRY_RESULT(reply_markup, get_reply_markup(query.get(), bot_user_ids_)); @@ -9696,14 +9737,15 @@ td::Status Client::process_send_media_group_query(PromisedQueryPtr &query) { resolve_reply_markup_bot_usernames( std::move(reply_markup), std::move(query), - [this, chat_id = chat_id.str(), message_thread_id, reply_to_message_id, allow_sending_without_reply, disable_notification, - protect_content, input_message_contents = std::move(input_message_contents), send_at = std::move(send_at)]( + [this, chat_id = chat_id.str(), message_thread_id, reply_parameters = std::move(reply_parameters), + send_at = std::move(send_at), + disable_notification, protect_content, input_message_contents = std::move(input_message_contents)]( object_ptr reply_markup, PromisedQueryPtr query) mutable { auto on_success = [this, disable_notification, protect_content, input_message_contents = std::move(input_message_contents), send_at = std::move(send_at), reply_markup = std::move(reply_markup)](int64 chat_id, int64 message_thread_id, - int64 reply_to_message_id, + CheckedReplyParameters reply_parameters, PromisedQueryPtr query) mutable { auto &count = yet_unsent_message_count_[chat_id]; if (count >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) { @@ -9714,12 +9756,12 @@ td::Status Client::process_send_media_group_query(PromisedQueryPtr &query) { send_request( make_object( - chat_id, message_thread_id, get_input_message_reply_to(reply_to_message_id), + chat_id, message_thread_id, get_input_message_reply_to(reply_parameters), get_message_send_options(disable_notification, protect_content), std::move(send_at), std::move(input_message_contents)), td::make_unique(this, chat_id, message_count, std::move(query))); }; - check_reply_parameters(chat_id, reply_to_message_id, allow_sending_without_reply, message_thread_id, - std::move(query), std::move(on_success)); + check_reply_parameters(chat_id, std::move(reply_parameters), message_thread_id, std::move(query), + std::move(on_success)); }); return td::Status::OK(); } @@ -11928,8 +11970,11 @@ void Client::do_send_message(object_ptr input_messa bool force) { auto chat_id = query->arg("chat_id"); auto message_thread_id = get_message_id(query.get(), "message_thread_id"); - auto reply_to_message_id = get_message_id(query.get(), "reply_to_message_id"); - auto allow_sending_without_reply = to_bool(query->arg("allow_sending_without_reply")); + auto r_reply_parameters = get_reply_parameters(query.get()); + if (r_reply_parameters.is_error()) { + return fail_query_with_error(std::move(query), 400, r_reply_parameters.error().message()); + } + auto reply_parameters = r_reply_parameters.move_as_ok(); auto disable_notification = to_bool(query->arg("disable_notification")); auto protect_content = to_bool(query->arg("protect_content")); auto r_reply_markup = get_reply_markup(query.get(), bot_user_ids_); @@ -11949,14 +11994,15 @@ void Client::do_send_message(object_ptr input_messa resolve_reply_markup_bot_usernames( std::move(reply_markup), std::move(query), - [this, chat_id = chat_id.str(), message_thread_id, reply_to_message_id, allow_sending_without_reply, disable_notification, - protect_content, input_message_content = std::move(input_message_content), send_at = std::move(send_at)]( + [this, chat_id = chat_id.str(), message_thread_id, reply_parameters = std::move(reply_parameters), + send_at = std::move(send_at), + disable_notification, protect_content, input_message_content = std::move(input_message_content)]( object_ptr reply_markup, PromisedQueryPtr query) mutable { auto on_success = [this, disable_notification, protect_content, input_message_content = std::move(input_message_content), send_at = std::move(send_at), reply_markup = std::move(reply_markup)](int64 chat_id, int64 message_thread_id, - int64 reply_to_message_id, + CheckedReplyParameters reply_parameters, PromisedQueryPtr query) mutable { auto &count = yet_unsent_message_count_[chat_id]; if (count >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) { @@ -11964,14 +12010,14 @@ void Client::do_send_message(object_ptr input_messa } count++; - send_request(make_object(chat_id, message_thread_id, - get_input_message_reply_to(reply_to_message_id), - get_message_send_options(disable_notification, protect_content), std::move(send_at)), - std::move(reply_markup), std::move(input_message_content)), - td::make_unique(this, chat_id, std::move(query))); + send_request( + make_object(chat_id, message_thread_id, get_input_message_reply_to(reply_parameters), + get_message_send_options(disable_notification, protect_content), std::move(send_at), + std::move(reply_markup), std::move(input_message_content)), + td::make_unique(this, chat_id, std::move(query))); }; - check_reply_parameters(chat_id, reply_to_message_id, allow_sending_without_reply, message_thread_id, - std::move(query), std::move(on_success)); + check_reply_parameters(chat_id, std::move(reply_parameters), message_thread_id, std::move(query), + std::move(on_success)); }); } diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 7b4382c..7542cce 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -284,6 +284,15 @@ class Client final : public WebhookActor::Callback { virtual ~TdQueryCallback() = default; }; + struct InputReplyParameters { + int64 reply_to_message_id = 0; + bool allow_sending_without_reply = false; + }; + + struct CheckedReplyParameters { + int64 reply_to_message_id = 0; + }; + struct UserInfo; struct ChatInfo; struct BotCommandScope; @@ -349,8 +358,8 @@ class Client final : public WebhookActor::Callback { td::Slice message_type, PromisedQueryPtr query, OnSuccess on_success); template - void check_reply_parameters(td::Slice chat_id_str, int64 reply_to_message_id, bool allow_sending_without_reply, - int64 message_thread_id, PromisedQueryPtr query, OnSuccess on_success); + void check_reply_parameters(td::Slice chat_id_str, InputReplyParameters &&reply_parameters, int64 message_thread_id, + PromisedQueryPtr query, OnSuccess on_success); template void resolve_sticker_set(const td::string &sticker_set_name, PromisedQueryPtr query, OnSuccess on_success); @@ -386,7 +395,12 @@ class Client final : public WebhookActor::Callback { static bool to_bool(td::MutableSlice value); - static object_ptr get_input_message_reply_to(int64 reply_to_message_id); + static object_ptr get_input_message_reply_to( + const CheckedReplyParameters &reply_parameters); + + static td::Result get_reply_parameters(const Query *query); + + static td::Result get_reply_parameters(td::JsonValue &&value); static td::Result> get_keyboard_button(td::JsonValue &button);