Skip to content

Commit

Permalink
Add custom deleteMessages implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cavallium committed Feb 11, 2024
1 parent faa7237 commit dd075ae
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 11 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ Get the member list of a supergroup or channel

###### 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
- `chat_id` Chat id
- `start` First message id to delete
Expand Down Expand Up @@ -122,6 +111,17 @@ _For Docker containers, `$TELEGRAM_VERBOSITY` can be set._
##### Method `getChat`
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`
The `Message` object now has two new fields:
- `views`: how many views has the message (usually the views are shown only for channel messages)
Expand Down
109 changes: 109 additions & 0 deletions tdlight-api-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,115 @@ paths:
application/json:
schema:
$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:
post:
tags:
Expand Down
46 changes: 46 additions & 0 deletions telegram-bot-api/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10592,6 +10592,10 @@ td::Status Client::process_delete_message_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");
TRY_RESULT(message_ids, get_message_ids(query.get(), 100));
if (message_ids.empty()) {
Expand Down Expand Up @@ -11975,6 +11979,48 @@ td::Status Client::process_disable_proxy_query(PromisedQueryPtr &query) {
td::make_unique<TdOnOkQueryCallback>(std::move(query)));
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
//start custom user methods impl

Expand Down
1 change: 1 addition & 0 deletions telegram-bot-api/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ class Client final : public WebhookActor::Callback {
td::Status process_delete_proxy_query(PromisedQueryPtr &query);
td::Status process_enable_proxy_query(PromisedQueryPtr &query);
td::Status process_disable_proxy_query(PromisedQueryPtr &query);
td::Status process_delete_messages_range_query(PromisedQueryPtr &query);

//custom user methods
td::Status process_get_chats_query(PromisedQueryPtr &query);
Expand Down

0 comments on commit dd075ae

Please sign in to comment.