Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of 'lineIf' method to messages #190

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/TelegramMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public function line(string $content): self
return $this;
}

public function lineIf(bool $boolean, string $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
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/TelegramMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Expand Down
Loading