* Discord notifications for events #433 * Style fixes * Check for blank webhook urls and disable * Cleanup items after review * Changes and fixes * Style fixes * Don't load env for testing * Fix status text * Refactor saving fields/fares so events get the latest data * Cleanup * Style fixes
This commit is contained in:
36
app/Notifications/Channels/Discord/Discord.php
Normal file
36
app/Notifications/Channels/Discord/Discord.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Channels\Discord;
|
||||
|
||||
use App\Contracts\Notification;
|
||||
use App\Support\HttpClient;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\Psr7;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Discord
|
||||
{
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(HttpClient $httpClient)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
public function send($notifiable, Notification $notification)
|
||||
{
|
||||
$message = $notification->toDiscordChannel($notifiable);
|
||||
if ($message === null || empty($message->webhook_url)) {
|
||||
Log::debug('Discord notifications not configured, skipping');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $message->toArray();
|
||||
$this->httpClient->post($message->webhook_url, $data);
|
||||
} catch (RequestException $e) {
|
||||
$response = Psr7\Message::toString($e->getResponse());
|
||||
Log::error('Error sending Discord notification: '.$e->getMessage().', '.$response);
|
||||
}
|
||||
}
|
||||
}
|
||||
140
app/Notifications/Channels/Discord/DiscordMessage.php
Normal file
140
app/Notifications/Channels/Discord/DiscordMessage.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Channels\Discord;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* Original is from https://gist.github.com/freekmurze/e4415090f650e070d3de8b905875cf78
|
||||
*
|
||||
* Markdown guide: https://birdie0.github.io/discord-webhooks-guide/other/discord_markdown.html
|
||||
*/
|
||||
class DiscordMessage
|
||||
{
|
||||
const COLOR_SUCCESS = '#0B6623';
|
||||
const COLOR_WARNING = '#FD6A02';
|
||||
const COLOR_ERROR = '#ED2939';
|
||||
|
||||
public $webhook_url;
|
||||
|
||||
protected $title;
|
||||
protected $url;
|
||||
protected $description;
|
||||
protected $timestamp;
|
||||
protected $footer;
|
||||
protected $color;
|
||||
protected $author = [];
|
||||
protected $fields = [];
|
||||
|
||||
/**
|
||||
* Supply the webhook URL that this should be going to
|
||||
*/
|
||||
public function webhook(string $url): self
|
||||
{
|
||||
$this->webhook_url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Title of the notification
|
||||
*/
|
||||
public function title(string $title): self
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function url(string $url): self
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $descriptionLines
|
||||
*/
|
||||
public function description($descriptionLines): self
|
||||
{
|
||||
if (!is_array($descriptionLines)) {
|
||||
$descriptionLines = [$descriptionLines];
|
||||
}
|
||||
|
||||
$this->description = implode(PHP_EOL, $descriptionLines);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the author information:
|
||||
* [
|
||||
* 'name' => '',
|
||||
* 'url' => '',
|
||||
* 'icon_url' => '',
|
||||
*/
|
||||
public function author(array $author): self
|
||||
{
|
||||
$this->author = $author;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fields
|
||||
*/
|
||||
public function fields(array $fields): self
|
||||
{
|
||||
$this->fields = [];
|
||||
foreach ($fields as $name => $value) {
|
||||
$this->fields[] = [
|
||||
'name' => '**'.$name.'**', // bold
|
||||
'value' => $value,
|
||||
'inline' => true,
|
||||
];
|
||||
}
|
||||
|
||||
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,
|
||||
'url' => $this->url,
|
||||
'type' => 'rich',
|
||||
'description' => $this->description,
|
||||
// 'color' => hexdec($this->color),
|
||||
'author' => $this->author,
|
||||
'fields' => $this->fields,
|
||||
'footer' => [
|
||||
'text' => $this->footer ?? '',
|
||||
],
|
||||
'timestamp' => Carbon::now('UTC'),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ trait MailChannel
|
||||
* @param string $template Markdown template to use
|
||||
* @param array $args Arguments to pass to the template
|
||||
*/
|
||||
public function setMailable($subject, $template, $args)
|
||||
public function setMailable(string $subject, string $template, array $args)
|
||||
{
|
||||
$this->mailSubject = $subject;
|
||||
$this->mailTemplate = $template;
|
||||
|
||||
Reference in New Issue
Block a user