Skip to content

Instantly share code, notes, and snippets.

@Cannonb4ll
Created October 13, 2019 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cannonb4ll/a1e9fa021ab940c9ea3aca7dd5b5a011 to your computer and use it in GitHub Desktop.
Save Cannonb4ll/a1e9fa021ab940c9ea3aca7dd5b5a011 to your computer and use it in GitHub Desktop.
Laravel Discord Notification Channel
<?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);
}
}
<?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,
]);
}
}
<?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