Skip to content

Commit 728536f

Browse files
committed
Added MessageStat & Attachments
1 parent c5d55a5 commit 728536f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1210
-95
lines changed

src/ModelFactory.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,32 @@
55
namespace BushlanovDev\MaxMessengerBot;
66

77
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Enums\InlineButtonType;
89
use BushlanovDev\MaxMessengerBot\Enums\MarkupType;
10+
use BushlanovDev\MaxMessengerBot\Enums\ReplyButtonType;
911
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
1012
use BushlanovDev\MaxMessengerBot\Models\Attachments\AbstractAttachment;
13+
use BushlanovDev\MaxMessengerBot\Models\Attachments\AudioAttachment;
14+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\AbstractInlineButton;
15+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\CallbackButton;
16+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\ChatButton;
17+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\LinkButton;
18+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\RequestContactButton;
19+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\RequestGeoLocationButton;
20+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Reply\AbstractReplyButton;
21+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Reply\SendContactButton;
22+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Reply\SendGeoLocationButton;
23+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Reply\SendMessageButton;
24+
use BushlanovDev\MaxMessengerBot\Models\Attachments\ContactAttachment;
1125
use BushlanovDev\MaxMessengerBot\Models\Attachments\DataAttachment;
26+
use BushlanovDev\MaxMessengerBot\Models\Attachments\FileAttachment;
27+
use BushlanovDev\MaxMessengerBot\Models\Attachments\InlineKeyboardAttachment;
28+
use BushlanovDev\MaxMessengerBot\Models\Attachments\LocationAttachment;
29+
use BushlanovDev\MaxMessengerBot\Models\Attachments\PhotoAttachment;
30+
use BushlanovDev\MaxMessengerBot\Models\Attachments\ReplyKeyboardAttachment;
1231
use BushlanovDev\MaxMessengerBot\Models\Attachments\ShareAttachment;
32+
use BushlanovDev\MaxMessengerBot\Models\Attachments\StickerAttachment;
33+
use BushlanovDev\MaxMessengerBot\Models\Attachments\VideoAttachment;
1334
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
1435
use BushlanovDev\MaxMessengerBot\Models\Chat;
1536
use BushlanovDev\MaxMessengerBot\Models\ChatList;
@@ -142,15 +163,80 @@ public function createMessage(array $data): Message
142163
*/
143164
public function createAttachment(array $data): AbstractAttachment
144165
{
145-
return match (AttachmentType::tryFrom($data['type'] ?? '')) {
166+
$attachmentType = AttachmentType::tryFrom($data['type'] ?? '');
167+
if ($attachmentType === AttachmentType::ReplyKeyboard
168+
&& isset($data['buttons']) && is_array($data['buttons'])) {
169+
$data['buttons'] = array_map(
170+
fn($rowOfButtons) => array_map([$this, 'createReplyButton'], $rowOfButtons),
171+
$data['buttons'],
172+
);
173+
}
174+
175+
if ($attachmentType === AttachmentType::InlineKeyboard
176+
&& isset($data['payload']['buttons']) && is_array($data['payload']['buttons'])) {
177+
$data['payload']['buttons'] = array_map(
178+
fn($rowOfButtons) => array_map([$this, 'createInlineButton'], $rowOfButtons),
179+
$data['payload']['buttons']
180+
);
181+
}
182+
183+
return match ($attachmentType) {
146184
AttachmentType::Data => DataAttachment::fromArray($data),
147185
AttachmentType::Share => ShareAttachment::fromArray($data),
186+
AttachmentType::Image => PhotoAttachment::fromArray($data),
187+
AttachmentType::Video => VideoAttachment::fromArray($data),
188+
AttachmentType::Audio => AudioAttachment::fromArray($data),
189+
AttachmentType::File => FileAttachment::fromArray($data),
190+
AttachmentType::Sticker => StickerAttachment::fromArray($data),
191+
AttachmentType::Contact => ContactAttachment::fromArray($data),
192+
AttachmentType::InlineKeyboard => InlineKeyboardAttachment::fromArray($data),
193+
AttachmentType::ReplyKeyboard => ReplyKeyboardAttachment::fromArray($data),
194+
AttachmentType::Location => LocationAttachment::fromArray($data),
195+
default => throw new LogicException("Unknown or unsupported attachment type: " . ($data['type'] ?? 'none')),
196+
};
197+
}
198+
199+
/**
200+
* Creates a specific ReplyButton model based on the 'type' field.
201+
*
202+
* @param array<string, mixed> $data
203+
*
204+
* @return AbstractReplyButton
205+
* @throws ReflectionException
206+
* @throws LogicException
207+
*/
208+
public function createReplyButton(array $data): AbstractReplyButton
209+
{
210+
return match (ReplyButtonType::tryFrom($data['type'] ?? '')) {
211+
ReplyButtonType::Message => SendMessageButton::fromArray($data),
212+
ReplyButtonType::UserContact => SendContactButton::fromArray($data),
213+
ReplyButtonType::UserGeoLocation => SendGeoLocationButton::fromArray($data),
148214
default => throw new LogicException(
149-
'Unknown or unsupported Attachment type: ' . ($data['type'] ?? 'none')
215+
'Unknown or unsupported reply button type: ' . ($data['type'] ?? 'none')
150216
),
151217
};
152218
}
153219

220+
/**
221+
* Creates a specific InlineButton model based on the 'type' field.
222+
*
223+
* @param array<string, mixed> $data
224+
* @return AbstractInlineButton
225+
* @throws ReflectionException
226+
* @throws LogicException
227+
*/
228+
public function createInlineButton(array $data): AbstractInlineButton
229+
{
230+
return match (InlineButtonType::tryFrom($data['type'] ?? '')) {
231+
InlineButtonType::Callback => CallbackButton::fromArray($data),
232+
InlineButtonType::Link => LinkButton::fromArray($data),
233+
InlineButtonType::RequestContact => RequestContactButton::fromArray($data),
234+
InlineButtonType::RequestGeoLocation => RequestGeoLocationButton::fromArray($data),
235+
InlineButtonType::Chat => ChatButton::fromArray($data),
236+
default => throw new LogicException("Unknown or unsupported inline button type: " . ($data['type'] ?? 'none')),
237+
};
238+
}
239+
154240
/**
155241
* List of messages.
156242
*

src/Models/AbstractModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private static function castValue(mixed $value, ReflectionProperty $property): m
137137
return $typeName::from($value);
138138
}
139139

140-
if (is_subclass_of($typeName, self::class)) {
140+
if (is_subclass_of($typeName, self::class) && is_array($value)) {
141141
return $typeName::fromArray($value);
142142
}
143143

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\MediaAttachmentPayload;
9+
10+
final readonly class AudioAttachment extends AbstractAttachment
11+
{
12+
/**
13+
* @param MediaAttachmentPayload $payload Audio attachment payload.
14+
* @param string|null $transcription Audio transcription.
15+
*/
16+
public function __construct(
17+
public MediaAttachmentPayload $payload,
18+
public ?string $transcription,
19+
) {
20+
parent::__construct(AttachmentType::Audio);
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\ContactAttachmentPayload;
9+
10+
final readonly class ContactAttachment extends AbstractAttachment
11+
{
12+
/**
13+
* @param ContactAttachmentPayload $payload Contact attachment payload.
14+
*/
15+
public function __construct(public ContactAttachmentPayload $payload)
16+
{
17+
parent::__construct(AttachmentType::Contact);
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\FileAttachmentPayload;
9+
10+
final readonly class FileAttachment extends AbstractAttachment
11+
{
12+
/**
13+
* @param FileAttachmentPayload $payload File attachment payload.
14+
* @param string $filename Uploaded file name.
15+
* @param int $size File size in bytes.
16+
*/
17+
public function __construct(
18+
public FileAttachmentPayload $payload,
19+
public string $filename,
20+
public int $size,
21+
) {
22+
parent::__construct(AttachmentType::File);
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\KeyboardPayload;
9+
10+
final readonly class InlineKeyboardAttachment extends AbstractAttachment
11+
{
12+
/**
13+
* @param KeyboardPayload $payload Keyboard payload.
14+
*/
15+
public function __construct(public KeyboardPayload $payload) {
16+
parent::__construct(AttachmentType::InlineKeyboard);
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments;
6+
7+
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
8+
9+
final readonly class LocationAttachment extends AbstractAttachment
10+
{
11+
/**
12+
* @param float $latitude
13+
* @param float $longitude
14+
*/
15+
public function __construct(
16+
public float $latitude,
17+
public float $longitude,
18+
) {
19+
parent::__construct(AttachmentType::Location);
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads;
6+
7+
use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
8+
use BushlanovDev\MaxMessengerBot\Models\User;
9+
10+
/**
11+
* Payload of a contact attachment.
12+
*/
13+
final readonly class ContactAttachmentPayload extends AbstractModel
14+
{
15+
/**
16+
* @param string|null $vcfInfo User info in VCF format.
17+
* @param User|null $maxInfo User info if the contact is a Max user.
18+
*/
19+
public function __construct(
20+
public ?string $vcfInfo,
21+
public ?User $maxInfo,
22+
) {
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads;
6+
7+
use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
8+
9+
/**
10+
* Payload for a file attachment.
11+
*/
12+
final readonly class FileAttachmentPayload extends AbstractModel
13+
{
14+
/**
15+
* @param string $url Media attachment URL.
16+
* @param string $token Token to reuse the same attachment in other messages.
17+
*/
18+
public function __construct(
19+
public string $url,
20+
public string $token,
21+
) {
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads;
6+
7+
use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Buttons\Inline\AbstractInlineButton;
9+
10+
/**
11+
* Represents an inline keyboard structure.
12+
*/
13+
final readonly class KeyboardPayload extends AbstractModel
14+
{
15+
/**
16+
* @param AbstractInlineButton[][] $buttons Two-dimensional array of buttons.
17+
*/
18+
public function __construct(public array $buttons)
19+
{
20+
}
21+
}

0 commit comments

Comments
 (0)