Added Chat

This commit is contained in:
Alex
2025-07-20 14:17:19 +03:00
parent 4661edffdf
commit 07c6bb35b4
7 changed files with 192 additions and 3 deletions
+23 -3
View File
@@ -14,6 +14,7 @@ use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\AbstractAttachmentRequest;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\PhotoAttachmentRequest;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageLink;
use BushlanovDev\MaxMessengerBot\Models\Result;
@@ -43,6 +44,7 @@ class Api
private const string ACTION_SUBSCRIPTIONS = '/subscriptions';
private const string ACTION_MESSAGES = '/messages';
private const string ACTION_UPLOADS = '/uploads';
private const string ACTION_CHATS = '/chats';
private readonly ClientApiInterface $client;
@@ -237,13 +239,13 @@ class Api
* @param string $filePath Path to the file on the local disk.
*
* @return AbstractAttachmentRequest
* @throws ReflectionException
* @throws SerializationException
* @throws InvalidArgumentException
* @throws RuntimeException
* @throws LogicException
* @throws NetworkException
* @throws ClientApiException
* @throws NetworkException
* @throws SerializationException
* @throws ReflectionException
*/
public function uploadAttachment(UploadType $type, string $filePath): AbstractAttachmentRequest
{
@@ -277,4 +279,22 @@ class Api
),
};
}
/**
* Returns info about chat.
*
* @param int $chatId Requested chat identifier.
*
* @return Chat
* @throws ClientApiException
* @throws NetworkException
* @throws SerializationException
* @throws ReflectionException
*/
public function getChat(int $chatId): Chat
{
return $this->modelFactory->createChat(
$this->client->request(self::METHOD_GET, self::ACTION_CHATS . '/' . $chatId)
);
}
}
+14
View File
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Enums;
enum ChatStatus: string
{
case Active = 'active';
case Removed = 'removed';
case Left = 'left';
case Closed = 'closed';
case Suspended = 'suspended';
}
+14
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
@@ -95,4 +96,17 @@ class ModelFactory
{
return UploadEndpoint::fromArray($data);
}
/**
* Chat information.
*
* @param array<string, mixed> $data
*
* @return Chat
* @throws ReflectionException
*/
public function createChat(array $data): Chat
{
return Chat::fromArray($data);
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
use BushlanovDev\MaxMessengerBot\Enums\ChatStatus;
use BushlanovDev\MaxMessengerBot\Enums\ChatType;
/**
* Information about the chat.
*/
final readonly class Chat extends AbstractModel
{
/**
* @param int $chatId Chats identifier.
* @param ChatType $type Type of chat. One of: `dialog`, `chat`, `channel`.
* @param ChatStatus $status Chat status.
* @param int $lastEventTime Time of last event occurred in chat.
* @param int $participantsCount Number of people in chat. Always 2 for `dialog` chat type.
* @param bool $isPublic Is current chat publicly available. Always false for dialogs.
* @param string|null $title Visible title of chat. Can be null for dialogs.
* @param Image|null $icon Icon of chat.
* @param int|null $ownerId Identifier of chat owner. Visible only for chat admins
* @param string|null $link Link on chat.
* @param string|null $description Chat description.
* @param User|null $dialogWithUser Another user in conversation. For `dialog` type chats only.
* @param int|null $messagesCount Messages count in chat. Only for group chats and channels. Not available for dialogs.
* @param string|null $chatMessageId Identifier of message that contains `chat` button initialized chat.
* @param Message|null $pinnedMessage Pinned message in chat or channel. Returned only when single chat is requested.
*/
public function __construct(
public int $chatId,
public ChatType $type,
public ChatStatus $status,
public int $lastEventTime,
public int $participantsCount,
public bool $isPublic,
public ?string $title,
public ?Image $icon,
public ?int $ownerId,
public ?string $link,
public ?string $description,
public ?User $dialogWithUser,
public ?int $messagesCount,
public ?string $chatMessageId,
public ?Message $pinnedMessage,
) {
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
final readonly class Image extends AbstractModel
{
/**
* @param string $url URL of image.
*/
public function __construct(
public string $url,
) {
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
final readonly class User extends AbstractModel
{
/**
* @param int $userId Users identifier.
* @param string $firstName Users first name.
* @param string|null $lastName Users last name.
* @param string|null $username Unique public user name. Can be `null` if user is not accessible or it is not set.
* @param bool $isBot `true` if user is bot.
* @param int $lastActivityTime Time of last user activity in Max (Unix timestamp in milliseconds).
* @param string|null $description User description. Can be `null` if user did not fill it out.
* @param string|null $avatarUrl URL of avatar.
* @param string|null $fullAvatarUrl URL of avatar of a bigger size.
*/
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,
) {
}
}
+43
View File
@@ -20,6 +20,7 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoToken;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\InlineKeyboardAttachmentRequest;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\PhotoAttachmentRequest;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
use BushlanovDev\MaxMessengerBot\Models\Recipient;
@@ -52,6 +53,7 @@ use ReflectionClass;
#[UsesClass(PhotoAttachmentRequest::class)]
#[UsesClass(PhotoAttachmentPayload::class)]
#[UsesClass(UploadEndpoint::class)]
#[UsesClass(Chat::class)]
final class ApiTest extends TestCase
{
private MockObject&ClientApiInterface $clientMock;
@@ -450,4 +452,45 @@ final class ApiTest extends TestCase
$this->expectExceptionMessageMatches('/File not found or not readable/');
$this->api->uploadAttachment(UploadType::Image, '/path/to/non/existent/file.jpg');
}
#[Test]
public function getChatCallsClientAndFactoryCorrectly(): void
{
$chatId = 100123456789;
$rawResponseData = [
'chat_id' => $chatId,
'type' => 'chat',
'status' => 'active',
'last_event_time' => 1678886400000,
'participants_count' => 50,
'is_public' => false,
'title' => 'Test Chat Title',
'icon' => null,
'owner_id' => null,
'link' => null,
'description' => null,
'dialog_with_user' => null,
'messages_count' => null,
'chat_message_id' => null,
'pinned_message' => null,
];
$expectedChatObject = Chat::fromArray($rawResponseData);
$this->clientMock
->expects($this->once())
->method('request')
->with('GET', '/chats/' . $chatId)
->willReturn($rawResponseData);
$this->modelFactoryMock
->expects($this->once())
->method('createChat')
->with($rawResponseData)
->willReturn($expectedChatObject);
$result = $this->api->getChat($chatId);
$this->assertSame($expectedChatObject, $result);
}
}