Skip to content

Commit

Permalink
Update Slack and Discord notifier classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Feb 8, 2024
1 parent 26842cb commit 3db60c7
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 146 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Works for [Discord webhooks](https://support.discord.com/hc/en-us/articles/22838
> When native Laravel notifications are for users, this package is designed for developers to help for debugging and monitoring, but you can use it for users too.
> [!IMPORTANT]
> This package offer a support for Discord and Slack webhooks, but Slack has only basic support, for more, you can use [`laravel/slack-notification-channel`](https://github.com/laravel/slack-notification-channel). To avoid dependencies, this package doesn't use it.
> This package offer a support for Discord and Slack webhooks, but Slack has only basic support (without legacy API support), for more, you can use [`laravel/slack-notification-channel`](https://github.com/laravel/slack-notification-channel). To avoid dependencies, this package doesn't use it.
## Installation

Expand Down
6 changes: 3 additions & 3 deletions src/Notifier/Discord/NotifierDiscordMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kiwilan\Notifier\Notifier\Discord;

use Kiwilan\Notifier\Utils\NotifierHelpers;

class NotifierDiscordMessage extends NotifierDiscordContainer
{
protected function __construct(
Expand All @@ -13,9 +15,7 @@ protected function __construct(

public static function create(string $webhook, string $message): self
{
if (strlen($message) > 2000) {
$message = substr($message, 0, 1980).'...';
}
$message = NotifierHelpers::truncate($message);

$self = new self($message);
$self->webhook = $webhook;
Expand Down
3 changes: 3 additions & 0 deletions src/Notifier/Discord/NotifierDiscordRich.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use DateTime;
use Kiwilan\Notifier\Utils\NotifierHelpers;

class NotifierDiscordRich extends NotifierDiscordContainer
{
Expand All @@ -28,6 +29,8 @@ protected function __construct(

public static function create(string $webhook, string $description): self
{
$description = NotifierHelpers::truncate($description);

$self = new self($description);
$self->webhook = $webhook;

Expand Down
14 changes: 12 additions & 2 deletions src/Notifier/NotifierDiscord.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ public static function make(string $webhook): self
return new self($webhook);
}

public function message(string $message): NotifierDiscordMessage
/**
* @param string[]|string $message
*/
public function message(array|string $message): NotifierDiscordMessage
{
$message = $this->arrayToString($message);

return NotifierDiscordMessage::create($this->webhook, $message);
}

public function rich(string $message): NotifierDiscordRich
/**
* @param string[]|string $message
*/
public function rich(array|string $message): NotifierDiscordRich
{
$message = $this->arrayToString($message);

return NotifierDiscordRich::create($this->webhook, $message);
}
}
39 changes: 11 additions & 28 deletions src/Notifier/NotifierSlack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace Kiwilan\Notifier\Notifier;

use Kiwilan\Notifier\Notifier;
use Kiwilan\Notifier\Utils\NotifierRequest;
use Kiwilan\Notifier\Notifier\Slack\NotifierSlackMessage;

/**
* @see https://api.slack.com/messaging/webhooks#advanced_message_formatting
* @see https://api.slack.com/block-kit
*/
class NotifierSlack extends Notifier
{
protected function __construct(
Expand All @@ -18,34 +22,13 @@ public static function make(string $webhook): self
return new self($webhook);
}

public function message(array|string $message): self
/**
* @param string[]|string $message
*/
public function message(array|string $message): NotifierSlackMessage
{
$this->message = $this->arrayToString($message);
$message = $this->arrayToString($message);

return $this;
}

public function send(): bool
{
$this->request = NotifierRequest::make($this->webhook)
->requestData($this->toArray())
->send();

if ($this->request->getStatusCode() !== 200) {
$this->logError("status code {$this->request->getStatusCode()}", [
$this->request->toArray(),
]);

return false;
}

return true;
}

public function toArray(): array
{
return [
'text' => $this->message ?? '',
];
return NotifierSlackMessage::create($this->webhook, $message);
}
}
2 changes: 1 addition & 1 deletion src/Notifier/Slack/NotifierSlackContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function __construct(
) {
}

abstract public static function create(string $webhook, string $description): self;
abstract public static function create(string $webhook, string $message): self;

abstract public function toArray(): array;

Expand Down
35 changes: 10 additions & 25 deletions src/Notifier/Slack/NotifierSlackMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,36 @@

namespace Kiwilan\Notifier\Notifier\Slack;

use Kiwilan\Notifier\Utils\NotifierHelpers;

class NotifierSlackMessage extends NotifierSlackContainer
{
protected function __construct(
protected ?string $message = null,
protected ?string $username = null,
protected ?string $avatarUrl = null,
protected ?string $text = null,
protected string $type = 'plain_text',
) {
}

public static function create(string $webhook, string $message): self
{
if (strlen($message) > 2000) {
$message = substr($message, 0, 1980).'...';
}
$message = NotifierHelpers::truncate($message);

$self = new self($message);
$self->webhook = $webhook;

return $self;
}

public function user(string $username, ?string $avatarUrl = null): self
{
$this->username = $username;

if ($avatarUrl) {
$this->avatarUrl = $avatarUrl;
}

return $this;
}

public function toArray(): array
{
$data = [];

if ($this->username) {
$data['username'] = $this->username;
}

if ($this->avatarUrl) {
$data['avatar_url'] = $this->avatarUrl;
}
// $data['text'] = [
// 'type' => $this->type,
// 'text' => $this->text,
// ];

$data['content'] = $this->message ?? 'Empty message.';
$data['text'] = $this->text;

return $data;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Utils/NotifierHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Kiwilan\Notifier\Utils;

class NotifierHelpers
{
public static function truncate(?string $string, int $length = 2000): ?string
{
if (! $string) {
return null;
}

if (strlen($string) > $length) {
$string = substr($string, 0, $length - 20).'...';
}

return $string;
}
}
16 changes: 15 additions & 1 deletion tests/NotifierDiscordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@
expect($notifier->isSuccess())->toBeTrue();

$notifier = Notifier::discord()
->rich('Rich simple')
->message([
'Hello, Discord!',
'This is a message.',
])
->user('Notifier', 'https://raw.githubusercontent.com/kiwilan/notifier-laravel/main/docs/banner.jpg')
->send();
expect($notifier->isSuccess())->toBeTrue();

$notifier = Notifier::discord()
->rich([
'Rich',
'simple',
'for',
'Discord',
])
->send();
expect($notifier->isSuccess())->toBeTrue();

Expand Down
121 changes: 121 additions & 0 deletions tests/NotifierSlackDefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

// it('can use', function () {
// $default = [
// 'text' => 'Content',
// ];

// $legacy = [
// 'username' => 'Ghostbot',
// 'icon_emoji' => ':ghost:',
// 'channel' => '#random',
// 'text' => 'Content',
// ];

// $attachments = [
// 'text' => [
// 'type' => 'mrkdwn',
// 'text' => '*Content with attachment*',
// ],
// 'attachments' => [
// [
// 'title' => 'Laravel',
// 'title_link' => 'https://laravel.com',
// 'text' => 'Attachment Content',
// 'fallback' => 'Attachment Fallback',
// 'fields' => [
// [
// 'title' => 'Project',
// 'value' => 'Laravel',
// 'short' => true,
// ],
// ],
// 'mrkdwn_in' => ['text'],
// 'footer' => 'Laravel',
// 'footer_icon' => 'https://laravel.com/fake.png',
// 'author_name' => 'Author',
// 'author_link' => 'https://laravel.com/fake_author',
// 'author_icon' => 'https://laravel.com/fake_author.png',
// 'ts' => 1234567890,
// ],
// ],
// ];

// $blocks = [
// [
// 'blocks' => [
// [
// 'type' => 'section',
// 'text' => [
// 'type' => 'mrkdwn',
// 'text' => 'Danny Torrence left the following review for your property:',
// ],
// 'fields' => [
// [
// 'type' => 'mrkdwn',
// 'text' => '*Markdown!*',
// ],
// [
// 'type' => 'plain_text',
// 'text' => str_repeat('a', 1997).'...',
// ],
// [
// 'type' => 'mrkdwn',
// 'text' => 'Location: 123 Main Street, New York, NY 10010',
// ],
// ],
// ],
// [
// 'type' => 'divider',
// ],
// [
// 'type' => 'actions',
// 'elements' => [
// [
// 'type' => 'button',
// 'text' => [
// 'type' => 'plain_text',
// 'text' => 'Example Button',
// ],
// 'action_id' => 'button_example-button',
// ],
// [
// 'type' => 'button',
// 'text' => [
// 'type' => 'plain_text',
// 'text' => 'Scary Button',
// ],
// 'action_id' => 'button_scary-button',
// 'style' => 'danger',
// ],
// ],
// ],
// [
// 'type' => 'header',
// 'text' => [
// 'type' => 'plain_text',
// 'text' => 'Budget Performance',
// ],
// ],
// [
// 'type' => 'image',
// 'image_url' => 'https://raw.githubusercontent.com/kiwilan/notifier-laravel/main/docs/banner.jpg',
// 'alt_text' => 'notifier banner',
// ],
// [
// 'type' => 'section',
// 'block_id' => 'section567',
// 'text' => [
// 'type' => 'mrkdwn',
// 'text' => 'Markdown *can* be so fun!',
// ],
// 'accessory' => [
// 'type' => 'image',
// 'image_url' => 'https://raw.githubusercontent.com/kiwilan/notifier-laravel/main/docs/banner.jpg',
// 'alt_text' => 'notifier banner',
// ],
// ],
// ],
// ],
// ];
// });
Loading

0 comments on commit 3db60c7

Please sign in to comment.