Added sendMessage method

This commit is contained in:
Alex
2025-07-12 21:39:16 +03:00
parent 0c91886ff0
commit c8f87c70b6
10 changed files with 297 additions and 3 deletions
+49
View File
@@ -4,11 +4,13 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot;
use BushlanovDev\MaxMessengerBot\Enums\MessageFormat;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException;
use BushlanovDev\MaxMessengerBot\Exceptions\NetworkException;
use BushlanovDev\MaxMessengerBot\Exceptions\SerializationException;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
use InvalidArgumentException;
@@ -28,6 +30,7 @@ class Api
private const string ACTION_ME = '/me';
private const string ACTION_SUBSCRIPTIONS = '/subscriptions';
private const string ACTION_MESSAGES = '/messages';
private readonly ClientApiInterface $client;
@@ -145,4 +148,50 @@ class Api
)
);
}
/**
* Sends a message to a chat.
*
* @param int|null $userId Fill this parameter if you want to send message to user.
* @param int|null $chatId Fill this if you send message to chat.
* @param string|null $text Message text.
* @param bool $notify If false, chat participants would not be notified.
* @param bool $disableLinkPreview If false, server will not generate media preview for links in text.
*
* @return Message
*
* @throws ClientApiException
* @throws NetworkException
* @throws SerializationException
* @throws ReflectionException
*/
public function sendMessage(
?int $userId = null,
?int $chatId = null,
?string $text = null,
?MessageFormat $format = null,
bool $notify = true,
bool $disableLinkPreview = false,
): Message {
$query = [
'user_id' => $userId,
'chat_id' => $chatId,
'disable_link_preview' => $disableLinkPreview,
];
$body = [
'text' => $text,
'format' => $format?->value,
'notify' => $notify,
];
$response = $this->client->request(
self::METHOD_POST,
self::ACTION_MESSAGES,
array_filter($query, fn($item) => null !== $item),
array_filter($body, fn($item) => null !== $item),
);
return $this->modelFactory->createMessage($response['message']);
}
}
+1 -3
View File
@@ -119,7 +119,7 @@ final class Client implements ClientApiInterface
$errorCode = $data['code'] ?? 'unknown';
$errorMessage = $data['message'] ?? 'An unknown error occurred.';
$exception = match ($statusCode) {
throw match ($statusCode) {
401 => new UnauthorizedException($errorMessage, $errorCode, $response),
403 => new ForbiddenException($errorMessage, $errorCode, $response),
404 => new NotFoundException($errorMessage, $errorCode, $response),
@@ -127,7 +127,5 @@ final class Client implements ClientApiInterface
429 => new RateLimitExceededException($errorMessage, $errorCode, $response),
default => new ClientApiException($errorMessage, $errorCode, $response, $statusCode),
};
throw $exception;
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Enums;
enum MessageFormat: string
{
case Markdown = 'markdown';
case Html = 'html';
}
+18
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\Result;
use BushlanovDev\MaxMessengerBot\Models\Subscription;
use ReflectionException;
@@ -20,6 +21,7 @@ class ModelFactory
* @param array<string, mixed> $data
*
* @return Result
*
* @throws ReflectionException
*/
public function createResult(array $data): Result
@@ -33,6 +35,7 @@ class ModelFactory
* @param array<string, mixed> $data
*
* @return BotInfo
*
* @throws ReflectionException
*/
public function createBotInfo(array $data): BotInfo
@@ -46,6 +49,7 @@ class ModelFactory
* @param array<string, mixed> $data
*
* @return Subscription
*
* @throws ReflectionException
*/
public function createSubscription(array $data): Subscription
@@ -67,4 +71,18 @@ class ModelFactory
? array_map([$this, 'createSubscription'], $data['subscriptions'])
: [];
}
/**
* Message.
*
* @param array<string, mixed> $data
*
* @return Message
*
* @throws ReflectionException
*/
public function createMessage(array $data): Message
{
return Message::fromArray($data);
}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
/**
* Message.
*/
final readonly class Message extends AbstractModel
{
/**
* @param MessageBody $body Body of created message. Text + attachments.
*/
public function __construct(
public MessageBody $body,
) {
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
/**
* Body of created message. Text + attachments.
*/
final readonly class MessageBody extends AbstractModel
{
/**
* @param string $mid Unique identifier of message.
* @param int $seq Sequence identifier of message in chat.
* @param string|null $text Message text.
*/
public function __construct(
public string $mid,
public int $seq,
public ?string $text,
) {
}
}