Skip to content

Commit 7134469

Browse files
authored
Add keyboard function to messages (#183)
* Fix styling * add keyboard function * Fix styling * rename function * Update README.md * Update README.md * add keyboard tests * fix typo --------- Co-authored-by: abbasudo <[email protected]>
1 parent 5c9fc73 commit 7134469

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ class InvoicePaid extends Notification
174174
Here's a screenshot preview of the above notification on Telegram Messenger:
175175

176176
![Laravel Telegram Notification Example](https://user-images.githubusercontent.com/1915268/66616627-39be6180-ebef-11e9-92cc-f2da81da047a.jpg)
177+
### Send with Keyboard
178+
```php
179+
public function toTelegram($notifiable)
180+
{
181+
return TelegramPoll::create()
182+
->to($notifiable)
183+
->content('Choose an option:')
184+
->keyboard('Button 1')
185+
->keyboard('Button 2');
186+
// ->keyboard('send your number', request_contact: true)
187+
// ->keyboard('send your location', request_location: true);
188+
}
189+
```
190+
191+
Preview:
192+
193+
![Laravel Telegram Notification Keyboard](https://github.com/abbasudo/telegram/assets/86796762/9c10c7d0-740b-4270-bc7c-f0600e57ba7b)
177194

178195
### Send a Poll
179196

src/TelegramFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function file(mixed $file, string $type, string $filename = null): self
6363
'contents' => is_resource($file) ? $file : fopen($file, 'rb'),
6464
];
6565

66-
if (null !== $filename) {
66+
if ($filename !== null) {
6767
$this->payload['file']['filename'] = $filename;
6868
}
6969

@@ -191,7 +191,7 @@ public function toMultipart(): array
191191
{
192192
$data = [];
193193
foreach ($this->payload as $name => $contents) {
194-
$data[] = ('file' === $name) ? $contents : compact('name', 'contents');
194+
$data[] = ($name === 'file') ? $contents : compact('name', 'contents');
195195
}
196196

197197
return $data;

src/Traits/HasSharedLogic.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ trait HasSharedLogic
1717
/** @var array Params payload. */
1818
protected array $payload = [];
1919

20+
/** @var array Keyboard Buttons. */
21+
protected array $keyboards = [];
22+
2023
/** @var array Inline Keyboard Buttons. */
2124
protected array $buttons = [];
2225

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

38+
/**
39+
* sets reply markup for payload
40+
*
41+
*
42+
* @return static
43+
*
44+
* @throws \JsonException
45+
*/
46+
public function keyboardMarkup(array $markup): self
47+
{
48+
$this->payload['reply_markup'] = json_encode($markup, JSON_THROW_ON_ERROR);
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* Add a normal keyboard button.
55+
*
56+
* @return static
57+
*
58+
* @throws \JsonException
59+
*/
60+
public function keyboard(string $text, int $columns = 2, bool $request_contact = false, bool $request_location = false): self
61+
{
62+
$this->keyboards[] = compact('text', 'request_contact', 'request_location');
63+
64+
$this->keyboardMarkup([
65+
'keyboard' => array_chunk($this->keyboards, $columns),
66+
'one_time_keyboard' => true, // Hide the keyboard after the user makes a selection
67+
'resize_keyboard' => true, // Allow the keyboard to be resized
68+
]);
69+
70+
return $this;
71+
}
72+
3573
/**
3674
* Add an inline button.
3775
*
@@ -43,9 +81,9 @@ public function button(string $text, string $url, int $columns = 2): self
4381
{
4482
$this->buttons[] = compact('text', 'url');
4583

46-
$this->payload['reply_markup'] = json_encode([
84+
$this->keyboardMarkup([
4785
'inline_keyboard' => array_chunk($this->buttons, $columns),
48-
], JSON_THROW_ON_ERROR);
86+
]);
4987

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

64-
$this->payload['reply_markup'] = json_encode([
102+
$this->keyboardMarkup([
65103
'inline_keyboard' => array_chunk($this->buttons, $columns),
66-
], JSON_THROW_ON_ERROR);
104+
]);
67105

68106
return $this;
69107
}
@@ -99,7 +137,7 @@ public function token(string $token): self
99137
*/
100138
public function hasToken(): bool
101139
{
102-
return null !== $this->token;
140+
return $this->token !== null;
103141
}
104142

105143
/**

tests/Feature/TelegramMessageTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,24 @@
138138
$message = TelegramMessage::create()->options(['disable_web_page_preview' => true]);
139139
expect($message->getPayloadValue('disable_web_page_preview'))->toBeTrue();
140140
});
141+
142+
test('a normal keyboard button can be added to the message', function () {
143+
$message = TelegramMessage::create()->keyboard('Laravel');
144+
expect($message->getPayloadValue('reply_markup'))->toEqual(
145+
'{"keyboard":[[{"text":"Laravel","request_contact":false,"request_location":false}]],"one_time_keyboard":true,"resize_keyboard":true}'
146+
);
147+
});
148+
149+
test('a request phone keyboard button can be added to the message', function () {
150+
$message = TelegramMessage::create()->keyboard('Laravel', request_contact: true);
151+
expect($message->getPayloadValue('reply_markup'))->toEqual(
152+
'{"keyboard":[[{"text":"Laravel","request_contact":true,"request_location":false}]],"one_time_keyboard":true,"resize_keyboard":true}'
153+
);
154+
});
155+
156+
test('a request location keyboard button can be added to the message', function () {
157+
$message = TelegramMessage::create()->keyboard('Laravel', request_location: true);
158+
expect($message->getPayloadValue('reply_markup'))->toEqual(
159+
'{"keyboard":[[{"text":"Laravel","request_contact":false,"request_location":true}]],"one_time_keyboard":true,"resize_keyboard":true}'
160+
);
161+
});

0 commit comments

Comments
 (0)