|
5 | 5 | namespace BushlanovDev\MaxMessengerBot;
|
6 | 6 |
|
7 | 7 | use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
|
| 8 | +use BushlanovDev\MaxMessengerBot\Enums\InlineButtonType; |
8 | 9 | use BushlanovDev\MaxMessengerBot\Enums\MarkupType;
|
| 10 | +use BushlanovDev\MaxMessengerBot\Enums\ReplyButtonType; |
9 | 11 | use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
10 | 12 | 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; |
11 | 25 | 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; |
12 | 31 | use BushlanovDev\MaxMessengerBot\Models\Attachments\ShareAttachment;
|
| 32 | +use BushlanovDev\MaxMessengerBot\Models\Attachments\StickerAttachment; |
| 33 | +use BushlanovDev\MaxMessengerBot\Models\Attachments\VideoAttachment; |
13 | 34 | use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
14 | 35 | use BushlanovDev\MaxMessengerBot\Models\Chat;
|
15 | 36 | use BushlanovDev\MaxMessengerBot\Models\ChatList;
|
@@ -142,15 +163,80 @@ public function createMessage(array $data): Message
|
142 | 163 | */
|
143 | 164 | public function createAttachment(array $data): AbstractAttachment
|
144 | 165 | {
|
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) { |
146 | 184 | AttachmentType::Data => DataAttachment::fromArray($data),
|
147 | 185 | 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), |
148 | 214 | default => throw new LogicException(
|
149 |
| - 'Unknown or unsupported Attachment type: ' . ($data['type'] ?? 'none') |
| 215 | + 'Unknown or unsupported reply button type: ' . ($data['type'] ?? 'none') |
150 | 216 | ),
|
151 | 217 | };
|
152 | 218 | }
|
153 | 219 |
|
| 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 | + |
154 | 240 | /**
|
155 | 241 | * List of messages.
|
156 | 242 | *
|
|
0 commit comments