Skip to content

Commit

Permalink
Merge pull request #599 from defstudio/#498-implement-sendMediaGroup-…
Browse files Browse the repository at this point in the history
…method

#498 implement sendMediaGroup method
  • Loading branch information
fabio-ivona authored Jun 26, 2024
2 parents 51a1918 + 087fb19 commit 3106ed7
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/12.features/7.attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,20 @@ Telegraph::document(Storage::path('brochure.pdf'))
->thumbnail(Storage::path('brochure_thumbnail.jpg'))
->send();
```

### Media Group

Group of photos, videos, documents or audios as an album can be sent through Telegraph `->mediaGroup()` method:

```php
Telegraph::mediaGroup([
[
'type' => 'photo',
'media' => 'https://my-repository/photo1.jpg',
],
[
'type' => 'photo',
'media' => 'https://my-repository/photo2.jpg',
]
])->send();
```
17 changes: 17 additions & 0 deletions docs/12.features/8.telegram-api-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ sends a photo
Telegraph::photo($pathToPhotoFile)->send();
```

### Media Group

sends a group of photos, videos, documents or audios as an album

```php
Telegraph::mediaGroup([
[
'type' => 'photo',
'media' => 'https://my-repository/photo1.jpg',
],
[
'type' => 'photo',
'media' => 'https://my-repository/photo2.jpg',
]
])->send();
```

## registerBotCommands

register commands in Telegram Bot in order to display them to the user when the "/" key is pressed
Expand Down
17 changes: 17 additions & 0 deletions docs/13.models/2.telegraph-chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,20 @@ $telegraphChat->setMenuButton()->default()->send(); //restore default
$telegraphChat->setMenuButton()->commands()->send(); //show bot commands in menu button
$telegraphChat->setMenuButton()->webApp("Web App", "https://my-web.app")->send(); //show start web app button
```

### `Media Group`

Group of photos, videos, documents or audios as an album can be sent through Telegraph `->mediaGroup()` method:

```php
$telegraphChat->mediaGroup([
[
'type' => 'photo',
'media' => 'https://my-repository/photo1.jpg',
],
[
'type' => 'photo',
'media' => 'https://my-repository/photo2.jpg',
]
])->send();
```
22 changes: 19 additions & 3 deletions src/Concerns/SendsAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function thumbnail(string $path): self
$telegraph = clone $this;

if (File::exists($path)) {
/* @phpstan-ignore-next-line */
/* @phpstan-ignore-next-line */
$maxSizeKb = floatval(config('telegraph.attachments.thumbnail.max_size_kb', 200));

if (($size = $telegraph->fileSizeInKb($path)) > $maxSizeKb) {
Expand Down Expand Up @@ -236,6 +236,22 @@ public function photo(string $path, string $filename = null): self
return $telegraph;
}

/**
* @param array<int|string, array<mixed>> $mediaInputs
*/
public function mediaGroup(array $mediaInputs): self
{
$telegraph = clone $this;

$telegraph->endpoint = self::ENDPOINT_SEND_MEDIA_GROUP;

$telegraph->data['chat_id'] = $telegraph->getChatId();

$telegraph->data['media'] = $mediaInputs;

return $telegraph;
}

private function imageHeight(string $path): int
{
return $this->imageDimensions($path)[1];
Expand Down Expand Up @@ -374,7 +390,7 @@ protected function attachVideo(self $telegraph, string $path, ?string $filename)
protected function attachAudio(self $telegraph, string $path, ?string $filename): void
{
if (File::exists($path)) {
/* @phpstan-ignore-next-line */
/* @phpstan-ignore-next-line */
$maxSizeMb = floatval(config('telegraph.attachments.audio.max_size_mb', 50));

if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) {
Expand All @@ -398,7 +414,7 @@ protected function attachAudio(self $telegraph, string $path, ?string $filename)
protected function attachDocument(self $telegraph, string $path, ?string $filename): void
{
if (File::exists($path)) {
/* @phpstan-ignore-next-line */
/* @phpstan-ignore-next-line */
$maxSizeMb = floatval(config('telegraph.attachments.document.max_size_mb', 50));

if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) {
Expand Down
1 change: 1 addition & 0 deletions src/Facades/Telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @method static \DefStudio\Telegraph\Telegraph video(string $path, string $filename = null)
* @method static \DefStudio\Telegraph\Telegraph audio(string $path, string $filename = null)
* @method static \DefStudio\Telegraph\Telegraph dice()
* @method static \DefStudio\Telegraph\Telegraph mediaGroup(string $path, array $media)
* @method static \DefStudio\Telegraph\Telegraph botUpdates()
* @method static \DefStudio\Telegraph\Telegraph botInfo()
* @method static \DefStudio\Telegraph\Telegraph setBaseUrl(string|null $url)
Expand Down
8 changes: 8 additions & 0 deletions src/Models/TelegraphChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ public function photo(string $path, string $filename = null): Telegraph
return TelegraphFacade::chat($this)->photo($path, $filename);
}

/**
* @param array<int|string, array<mixed>> $media
*/
public function mediaGroup(array $media): Telegraph
{
return TelegraphFacade::chat($this)->mediaGroup($media);
}

public function animation(string $path, string $filename = null): Telegraph
{
return TelegraphFacade::chat($this)->animation($path, $filename);
Expand Down
1 change: 1 addition & 0 deletions src/Telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Telegraph
public const ENDPOINT_SEND_LOCATION = 'sendLocation';
public const ENDPOINT_SEND_ANIMATION = 'sendAnimation';
public const ENDPOINT_SEND_VOICE = 'sendVoice';
public const ENDPOINT_SEND_MEDIA_GROUP = 'sendMediaGroup';
public const ENDPOINT_SEND_CHAT_ACTION = 'sendChatAction';
public const ENDPOINT_SEND_DOCUMENT = 'sendDocument';
public const ENDPOINT_SEND_PHOTO = 'sendPhoto';
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Models/TelegraphChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,32 @@
'caption' => 'test',
]);
});

it('can send a mediaGroup from remote url', function () {
Telegraph::fake();
$chat = make_chat();

$chat->mediaGroup([
[
'type' => 'photo',
'media' => 'https://test.dev/photo.jpg',
],
[
'type' => 'photo',
'media' => 'https://test.dev/photo.jpg',
],
])->send();

Telegraph::assertSentData(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_MEDIA_GROUP, [
'media' => [
[
'type' => 'photo',
'media' => 'https://test.dev/photo.jpg',
],
[
'type' => 'photo',
'media' => 'https://test.dev/photo.jpg',
],
],
]);
});

0 comments on commit 3106ed7

Please sign in to comment.