2021-06-04 22:51:59 +08:00
|
|
|
<?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;
|
|
|
|
|
2021-06-05 04:57:27 +08:00
|
|
|
class DiscordWebhook
|
2021-06-04 22:51:59 +08:00
|
|
|
{
|
|
|
|
private $httpClient;
|
|
|
|
|
|
|
|
public function __construct(HttpClient $httpClient)
|
|
|
|
{
|
|
|
|
$this->httpClient = $httpClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function send($notifiable, Notification $notification)
|
|
|
|
{
|
|
|
|
$message = $notification->toDiscordChannel($notifiable);
|
2022-02-09 04:07:02 +08:00
|
|
|
if ($message === null) {
|
|
|
|
//Log::debug('Discord notifications not configured, skipping');
|
2021-06-04 22:51:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-09 04:07:02 +08:00
|
|
|
$webhook_url = $message->webhook_url;
|
|
|
|
if (empty($webhook_url)) {
|
|
|
|
$webhook_url = setting('notifications.discord_private_webhook_url');
|
|
|
|
if (empty($webhook_url)) {
|
|
|
|
//Log::debug('Discord notifications not configured, skipping');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
try {
|
|
|
|
$data = $message->toArray();
|
2022-02-09 04:07:02 +08:00
|
|
|
$this->httpClient->post($webhook_url, $data);
|
2021-06-04 22:51:59 +08:00
|
|
|
} catch (RequestException $e) {
|
2021-06-05 01:57:16 +08:00
|
|
|
$request = Psr7\Message::toString($e->getRequest());
|
2021-06-04 22:51:59 +08:00
|
|
|
$response = Psr7\Message::toString($e->getResponse());
|
2021-06-05 01:57:16 +08:00
|
|
|
Log::error('Error sending Discord notification: request: '.$e->getMessage().', '.$request);
|
|
|
|
Log::error('Error sending Discord notification: response: '.$response);
|
2021-06-04 22:51:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|