mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-30 03:57:41 +00:00
Added getAdmins & deleteAdmins & getMembers & removeMember
This commit is contained in:
+112
-1
@@ -22,6 +22,7 @@ use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Chat;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatList;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatMembersList;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Message;
|
||||
use BushlanovDev\MaxMessengerBot\Models\MessageLink;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Result;
|
||||
@@ -60,6 +61,9 @@ class Api
|
||||
private const string ACTION_CHATS_ACTIONS = '/chats/%d/actions';
|
||||
private const string ACTION_CHATS_PIN = '/chats/%d/pin';
|
||||
private const string ACTION_CHATS_MEMBERS_ME = '/chats/%d/members/me';
|
||||
private const string ACTION_CHATS_MEMBERS_ADMINS = '/chats/%d/members/admins';
|
||||
private const string ACTION_CHATS_MEMBERS_ADMINS_ID = '/chats/%d/members/admins/%d';
|
||||
private const string ACTION_CHATS_MEMBERS = '/chats/%d/members';
|
||||
private const string ACTION_UPDATES = '/updates';
|
||||
|
||||
private readonly ClientApiInterface $client;
|
||||
@@ -703,7 +707,7 @@ class Api
|
||||
$response = $this->client->request(
|
||||
self::METHOD_GET,
|
||||
self::ACTION_MESSAGES,
|
||||
array_filter($query, fn ($value) => $value !== null)
|
||||
array_filter($query, fn($value) => $value !== null),
|
||||
);
|
||||
|
||||
return $this->modelFactory->createMessages($response);
|
||||
@@ -779,4 +783,111 @@ class Api
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all chat administrators. The bot must be an administrator in the requested chat.
|
||||
*
|
||||
* @param int $chatId Chat identifier.
|
||||
*
|
||||
* @return ChatMembersList
|
||||
* @throws ClientApiException
|
||||
* @throws NetworkException
|
||||
* @throws ReflectionException
|
||||
* @throws SerializationException
|
||||
*/
|
||||
public function getAdmins(int $chatId): ChatMembersList
|
||||
{
|
||||
return $this->modelFactory->createChatMembersList(
|
||||
$this->client->request(
|
||||
self::METHOD_GET,
|
||||
sprintf(self::ACTION_CHATS_MEMBERS_ADMINS, $chatId),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a paginated list of users who are participating in a chat.
|
||||
*
|
||||
* @param int $chatId The identifier of the chat.
|
||||
* @param int[]|null $userIds A list of user identifiers to get their specific membership.
|
||||
* When this parameter is passed, `count` and `marker` are ignored.
|
||||
* @param int|null $marker The pagination marker to get the next page of members.
|
||||
* @param int|null $count The number of members to return (1-100, default is 20).
|
||||
*
|
||||
* @return ChatMembersList
|
||||
* @throws ClientApiException
|
||||
* @throws NetworkException
|
||||
* @throws ReflectionException
|
||||
* @throws SerializationException
|
||||
*/
|
||||
public function getMembers(
|
||||
int $chatId,
|
||||
?array $userIds = null,
|
||||
?int $marker = null,
|
||||
?int $count = null
|
||||
): ChatMembersList {
|
||||
$query = [
|
||||
'user_ids' => $userIds !== null ? implode(',', $userIds) : null,
|
||||
'marker' => $marker,
|
||||
'count' => $count,
|
||||
];
|
||||
|
||||
return $this->modelFactory->createChatMembersList(
|
||||
$this->client->request(
|
||||
self::METHOD_GET,
|
||||
sprintf(self::ACTION_CHATS_MEMBERS, $chatId),
|
||||
array_filter($query, fn($value) => $value !== null),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revokes admin rights from a user in the chat.
|
||||
*
|
||||
* @param int $chatId The identifier of the chat.
|
||||
* @param int $userId The identifier of the user to revoke admin rights from.
|
||||
*
|
||||
* @return Result
|
||||
* @throws ClientApiException
|
||||
* @throws NetworkException
|
||||
* @throws ReflectionException
|
||||
* @throws SerializationException
|
||||
*/
|
||||
public function deleteAdmins(int $chatId, int $userId): Result
|
||||
{
|
||||
return $this->modelFactory->createResult(
|
||||
$this->client->request(
|
||||
self::METHOD_DELETE,
|
||||
sprintf(self::ACTION_CHATS_MEMBERS_ADMINS_ID, $chatId, $userId),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a member from a chat. The bot may require additional permissions.
|
||||
*
|
||||
* @param int $chatId The identifier of the chat.
|
||||
* @param int $userId The identifier of the user to remove.
|
||||
* @param bool $block Set to true if the user should also be blocked in the chat.
|
||||
* Applicable only for chats with a public or private link.
|
||||
*
|
||||
* @return Result
|
||||
* @throws ClientApiException
|
||||
* @throws NetworkException
|
||||
* @throws ReflectionException
|
||||
* @throws SerializationException
|
||||
*/
|
||||
public function removeMember(int $chatId, int $userId, bool $block = false): Result
|
||||
{
|
||||
return $this->modelFactory->createResult(
|
||||
$this->client->request(
|
||||
self::METHOD_DELETE,
|
||||
sprintf(self::ACTION_CHATS_MEMBERS, $chatId),
|
||||
[
|
||||
'user_id' => $userId,
|
||||
'block' => $block,
|
||||
],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Chat;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatList;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatMembersList;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Message;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Result;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Subscription;
|
||||
@@ -220,4 +221,17 @@ class ModelFactory
|
||||
{
|
||||
return ChatMember::fromArray($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ChatMembersList object from raw API data.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*
|
||||
* @return ChatMembersList
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function createChatMembersList(array $data): ChatMembersList
|
||||
{
|
||||
return ChatMembersList::fromArray($data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
|
||||
|
||||
/**
|
||||
* Represents a paginated list of chat members.
|
||||
*/
|
||||
final readonly class ChatMembersList extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* @param ChatMember[] $members A list of chat members.
|
||||
* @param int|null $marker A pointer to the next page of data. Null if this is the last page.
|
||||
*/
|
||||
public function __construct(
|
||||
#[ArrayOf(ChatMember::class)]
|
||||
public array $members,
|
||||
public ?int $marker,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user