From a7c948dd95c53b42288a8823552d8aa6321d0e1b Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 15 Jul 2025 19:12:42 +0300 Subject: [PATCH] Added attachment CallbackButton --- src/Api.php | 12 ++- src/Enums/AttachmentType.php | 18 +++++ src/Enums/ButtonType.php | 15 ++++ src/Enums/Intent.php | 12 +++ .../Attachments/Buttons/AbstractButton.php | 17 ++++ .../Attachments/Buttons/CallbackButton.php | 22 +++++ .../Payloads/AbstractAttachmentPayload.php | 11 +++ .../Payloads/InlineKeyboardPayload.php | 18 +++++ .../Requests/AbstractAttachmentRequest.php | 21 +++++ .../InlineKeyboardAttachmentRequest.php | 23 ++++++ tests/ApiTest.php | 81 +++++++++++++++++++ .../Buttons/CallbackButtonTest.php | 33 ++++++++ 12 files changed, 280 insertions(+), 3 deletions(-) create mode 100644 src/Enums/AttachmentType.php create mode 100644 src/Enums/ButtonType.php create mode 100644 src/Enums/Intent.php create mode 100644 src/Models/Attachments/Buttons/AbstractButton.php create mode 100644 src/Models/Attachments/Buttons/CallbackButton.php create mode 100644 src/Models/Attachments/Payloads/AbstractAttachmentPayload.php create mode 100644 src/Models/Attachments/Payloads/InlineKeyboardPayload.php create mode 100644 src/Models/Attachments/Requests/AbstractAttachmentRequest.php create mode 100644 src/Models/Attachments/Requests/InlineKeyboardAttachmentRequest.php create mode 100644 tests/Models/Attachments/Buttons/CallbackButtonTest.php diff --git a/src/Api.php b/src/Api.php index 56d27ad..4f3dd6c 100644 --- a/src/Api.php +++ b/src/Api.php @@ -9,6 +9,8 @@ use BushlanovDev\MaxMessengerBot\Enums\UpdateType; use BushlanovDev\MaxMessengerBot\Exceptions\ClientApiException; use BushlanovDev\MaxMessengerBot\Exceptions\NetworkException; use BushlanovDev\MaxMessengerBot\Exceptions\SerializationException; +use BushlanovDev\MaxMessengerBot\Models\AbstractModel; +use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\AbstractAttachmentRequest; use BushlanovDev\MaxMessengerBot\Models\BotInfo; use BushlanovDev\MaxMessengerBot\Models\Message; use BushlanovDev\MaxMessengerBot\Models\Result; @@ -155,20 +157,20 @@ class Api * @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 AbstractAttachmentRequest[]|null $attachments Message attachments. + * @param MessageFormat|null $format * @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, + ?array $attachments = null, ?MessageFormat $format = null, bool $notify = true, bool $disableLinkPreview = false, @@ -183,6 +185,10 @@ class Api 'text' => $text, 'format' => $format?->value, 'notify' => $notify, + 'attachments' => $attachments !== null ? array_map( + fn(AbstractModel $attachment) => $attachment->toArray(), + $attachments, + ) : null, ]; $response = $this->client->request( diff --git a/src/Enums/AttachmentType.php b/src/Enums/AttachmentType.php new file mode 100644 index 0000000..b6448cf --- /dev/null +++ b/src/Enums/AttachmentType.php @@ -0,0 +1,18 @@ +payload = $payload; + $this->intent = $intent; + } +} diff --git a/src/Models/Attachments/Payloads/AbstractAttachmentPayload.php b/src/Models/Attachments/Payloads/AbstractAttachmentPayload.php new file mode 100644 index 0000000..eb9c5b1 --- /dev/null +++ b/src/Models/Attachments/Payloads/AbstractAttachmentPayload.php @@ -0,0 +1,11 @@ +assertSame($expectedMessageObject, $result); } + + #[Test] + public function sendMessageWithAttachmentsBuildsCorrectRequest(): void + { + $chatId = 123456; + $text = 'Test message with a keyboard'; + $disableLinkPreview = true; + $button = new CallbackButton('Press Me', 'payload_123'); + $keyboard = new InlineKeyboardAttachmentRequest([[$button]]); + + $expectedAttachmentsJson = [ + [ + 'type' => AttachmentType::InlineKeyboard->value, + 'payload' => [ + 'buttons' => [ + [ + [ + 'type' => ButtonType::Callback->value, + 'text' => 'Press Me', + 'payload' => 'payload_123', + 'intent' => null, + ] + ] + ] + ] + ] + ]; + + $expectedBody = [ + 'text' => $text, + 'attachments' => $expectedAttachmentsJson, + 'notify' => true, + ]; + + $expectedQuery = ['chat_id' => $chatId, 'disable_link_preview' => $disableLinkPreview]; + + $apiResponse = [ + 'message' => [ + 'timestamp' => time(), + 'body' => ['mid' => 'mid.test.123', 'seq' => 1, 'text' => $text], + 'recipient' => ['chat_type' => 'dialog', 'user_id' => 123, 'chat_id' => null], + ] + ]; + + $expectedMessageObject = Message::fromArray($apiResponse['message']); + + $this->clientMock + ->expects($this->once()) + ->method('request') + ->with( + 'POST', + '/messages', + $expectedQuery, + $expectedBody, + ) + ->willReturn($apiResponse); + + $this->modelFactoryMock + ->expects($this->once()) + ->method('createMessage') + ->with($apiResponse['message']) + ->willReturn($expectedMessageObject); + + $result = $this->api->sendMessage( + chatId: $chatId, + text: $text, + attachments: [$keyboard], + disableLinkPreview: $disableLinkPreview, + ); + + $this->assertSame($expectedMessageObject, $result); + } } diff --git a/tests/Models/Attachments/Buttons/CallbackButtonTest.php b/tests/Models/Attachments/Buttons/CallbackButtonTest.php new file mode 100644 index 0000000..eda8f99 --- /dev/null +++ b/tests/Models/Attachments/Buttons/CallbackButtonTest.php @@ -0,0 +1,33 @@ + 'test_payload', + 'intent' => Intent::Default->value, + 'type' => ButtonType::Callback->value, + 'text' => 'Test Button', + ]; + + $resultArray = $button->toArray(); + + $this->assertSame($expectedArray, $resultArray); + } +}