From 7b2a3eff8e6b32cd016c035900c2f203d55cd577 Mon Sep 17 00:00:00 2001 From: abbasudo Date: Thu, 19 Oct 2023 21:13:10 +0000 Subject: [PATCH 1/8] Fix styling --- src/TelegramFile.php | 4 ++-- src/Traits/HasSharedLogic.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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..862beb7 100644 --- a/src/Traits/HasSharedLogic.php +++ b/src/Traits/HasSharedLogic.php @@ -99,7 +99,7 @@ public function token(string $token): self */ public function hasToken(): bool { - return null !== $this->token; + return $this->token !== null; } /** From 8871e097e62899c6e7cc32023ce8ce4e2927a629 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 20 Oct 2023 01:22:58 +0330 Subject: [PATCH 2/8] add keyboard function --- src/Traits/HasSharedLogic.php | 45 +++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/Traits/HasSharedLogic.php b/src/Traits/HasSharedLogic.php index 862beb7..ca2a383 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,40 @@ public function to(int|string $chatId): self return $this; } + /** + * sets reply markup for payload + * + * @param array $markup + * + * @return static + * @throws \JsonException + */ + public function reply(array $markup): self + { + $this->payload['reply_markup'] = json_encode($markup,JSON_THROW_ON_ERROR); + + return $this; + } + + /** + * Add an inline button with callback_data. + * + * @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->reply([ + '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 +80,9 @@ public function button(string $text, string $url, int $columns = 2): self { $this->buttons[] = compact('text', 'url'); - $this->payload['reply_markup'] = json_encode([ + $this->reply([ 'inline_keyboard' => array_chunk($this->buttons, $columns), - ], JSON_THROW_ON_ERROR); + ]); return $this; } @@ -61,9 +98,9 @@ public function buttonWithCallback(string $text, string $callback_data, int $col { $this->buttons[] = compact('text', 'callback_data'); - $this->payload['reply_markup'] = json_encode([ + $this->reply([ 'inline_keyboard' => array_chunk($this->buttons, $columns), - ], JSON_THROW_ON_ERROR); + ]); return $this; } From cbd7dee79fa64ebdbe013b43a652f5e876353887 Mon Sep 17 00:00:00 2001 From: abbasudo Date: Thu, 19 Oct 2023 21:53:32 +0000 Subject: [PATCH 3/8] Fix styling --- src/Traits/HasSharedLogic.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Traits/HasSharedLogic.php b/src/Traits/HasSharedLogic.php index ca2a383..d4e1553 100644 --- a/src/Traits/HasSharedLogic.php +++ b/src/Traits/HasSharedLogic.php @@ -38,14 +38,14 @@ public function to(int|string $chatId): self /** * sets reply markup for payload * - * @param array $markup * * @return static + * * @throws \JsonException */ public function reply(array $markup): self { - $this->payload['reply_markup'] = json_encode($markup,JSON_THROW_ON_ERROR); + $this->payload['reply_markup'] = json_encode($markup, JSON_THROW_ON_ERROR); return $this; } @@ -57,7 +57,8 @@ public function reply(array $markup): self * * @throws \JsonException */ - public function keyboard(string $text, int $columns = 2, bool $request_contact = false, bool $request_location = false): self { + 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->reply([ From 5dbe7c2ef62526b8bd20b3d596abe995a50d4a71 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 20 Oct 2023 02:05:13 +0330 Subject: [PATCH 4/8] rename function --- src/Traits/HasSharedLogic.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Traits/HasSharedLogic.php b/src/Traits/HasSharedLogic.php index ca2a383..6cd54c3 100644 --- a/src/Traits/HasSharedLogic.php +++ b/src/Traits/HasSharedLogic.php @@ -43,7 +43,7 @@ public function to(int|string $chatId): self * @return static * @throws \JsonException */ - public function reply(array $markup): self + public function keyboardMarkup(array $markup): self { $this->payload['reply_markup'] = json_encode($markup,JSON_THROW_ON_ERROR); @@ -51,7 +51,7 @@ public function reply(array $markup): self } /** - * Add an inline button with callback_data. + * Add a normal keyboard button. * * @return static * @@ -60,7 +60,7 @@ public function reply(array $markup): self 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->reply([ + $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 @@ -80,7 +80,7 @@ public function button(string $text, string $url, int $columns = 2): self { $this->buttons[] = compact('text', 'url'); - $this->reply([ + $this->keyboardMarkup([ 'inline_keyboard' => array_chunk($this->buttons, $columns), ]); @@ -98,7 +98,7 @@ public function buttonWithCallback(string $text, string $callback_data, int $col { $this->buttons[] = compact('text', 'callback_data'); - $this->reply([ + $this->keyboardMarkup([ 'inline_keyboard' => array_chunk($this->buttons, $columns), ]); From 4a8e9502fc811cab9488aa1a48aea3ce79f4914c Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 20 Oct 2023 02:21:25 +0330 Subject: [PATCH 5/8] Update README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 8249285..f9bbf82 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', request_contact: true) + ->keyboard('Button 2', request_location: true); + // ->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 From 18394f3d6a460dc28caabc798fcb60ddb7a9d547 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 20 Oct 2023 02:22:37 +0330 Subject: [PATCH 6/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f9bbf82..9f10f25 100644 --- a/README.md +++ b/README.md @@ -181,8 +181,8 @@ public function toTelegram($notifiable) return TelegramPoll::create() ->to($notifiable) ->content('Choose an option:') - ->keyboard('Button 1', request_contact: true) - ->keyboard('Button 2', request_location: true); + ->keyboard('Button 1') + ->keyboard('Button 2'); // ->keyboard('send your number', request_contact: true) // ->keyboard('send your location', request_location: true); } From 74e1794a685ea0fc9396826abf8fc33fcb853eb8 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 20 Oct 2023 02:36:56 +0330 Subject: [PATCH 7/8] add keyboard tests --- tests/Feature/TelegramMessageTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Feature/TelegramMessageTest.php b/tests/Feature/TelegramMessageTest.php index cf8ff54..afb8804 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('an 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('an 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('an 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}' + ); +}); From 5102df4cc139b97f181cfdbcd90388abb650f76c Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 20 Oct 2023 02:40:14 +0330 Subject: [PATCH 8/8] fix typo --- tests/Feature/TelegramMessageTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/TelegramMessageTest.php b/tests/Feature/TelegramMessageTest.php index afb8804..e1be4eb 100644 --- a/tests/Feature/TelegramMessageTest.php +++ b/tests/Feature/TelegramMessageTest.php @@ -139,21 +139,21 @@ expect($message->getPayloadValue('disable_web_page_preview'))->toBeTrue(); }); -test('an normal keyboard button can be added to the message', function () { +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('an request phone keyboard button can be added to the message', function () { +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('an request location keyboard button can be added to the message', function () { +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}'