Skip to content

Commit 3810f34

Browse files
committed
Added getAdmins & deleteAdmins & getMembers & removeMember
1 parent 46e7c2e commit 3810f34

File tree

7 files changed

+471
-6
lines changed

7 files changed

+471
-6
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
- [x] `DELETE /chats/{chatId}/pin` (`unpinMessage`) — *Открепление сообщения.*
4242
- [x] `GET /chats/{chatId}/members/me` (`getMembership`) — *Получение информации о членстве бота в чате.*
4343
- [x] `DELETE /chats/{chatId}/members/me` (`leaveChat`) — *Выход бота из чата.*
44-
- [ ] `GET /chats/{chatId}/members/admins` (`getAdmins`) — *Получение администраторов чата.*
44+
- [x] `GET /chats/{chatId}/members/admins` (`getAdmins`) — *Получение администраторов чата.*
4545
- [ ] `POST /chats/{chatId}/members/admins` (`postAdmins`) — *Назначение администраторов чата.*
46-
- [ ] `DELETE /chats/{chatId}/members/admins/{userId}` (`deleteAdmins`) — *Снятие прав администратора.*
47-
- [ ] `GET /chats/{chatId}/members` (`getMembers`) — *Получение участников чата.*
46+
- [x] `DELETE /chats/{chatId}/members/admins/{userId}` (`deleteAdmins`) — *Снятие прав администратора.*
47+
- [x] `GET /chats/{chatId}/members` (`getMembers`) — *Получение участников чата.*
4848
- [ ] `POST /chats/{chatId}/members` (`addMembers`) — *Добавление участников в чат.*
49-
- [ ] `DELETE /chats/{chatId}/members` (`removeMember`) — *Удаление участника из чата.*
49+
- [x] `DELETE /chats/{chatId}/members` (`removeMember`) — *Удаление участника из чата.*
5050

5151
#### Subscriptions
5252

@@ -71,4 +71,4 @@
7171

7272
## Лицензия
7373

74-
Данная библиотека распространяется по лицензии MIT - подробности см. в файле [LICENSE](LICENSE).
74+
Данная библиотека распространяется под лицензией MIT - подробности см. в файле [LICENSE](LICENSE).

src/Api.php

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use BushlanovDev\MaxMessengerBot\Models\Chat;
2323
use BushlanovDev\MaxMessengerBot\Models\ChatList;
2424
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
25+
use BushlanovDev\MaxMessengerBot\Models\ChatMembersList;
2526
use BushlanovDev\MaxMessengerBot\Models\Message;
2627
use BushlanovDev\MaxMessengerBot\Models\MessageLink;
2728
use BushlanovDev\MaxMessengerBot\Models\Result;
@@ -60,6 +61,9 @@ class Api
6061
private const string ACTION_CHATS_ACTIONS = '/chats/%d/actions';
6162
private const string ACTION_CHATS_PIN = '/chats/%d/pin';
6263
private const string ACTION_CHATS_MEMBERS_ME = '/chats/%d/members/me';
64+
private const string ACTION_CHATS_MEMBERS_ADMINS = '/chats/%d/members/admins';
65+
private const string ACTION_CHATS_MEMBERS_ADMINS_ID = '/chats/%d/members/admins/%d';
66+
private const string ACTION_CHATS_MEMBERS = '/chats/%d/members';
6367
private const string ACTION_UPDATES = '/updates';
6468

