Added getPinnedMessage & unpinMessage & getMembership & leaveChat

This commit is contained in:
Alex
2025-07-28 18:57:27 +03:00
parent 67ce2958a6
commit a3dd4d9a29
8 changed files with 437 additions and 5 deletions
+94 -1
View File
@@ -21,6 +21,7 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\VideoAttachmentRequ
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\ChatList;
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageLink;
use BushlanovDev\MaxMessengerBot\Models\Result;
@@ -55,6 +56,9 @@ class Api
private const string ACTION_MESSAGES = '/messages';
private const string ACTION_UPLOADS = '/uploads';
private const string ACTION_CHATS = '/chats';
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_UPDATES = '/updates';
private readonly ClientApiInterface $client;
@@ -569,10 +573,99 @@ class Api
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_POST,
self::ACTION_CHATS . '/' . $chatId . '/actions',
sprintf(self::ACTION_CHATS_ACTIONS, $chatId),
[],
['action' => $action->value],
)
);
}
/**
* Gets the pinned message in a chat or channel.
*
* @param int $chatId Identifier of the chat to get its pinned message from.
*
* @return Message|null
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function getPinnedMessage(int $chatId): ?Message
{
$response = $this->client->request(
self::METHOD_GET,
sprintf(self::ACTION_CHATS_PIN, $chatId),
);
if (!isset($response['message']) || empty($response['message'])) {
return null;
}
return $this->modelFactory->createMessage($response['message']);
}
/**
* Unpins a message in a chat or channel.
*
* @param int $chatId Chat identifier to remove the pinned message from.
*
* @return Result
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function unpinMessage(int $chatId): Result
{
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_DELETE,
sprintf(self::ACTION_CHATS_PIN, $chatId),
)
);
}
/**
* Returns chat membership info for the current bot.
*
* @param int $chatId Chat identifier.
*
* @return ChatMember
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function getMembership(int $chatId): ChatMember
{
return $this->modelFactory->createChatMember(
$this->client->request(
self::METHOD_GET,
sprintf(self::ACTION_CHATS_MEMBERS_ME, $chatId),
)
);
}
/**
* Removes the bot from a chat's members.
*
* @param int $chatId Chat identifier to leave from.
*
* @return Result A simple success/fail result.
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function leaveChat(int $chatId): Result
{
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_DELETE,
sprintf(self::ACTION_CHATS_MEMBERS_ME, $chatId),
)
);
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Enums;
/**
* Defines the permissions an administrator can have in a chat.
*/
enum ChatAdminPermission: string
{
case ReadAllMessages = 'read_all_messages';
case AddRemoveMembers = 'add_remove_members';
case AddAdmins = 'add_admins';
case ChangeChatInfo = 'change_chat_info';
case PinMessage = 'pin_message';
case Write = 'write';
}
+14
View File
@@ -8,6 +8,7 @@ use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\ChatList;
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
@@ -192,4 +193,17 @@ class ModelFactory
{
return ChatList::fromArray($data);
}
/**
* Creates a ChatMember object from raw API data.
*
* @param array<string, mixed> $data
*
* @return ChatMember
* @throws ReflectionException
*/
public function createChatMember(array $data): ChatMember
{
return ChatMember::fromArray($data);
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
/**
* Represents a member of a chat, including their user information and chat-specific status.
*/
final readonly class ChatMember extends AbstractModel
{
/**
* @param int $userId User's identifier.
* @param string $firstName User's first name.
* @param string|null $lastName User's last name.
* @param string|null $username User's public username.
* @param bool $isBot True if the user is a bot.
* @param int $lastActivityTime Time of the user's last activity in Max.
* @param string|null $description User's profile description.
* @param string|null $avatarUrl URL of the user's avatar.
* @param string|null $fullAvatarUrl URL of the user's full-sized avatar.
* @param int $lastAccessTime The time the user last accessed the chat.
* @param bool $isOwner True if this member is the owner of the chat.
* @param bool $isAdmin True if this member is an administrator of the chat.
* @param int $joinTime The time the user joined the chat.
* @param ChatAdminPermission[]|null $permissions A list of permissions if the member is an admin, otherwise null.
*/
public function __construct(
public int $userId,
public string $firstName,
public ?string $lastName,
public ?string $username,
public bool $isBot,
public int $lastActivityTime,
public ?string $description,
public ?string $avatarUrl,
public ?string $fullAvatarUrl,
public int $lastAccessTime,
public bool $isOwner,
public bool $isAdmin,
public int $joinTime,
#[ArrayOf(ChatAdminPermission::class)]
public ?array $permissions,
) {
}
}