Initial commit

This commit is contained in:
distinctm
2019-01-22 17:58:20 -05:00
commit d6ced07199
6519 changed files with 752220 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) <Taylor Otwell>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,46 @@
{
"name": "laravel/slack-notification-channel",
"description": "Slack Notification Channel for laravel.",
"keywords": ["laravel", "notifications", "slack"],
"license": "MIT",
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"require": {
"php": "^7.1.3",
"guzzlehttp/guzzle": "^6.0"
},
"require-dev": {
"illuminate/notifications": "~5.7",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"Illuminate\\Notifications\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Illuminate\\Tests\\Notifications\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"laravel": {
"providers": [
"Illuminate\\Notifications\\SlackChannelServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

View File

View File

@@ -0,0 +1,122 @@
<?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 void
*/
public function send($notifiable, Notification $notification)
{
if (! $url = $notifiable->routeNotificationFor('slack', $notification)) {
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([
'channel' => data_get($message, 'channel'),
'icon_emoji' => data_get($message, 'icon'),
'icon_url' => data_get($message, 'image'),
'link_names' => data_get($message, 'linkNames'),
'unfurl_links' => data_get($message, 'unfurlLinks'),
'unfurl_media' => data_get($message, 'unfurlMedia'),
'username' => data_get($message, 'username'),
]);
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([
'actions' => $attachment->actions,
'author_icon' => $attachment->authorIcon,
'author_link' => $attachment->authorLink,
'author_name' => $attachment->authorName,
'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,
'pretext' => $attachment->pretext,
'text' => $attachment->content,
'thumb_url' => $attachment->thumbUrl,
'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();
}
}

View File

@@ -0,0 +1,348 @@
<?php
namespace Illuminate\Notifications\Messages;
use Illuminate\Support\InteractsWithTime;
class SlackAttachment
{
use InteractsWithTime;
/**
* The attachment's title.
*
* @var string
*/
public $title;
/**
* The attachment's URL.
*
* @var string
*/
public $url;
/**
* The attachment's pretext.
*
* @var string
*/
public $pretext;
/**
* The attachment's text content.
*
* @var string
*/
public $content;
/**
* A plain-text summary of the attachment.
*
* @var string
*/
public $fallback;
/**
* The attachment's color.
*
* @var string
*/
public $color;
/**
* The attachment's fields.
*
* @var array
*/
public $fields;
/**
* The fields containing markdown.
*
* @var array
*/
public $markdown;
/**
* The attachment's image url.
*
* @var string
*/
public $imageUrl;
/**
* The attachment's thumb url.
*
* @var string
*/
public $thumbUrl;
/**
* The attachment's actions.
*
* @var array
*/
public $actions = [];
/**
* The attachment author's name.
*
* @var string
*/
public $authorName;
/**
* The attachment author's link.
*
* @var string
*/
public $authorLink;
/**
* The attachment author's icon.
*
* @var string
*/
public $authorIcon;
/**
* The attachment's footer.
*
* @var string
*/
public $footer;
/**
* The attachment's footer icon.
*
* @var string
*/
public $footerIcon;
/**
* The attachment's timestamp.
*
* @var int
*/
public $timestamp;
/**
* Set the title of the attachment.
*
* @param string $title
* @param string|null $url
* @return $this
*/
public function title($title, $url = null)
{
$this->title = $title;
$this->url = $url;
return $this;
}
/**
* Set the pretext of the attachment.
*
* @param string $pretext
* @return $this
*/
public function pretext($pretext)
{
$this->pretext = $pretext;
return $this;
}
/**
* Set the content (text) of the attachment.
*
* @param string $content
* @return $this
*/
public function content($content)
{
$this->content = $content;
return $this;
}
/**
* A plain-text summary of the attachment.
*
* @param string $fallback
* @return $this
*/
public function fallback($fallback)
{
$this->fallback = $fallback;
return $this;
}
/**
* Set the color of the attachment.
*
* @param string $color
* @return $this
*/
public function color($color)
{
$this->color = $color;
return $this;
}
/**
* Add a field to the attachment.
*
* @param \Closure|string $title
* @param string $content
* @return $this
*/
public function field($title, $content = '')
{
if (is_callable($title)) {
$callback = $title;
$callback($attachmentField = new SlackAttachmentField);
$this->fields[] = $attachmentField;
return $this;
}
$this->fields[$title] = $content;
return $this;
}
/**
* Set the fields of the attachment.
*
* @param array $fields
* @return $this
*/
public function fields(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* Set the fields containing markdown.
*
* @param array $fields
* @return $this
*/
public function markdown(array $fields)
{
$this->markdown = $fields;
return $this;
}
/**
* Set the image URL.
*
* @param string $url
* @return $this
*/
public function image($url)
{
$this->imageUrl = $url;
return $this;
}
/**
* Set the URL to the attachment thumbnail.
*
* @param string $url
* @return $this
*/
public function thumb($url)
{
$this->thumbUrl = $url;
return $this;
}
/**
* Add an action (button) under the attachment.
*
* @param string $title
* @param string $url
* @param string $style
* @return $this
*/
public function action($title, $url, $style = '')
{
$this->actions[] = [
'type' => 'button',
'text' => $title,
'url' => $url,
'style' => $style,
];
return $this;
}
/**
* Set the author of the attachment.
*
* @param string $name
* @param string|null $link
* @param string|null $icon
* @return $this
*/
public function author($name, $link = null, $icon = null)
{
$this->authorName = $name;
$this->authorLink = $link;
$this->authorIcon = $icon;
return $this;
}
/**
* Set the footer content.
*
* @param string $footer
* @return $this
*/
public function footer($footer)
{
$this->footer = $footer;
return $this;
}
/**
* Set the footer icon.
*
* @param string $icon
* @return $this
*/
public function footerIcon($icon)
{
$this->footerIcon = $icon;
return $this;
}
/**
* Set the timestamp.
*
* @param \DateTimeInterface|\DateInterval|int $timestamp
* @return $this
*/
public function timestamp($timestamp)
{
$this->timestamp = $this->availableAt($timestamp);
return $this;
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Illuminate\Notifications\Messages;
class SlackAttachmentField
{
/**
* The title field of the attachment field.
*
* @var string
*/
protected $title;
/**
* The content of the attachment field.
*
* @var string
*/
protected $content;
/**
* Whether the content is short.
*
* @var bool
*/
protected $short = true;
/**
* Set the title of the field.
*
* @param string $title
* @return $this
*/
public function title($title)
{
$this->title = $title;
return $this;
}
/**
* Set the content of the field.
*
* @param string $content
* @return $this
*/
public function content($content)
{
$this->content = $content;
return $this;
}
/**
* Indicates that the content should not be displayed side-by-side with other fields.
*
* @return $this
*/
public function long()
{
$this->short = false;
return $this;
}
/**
* Get the array representation of the attachment field.
*
* @return array
*/
public function toArray()
{
return [
'title' => $this->title,
'value' => $this->content,
'short' => $this->short,
];
}
}

View File

@@ -0,0 +1,273 @@
<?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;
/**
* Indicates if channel names and usernames should be linked.
*
* @var bool
*/
public $linkNames = 0;
/**
* Indicates if you want a preview of links inlined in the message.
*
* @var bool
*/
public $unfurlLinks;
/**
* Indicates if you want a preview of links to media inlined in the message.
*
* @var bool
*/
public $unfurlMedia;
/**
* 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 an operation.
*
* @return $this
*/
public function info()
{
$this->level = 'info';
return $this;
}
/**
* 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 $image
* @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;
}
/**
* 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|null
*/
public function color()
{
switch ($this->level) {
case 'success':
return 'good';
case 'error':
return 'danger';
case 'warning':
return 'warning';
}
}
/**
* Find and link channel names and usernames.
*
* @return $this
*/
public function linkNames()
{
$this->linkNames = 1;
return $this;
}
/**
* Find and link channel names and usernames.
*
* @param string $unfurl
* @return $this
*/
public function unfurlLinks($unfurl)
{
$this->unfurlLinks = $unfurl;
return $this;
}
/**
* Find and link channel names and usernames.
*
* @param string $unfurl
* @return $this
*/
public function unfurlMedia($unfurl)
{
$this->unfurlMedia = $unfurl;
return $this;
}
/**
* Set additional request options for the Guzzle HTTP client.
*
* @param array $options
* @return $this
*/
public function http(array $options)
{
$this->http = $options;
return $this;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Illuminate\Notifications;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Notification;
class SlackChannelServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
Notification::extend('slack', function ($app) {
return new Channels\SlackWebhookChannel(new HttpClient);
});
}
}