Skip to content

Instantly share code, notes, and snippets.

@mayoz
Forked from sevenpixels/SlackMessage.php
Created June 19, 2023 09:05
Show Gist options
  • Save mayoz/5a560c0f63ee81d13cb786b637fdd767 to your computer and use it in GitHub Desktop.
Save mayoz/5a560c0f63ee81d13cb786b637fdd767 to your computer and use it in GitHub Desktop.
Add link_names to Slack Notification
<?php
namespace Illuminate\Notifications\Messages;
use Closure;
class SlackMessage
{
/**
* The "level" of the notification (info, success, warning, error).
*
* @var string
*/
public $level = 'info';
/**
* The username to send the message from.
*
* @var string|null
*/
public $username;
/**
* The user emoji icon for the message.
*
* @var string|null
*/
public $icon;
/**
* The user image icon for the message.
*
* @var string|null
*/
public $image;
/**
* The channel to send the message on.
*
* @var string|null
*/
public $channel;
/**
* The text content of the message.
*
* @var string
*/
public $content;
/**
* Linkify channel names and usernames.
*
* @var bool
*/
public $linkNames = 0;
/**
* The message's attachments.
*
* @var array
*/
public $attachments = [];
/**
* Additional request options for the Guzzle HTTP client.
*
* @var array
*/
public $http = [];
/**
* Indicate that the notification gives information about a successful operation.
*
* @return $this
*/
public function success()
{
$this->level = 'success';
return $this;
}
/**
* Indicate that the notification gives information about a warning.
*
* @return $this
*/
public function warning()
{
$this->level = 'warning';
return $this;
}
/**
* Indicate that the notification gives information about an error.
*
* @return $this
*/
public function error()
{
$this->level = 'error';
return $this;
}
/**
* Set a custom username and optional emoji icon for the Slack message.
*
* @param string $username
* @param string|null $icon
* @return $this
*/
public function from($username, $icon = null)
{
$this->username = $username;
if (! is_null($icon)) {
$this->icon = $icon;
}
return $this;
}
/**
* Set a custom image icon the message should use.
*
* @param string $channel
* @return $this
*/
public function image($image)
{
$this->image = $image;
return $this;
}
/**
* Set the Slack channel the message should be sent to.
*
* @param string $channel
* @return $this
*/
public function to($channel)
{
$this->channel = $channel;
return $this;
}
/**
* Set the content of the Slack message.
*
* @param string $content
* @return $this
*/
public function content($content)
{
$this->content = $content;
return $this;
}
/**
* Linkify channel names and usernames.
*
* @return $this
*/
public function linkNames()
{
$this->linkNames = 1;
return $this;
}
/**
* Define an attachment for the message.
*
* @param \Closure $callback
* @return $this
*/
public function attachment(Closure $callback)
{
$this->attachments[] = $attachment = new SlackAttachment;
$callback($attachment);
return $this;
}
/**
* Get the color for the message.
*
* @return string
*/
public function color()
{
switch ($this->level) {
case 'success':
return 'good';
case 'error':
return 'danger';
case 'warning':
return 'warning';
}
}
/**
* Set additional request options for the Guzzle HTTP client.
*
* @param array $options
* @return $this
*/
public function http(array $options)
{
$this->http = $options;
return $this;
}
}
<?php
namespace Illuminate\Notifications\Channels;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackAttachmentField;
class SlackWebhookChannel
{
/**
* The HTTP client instance.
*
* @var \GuzzleHttp\Client
*/
protected $http;
/**
* Create a new Slack channel instance.
*
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http)
{
$this->http = $http;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Psr\Http\Message\ResponseInterface
*/
public function send($notifiable, Notification $notification)
{
if (! $url = $notifiable->routeNotificationFor('slack')) {
return;
}
$this->http->post($url, $this->buildJsonPayload(
$notification->toSlack($notifiable)
));
}
/**
* Build up a JSON payload for the Slack webhook.
*
* @param \Illuminate\Notifications\Messages\SlackMessage $message
* @return array
*/
protected function buildJsonPayload(SlackMessage $message)
{
$optionalFields = array_filter([
'username' => data_get($message, 'username'),
'icon_emoji' => data_get($message, 'icon'),
'icon_url' => data_get($message, 'image'),
'channel' => data_get($message, 'channel'),
'link_names' => data_get($message, 'linkNames'),
]);
return array_merge([
'json' => array_merge([
'text' => $message->content,
'attachments' => $this->attachments($message),
], $optionalFields),
], $message->http);
}
/**
* Format the message's attachments.
*
* @param \Illuminate\Notifications\Messages\SlackMessage $message
* @return array
*/
protected function attachments(SlackMessage $message)
{
return collect($message->attachments)->map(function ($attachment) use ($message) {
return array_filter([
'color' => $attachment->color ?: $message->color(),
'fallback' => $attachment->fallback,
'fields' => $this->fields($attachment),
'footer' => $attachment->footer,
'footer_icon' => $attachment->footerIcon,
'image_url' => $attachment->imageUrl,
'mrkdwn_in' => $attachment->markdown,
'text' => $attachment->content,
'title' => $attachment->title,
'title_link' => $attachment->url,
'ts' => $attachment->timestamp,
]);
})->all();
}
/**
* Format the attachment's fields.
*
* @param \Illuminate\Notifications\Messages\SlackAttachment $attachment
* @return array
*/
protected function fields(SlackAttachment $attachment)
{
return collect($attachment->fields)->map(function ($value, $key) {
if ($value instanceof SlackAttachmentField) {
return $value->toArray();
}
return ['title' => $key, 'value' => $value, 'short' => true];
})->values()->all();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment