Skip to content

Commit

Permalink
Merge pull request #369 from defstudio/#368_implementig_with_data_method
Browse files Browse the repository at this point in the history
#368 implementing withData() method
  • Loading branch information
fabio-ivona authored May 15, 2023
2 parents c1b806d + 7d8b176 commit 71b6b1f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/content/en/features/attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ position: 36

Telegraph supports different types of attachments both from local files, remote urls and existing files on Telegram servers (using their file_id)

## Optional parameters

Attachments methods only supports required parameters, optional parameters can be sent through Telegraph `->withData()` method:

```php
Telegraph::message('hi')->withData('caption', 'test')->send();
Telegraph::withData('caption', 'test')->message('hi')->send();
```

## Attachment types

### Photos
Expand Down
9 changes: 9 additions & 0 deletions docs/content/en/features/telegram-api-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,12 @@ print a `dd()` of the current api call status for testing purposes
```php
Telegraph::message('test')->dd();
```


## withData

set custom Telegraph data attribute

```php
Telegraph::withData('key', 'value');
```
9 changes: 9 additions & 0 deletions docs/content/en/models/telegraph-chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ Retrieves the chat member count from telegram
$telegraphChat->info(); /* 42 */
```

## Optional parameters

Attachments methods only supports required parameters, optional parameters can be sent through Telegraph `->withData()` method:

```php
$telegraphChat->message('hi')->withData('caption', 'test')->send();
$telegraphChat->withData('caption', 'test')->message('hi')->send();
```

### `message()`

Starts a `Telegraph` call to send a message
Expand Down
1 change: 1 addition & 0 deletions src/Facades/Telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @method static \DefStudio\Telegraph\Telegraph bot(TelegraphBot|string $bot)
* @method static \DefStudio\Telegraph\Telegraph chat(TelegraphChat|string $chat)
* @method static \DefStudio\Telegraph\Telegraph message(string $message)
* @method static \DefStudio\Telegraph\Telegraph withData(string $key, mixed $value)
* @method static \DefStudio\Telegraph\Telegraph html(string $message)
* @method static \DefStudio\Telegraph\Telegraph reply(int $messageId)
* @method static \DefStudio\Telegraph\Telegraph edit(string $messageId)
Expand Down
5 changes: 5 additions & 0 deletions src/Models/TelegraphChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public function memberCount(): int
return $reply->json('result');
}

public function withData(string $key, mixed $value): Telegraph
{
return TelegraphFacade::chat($this)->withData($key, $value);
}

public function message(string $message): Telegraph
{
return TelegraphFacade::chat($this)->message($message);
Expand Down
9 changes: 9 additions & 0 deletions src/Telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,13 @@ public function dump(): Telegraph

return $this;
}

public function withData(string $key, mixed $value): static
{
$telegraph = clone $this;

data_set($telegraph->data, $key, $value);

return $telegraph;
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Models/TelegraphChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,27 @@
],
]);
});

it('can edit Telegraph data before sending a media ', function () {
Telegraph::fake();
$chat = make_chat();

$chat->withData('caption', 'test')->video('test.url')->send();

Telegraph::assertSentData(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_VIDEO, [
'video' => 'test.url',
'caption' => 'test',
]);
});

it('can edit Telegraph data after sending a media ', function () {
Telegraph::fake();
$chat = make_chat();

$chat->video('test.url')->withData('caption', 'test')->send();

Telegraph::assertSentData(\DefStudio\Telegraph\Telegraph::ENDPOINT_SEND_VIDEO, [
'video' => 'test.url',
'caption' => 'test',
]);
});

0 comments on commit 71b6b1f

Please sign in to comment.