6569
private readonly ClientApiInterface $client;
@@ -703,7 +707,7 @@ public function getMessages(
703707
$response = $this->client->request(
704708
self::METHOD_GET,
705709
self::ACTION_MESSAGES,
706-
array_filter($query, fn ($value) => $value !== null)
710+
array_filter($query, fn($value) => $value !== null),
707711
);
708712

709713
return $this->modelFactory->createMessages($response);
@@ -779,4 +783,111 @@ public function pinMessage(int $chatId, string $messageId, bool $notify = true):
779783
)
780784
);
781785
}
786+
787+
/**
788+
* Returns all chat administrators. The bot must be an administrator in the requested chat.
789+
*
790+
* @param int $chatId Chat identifier.
791+
*
792+
* @return ChatMembersList
793+
* @throws ClientApiException
794+
* @throws NetworkException
795+
* @throws ReflectionException
796+
* @throws SerializationException
797+
*/
798+
public function getAdmins(int $chatId): ChatMembersList
799+
{
800+
return $this->modelFactory->createChatMembersList(
801+
$this->client->request(
802+
self::METHOD_GET,
803+
sprintf(self::ACTION_CHATS_MEMBERS_ADMINS, $chatId),
804+
)
805+
);
806+
}
807+
808+
/**
809+
* Returns a paginated list of users who are participating in a chat.
810+
*
811+
* @param int $chatId The identifier of the chat.
812+
* @param int[]|null $userIds A list of user identifiers to get their specific membership.
813+
* When this parameter is passed, `count` and `marker` are ignored.
814+
* @param int|null $marker The pagination marker to get the next page of members.
815+
* @param int|null $count The number of members to return (1-100, default is 20).
816+
*
817+
* @return ChatMembersList
818+
* @throws ClientApiException
819+
* @throws NetworkException
820+
* @throws ReflectionException
821+
* @throws SerializationException
822+
*/
823+
public function getMembers(
824+
int $chatId,
825+
?array $userIds = null,
826+
?int $marker = null,
827+
?int $count = null
828+
): ChatMembersList {
829+
$query = [
830+
'user_ids' => $userIds !== null ? implode(',', $userIds) : null,
831+
'marker' => $marker,
832+
'count' => $count,
833+
];
834+
835+
return $this->modelFactory->createChatMembersList(
836+
$this->client->request(
837+
self::METHOD_GET,
838+
sprintf(self::ACTION_CHATS_MEMBERS, $chatId),
839+
array_filter($query, fn($value) => $value !== null),
840+
)
841+
);
842+
}
843+
844+
/**
845+
* Revokes admin rights from a user in the chat.
846+
*
847+
* @param int $chatId The identifier of the chat.
848+
* @param int $userId The identifier of the user to revoke admin rights from.
849+
*
850+
* @return Result
851+
* @throws ClientApiException
852+
* @throws NetworkException
853+
* @throws ReflectionException
854+
* @throws SerializationException
855+
*/
856+
public function deleteAdmins(int $chatId, int $userId): Result
857+
{
858+
return $this->modelFactory->createResult(
859+
$this->client->request(
860+
self::METHOD_DELETE,
861+
sprintf(self::ACTION_CHATS_MEMBERS_ADMINS_ID, $chatId, $userId),
862+
)
863+
);
864+
}
865+
866+
/**
867+
* Removes a member from a chat. The bot may require additional permissions.
868+
*
869+
* @param int $chatId The identifier of the chat.
870+
* @param int $userId The identifier of the user to remove.
871+
* @param bool $block Set to true if the user should also be blocked in the chat.
872+
* Applicable only for chats with a public or private link.
873+
*
874+
* @return Result
875+
* @throws ClientApiException
876+
* @throws NetworkException
877+
* @throws ReflectionException
878+
* @throws SerializationException
879+
*/
880+
public function removeMember(int $chatId, int $userId, bool $block = false): Result
881+
{
882+
return $this->modelFactory->createResult(
883+
$this->client->request(
884+
self::METHOD_DELETE,
885+
sprintf(self::ACTION_CHATS_MEMBERS, $chatId),
886+
[
887+
'user_id' => $userId,
888+
'block' => $block,
889+
],
890+
)
891+
);
892+
}
782893
}

src/ModelFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BushlanovDev\MaxMessengerBot\Models\Chat;
1010
use BushlanovDev\MaxMessengerBot\Models\ChatList;
1111
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
12+
use BushlanovDev\MaxMessengerBot\Models\ChatMembersList;
1213
use BushlanovDev\MaxMessengerBot\Models\Message;
1314
use BushlanovDev\MaxMessengerBot\Models\Result;
1415
use BushlanovDev\MaxMessengerBot\Models\Subscription;
@@ -220,4 +221,17 @@ public function createChatMember(array $data): ChatMember
220221
{
221222
return ChatMember::fromArray($data);
222223
}
224+
225+
/**
226+
* Creates a ChatMembersList object from raw API data.
227+
*
228+
* @param array<string, mixed> $data
229+
*
230+
* @return ChatMembersList
231+
* @throws ReflectionException
232+
*/
233+
public function createChatMembersList(array $data): ChatMembersList
234+
{
235+
return ChatMembersList::fromArray($data);
236+
}
223237
}

src/Models/ChatMembersList.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models;
6+
7+
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
8+
9+
/**
10+
* Represents a paginated list of chat members.
11+
*/
12+
final readonly class ChatMembersList extends AbstractModel
13+
{
14+
/**
15+
* @param ChatMember[] $members A list of chat members.
16+
* @param int|null $marker A pointer to the next page of data. Null if this is the last page.
17+
*/
18+
public function __construct(
19+
#[ArrayOf(ChatMember::class)]
20+
public array $members,
21+
public ?int $marker,
22+
) {
23+
}
24+
}

0 commit comments

Comments
 (0)