From 13a7acfad2290ebdb520273f3ac72f3b2f95d2bc Mon Sep 17 00:00:00 2001 From: Alex Kovalchuk Date: Thu, 22 Feb 2024 21:59:04 +0200 Subject: [PATCH 1/2] Add lineIf method to messages --- src/TelegramMessage.php | 9 +++++++++ tests/Feature/TelegramMessageTest.php | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/TelegramMessage.php b/src/TelegramMessage.php index a6664bb..fd7b2f1 100644 --- a/src/TelegramMessage.php +++ b/src/TelegramMessage.php @@ -51,6 +51,15 @@ public function line(string $content): self return $this; } + public function lineIf($boolean, $line): self + { + if ($boolean) { + return $this->line($line); + } + + return $this; + } + public function escapedLine(string $content): self { // code taken from public gist https://gist.github.com/vijinho/3d66fab3270fc377b8485387ce7e7455 diff --git a/tests/Feature/TelegramMessageTest.php b/tests/Feature/TelegramMessageTest.php index e1be4eb..4cd4378 100644 --- a/tests/Feature/TelegramMessageTest.php +++ b/tests/Feature/TelegramMessageTest.php @@ -25,6 +25,14 @@ expect($message->getPayloadValue('text'))->toEqual("Laravel Notification Channels are awesome!\nTelegram Notification Channel is fantastic :)\n"); }); +it('can add one message per lineIf if first argument is true', function () { + $message = TelegramMessage::create() + ->lineIf(true,'Laravel Notification Channels are awesome!') + ->lineIf(false,'Telegram Notification Channel is fantastic :)') + ->lineIf(true,'Telegram Notification Channel is fantastic =)'); + expect($message->getPayloadValue('text'))->toEqual("Laravel Notification Channels are awesome!\nTelegram Notification Channel is fantastic =)\n"); +}); + it('can escape special markdown characters per line', function () { $message = TelegramMessage::create() ->escapedLine('Laravel Notification_Channels are awesome!') From 8fe9c0bcfdbec319b80a7e416d08453ac8cefd5e Mon Sep 17 00:00:00 2001 From: Alex Kovalchuk Date: Thu, 22 Feb 2024 22:12:35 +0200 Subject: [PATCH 2/2] Add lineIf to README.md --- README.md | 2 ++ src/TelegramMessage.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f10f25..803551d 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,7 @@ class InvoicePaid extends Notification // Markdown supported. ->content("Hello there!") ->line("Your invoice has been *PAID*") + ->lineIf($notifiable->amount > 0, "Amount paid: {$notifiable->amount}") ->line("Thank you!") // (Optional) Blade template for the content. @@ -395,6 +396,7 @@ For more information on supported parameters, check out these [docs](https://cor - `content(string $content, int $limit = null)`: Notification message, supports markdown. For more information on supported markdown styles, check out these [docs](https://core.telegram.org/bots/api#formatting-options). - `line(string $content)`: Adds a message in a new line. +- `lineIf(bool $boolean, string $line)`: Adds a message in a new line if the given condition is true. - `escapedLine(string $content)`: Adds a message in a new line while escaping special characters (For Markdown). - `view(string $view, array $data = [], array $mergeData = [])`: (optional) Blade template name with Telegram supported HTML or Markdown syntax content if you wish to use a view file instead of the `content()` method. - `chunk(int $limit = 4096)`: (optional) Message chars chunk size to send in parts (For long messages). Note: Chunked messages will be rate limited to one message per second to comply with rate limitation requirements from Telegram. diff --git a/src/TelegramMessage.php b/src/TelegramMessage.php index fd7b2f1..d3aeeea 100644 --- a/src/TelegramMessage.php +++ b/src/TelegramMessage.php @@ -51,7 +51,7 @@ public function line(string $content): self return $this; } - public function lineIf($boolean, $line): self + public function lineIf(bool $boolean, string $line): self { if ($boolean) { return $this->line($line);