-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fabio Rocha
committed
Apr 30, 2021
1 parent
df6248e
commit 951c562
Showing
5 changed files
with
149 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
"Frf\\DiscordNotification\\": "src/" | ||
}, | ||
"files": [ | ||
"src/helpers.php" | ||
"src/Helper/" | ||
] | ||
}, | ||
"extra": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Frf\DiscordNotification\Helper; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\RequestOptions; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class DiscordChannel | ||
{ | ||
public function send($notifiable, Notification $notification) | ||
{ | ||
$discordMessage = $notification->toDiscord(); | ||
|
||
$discordWebhook = $notifiable->destination['url']; | ||
|
||
(new Client())->post($discordWebhook, [ | ||
RequestOptions::JSON => $discordMessage->toArray(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Frf\DiscordNotification\Helper; | ||
|
||
use Carbon\Carbon; | ||
|
||
class DiscordMessage | ||
{ | ||
const COLOR_SUCCESS = '#0B6623'; | ||
const COLOR_WARNING = '#FD6A02'; | ||
const COLOR_ERROR = '#ED2939'; | ||
|
||
/** @var string */ | ||
protected $title; | ||
|
||
/** @var string */ | ||
protected $description; | ||
|
||
/** @var string */ | ||
protected $timestamp; | ||
|
||
/** @var string */ | ||
protected $footer; | ||
|
||
/** @var string */ | ||
protected $color; | ||
|
||
public function title($title) | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|string $descriptionLines | ||
* | ||
* @return \App\Services\NotificationChannels\Discord\DiscordMessage | ||
*/ | ||
public function description($descriptionLines): self | ||
{ | ||
if (! is_array($descriptionLines)) { | ||
$descriptionLines = [$descriptionLines]; | ||
} | ||
|
||
$this->description = implode(PHP_EOL, $descriptionLines); | ||
|
||
return $this; | ||
} | ||
|
||
public function timestamp(Carbon $carbon): self | ||
{ | ||
$this->timestamp = $carbon->toIso8601String(); | ||
|
||
return $this; | ||
} | ||
|
||
public function footer(string $footer): self | ||
{ | ||
$this->footer = $footer; | ||
|
||
return $this; | ||
} | ||
|
||
public function success(): self | ||
{ | ||
$this->color = static::COLOR_SUCCESS; | ||
|
||
return $this; | ||
} | ||
|
||
public function warning(): self | ||
{ | ||
$this->color = static::COLOR_WARNING; | ||
|
||
return $this; | ||
} | ||
|
||
public function error(): self | ||
{ | ||
$this->color = static::COLOR_ERROR; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'embeds' => [ | ||
[ | ||
'title' => $this->title, | ||
'type' => 'rich', | ||
'description' => $this->description, | ||
'color' => hexdec($this->color), | ||
'footer' => [ | ||
'text' => $this->footer ?? '', | ||
], | ||
'timestamp' => $this->timestamp, | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Frf\DiscordNotification\Helper; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\RequestOptions; | ||
use Carbon\Carbon; | ||
|
||
class DiscordMessageHelper | ||
{ | ||
public static function send(string $title, array $descriptions, string $footer = "Footer") | ||
{ | ||
$discordMessage = (new DiscordMessage()) | ||
->error() | ||
->title($title) | ||
->description($descriptions) | ||
->footer($footer) | ||
->timestamp(Carbon::now()); | ||
|
||
(new Client())->post(env('DISCORD_HOOK'), [ | ||
RequestOptions::JSON => $discordMessage->toArray(), | ||
]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.