Skip to content

Commit

Permalink
update docs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Aug 26, 2024
1 parent fd3ef88 commit 02f004a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/12.features/4.reply-keyboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ $keyboard = ReplyKeyboard::make()
->selective();
```

## Setting a keyboard as persistent

A keyboard can be set as always visible when the regular keyboard is hidden (more info in telegram [docs](https://core.telegram.org/bots/api#replykeyboardmarkup)).

```php
use DefStudio\Telegraph\Keyboard\ReplyKeyboard;

$keyboard = ReplyKeyboard::make()
->button('Send Contact')->requestContact()
->button('Send Location')->requestLocation()
->persistent();
```

## Removing a keyboard

A reply keyboard can be removed from clients with this call:
Expand Down
8 changes: 4 additions & 4 deletions src/Keyboard/ReplyKeyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReplyKeyboard implements Arrayable
protected Collection $buttons;

protected bool $rtl = false;
protected bool $isPersistent = false;
protected bool $persistent = false;
protected bool $resize = false;
protected bool $oneTime = false;
protected bool $selective = false;
Expand Down Expand Up @@ -97,11 +97,11 @@ public static function fromArray(array $arrayKeyboard): self
return $keyboard;
}

public function isPersistent(bool $isPersistent = true): self
public function persistent(bool $isPersistent = true): self
{
$clone = $this->clone();

$clone->isPersistent = $isPersistent;
$clone->persistent = $isPersistent;

return $clone;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public function toArray(): array
public function options(): array
{
return array_filter([
'is_persistent' => $this->isPersistent,
'is_persistent' => $this->persistent,
'resize_keyboard' => $this->resize,
'one_time_keyboard' => $this->oneTime,
'selective' => $this->selective,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"url": "https:\/\/api.telegram.org\/bot3f3814e1-5836-3d77-904e-60f64b15df36\/sendMessage",
"payload": {
"text": "foobar",
"chat_id": "-123456789",
"parse_mode": "html",
"reply_markup": {
"keyboard": [
[
{
"text": "foo",
"request_contact": true
}
]
],
"is_persistent": true
}
},
"files": []
}
7 changes: 7 additions & 0 deletions tests/Unit/Concerns/ManagesKeyboardsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@
})->toMatchTelegramSnapshot();
});

it('can attach a persistent reply keyboard', function () {
expect(function (Telegraph $telegraph) {
return $telegraph->html('foobar')
->replyKeyboard(fn (ReplyKeyboard $keyboard) => $keyboard->button('foo')->requestContact()->persistent());
})->toMatchTelegramSnapshot();
});

it('can force a reply', function () {
expect(function (Telegraph $telegraph) {
return $telegraph->message('foobar')->forceReply();
Expand Down

0 comments on commit 02f004a

Please sign in to comment.