Skip to content

Commit 89baf4f

Browse files
authored
Merge pull request #114 from digislexia/buttonWithCallback
buttonWithCallback
2 parents d6ab13d + 0b2cfb4 commit 89baf4f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ class InvoicePaid extends Notification
9494

9595
// (Optional) Inline Buttons
9696
->button('View Invoice', $url)
97-
->button('Download Invoice', $url);
97+
->button('Download Invoice', $url)
98+
// (Optional) Inline Button with callback. You can handle callback in your bot instance
99+
->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id);
98100
}
99101
}
100102
```
@@ -226,6 +228,7 @@ Notification::route('telegram', 'TELEGRAM_CHAT_ID')
226228
- `content('')`: (string) Notification message, supports markdown. For more information on supported markdown styles, check out these [docs](https://telegram-bot-sdk.readme.io/reference#section-formatting-options).
227229
- `view($view, $data = [], $mergeData = [])`: (string) Blade template name with Telegram supported HTML or Markdown syntax content if you wish to use a view file instead of the `content()` method.
228230
- `button($text, $url)`: (string) Adds an inline "Call to Action" button. You can add as many as you want, and they'll be placed 2 in a row.
231+
- `buttonWithCallback($text, $callback_data)`: (string) Adds an inline button with callback. You can add as many as you want, and they'll be placed 2 in a row.
229232
- `disableNotification($disableNotification = true)`: (bool) Send the message silently. Users will receive a notification with no sound.
230233
- `options([])`: (array) Allows you to add additional or override `sendMessage` payload (A Telegram Bot API method used to send message internally). For more information on supported parameters, check out these [docs](https://telegram-bot-sdk.readme.io/docs/sendmessage).
231234

@@ -236,6 +239,7 @@ Notification::route('telegram', 'TELEGRAM_CHAT_ID')
236239
- `latitude($latitude)`: (float|string) Latitude of the location.
237240
- `longitude($longitude)`: (float|string) Longitude of the location.
238241
- `button($text, $url)`: (string) Adds an inline "Call to Action" button. You can add as many as you want, and they'll be placed 2 in a row.
242+
- `buttonWithCallback($text, $callback_data)`: (string) Adds an inline button with callback. You can add as many as you want, and they'll be placed 2 in a row.
239243
- `disableNotification($disableNotification = true)`: (bool) Send the message silently. Users will receive a notification with no sound.
240244
- `options([])`: (array) Allows you to add additional or override the payload.
241245

@@ -254,6 +258,7 @@ Notification::route('telegram', 'TELEGRAM_CHAT_ID')
254258
- `voice($file)`: Helper method to attach a voice note (`.ogg` file with OPUS encoded).
255259
- `videoNote($file)`: Helper method to attach a video note file (Upto 1 min long, rounded square video).
256260
- `button($text, $url)`: (string) Adds an inline "Call to Action" button. You can add as many as you want, and they'll be placed 2 in a row.
261+
- `buttonWithCallback($text, $callback_data)`: (string) Adds an inline button with callback. You can add as many as you want, and they'll be placed 2 in a row.
257262
- `disableNotification($disableNotification = true)`: (bool) Send the message silently. Users will receive a notification with no sound.
258263
- `options([])`: (array) Allows you to add additional or override the payload.
259264

src/Traits/HasSharedLogic.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ public function button(string $text, string $url, int $columns = 2): self
5050
return $this;
5151
}
5252

53+
/**
54+
* Add an inline button with callback_data.
55+
*
56+
* @param string $text
57+
* @param string $callback_data
58+
* @param int $columns
59+
*
60+
* @return $this
61+
*/
62+
public function buttonWithCallback(string $text, string $callback_data, int $columns = 2): self
63+
{
64+
$this->buttons[] = compact('text', 'callback_data');
65+
66+
$this->payload['reply_markup'] = json_encode([
67+
'inline_keyboard' => array_chunk($this->buttons, $columns),
68+
]);
69+
70+
return $this;
71+
}
72+
5373
/**
5474
* Send the message silently.
5575
* Users will receive a notification with no sound.

tests/TelegramMessageTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public function an_inline_button_can_be_added_to_the_message(): void
4949
$message->getPayloadValue('reply_markup'));
5050
}
5151

52+
/** @test */
53+
public function an_inline_button_with_callback_can_be_added_to_the_message(): void
54+
{
55+
$message = new TelegramMessage();
56+
$message->buttonWithCallback('Laravel', 'laravel_callback');
57+
$this->assertEquals('{"inline_keyboard":[[{"text":"Laravel","callback_data":"laravel_callback"}]]}',
58+
$message->getPayloadValue('reply_markup'));
59+
}
60+
5261
/** @test */
5362
public function additional_options_can_be_set_for_the_message(): void
5463
{

0 commit comments

Comments
 (0)