diff --git a/composer.json b/composer.json index e573044..f89ec2a 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "Frf\\DiscordNotification\\": "src/" }, "files": [ - "src/helpers.php" + "src/Helper/" ] }, "extra": { diff --git a/src/Helper/DiscordChannel.php b/src/Helper/DiscordChannel.php new file mode 100644 index 0000000..3e56efc --- /dev/null +++ b/src/Helper/DiscordChannel.php @@ -0,0 +1,21 @@ +toDiscord(); + + $discordWebhook = $notifiable->destination['url']; + + (new Client())->post($discordWebhook, [ + RequestOptions::JSON => $discordMessage->toArray(), + ]); + } +} diff --git a/src/Helper/DiscordMessage.php b/src/Helper/DiscordMessage.php new file mode 100644 index 0000000..2e13c41 --- /dev/null +++ b/src/Helper/DiscordMessage.php @@ -0,0 +1,103 @@ +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, + ], + ], + ]; + } +} diff --git a/src/Helper/DiscordMessageHelper.php b/src/Helper/DiscordMessageHelper.php new file mode 100644 index 0000000..34baae7 --- /dev/null +++ b/src/Helper/DiscordMessageHelper.php @@ -0,0 +1,24 @@ +error() + ->title($title) + ->description($descriptions) + ->footer($footer) + ->timestamp(Carbon::now()); + + (new Client())->post(env('DISCORD_HOOK'), [ + RequestOptions::JSON => $discordMessage->toArray(), + ]); + } +} diff --git a/src/helpers.php b/src/helpers.php deleted file mode 100644 index 16433e1..0000000 --- a/src/helpers.php +++ /dev/null @@ -1,23 +0,0 @@ -request('POST', env('DISCORD_HOOK'), [ - 'headers' => [ - 'Content-type' => 'application/json', - ], - 'content' => $message, - ]); - } catch (\Exception $exception) { - dump($exception->getMessage()); - } - } -}