From cb7dcb5c0bdba4683b7177bf4a64bfd43f34bb8d Mon Sep 17 00:00:00 2001 From: Giuseppe Marino Date: Wed, 11 Nov 2020 17:07:36 +0100 Subject: [PATCH] Implemented `deleteMessages` method parameters: - chat_id the chat_id of the supergroup or channel - start first message id to delete - end last message id to delete the method will always return `true` as a result, even if the messages cannot be deleted this method does not work on private chat or normal groups it is not suggested to delete more than 200 messages per call --- telegram-bot-api/Client.cpp | 42 ++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 02f6a3c..51705d8 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -7453,7 +7453,47 @@ td::Status Client::process_get_participants_query(PromisedQueryPtr &query) { } td::Status Client::process_delete_messages_query(PromisedQueryPtr &query) { - answer_query(td::JsonFalse(), std::move(query), "Not implemented"); + auto chat_id = query->arg("chat_id"); + + if (chat_id.empty()) { + return Status::Error(400, "Chat identifier is not specified"); + } + + auto start = as_client_message_id(get_message_id(query.get(), "start")); + auto end = as_client_message_id(get_message_id(query.get(), "end")); + + if (start == 0 || end == 0) { + return Status::Error(400, "Message identifier is not specified"); + } + + if (start >= end) { + return Status::Error(400, "Initial message identifier is not lower than last message identifier"); + } + + check_chat(chat_id, AccessRights::Write, std::move(query), [this, start, end](int64 chat_id, PromisedQueryPtr query) { + if (get_chat_type(chat_id) != ChatType::Supergroup) { + return fail_query(400, "Bad Request: method is available only for supergroups", std::move(query)); + } + + td::vector ids; + ids.reserve(200); + for (td::int32 i = start; i <= end; i++) { + ids.push_back(as_tdlib_message_id(i)); + + if (ids.size() == 200) { + send_request(make_object(chat_id, std::move(ids), true), + std::make_unique()); + ids.clear(); + } + } + + if (ids.size() != 0) { + send_request(make_object(chat_id, std::move(ids), true), + std::make_unique()); + } + answer_query(td::JsonTrue(), std::move(query)); + }); + return Status::OK(); }