Skip to content

Commit

Permalink
Added username, icon and channel options for Slack Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristoffer Alfheim committed Aug 20, 2016
1 parent 654905b commit 1f9a587
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 21 deletions.
25 changes: 22 additions & 3 deletions src/Illuminate/Notifications/Channels/SlackWebhookChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,31 @@ public function send($notifiable, Notification $notification)

$message = $notification->toSlack($notifiable);

$this->http->post($url, [
$this->http->post($url, $this->buildJsonPayload($message));
}

/**
* Build up a JSON payload for the Slack webhook.
*
* @param \Illuminate\Notifications\Messages\SlackMessage $message
* @return array
*/
protected function buildJsonPayload(SlackMessage $message)
{
$optionalFields = collect([
'username' => null, 'icon_emoji' => null, 'channel' => null,
])->map(function ($value, $key) use ($message) {
return $message->{$key};
})->reject(function ($item) {
return is_null($item);
})->all();

return [
'json' => [
'text' => $message->content,
'attachments' => $this->attachments($message),
],
]);
] + $optionalFields,
];
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/Illuminate/Notifications/Messages/SlackMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ class SlackMessage
*/
public $level = 'info';

/**
* Webhook username.
*
* @var string|null
*/
public $username;

/**
* Webhook icon emoji.
*
* @var string|null
*/
public $icon_emoji;

/**
* Channel override.
*
* @var string|null
*/
public $channel;

/**
* The text content of the message.
*
Expand Down Expand Up @@ -51,6 +72,37 @@ public function error()
return $this;
}

/**
* Set a custom username and emoji icon for the Slack message.
*
* @param string $username
* @param string|null $icon
* @return $this
*/
public function as($username, $icon = null)
{
$this->username = $username;

if (! is_null($icon)) {
$this->icon_emoji = $icon;
}

return $this;
}

/**
* Set which channel the Slack message should be posted in.
*
* @param string $channel
* @return $this
*/
public function in($channel)
{
$this->channel = $channel;

return $this;
}

/**
* Set the content of the Slack message.
*
Expand Down
94 changes: 76 additions & 18 deletions tests/Notifications/NotificationSlackChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,76 @@ public function tearDown()
Mockery::close();
}

public function testCorrectPayloadIsSentToSlack()
/**
* @param \Illuminate\Notifications\Notification $notification
* @param array $payload
*/
protected function validatePayload($notification, $payload)
{
$notification = new NotificationSlackChannelTestNotification;
$notifiable = new NotificationSlackChannelTestNotifiable;

$channel = new Illuminate\Notifications\Channels\SlackWebhookChannel(
$http = Mockery::mock('GuzzleHttp\Client')
);

$http->shouldReceive('post')->with('url', [
'json' => [
'text' => 'Content',
'attachments' => [
[
'title' => 'Laravel',
'title_link' => 'https://laravel.com',
'text' => 'Attachment Content',
'fields' => [
[
'title' => 'Project',
'value' => 'Laravel',
'short' => true,
$http->shouldReceive('post')->with('url', $payload);

$channel->send($notifiable, $notification);
}

public function testCorrectPayloadIsSentToSlack()
{
$this->validatePayload(
new NotificationSlackChannelTestNotification,
[
'json' => [
'username' => 'Ghostbot',
'icon_emoji' => ':ghost:',
'channel' => '#ghost-talk',
'text' => 'Content',
'attachments' => [
[
'title' => 'Laravel',
'title_link' => 'https://laravel.com',
'text' => 'Attachment Content',
'fields' => [
[
'title' => 'Project',
'value' => 'Laravel',
'short' => true,
],
],
],
],
],
],
]);
]
);
}

$channel->send($notifiable, $notification);
public function testCorrectPayloadWithoutOptionalFieldsIsSentToSlack()
{
$this->validatePayload(
new NotificationSlackChannelWithoutOptionalFieldsTestNotification,
[
'json' => [
'text' => 'Content',
'attachments' => [
[
'title' => 'Laravel',
'title_link' => 'https://laravel.com',
'text' => 'Attachment Content',
'fields' => [
[
'title' => 'Project',
'value' => 'Laravel',
'short' => true,
],
],
],
],
],
]
);
}
}

Expand All @@ -54,6 +94,24 @@ public function routeNotificationForSlack()
}

class NotificationSlackChannelTestNotification extends Notification
{
public function toSlack($notifiable)
{
return (new SlackMessage)
->as('Ghostbot', ':ghost:')
->in('#ghost-talk')
->content('Content')
->attachment(function ($attachment) {
$attachment->title('Laravel', 'https://laravel.com')
->content('Attachment Content')
->fields([
'Project' => 'Laravel',
]);
});
}
}

class NotificationSlackChannelWithoutOptionalFieldsTestNotification extends Notification
{
public function toSlack($notifiable)
{
Expand Down

0 comments on commit 1f9a587

Please sign in to comment.