mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-11-26 23:02:35 +00:00
Added sendMessage method
This commit is contained in:
+49
@@ -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
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Enums;
|
||||
|
||||
enum MessageFormat: string
|
||||
{
|
||||
case Markdown = 'markdown';
|
||||
case Html = 'html';
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user