Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard function to messages #183

Merged
merged 9 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ class InvoicePaid extends Notification
Here's a screenshot preview of the above notification on Telegram Messenger:

![Laravel Telegram Notification Example](https://user-images.githubusercontent.com/1915268/66616627-39be6180-ebef-11e9-92cc-f2da81da047a.jpg)
### Send with Keyboard
```php
public function toTelegram($notifiable)
{
return TelegramPoll::create()
->to($notifiable)
->content('Choose an option:')
->keyboard('Button 1')
->keyboard('Button 2');
// ->keyboard('send your number', request_contact: true)
// ->keyboard('send your location', request_location: true);
}
```

Preview:

![Laravel Telegram Notification Keyboard](https://github.com/abbasudo/telegram/assets/86796762/9c10c7d0-740b-4270-bc7c-f0600e57ba7b)

### Send a Poll

Expand Down
4 changes: 2 additions & 2 deletions src/TelegramFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function file(mixed $file, string $type, string $filename = null): self
'contents' => is_resource($file) ? $file : fopen($file, 'rb'),
];

if (null !== $filename) {
if ($filename !== null) {
abbasudo marked this conversation as resolved.
Show resolved Hide resolved
$this->payload['file']['filename'] = $filename;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ public function toMultipart(): array
{
$data = [];
foreach ($this->payload as $name => $contents) {
$data[] = ('file' === $name) ? $contents : compact('name', 'contents');
$data[] = ($name === 'file') ? $contents : compact('name', 'contents');
}

return $data;
Expand Down
48 changes: 43 additions & 5 deletions src/Traits/HasSharedLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ trait HasSharedLogic
/** @var array Params payload. */
protected array $payload = [];

/** @var array Keyboard Buttons. */
protected array $keyboards = [];

/** @var array Inline Keyboard Buttons. */
protected array $buttons = [];

Expand All @@ -32,6 +35,41 @@ public function to(int|string $chatId): self
return $this;
}

/**
* sets reply markup for payload
*
*
* @return static
*
* @throws \JsonException
*/
public function keyboardMarkup(array $markup): self
{
$this->payload['reply_markup'] = json_encode($markup, JSON_THROW_ON_ERROR);

return $this;
}

/**
* Add a normal keyboard button.
*
* @return static
*
* @throws \JsonException
*/
public function keyboard(string $text, int $columns = 2, bool $request_contact = false, bool $request_location = false): self
{
$this->keyboards[] = compact('text', 'request_contact', 'request_location');

$this->keyboardMarkup([
'keyboard' => array_chunk($this->keyboards, $columns),
'one_time_keyboard' => true, // Hide the keyboard after the user makes a selection
'resize_keyboard' => true, // Allow the keyboard to be resized
]);

return $this;
}

/**
* Add an inline button.
*
Expand All @@ -43,9 +81,9 @@ public function button(string $text, string $url, int $columns = 2): self
{
$this->buttons[] = compact('text', 'url');

$this->payload['reply_markup'] = json_encode([
$this->keyboardMarkup([
'inline_keyboard' => array_chunk($this->buttons, $columns),
], JSON_THROW_ON_ERROR);
]);

return $this;
}
Expand All @@ -61,9 +99,9 @@ public function buttonWithCallback(string $text, string $callback_data, int $col
{
$this->buttons[] = compact('text', 'callback_data');

$this->payload['reply_markup'] = json_encode([
$this->keyboardMarkup([
'inline_keyboard' => array_chunk($this->buttons, $columns),
], JSON_THROW_ON_ERROR);
]);

return $this;
}
Expand Down Expand Up @@ -99,7 +137,7 @@ public function token(string $token): self
*/
public function hasToken(): bool
{
return null !== $this->token;
return $this->token !== null;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/TelegramMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,24 @@
$message = TelegramMessage::create()->options(['disable_web_page_preview' => true]);
expect($message->getPayloadValue('disable_web_page_preview'))->toBeTrue();
});

test('a normal keyboard button can be added to the message', function () {
$message = TelegramMessage::create()->keyboard('Laravel');
expect($message->getPayloadValue('reply_markup'))->toEqual(
'{"keyboard":[[{"text":"Laravel","request_contact":false,"request_location":false}]],"one_time_keyboard":true,"resize_keyboard":true}'
);
});

test('a request phone keyboard button can be added to the message', function () {
$message = TelegramMessage::create()->keyboard('Laravel', request_contact: true);
expect($message->getPayloadValue('reply_markup'))->toEqual(
'{"keyboard":[[{"text":"Laravel","request_contact":true,"request_location":false}]],"one_time_keyboard":true,"resize_keyboard":true}'
);
});

test('a request location keyboard button can be added to the message', function () {
$message = TelegramMessage::create()->keyboard('Laravel', request_location: true);
expect($message->getPayloadValue('reply_markup'))->toEqual(
'{"keyboard":[[{"text":"Laravel","request_contact":false,"request_location":true}]],"one_time_keyboard":true,"resize_keyboard":true}'
);
});