diff --git a/README.md b/README.md index 8249285..9f10f25 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/TelegramFile.php b/src/TelegramFile.php index 31ba2bc..47acca4 100644 --- a/src/TelegramFile.php +++ b/src/TelegramFile.php @@ -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) { $this->payload['file']['filename'] = $filename; } @@ -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; diff --git a/src/Traits/HasSharedLogic.php b/src/Traits/HasSharedLogic.php index d5971dc..1adef5c 100644 --- a/src/Traits/HasSharedLogic.php +++ b/src/Traits/HasSharedLogic.php @@ -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 = []; @@ -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. * @@ -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; } @@ -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; } @@ -99,7 +137,7 @@ public function token(string $token): self */ public function hasToken(): bool { - return null !== $this->token; + return $this->token !== null; } /** diff --git a/tests/Feature/TelegramMessageTest.php b/tests/Feature/TelegramMessageTest.php index cf8ff54..e1be4eb 100644 --- a/tests/Feature/TelegramMessageTest.php +++ b/tests/Feature/TelegramMessageTest.php @@ -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}' + ); +});