Skip to content

Commit

Permalink
Merge pull request defstudio#598 from ivankuraev/main
Browse files Browse the repository at this point in the history
Implements sendSticker method
  • Loading branch information
fabio-ivona authored Jun 28, 2024
2 parents 6511d73 + 529b4aa commit fd1aba5
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,8 @@
'document' => [
'max_size_mb' => 50,
],
'sticker' => [
'max_size_mb' => 50,
],
],
];
12 changes: 12 additions & 0 deletions docs/12.features/7.attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ Different items can be used as "dice"
Telegraph::dice(\DefStudio\Telegraph\Enums\Emojis::SLOT_MACHINE)->send();
```

### Sticker

Stickers can be sent through Telegraph `->sticker()` method:

```php
Telegraph::sticker(Storage::path('my_sticker.tgs'))->send();
Telegraph::sticker('https://my-repository/my_sticker.tgs')->send();
Telegraph::sticker($telegramFileId)->send();
```

Where `$telegramFileId` is file_id from telegram sticker set. File_id can obtain from Telegram Raw Bot (@RawDataBot). Just simply send a sticker to bot and you receive json data in answer. The required value is contained in 'message > sticker > file_id'.

## Options

When sending files, some options are available:
Expand Down
10 changes: 10 additions & 0 deletions docs/13.models/2.telegraph-chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ An audio attachment can be sent through Telegraph `->audio()` method:
$telegraphChat->audio()->send();
```

### `sticker`

A sticker attachment can be sent through Telegraph `->sticker()` method:

```php
/** @var DefStudio\Telegraph\Models\TelegraphChat $telegraphChat */
$telegraphChat->sticker()->send();
```


### `menuButton`

Retrieves chat menu button
Expand Down
29 changes: 29 additions & 0 deletions src/Concerns/SendsAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ public function dice(string $emoji = null): self
return $telegraph;
}

public function sticker(string $path, string $filename = null): self
{
$telegraph = clone $this;

$telegraph->endpoint = self::ENDPOINT_SEND_STICKER;
$telegraph->data['chat_id'] = $telegraph->getChatId();

$this->attachSticker($telegraph, $path, $filename);

return $telegraph;
}

protected function attachPhoto(self $telegraph, string $path, ?string $filename): void
{
if (File::exists($path)) {
Expand Down Expand Up @@ -427,4 +439,21 @@ protected function attachDocument(self $telegraph, string $path, ?string $filena
$telegraph->data['caption'] ??= '';
}
}

protected function attachSticker(self $telegraph, string $path, ?string $filename): void
{
if (File::exists($path)) {
/* @phpstan-ignore-next-line */
$maxSizeMb = floatval(config('telegraph.attachments.sticker.max_size_mb', 50));

if (($size = $telegraph->fileSizeInMb($path)) > $maxSizeMb) {
throw FileException::documentSizeExceeded($size, $maxSizeMb);
}

$telegraph->files->put('sticker', new Attachment($path, $filename));
} else {
$telegraph->data['sticker'] = $path;
$telegraph->data['emoji'] ??= '';
}
}
}
5 changes: 5 additions & 0 deletions src/Models/TelegraphChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public function mediaGroup(array $media): Telegraph
return TelegraphFacade::chat($this)->mediaGroup($media);
}

public function sticker(string $path, string $filename = null): Telegraph
{
return TelegraphFacade::chat($this)->sticker($path, $filename);
}

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 @@ -104,6 +104,7 @@ class Telegraph
public const ENDPOINT_SET_CHAT_MENU_BUTTON = 'setChatMenuButton';
public const ENDPOINT_GET_CHAT_MENU_BUTTON = 'getChatMenuButton';
public const ENDPOINT_DICE = 'sendDice';
public const ENDPOINT_SEND_STICKER = 'sendSticker';


/** @var array<string, mixed> */
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Concerns/SendsAttachmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
'SLOT_MACHINE' => Emojis::SLOT_MACHINE,
]);

it('can send a sticker with own .tgs file', function () {
expect(fn (Telegraph $telegraph) => $telegraph->sticker(Storage::path('sticker.tgs')))
->toMatchUtf8TelegramSnapshot();
});

it('can send a sticker with telegram sticker set file_id', function (string $file_id) {
expect(fn (Telegraph $telegraph) => $telegraph->sticker($file_id))
->toMatchUtf8TelegramSnapshot();
})->with([
'hourglass' => 'CAACAgEAAxkBAAEr3Y1mZFR5Gf4X5m0CLLNUbpzwuPhcFQACLQIAAqcjIUQ9QDDJ7YO0tjUE',
]);

it('requires a chat to send a document', function () {
TelegraphFacade::document(Storage::path('test.txt'));
})->throws(TelegraphException::class, 'No TelegraphChat defined for this request');
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/Models/TelegraphChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,42 @@
]);
});

it('can send a sticker', function () {
Telegraph::fake();
$chat = make_chat();

$chat->sticker(Storage::path('sticker.tgs'))->send();

Telegraph::assertSentFiles(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_STICKER, [
'sticker' => new Attachment(Storage::path('sticker.tgs'), 'sticker.tgs'),
]);
});

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

$chat->sticker('https://test.dev/sticker.tgs')->send();

Telegraph::assertSentData(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_STICKER, [
'sticker' => 'https://test.dev/sticker.tgs',
]);
});

it('can send a sticker from file_id', function () {
Telegraph::fake();
$chat = make_chat();

$uuid = Str::uuid();

$chat->sticker($uuid)->send();

Telegraph::assertSentData(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_STICKER, [
'sticker' => $uuid,
]);
});


it('can send a voice', function () {
Telegraph::fake();
$chat = make_chat();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"url":"https:\/\/api.telegram.org\/bot3f3814e1-5836-3d77-904e-60f64b15df36\/sendSticker","payload":{"chat_id":"-123456789","sticker":"CAACAgEAAxkBAAEr3Y1mZFR5Gf4X5m0CLLNUbpzwuPhcFQACLQIAAqcjIUQ9QDDJ7YO0tjUE","emoji":""},"files":[]}
Binary file added tests/storage/sticker.tgs
Binary file not shown.

0 comments on commit fd1aba5

Please sign in to comment.