Created
October 13, 2019 13:45
-
-
Save Cannonb4ll/a1e9fa021ab940c9ea3aca7dd5b5a011 to your computer and use it in GitHub Desktop.
Laravel Discord Notification Channel
This file contains hidden or 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
<?php | |
namespace App\Utilities\Discord; | |
use Exception; | |
use GuzzleHttp\Client as HttpClient; | |
use GuzzleHttp\Exception\RequestException; | |
use App\Exceptions\CouldNotSendNotification; | |
class Discord | |
{ | |
/** @var string */ | |
protected $baseUrl = 'https://discordapp.com/api'; | |
/** @var \GuzzleHttp\Client */ | |
protected $httpClient; | |
/** | |
* @param \GuzzleHttp\Client $http | |
*/ | |
public function __construct(HttpClient $http) | |
{ | |
$this->httpClient = $http; | |
} | |
/** | |
* @param $notifiable | |
* @param array $data | |
* | |
* @return array | |
* @throws \GuzzleHttp\Exception\GuzzleException | |
*/ | |
public function send($notifiable, array $data) | |
{ | |
return $this->request('POST', $notifiable->routeNotificationForDiscord(), $data); | |
} | |
/** | |
* @param mixed $user | |
* | |
* @return string | |
* @throws \GuzzleHttp\Exception\GuzzleException | |
*/ | |
public function getPrivateChannel($user) | |
{ | |
return $this->request('POST', 'users/@me/channels', ['recipient_id' => $user])['id']; | |
} | |
/** | |
* @param $verb | |
* @param string $endpoint | |
* @param array $data | |
* | |
* @return array | |
* | |
* @throws \GuzzleHttp\Exception\GuzzleException | |
*/ | |
protected function request($verb, $endpoint, array $data) | |
{ | |
$url = $endpoint; | |
$response = null; | |
try { | |
$response = $this->httpClient->request($verb, $url, [ | |
'json' => $data, | |
'headers' => [ | |
'Content-Type' => 'application/json' | |
] | |
]); | |
} catch (RequestException $exception) { | |
info('DSERROR1:' . $exception->getMessage()); | |
// throw CouldNotSendNotification::serviceRespondedWithAnHttpError($exception->getResponse()); | |
} catch (Exception $exception) { | |
info('DSERROR2:' .$exception->getMessage()); | |
// throw CouldNotSendNotification::serviceCommunicationError($exception); | |
} | |
if (!$response) { | |
return; | |
} | |
return json_decode($response->getBody(), true); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Utilities\Discord; | |
use Illuminate\Notifications\Notification; | |
class DiscordChannel | |
{ | |
protected $discord; | |
/** | |
* @param \App\Utilities\Discord\Discord $discord | |
*/ | |
public function __construct(Discord $discord) | |
{ | |
$this->discord = $discord; | |
} | |
/** | |
* Send the given notification. | |
* | |
* @param mixed $notifiable | |
* @param \Illuminate\Notifications\Notification $notification | |
* | |
* @throws \GuzzleHttp\Exception\GuzzleException | |
*/ | |
public function send($notifiable, Notification $notification) | |
{ | |
/* @var $message \App\Utilities\Discord\DiscordMessage */ | |
$message = $notification->toDiscord($notifiable); | |
$this->discord->send($notifiable, [ | |
'content' => str_limit($message->content, 1500), | |
'username' => $message->username, | |
'avatar_url' => $message->image, | |
'embeds' => $message->embeds, | |
]); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Utilities\Discord; | |
class DiscordMessage | |
{ | |
/** | |
* The text content of the message. | |
* | |
* @var string | |
*/ | |
public $content; | |
public $username; | |
public $image; | |
public $embeds; | |
/** | |
* Set the text content of the message. | |
* | |
* @param string $content | |
* | |
* @return $this | |
*/ | |
public function content($content) | |
{ | |
$this->content = $content; | |
return $this; | |
} | |
public function from($name) | |
{ | |
$this->username = $name; | |
return $this; | |
} | |
public function image($image) | |
{ | |
$this->image = $image; | |
return $this; | |
} | |
public function embeds(array $embeds) | |
{ | |
$this->embeds = $embeds; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment