Add custom deleteMessages implementation

This commit is contained in:
Andrea Cavalli 2024-02-11 12:53:28 +01:00
parent faa723716f
commit dd075ae89f
4 changed files with 167 additions and 11 deletions

View File

@ -54,17 +54,6 @@ Get the member list of a supergroup or channel
###### Returns `ChatMember` ###### Returns `ChatMember`
##### Method `deleteMessages`
Delete all the messages with message_id in range between `start` and `end`.
The `start` parameter MUST be less than the `end` parameter
Both `start` and `end` must be positive non zero numbers
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
**NOTE**
The maximum number of messages to be deleted in a single batch is determined by the `max-batch-operations` parameter and is 10000 by default
###### Parameters ###### Parameters
- `chat_id` Chat id - `chat_id` Chat id
- `start` First message id to delete - `start` First message id to delete
@ -122,6 +111,17 @@ _For Docker containers, `$TELEGRAM_VERBOSITY` can be set._
##### Method `getChat` ##### Method `getChat`
The command `getChat` will also try to resolve the username online, if it can't be found locally The command `getChat` will also try to resolve the username online, if it can't be found locally
##### Method `deleteMessages`
The command `deleteMessages` can also delete all the messages with message_id in range between `start` and `end`.
The `start` parameter MUST be less than the `end` parameter
Both `start` and `end` must be positive non-zero numbers
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
**NOTE**
The maximum number of messages to be deleted in a single batch is determined by the `max-batch-operations` parameter and is 10000 by default
##### Object `Message` ##### Object `Message`
The `Message` object now has two new fields: The `Message` object now has two new fields:
- `views`: how many views has the message (usually the views are shown only for channel messages) - `views`: how many views has the message (usually the views are shown only for channel messages)

View File

@ -544,6 +544,115 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
/deleteMessages:
post:
tags:
- modified
description: |-
Use this method to delete multiple messages simultaneously.
This method can delete a set of message ids, or a range of message ids.
If you specify "message_ids", this method tries to delete the specified set of ids:
If some of the specified messages can't be found, they are skipped.
Returns True on success.
If you specify "start" and "end", this method deletes all the messages with message_id in range between start and end:
The start parameter MUST be less than the end parameter
Both start and end must be positive non zero numbers
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.
*NOTE*
The maximum number of messages to be deleted in a single batch is determined by the max-batch-operations parameter and is 10000 by default.
requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
chat_id:
description: Unique identifier for the target chat or username of the target channel (in the format `@channelusername`)
anyOf:
- type: integer
- type: string
message_ids:
type: array
items:
type: integer
start:
description: First message id to delete
type: integer
end:
description: Last message id to delete
type: integer
required:
- chat_id
multipart/form-data:
schema:
type: object
properties:
chat_id:
description: Unique identifier for the target chat or username of the target channel (in the format `@channelusername`)
anyOf:
- type: integer
- type: string
message_ids:
type: array
items:
type: integer
start:
description: First message id to delete
type: integer
end:
description: Last message id to delete
type: integer
required:
- chat_id
application/json:
schema:
type: object
properties:
chat_id:
description: Unique identifier for the target chat or username of the target channel (in the format `@channelusername`)
anyOf:
- type: integer
- type: string
message_ids:
type: array
items:
type: integer
start:
description: First message id to delete
type: integer
end:
description: Last message id to delete
type: integer
required:
- chat_id
required: true
responses:
'200':
description: 'Request was successful, the result is returned.'
content:
application/json:
schema:
type: object
properties:
ok:
default: true
type: boolean
result:
default: true
type: boolean
required:
- ok
- result
default:
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/ping: /ping:
post: post:
tags: tags:

View File

@ -10592,6 +10592,10 @@ td::Status Client::process_delete_message_query(PromisedQueryPtr &query) {
} }
td::Status Client::process_delete_messages_query(PromisedQueryPtr &query) { td::Status Client::process_delete_messages_query(PromisedQueryPtr &query) {
auto is_range_delete_query = query->arg("start").empty() && query->arg("end").empty();
if (is_range_delete_query) {
return process_delete_messages_range_query(query);
}
auto chat_id = query->arg("chat_id"); auto chat_id = query->arg("chat_id");
TRY_RESULT(message_ids, get_message_ids(query.get(), 100)); TRY_RESULT(message_ids, get_message_ids(query.get(), 100));
if (message_ids.empty()) { if (message_ids.empty()) {
@ -11975,6 +11979,48 @@ td::Status Client::process_disable_proxy_query(PromisedQueryPtr &query) {
td::make_unique<TdOnOkQueryCallback>(std::move(query))); td::make_unique<TdOnOkQueryCallback>(std::move(query)));
return td::Status::OK(); return td::Status::OK();
} }
td::Status Client::process_delete_messages_range_query(PromisedQueryPtr &query) {
auto chat_id = query->arg("chat_id");
if (chat_id.empty()) {
return td::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 td::Status::Error(400, "Message identifier is not specified");
}
if (start >= end) {
return td::Status::Error(400, "Initial message identifier is not lower than last message identifier");
}
if (static_cast<td::uint32>(end-start) > parameters_->max_batch_operations) {
return td::Status::Error(400, PSLICE() << "Too many operations: maximum number of batch operation is " << parameters_->max_batch_operations);
}
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<td::int64> ids;
ids.reserve(end-start+1);
for (td::int32 i = start; i <= end; i++) {
ids.push_back(as_tdlib_message_id(i));
}
if (!ids.empty()) {
send_request(make_object<td_api::deleteMessages>(chat_id, std::move(ids), true),
td::make_unique<TdOnOkQueryCallback>(std::move(query)));
}
});
return td::Status::OK();
}
//end custom methods impl //end custom methods impl
//start custom user methods impl //start custom user methods impl

View File

@ -762,6 +762,7 @@ class Client final : public WebhookActor::Callback {
td::Status process_delete_proxy_query(PromisedQueryPtr &query); td::Status process_delete_proxy_query(PromisedQueryPtr &query);
td::Status process_enable_proxy_query(PromisedQueryPtr &query); td::Status process_enable_proxy_query(PromisedQueryPtr &query);
td::Status process_disable_proxy_query(PromisedQueryPtr &query); td::Status process_disable_proxy_query(PromisedQueryPtr &query);
td::Status process_delete_messages_range_query(PromisedQueryPtr &query);
//custom user methods //custom user methods
td::Status process_get_chats_query(PromisedQueryPtr &query); td::Status process_get_chats_query(PromisedQueryPtr &query);